QGIS API Documentation  3.6.0-Noosa (5873452)
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"
28 #include "qgssymbollayer.h"
30 #include "qgsvectorlayer.h"
32 
33 
35  : QgsPanelWidget( parent )
36  , mSizeProperty( ddSize )
37  , mMapCanvas( canvas )
38 {
39  setupUi( this );
40  setPanelTitle( tr( "Data-defined Size Legend" ) );
41 
42  QgsMarkerSymbol *symbol = nullptr;
43 
44  if ( !ddsLegend )
45  {
46  radDisabled->setChecked( true );
47  }
48  else
49  {
51  radSeparated->setChecked( true );
52  else
53  radCollapsed->setChecked( true );
54 
56  cboAlignSymbols->setCurrentIndex( 0 );
57  else
58  cboAlignSymbols->setCurrentIndex( 1 );
59 
60  symbol = ddsLegend->symbol() ? ddsLegend->symbol()->clone() : nullptr; // may be null (undefined)
61  }
62 
63  if ( overrideSymbol )
64  {
65  symbol = overrideSymbol; // takes ownership
66  mOverrideSymbol = true;
67  }
68 
69  if ( !symbol )
70  {
72  }
73  mSourceSymbol.reset( symbol );
74 
75  btnChangeSymbol->setEnabled( !mOverrideSymbol );
76 
77  QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( mSourceSymbol.get(), btnChangeSymbol->iconSize() );
78  btnChangeSymbol->setIcon( icon );
79 
80  editTitle->setText( ddsLegend ? ddsLegend->title() : QString() );
81 
82  mSizeClassesModel = new QStandardItemModel( viewSizeClasses );
83  mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr( "Value" ) << tr( "Label" ) );
84  mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
85  if ( ddsLegend )
86  {
87  groupManualSizeClasses->setChecked( !ddsLegend->classes().isEmpty() );
88  Q_FOREACH ( const QgsDataDefinedSizeLegend::SizeClass &sc, ddsLegend->classes() )
89  {
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 );
94  }
95  mSizeClassesModel->sort( 0 );
96  }
97 
98  connect( btnAddClass, &QToolButton::clicked, this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
99  connect( btnRemoveClass, &QToolButton::clicked, this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
100 
101  viewSizeClasses->setItemDelegateForColumn( 0, new SizeClassDelegate( viewSizeClasses ) );
102  viewSizeClasses->setModel( mSizeClassesModel );
103  connect( mSizeClassesModel, &QStandardItemModel::dataChanged, this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
104 
105  // prepare layer and model to preview legend
106  mPreviewLayer = new QgsVectorLayer( QStringLiteral( "Point?crs=EPSG:4326" ), QStringLiteral( "Preview" ), QStringLiteral( "memory" ) );
107  mPreviewTree = new QgsLayerTree;
108  mPreviewLayerNode = mPreviewTree->addLayer( mPreviewLayer ); // node owned by the tree
109  mPreviewModel = new QgsLayerTreeModel( mPreviewTree );
110  if ( canvas )
111  mPreviewModel->setLegendMapViewData( canvas->mapUnitsPerPixel(), canvas->mapSettings().outputDpi(), canvas->scale() );
112  viewLayerTree->setModel( mPreviewModel );
113 
114  connect( cboAlignSymbols, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ] { emit widgetChanged(); } );
115  connect( radDisabled, &QRadioButton::clicked, this, &QgsPanelWidget::widgetChanged );
116  connect( radSeparated, &QRadioButton::clicked, this, &QgsPanelWidget::widgetChanged );
117  connect( radCollapsed, &QRadioButton::clicked, this, &QgsPanelWidget::widgetChanged );
118  connect( groupManualSizeClasses, &QGroupBox::clicked, this, &QgsPanelWidget::widgetChanged );
119  connect( btnChangeSymbol, &QPushButton::clicked, this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
120  connect( editTitle, &QLineEdit::textChanged, this, &QgsPanelWidget::widgetChanged );
121  connect( this, &QgsPanelWidget::widgetChanged, this, &QgsDataDefinedSizeLegendWidget::updatePreview );
122  updatePreview();
123 }
124 
126 {
127  delete mPreviewModel;
128  delete mPreviewTree;
129  delete mPreviewLayer;
130 }
131 
133 {
134  if ( radDisabled->isChecked() )
135  return nullptr;
136 
139  ddsLegend->setVerticalAlignment( cboAlignSymbols->currentIndex() == 0 ? QgsDataDefinedSizeLegend::AlignBottom : QgsDataDefinedSizeLegend::AlignCenter );
140  if ( !mOverrideSymbol )
141  {
142  ddsLegend->setSymbol( mSourceSymbol->clone() );
143  }
144 
145  ddsLegend->setTitle( editTitle->text() );
146 
147  if ( groupManualSizeClasses->isChecked() )
148  {
149  QList<QgsDataDefinedSizeLegend::SizeClass> classes;
150  for ( int i = 0; i < mSizeClassesModel->rowCount(); ++i )
151  {
152  double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
153  QString label = mSizeClassesModel->item( i, 1 )->text();
154  classes << QgsDataDefinedSizeLegend::SizeClass( value, label );
155  }
156  ddsLegend->setClasses( classes );
157  }
158  return ddsLegend;
159 }
160 
161 void QgsDataDefinedSizeLegendWidget::updatePreview()
162 {
163  QgsMarkerSymbol *symbol = mSourceSymbol->clone();
164  symbol->setDataDefinedSize( mSizeProperty );
167  mPreviewLayer->setRenderer( r );
168  mPreviewModel->refreshLayerLegend( mPreviewLayerNode );
169  viewLayerTree->expandAll();
170 }
171 
172 void QgsDataDefinedSizeLegendWidget::changeSymbol()
173 {
174  std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
175  QgsSymbolWidgetContext context;
176  if ( mMapCanvas )
177  context.setMapCanvas( mMapCanvas );
178 
183  if ( mMapCanvas )
185  context.setExpressionContext( &ec );
186 
187  QString crsAuthId = mMapCanvas ? mMapCanvas->mapSettings().destinationCrs().authid() : QString();
188  std::unique_ptr<QgsVectorLayer> layer( new QgsVectorLayer( "Point?crs=" + crsAuthId, QStringLiteral( "tmp" ), QStringLiteral( "memory" ) ) );
189 
190  QgsSymbolSelectorDialog d( newSymbol.get(), QgsStyle::defaultStyle(), layer.get(), this );
191  d.setContext( context );
192 
193  if ( d.exec() != QDialog::Accepted )
194  return;
195 
196  mSourceSymbol = std::move( newSymbol );
197  QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( mSourceSymbol.get(), btnChangeSymbol->iconSize() );
198  btnChangeSymbol->setIcon( icon );
199 
200  emit widgetChanged();
201 }
202 
203 void QgsDataDefinedSizeLegendWidget::addSizeClass()
204 {
205  bool ok;
206  double v = QInputDialog::getDouble( this, tr( "Add Size Class" ), tr( "Enter value for a new class" ),
207  0, -2147483647, 2147483647, 6, &ok );
208  if ( !ok )
209  return;
210 
211  QStandardItem *item = new QStandardItem( QString::number( v ) );
212  item->setData( v );
213  QStandardItem *itemLabel = new QStandardItem( QString::number( v ) );
214  mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
215  mSizeClassesModel->sort( 0 );
216  emit widgetChanged();
217 }
218 
219 void QgsDataDefinedSizeLegendWidget::removeSizeClass()
220 {
221  QModelIndex idx = viewSizeClasses->currentIndex();
222  if ( !idx.isValid() )
223  return;
224 
225  mSizeClassesModel->removeRow( idx.row() );
226  emit widgetChanged();
227 }
228 
229 void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
230 {
231  for ( int row = 0; row < mSizeClassesModel->rowCount(); ++row )
232  {
233  QStandardItem *item = mSizeClassesModel->item( row, 0 );
234  item->setData( item->text().toDouble() );
235  }
236 
237  mSizeClassesModel->sort( 0 );
238  emit widgetChanged();
239 }
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.
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 ...
Base class for any widget that can be shown as a inline panel.
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
Definition: qgis.h:587
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.
Definition: qgsmapcanvas.h:73
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:732
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:46
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.
Definition: qgslayertree.h:32
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.
Definition: qgsproperty.h:229
void widgetChanged()
Emitted when the widget state changes.
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.
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration as set up in the dialog (may be null). Ownership is passed to the caller...
QString title() const
Returns title label for data-defined size legend.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:430
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.
Definition: qgssymbol.cpp:1126
void setDataDefinedSize(const QgsProperty &property)
Set data defined size for whole symbol (including all symbol layers).
Definition: qgssymbol.cpp:1392
Represents a vector layer which manages a vector based data sets.
Object that keeps configuration of appearance of marker symbol&#39;s data-defined size in legend...
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
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.
Definition: qgssymbol.cpp:1585
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...