18 #include <QInputDialog> 19 #include <QStyledItemDelegate> 36 , mSizeProperty( ddSize )
37 , mMapCanvas( canvas )
46 radDisabled->setChecked(
true );
51 radSeparated->setChecked(
true );
53 radCollapsed->setChecked(
true );
56 cboAlignSymbols->setCurrentIndex( 0 );
58 cboAlignSymbols->setCurrentIndex( 1 );
65 symbol = overrideSymbol;
66 mOverrideSymbol =
true;
73 mSourceSymbol.reset( symbol );
75 btnChangeSymbol->setEnabled( !mOverrideSymbol );
78 btnChangeSymbol->setIcon( icon );
80 editTitle->setText( ddsLegend ? ddsLegend->
title() : QString() );
82 mSizeClassesModel =
new QStandardItemModel( viewSizeClasses );
83 mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr(
"Value" ) << tr(
"Label" ) );
84 mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
87 groupManualSizeClasses->setChecked( !ddsLegend->
classes().isEmpty() );
90 QStandardItem *item =
new QStandardItem( QString::number( sc.
size ) );
91 item->setData( sc.
size );
92 QStandardItem *itemLabel =
new QStandardItem( sc.
label );
93 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
95 mSizeClassesModel->sort( 0 );
98 connect( btnAddClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
99 connect( btnRemoveClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
101 viewSizeClasses->setItemDelegateForColumn( 0,
new SizeClassDelegate( viewSizeClasses ) );
102 viewSizeClasses->setModel( mSizeClassesModel );
103 connect( mSizeClassesModel, &QStandardItemModel::dataChanged,
this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
106 mPreviewLayer =
new QgsVectorLayer( QStringLiteral(
"Point?crs=EPSG:4326" ), QStringLiteral(
"Preview" ), QStringLiteral(
"memory" ) );
108 mPreviewLayerNode = mPreviewTree->
addLayer( mPreviewLayer );
112 viewLayerTree->setModel( mPreviewModel );
114 connect( cboAlignSymbols,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ] { emit
widgetChanged(); } );
119 connect( btnChangeSymbol, &QPushButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
127 delete mPreviewModel;
129 delete mPreviewLayer;
134 if ( radDisabled->isChecked() )
140 if ( !mOverrideSymbol )
142 ddsLegend->
setSymbol( mSourceSymbol->clone() );
145 ddsLegend->
setTitle( editTitle->text() );
147 if ( groupManualSizeClasses->isChecked() )
149 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
150 for (
int i = 0; i < mSizeClassesModel->rowCount(); ++i )
152 double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
153 QString label = mSizeClassesModel->item( i, 1 )->text();
161 void QgsDataDefinedSizeLegendWidget::updatePreview()
169 viewLayerTree->expandAll();
172 void QgsDataDefinedSizeLegendWidget::changeSymbol()
174 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
185 context.setExpressionContext( &ec );
188 std::unique_ptr<QgsVectorLayer> layer(
new QgsVectorLayer(
"Point?crs=" + crsAuthId, QStringLiteral(
"tmp" ), QStringLiteral(
"memory" ) ) );
193 if ( d.exec() != QDialog::Accepted )
196 mSourceSymbol = std::move( newSymbol );
198 btnChangeSymbol->setIcon( icon );
203 void QgsDataDefinedSizeLegendWidget::addSizeClass()
206 double v = QInputDialog::getDouble(
this, tr(
"Add Size Class" ), tr(
"Enter value for a new class" ),
207 0, -2147483647, 2147483647, 6, &ok );
211 QStandardItem *item =
new QStandardItem( QString::number( v ) );
213 QStandardItem *itemLabel =
new QStandardItem( QString::number( v ) );
214 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
215 mSizeClassesModel->sort( 0 );
219 void QgsDataDefinedSizeLegendWidget::removeSizeClass()
221 QModelIndex idx = viewSizeClasses->currentIndex();
222 if ( !idx.isValid() )
225 mSizeClassesModel->removeRow( idx.row() );
229 void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
231 for (
int row = 0; row < mSizeClassesModel->rowCount(); ++row )
233 QStandardItem *item = mSizeClassesModel->item( row, 0 );
234 item->setData( item->text().toDouble() );
237 mSizeClassesModel->sort( 0 );
Symbols are aligned to the center.
Each class (size value) has a separate legend node.
QString label
Label to be shown with the particular symbol size.
void setRenderer(QgsFeatureRenderer *r)
Set renderer which will be invoked to represent this layer.
Definition of one class for the legend.
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0)
Returns an icon preview for a color ramp.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
void setTitle(const QString &title)
Sets title label for data-defined size legend.
void setLegendType(LegendType type)
Sets how the legend should be rendered.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
QMap< QString, QString > QgsStringMap
QgsMarkerSymbol * symbol() const
Returns marker symbol that will be used to draw markers in legend.
Map canvas is a class for displaying all GIS data types on a canvas.
A marker symbol type, for rendering Point and MultiPoint geometries.
static QgsStyle * defaultStyle()
Returns default application-wide style.
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
Namespace with helper functions for layer tree operations.
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...
Contains settings which reflect the context in which a symbol (or renderer) widget is shown...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
double size
Marker size in units used by the symbol (usually millimeters). May be further scaled before rendering...
A store for object properties.
void setVerticalAlignment(VerticalAlignment vAlign)
Sets vertical alignment of symbols - only valid for collapsed legend.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
Symbols are aligned to the bottom.
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...
double scale() const
Returns the last reported scale of the canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
static QgsExpressionContextScope * atlasScope(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...
void setSymbol(QgsMarkerSymbol *symbol SIP_TRANSFER)
Sets marker symbol that will be used to draw markers in legend.
QString title() const
Returns title label for data-defined size legend.
static QgsProject * instance()
Returns the QgsProject singleton instance.
LegendType legendType() const
Returns how the legend should be rendered.
VerticalAlignment verticalAlignment() const
Returns vertical alignment of symbols - only valid for collapsed legend.
static QgsMarkerSymbol * createSimple(const QgsStringMap &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).
Represents a vector layer which manages a vector based data sets.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend...
All classes are rendered within one legend node.
QString authid() const
Returns the authority identifier for the CRS.
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
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...