QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsdatadefinedsizelegendwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatadefinedsizelegendwidget.cpp
3 --------------------------------------
4 Date : June 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <QInputDialog>
19#include <QStyledItemDelegate>
20
22#include "qgslayertree.h"
23#include "qgslayertreemodel.h"
24#include "qgsmapcanvas.h"
26#include "qgsstyle.h"
27#include "qgssymbol.h"
29#include "qgsvectorlayer.h"
31#include "qgsdoublevalidator.h"
32#include "qgsmarkersymbol.h"
33#include "qgslinesymbol.h"
34
36 : QgsPanelWidget( parent )
37 , mSizeProperty( ddSize )
38 , mMapCanvas( canvas )
39{
40 setupUi( this );
41 setPanelTitle( tr( "Data-defined Size Legend" ) );
42
43 mLineSymbolButton->setSymbolType( Qgis::SymbolType::Line );
44
45 QgsMarkerSymbol *symbol = nullptr;
46
47 if ( !ddsLegend )
48 {
49 radDisabled->setChecked( true );
50 }
51 else
52 {
54 radSeparated->setChecked( true );
55 else
56 radCollapsed->setChecked( true );
57
59 cboAlignSymbols->setCurrentIndex( 0 );
60 else
61 cboAlignSymbols->setCurrentIndex( 1 );
62
63 if ( ddsLegend->lineSymbol() )
64 mLineSymbolButton->setSymbol( ddsLegend->lineSymbol()->clone() );
65
66 symbol = ddsLegend->symbol() ? ddsLegend->symbol()->clone() : nullptr; // may be null (undefined)
67 }
68 groupBoxOptions->setEnabled( radSeparated->isChecked() );
69
70 if ( overrideSymbol )
71 {
72 symbol = overrideSymbol; // takes ownership
73 mOverrideSymbol = true;
74 }
75
76 if ( !symbol )
77 {
78 symbol = QgsMarkerSymbol::createSimple( QVariantMap() );
79 }
80 mSourceSymbol.reset( symbol );
81
82 btnChangeSymbol->setEnabled( !mOverrideSymbol );
83
84 const QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( mSourceSymbol.get(), btnChangeSymbol->iconSize(), 0, nullptr, QgsScreenProperties( screen() ) );
85 btnChangeSymbol->setIcon( icon );
86
87 editTitle->setText( ddsLegend ? ddsLegend->title() : QString() );
88
89 mSizeClassesModel = new QStandardItemModel( viewSizeClasses );
90 mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr( "Value" ) << tr( "Label" ) );
91 mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
92 if ( ddsLegend )
93 {
94 groupManualSizeClasses->setChecked( !ddsLegend->classes().isEmpty() );
95 const auto constClasses = ddsLegend->classes();
96 for ( const QgsDataDefinedSizeLegend::SizeClass &sc : constClasses )
97 {
98 QStandardItem *item = new QStandardItem( QLocale().toString( sc.size ) );
99 item->setData( sc.size );
100 QStandardItem *itemLabel = new QStandardItem( sc.label );
101 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
102 }
103 mSizeClassesModel->sort( 0 );
104 }
105
106 connect( btnAddClass, &QToolButton::clicked, this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
107 connect( btnRemoveClass, &QToolButton::clicked, this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
108
109 viewSizeClasses->setItemDelegateForColumn( 0, new SizeClassDelegate( viewSizeClasses ) );
110 viewSizeClasses->setModel( mSizeClassesModel );
111 connect( mSizeClassesModel, &QStandardItemModel::dataChanged, this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
112
113 // prepare layer and model to preview legend
115 mPreviewLayer = new QgsVectorLayer( QStringLiteral( "Point?crs=EPSG:4326" ), QStringLiteral( "Preview" ), QStringLiteral( "memory" ), options );
116 mPreviewTree = new QgsLayerTree;
117 mPreviewLayerNode = mPreviewTree->addLayer( mPreviewLayer ); // node owned by the tree
118 mPreviewModel = new QgsLayerTreeModel( mPreviewTree );
119 if ( canvas )
120 mPreviewModel->setLegendMapViewData( canvas->mapUnitsPerPixel(), canvas->mapSettings().outputDpi(), canvas->scale() );
121 viewLayerTree->setModel( mPreviewModel );
122
123 connect( cboAlignSymbols, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ] { emit widgetChanged(); } );
124 connect( radDisabled, &QRadioButton::clicked, this, &QgsPanelWidget::widgetChanged );
125 connect( radSeparated, &QRadioButton::clicked, this, &QgsPanelWidget::widgetChanged );
126 connect( radCollapsed, &QRadioButton::clicked, this, &QgsPanelWidget::widgetChanged );
127 connect( groupManualSizeClasses, &QGroupBox::clicked, this, &QgsPanelWidget::widgetChanged );
128 connect( btnChangeSymbol, &QPushButton::clicked, this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
129 connect( editTitle, &QLineEdit::textChanged, this, &QgsPanelWidget::widgetChanged );
130 connect( mLineSymbolButton, &QgsSymbolButton::changed, this, &QgsPanelWidget::widgetChanged );
131 connect( this, &QgsPanelWidget::widgetChanged, this, &QgsDataDefinedSizeLegendWidget::updatePreview );
132 connect( radCollapsed, &QRadioButton::toggled, this, [ = ]( bool toggled ) {groupBoxOptions->setEnabled( toggled );} );
133 updatePreview();
134}
135
137{
138 delete mPreviewModel;
139 delete mPreviewTree;
140 delete mPreviewLayer;
141}
142
144{
145 if ( radDisabled->isChecked() )
146 return nullptr;
147
150 ddsLegend->setVerticalAlignment( cboAlignSymbols->currentIndex() == 0 ? QgsDataDefinedSizeLegend::AlignBottom : QgsDataDefinedSizeLegend::AlignCenter );
151 if ( !mOverrideSymbol )
152 {
153 ddsLegend->setSymbol( mSourceSymbol->clone() );
154 }
155
156 ddsLegend->setTitle( editTitle->text() );
157
158 if ( groupManualSizeClasses->isChecked() )
159 {
160 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
161 for ( int i = 0; i < mSizeClassesModel->rowCount(); ++i )
162 {
163 const double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
164 const QString label = mSizeClassesModel->item( i, 1 )->text();
165 classes << QgsDataDefinedSizeLegend::SizeClass( value, label );
166 }
167 ddsLegend->setClasses( classes );
168 }
169
170 ddsLegend->setLineSymbol( mLineSymbolButton->clonedSymbol< QgsLineSymbol >() );
171 return ddsLegend;
172}
173
174void QgsDataDefinedSizeLegendWidget::updatePreview()
175{
176 QgsMarkerSymbol *symbol = mSourceSymbol->clone();
177 symbol->setDataDefinedSize( mSizeProperty );
180 mPreviewLayer->setRenderer( r );
181 mPreviewModel->refreshLayerLegend( mPreviewLayerNode );
182 viewLayerTree->expandAll();
183}
184
185void QgsDataDefinedSizeLegendWidget::changeSymbol()
186{
187 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
189 if ( mMapCanvas )
190 context.setMapCanvas( mMapCanvas );
191
193 if ( mMapCanvas )
194 {
195 ec = mMapCanvas->createExpressionContext();
196 }
197 else
198 {
203 }
204 context.setExpressionContext( &ec );
205
206 const QString crsAuthId = mMapCanvas ? mMapCanvas->mapSettings().destinationCrs().authid() : QString();
208 const std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( QStringLiteral( "Point?crs=%1" ).arg( crsAuthId ),
209 QStringLiteral( "tmp" ),
210 QStringLiteral( "memory" ),
211 options ) ;
212
213 QgsSymbolSelectorDialog d( newSymbol.get(), QgsStyle::defaultStyle(), layer.get(), this );
214 d.setContext( context );
215
216 if ( d.exec() != QDialog::Accepted )
217 return;
218
219 mSourceSymbol = std::move( newSymbol );
220 const QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( mSourceSymbol.get(), btnChangeSymbol->iconSize(), 0, nullptr, QgsScreenProperties( screen() ) );
221 btnChangeSymbol->setIcon( icon );
222
223 emit widgetChanged();
224}
225
226void QgsDataDefinedSizeLegendWidget::addSizeClass()
227{
228 bool ok;
229 const double v = QInputDialog::getDouble( this, tr( "Add Size Class" ), tr( "Enter value for a new class" ),
230 0, -2147483647, 2147483647, 6, &ok );
231 if ( !ok )
232 return;
233
234 QStandardItem *item = new QStandardItem( QLocale().toString( v ) );
235 item->setData( v );
236 QStandardItem *itemLabel = new QStandardItem( QLocale().toString( v ) );
237 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
238 mSizeClassesModel->sort( 0 );
239 emit widgetChanged();
240}
241
242void QgsDataDefinedSizeLegendWidget::removeSizeClass()
243{
244 const QModelIndex idx = viewSizeClasses->currentIndex();
245 if ( !idx.isValid() )
246 return;
247
248 mSizeClassesModel->removeRow( idx.row() );
249 emit widgetChanged();
250}
251
252void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
253{
254 for ( int row = 0; row < mSizeClassesModel->rowCount(); ++row )
255 {
256 QStandardItem *item = mSizeClassesModel->item( row, 0 );
257 item->setData( QgsDoubleValidator::toDouble( item->text() ) );
258 }
259
260 mSizeClassesModel->sort( 0 );
261 emit widgetChanged();
262}
@ Line
Line symbol.
QgsDataDefinedSizeLegendWidget(const QgsDataDefinedSizeLegend *ddsLegend, const QgsProperty &ddSize, QgsMarkerSymbol *overrideSymbol, QgsMapCanvas *canvas=nullptr, QWidget *parent=nullptr)
Creates the dialog and initializes the content to what is passed in the legend configuration (may be ...
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration as set up in the dialog (may be nullptr). Ownership is passed to the caller.
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 double toDouble(const QString &input, bool *ok)
Converts input string to double value.
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.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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.
The QgsMapSettings class contains configuration for rendering of the map.
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) const
Set data defined size for whole symbol (including all symbol layers).
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
Base class for any widget that can be shown as a inline panel.
void widgetChanged()
Emitted when the widget state changes.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
Definition qgsproject.h:113
A store for object properties.
Stores properties relating to a screen.
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:145
void changed()
Emitted when the symbol's settings are changed.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Returns an icon preview for a color ramp.
A dialog that can be used to select and build a symbol.
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.