18 #include <QInputDialog> 19 #include <QStyledItemDelegate> 35 , mSizeProperty( ddSize )
36 , mMapCanvas( canvas )
45 radDisabled->setChecked(
true );
50 radSeparated->setChecked(
true );
52 radCollapsed->setChecked(
true );
55 cboAlignSymbols->setCurrentIndex( 0 );
57 cboAlignSymbols->setCurrentIndex( 1 );
64 symbol = overrideSymbol;
65 mOverrideSymbol =
true;
72 mSourceSymbol.reset( symbol );
74 btnChangeSymbol->setEnabled( !mOverrideSymbol );
77 btnChangeSymbol->setIcon( icon );
79 editTitle->setText( ddsLegend ? ddsLegend->
title() : QString() );
81 mSizeClassesModel =
new QStandardItemModel( viewSizeClasses );
82 mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr(
"Value" ) << tr(
"Label" ) );
83 mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
86 groupManualSizeClasses->setChecked( !ddsLegend->
classes().isEmpty() );
89 QStandardItem *item =
new QStandardItem( QString::number( sc.
size ) );
90 item->setData( sc.
size );
91 QStandardItem *itemLabel =
new QStandardItem( sc.
label );
92 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
94 mSizeClassesModel->sort( 0 );
97 connect( btnAddClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
98 connect( btnRemoveClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
100 viewSizeClasses->setItemDelegateForColumn( 0,
new SizeClassDelegate( viewSizeClasses ) );
101 viewSizeClasses->setModel( mSizeClassesModel );
102 connect( mSizeClassesModel, &QStandardItemModel::dataChanged,
this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
105 mPreviewLayer =
new QgsVectorLayer( QStringLiteral(
"Point?crs=EPSG:4326" ), QStringLiteral(
"Preview" ), QStringLiteral(
"memory" ) );
107 mPreviewLayerNode = mPreviewTree->
addLayer( mPreviewLayer );
111 viewLayerTree->setModel( mPreviewModel );
113 connect( cboAlignSymbols,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ] { emit
widgetChanged(); } );
118 connect( btnChangeSymbol, &QPushButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
126 delete mPreviewModel;
128 delete mPreviewLayer;
133 if ( radDisabled->isChecked() )
139 if ( !mOverrideSymbol )
141 ddsLegend->
setSymbol( mSourceSymbol->clone() );
144 ddsLegend->
setTitle( editTitle->text() );
146 if ( groupManualSizeClasses->isChecked() )
148 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
149 for (
int i = 0; i < mSizeClassesModel->rowCount(); ++i )
151 double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
152 QString label = mSizeClassesModel->item( i, 1 )->text();
160 void QgsDataDefinedSizeLegendWidget::updatePreview()
168 viewLayerTree->expandAll();
171 void QgsDataDefinedSizeLegendWidget::changeSymbol()
173 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
184 context.setExpressionContext( &ec );
187 std::unique_ptr<QgsVectorLayer> layer(
new QgsVectorLayer(
"Point?crs=" + crsAuthId, QStringLiteral(
"tmp" ), QStringLiteral(
"memory" ) ) );
192 if ( d.exec() != QDialog::Accepted )
195 mSourceSymbol = std::move( newSymbol );
197 btnChangeSymbol->setIcon( icon );
202 void QgsDataDefinedSizeLegendWidget::addSizeClass()
205 double v = QInputDialog::getDouble(
this, tr(
"Add Size Class" ), tr(
"Enter value for a new class" ),
206 0, -2147483647, 2147483647, 6, &ok );
210 QStandardItem *item =
new QStandardItem( QString::number( v ) );
212 QStandardItem *itemLabel =
new QStandardItem( QString::number( v ) );
213 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
214 mSizeClassesModel->sort( 0 );
218 void QgsDataDefinedSizeLegendWidget::removeSizeClass()
220 QModelIndex idx = viewSizeClasses->currentIndex();
221 if ( !idx.isValid() )
224 mSizeClassesModel->removeRow( idx.row() );
228 void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
230 for (
int row = 0; row < mSizeClassesModel->rowCount(); ++row )
232 QStandardItem *item = mSizeClassesModel->item( row, 0 );
233 item->setData( item->text().toDouble() );
236 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 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.
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.
static QIcon symbolPreviewIcon(QgsSymbol *symbol, QSize size, int padding=0)
Returns an icon preview for a color ramp.
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
Get access to properties used for map rendering.
double outputDpi() const
Return 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
Get 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...