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() );
88 const auto constClasses = ddsLegend->
classes();
91 QStandardItem *item =
new QStandardItem( QString::number( sc.size ) );
92 item->setData( sc.size );
93 QStandardItem *itemLabel =
new QStandardItem( sc.label );
94 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
96 mSizeClassesModel->sort( 0 );
99 connect( btnAddClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
100 connect( btnRemoveClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
102 viewSizeClasses->setItemDelegateForColumn( 0,
new SizeClassDelegate( viewSizeClasses ) );
103 viewSizeClasses->setModel( mSizeClassesModel );
104 connect( mSizeClassesModel, &QStandardItemModel::dataChanged,
this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
108 mPreviewLayer =
new QgsVectorLayer( QStringLiteral(
"Point?crs=EPSG:4326" ), QStringLiteral(
"Preview" ), QStringLiteral(
"memory" ), options );
110 mPreviewLayerNode = mPreviewTree->
addLayer( mPreviewLayer );
114 viewLayerTree->setModel( mPreviewModel );
116 connect( cboAlignSymbols,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ] { emit
widgetChanged(); } );
121 connect( btnChangeSymbol, &QPushButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
129 delete mPreviewModel;
131 delete mPreviewLayer;
136 if ( radDisabled->isChecked() )
142 if ( !mOverrideSymbol )
144 ddsLegend->
setSymbol( mSourceSymbol->clone() );
147 ddsLegend->
setTitle( editTitle->text() );
149 if ( groupManualSizeClasses->isChecked() )
151 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
152 for (
int i = 0; i < mSizeClassesModel->rowCount(); ++i )
154 double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
155 QString label = mSizeClassesModel->item( i, 1 )->text();
163 void QgsDataDefinedSizeLegendWidget::updatePreview()
171 viewLayerTree->expandAll();
174 void QgsDataDefinedSizeLegendWidget::changeSymbol()
176 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
187 context.setExpressionContext( &ec );
191 std::unique_ptr<QgsVectorLayer> layer = qgis::make_unique<QgsVectorLayer>( QStringLiteral(
"Point?crs=%1" ).arg( crsAuthId ),
192 QStringLiteral(
"tmp" ),
193 QStringLiteral(
"memory" ),
199 if ( d.exec() != QDialog::Accepted )
202 mSourceSymbol = std::move( newSymbol );
204 btnChangeSymbol->setIcon( icon );
209 void QgsDataDefinedSizeLegendWidget::addSizeClass()
212 double v = QInputDialog::getDouble(
this, tr(
"Add Size Class" ), tr(
"Enter value for a new class" ),
213 0, -2147483647, 2147483647, 6, &ok );
217 QStandardItem *item =
new QStandardItem( QString::number( v ) );
219 QStandardItem *itemLabel =
new QStandardItem( QString::number( v ) );
220 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
221 mSizeClassesModel->sort( 0 );
225 void QgsDataDefinedSizeLegendWidget::removeSizeClass()
227 QModelIndex idx = viewSizeClasses->currentIndex();
228 if ( !idx.isValid() )
231 mSizeClassesModel->removeRow( idx.row() );
235 void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
237 for (
int row = 0; row < mSizeClassesModel->rowCount(); ++row )
239 QStandardItem *item = mSizeClassesModel->item( row, 0 );
240 item->setData( item->text().toDouble() );
243 mSizeClassesModel->sort( 0 );
Symbols are aligned to the center.
Each class (size value) has a separate legend node.
void setRenderer(QgsFeatureRenderer *r)
Sets renderer which will be invoked to represent this layer.
Definition of one class for the legend.
Setting options for loading vector layers.
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.
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
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...
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.
QgsCoordinateTransformContext transformContext
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 * 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...