18 #include <QInputDialog>
19 #include <QStyledItemDelegate>
37 , mSizeProperty( ddSize )
38 , mMapCanvas( canvas )
49 radDisabled->setChecked(
true );
54 radSeparated->setChecked(
true );
56 radCollapsed->setChecked(
true );
59 cboAlignSymbols->setCurrentIndex( 0 );
61 cboAlignSymbols->setCurrentIndex( 1 );
71 symbol = overrideSymbol;
72 mOverrideSymbol =
true;
79 mSourceSymbol.reset( symbol );
81 btnChangeSymbol->setEnabled( !mOverrideSymbol );
84 btnChangeSymbol->setIcon( icon );
86 editTitle->setText( ddsLegend ? ddsLegend->
title() : QString() );
88 mSizeClassesModel =
new QStandardItemModel( viewSizeClasses );
89 mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr(
"Value" ) << tr(
"Label" ) );
90 mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
93 groupManualSizeClasses->setChecked( !ddsLegend->
classes().isEmpty() );
94 const auto constClasses = ddsLegend->
classes();
97 QStandardItem *item =
new QStandardItem( QString::number( sc.size ) );
98 item->setData( sc.size );
99 QStandardItem *itemLabel =
new QStandardItem( sc.label );
100 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
102 mSizeClassesModel->sort( 0 );
105 connect( btnAddClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
106 connect( btnRemoveClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
108 viewSizeClasses->setItemDelegateForColumn( 0,
new SizeClassDelegate( viewSizeClasses ) );
109 viewSizeClasses->setModel( mSizeClassesModel );
110 connect( mSizeClassesModel, &QStandardItemModel::dataChanged,
this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
114 mPreviewLayer =
new QgsVectorLayer( QStringLiteral(
"Point?crs=EPSG:4326" ), QStringLiteral(
"Preview" ), QStringLiteral(
"memory" ), options );
116 mPreviewLayerNode = mPreviewTree->
addLayer( mPreviewLayer );
120 viewLayerTree->setModel( mPreviewModel );
122 connect( cboAlignSymbols,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ] { emit
widgetChanged(); } );
127 connect( btnChangeSymbol, &QPushButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
136 delete mPreviewModel;
138 delete mPreviewLayer;
143 if ( radDisabled->isChecked() )
149 if ( !mOverrideSymbol )
151 ddsLegend->
setSymbol( mSourceSymbol->clone() );
154 ddsLegend->
setTitle( editTitle->text() );
156 if ( groupManualSizeClasses->isChecked() )
158 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
159 for (
int i = 0; i < mSizeClassesModel->rowCount(); ++i )
161 double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
162 QString label = mSizeClassesModel->item( i, 1 )->text();
172 void QgsDataDefinedSizeLegendWidget::updatePreview()
180 viewLayerTree->expandAll();
183 void QgsDataDefinedSizeLegendWidget::changeSymbol()
185 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
200 std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( QStringLiteral(
"Point?crs=%1" ).arg( crsAuthId ),
201 QStringLiteral(
"tmp" ),
202 QStringLiteral(
"memory" ),
206 d.setContext( context );
208 if ( d.exec() != QDialog::Accepted )
211 mSourceSymbol = std::move( newSymbol );
213 btnChangeSymbol->setIcon( icon );
218 void QgsDataDefinedSizeLegendWidget::addSizeClass()
221 double v = QInputDialog::getDouble(
this, tr(
"Add Size Class" ), tr(
"Enter value for a new class" ),
222 0, -2147483647, 2147483647, 6, &ok );
226 QStandardItem *item =
new QStandardItem( QString::number( v ) );
228 QStandardItem *itemLabel =
new QStandardItem( QString::number( v ) );
229 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
230 mSizeClassesModel->sort( 0 );
234 void QgsDataDefinedSizeLegendWidget::removeSizeClass()
236 QModelIndex idx = viewSizeClasses->currentIndex();
237 if ( !idx.isValid() )
240 mSizeClassesModel->removeRow( idx.row() );
244 void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
246 for (
int row = 0; row < mSizeClassesModel->rowCount(); ++row )
248 QStandardItem *item = mSizeClassesModel->item( row, 0 );
249 item->setData( item->text().toDouble() );
252 mSizeClassesModel->sort( 0 );
QString authid() const
Returns the authority identifier for the CRS.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
void setTitle(const QString &title)
Sets title label for data-defined size legend.
QList< QgsDataDefinedSizeLegend::SizeClass > classes() const
Returns list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setSymbol(QgsMarkerSymbol *symbol SIP_TRANSFER)
Sets marker symbol that will be used to draw markers in legend.
void setVerticalAlignment(VerticalAlignment vAlign)
Sets vertical alignment of symbols - only valid for collapsed legend.
void setLineSymbol(QgsLineSymbol *symbol SIP_TRANSFER)
Sets the line symbol that will be used to draw callout lines in legend.
LegendType legendType() const
Returns how the legend should be rendered.
QgsMarkerSymbol * symbol() const
Returns marker symbol that will be used to draw markers in legend.
@ AlignCenter
Symbols are aligned to the center.
@ AlignBottom
Symbols are aligned to the bottom.
void setClasses(const QList< QgsDataDefinedSizeLegend::SizeClass > &classes)
Sets list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setLegendType(LegendType type)
Sets how the legend should be rendered.
QString title() const
Returns title label for data-defined size legend.
@ LegendSeparated
Each class (size value) has a separate legend node.
@ LegendCollapsed
All classes are rendered within one legend node.
QgsLineSymbol * lineSymbol() const
Returns the line symbol that will be used to draw callout lines in legend.
VerticalAlignment verticalAlignment() const
Returns vertical alignment of symbols - only valid for collapsed legend.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
void setLegendMapViewData(double mapUnitsPerPixel, int dpi, double scale)
Give the layer tree model hints about the currently associated map view so that legend nodes that use...
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
Namespace with helper functions for layer tree operations.
A line symbol type, for rendering LineString and MultiLineString geometries.
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
Map canvas is a class for displaying all GIS data types on a canvas.
double scale() const
Returns the last reported scale of the canvas.
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
A marker symbol type, for rendering Point and MultiPoint geometries.
static QgsMarkerSymbol * createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
void setDataDefinedSize(const QgsProperty &property)
Set data defined size for whole symbol (including all symbol layers).
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
A store for object properties.
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
static QgsStyle * defaultStyle()
Returns default application-wide style.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr)
Returns an icon preview for a color ramp.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
Represents a vector layer which manages a vector based data sets.
void setRenderer(QgsFeatureRenderer *r)
Sets the feature renderer which will be invoked to represent this layer in 2D map views.
Definition of one class for the legend.
Setting options for loading vector layers.