QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgspointclusterrendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointclusterrendererwidget.cpp
3 ---------------------------------
4 begin : February 2016
5 copyright : (C) 2016 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "moc_qgspointclusterrendererwidget.cpp"
21#include "qgsrendererregistry.h"
22#include "qgsstyle.h"
24#include "qgsvectorlayer.h"
25#include "qgsapplication.h"
26#include "qgsmarkersymbol.h"
27
32
34 : QgsRendererWidget( layer, style )
35
36{
37 if ( !layer )
38 {
39 return;
40 }
41
42 //the renderer only applies to point vector layers
44 {
45 //setup blank dialog
46 mRenderer = nullptr;
47 setupBlankUi( layer->name() );
48 return;
49 }
50 setupUi( this );
51 connect( mRendererComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPointClusterRendererWidget::mRendererComboBox_currentIndexChanged );
52 connect( mDistanceSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsPointClusterRendererWidget::mDistanceSpinBox_valueChanged );
53 connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsPointClusterRendererWidget::mDistanceUnitWidget_changed );
54 connect( mRendererSettingsButton, &QPushButton::clicked, this, &QgsPointClusterRendererWidget::mRendererSettingsButton_clicked );
55 this->layout()->setContentsMargins( 0, 0, 0, 0 );
56
59
60 mCenterSymbolToolButton->setSymbolType( Qgis::SymbolType::Marker );
61
62 if ( renderer )
63 {
65 }
66 if ( !mRenderer )
67 {
68 mRenderer = std::make_unique< QgsPointClusterRenderer >();
69 if ( renderer )
70 renderer->copyRendererData( mRenderer.get() );
71 }
72
73 blockAllSignals( true );
74
75 //insert possible renderer types
77 QStringList::const_iterator it = rendererList.constBegin();
78 for ( ; it != rendererList.constEnd(); ++it )
79 {
80 if ( *it != QLatin1String( "pointDisplacement" ) && *it != QLatin1String( "pointCluster" ) && *it != QLatin1String( "heatmapRenderer" ) )
81 {
83 mRendererComboBox->addItem( m->icon(), m->visibleName(), *it );
84 }
85 }
86
87 mDistanceSpinBox->setValue( mRenderer->tolerance() );
88 mDistanceUnitWidget->setUnit( mRenderer->toleranceUnit() );
89 mDistanceUnitWidget->setMapUnitScale( mRenderer->toleranceMapUnitScale() );
90 mCenterSymbolToolButton->setSymbol( mRenderer->clusterSymbol()->clone() );
91
92 blockAllSignals( false );
93
94 //set the appropriate renderer dialog
95 if ( mRenderer->embeddedRenderer() )
96 {
97 const QString rendererName = mRenderer->embeddedRenderer()->type();
98 const int rendererIndex = mRendererComboBox->findData( rendererName );
99 if ( rendererIndex != -1 )
100 {
101 mRendererComboBox->setCurrentIndex( rendererIndex );
102 mRendererComboBox_currentIndexChanged( rendererIndex );
103 }
104 }
105
106 connect( mCenterSymbolToolButton, &QgsSymbolButton::changed, this, &QgsPointClusterRendererWidget::centerSymbolChanged );
107 mCenterSymbolToolButton->setDialogTitle( tr( "Cluster symbol" ) );
108 mCenterSymbolToolButton->setLayer( mLayer );
109 mCenterSymbolToolButton->registerExpressionContextGenerator( this );
110}
111
113
115{
116 return mRenderer.get();
117}
118
120{
122 if ( mDistanceUnitWidget )
123 mDistanceUnitWidget->setMapCanvas( context.mapCanvas() );
124 if ( mCenterSymbolToolButton )
125 {
126 mCenterSymbolToolButton->setMapCanvas( context.mapCanvas() );
127 mCenterSymbolToolButton->setMessageBar( context.messageBar() );
128 }
129}
130
131void QgsPointClusterRendererWidget::mRendererComboBox_currentIndexChanged( int index )
132{
133 const QString rendererId = mRendererComboBox->itemData( index ).toString();
135 if ( m )
136 {
137 // unfortunately renderer conversion is only available through the creation of a widget...
138 const std::unique_ptr< QgsFeatureRenderer > oldRenderer( mRenderer->embeddedRenderer()->clone() );
139 QgsRendererWidget *tempRenderWidget = m->createRendererWidget( mLayer, mStyle, oldRenderer.get() );
140 mRenderer->setEmbeddedRenderer( tempRenderWidget->renderer()->clone() );
141 delete tempRenderWidget;
142 }
143 emit widgetChanged();
144}
145
146void QgsPointClusterRendererWidget::mRendererSettingsButton_clicked()
147{
148 if ( !mRenderer )
149 return;
150
151 QgsRendererAbstractMetadata *m = QgsApplication::rendererRegistry()->rendererMetadata( mRenderer->embeddedRenderer()->type() );
152 if ( m )
153 {
154 QgsRendererWidget *w = m->createRendererWidget( mLayer, mStyle, mRenderer->embeddedRenderer()->clone() );
155 w->setPanelTitle( tr( "Renderer Settings" ) );
156
160 QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
161 scopes << scope;
164 w->setContext( context );
166 connect( w, &QgsPanelWidget::widgetChanged, this, &QgsPointClusterRendererWidget::updateRendererFromWidget );
167 w->setDockMode( this->dockMode() );
168 openPanel( w );
169 }
170}
171
172void QgsPointClusterRendererWidget::mDistanceSpinBox_valueChanged( double d )
173{
174 if ( mRenderer )
175 {
176 mRenderer->setTolerance( d );
177 }
178 emit widgetChanged();
179}
180
181void QgsPointClusterRendererWidget::mDistanceUnitWidget_changed()
182{
183 if ( mRenderer )
184 {
185 mRenderer->setToleranceUnit( mDistanceUnitWidget->unit() );
186 mRenderer->setToleranceMapUnitScale( mDistanceUnitWidget->getMapUnitScale() );
187 }
188 emit widgetChanged();
189}
190
191void QgsPointClusterRendererWidget::blockAllSignals( bool block )
192{
193 mRendererComboBox->blockSignals( block );
194 mCenterSymbolToolButton->blockSignals( block );
195 mDistanceSpinBox->blockSignals( block );
196 mDistanceUnitWidget->blockSignals( block );
197}
198
200{
202 if ( auto *lExpressionContext = mContext.expressionContext() )
203 context = *lExpressionContext;
204 else
209 QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
210 scopes << scope;
211 const auto constScopes = scopes;
212 for ( const QgsExpressionContextScope &s : constScopes )
213 {
215 }
216 return context;
217}
218
219void QgsPointClusterRendererWidget::centerSymbolChanged()
220{
221 mRenderer->setClusterSymbol( mCenterSymbolToolButton->clonedSymbol< QgsMarkerSymbol >() );
222 emit widgetChanged();
223}
224
225void QgsPointClusterRendererWidget::updateRendererFromWidget()
226{
227 QgsRendererWidget *w = qobject_cast<QgsRendererWidget *>( sender() );
228 if ( !w )
229 return;
230
231 mRenderer->setEmbeddedRenderer( w->renderer()->clone() );
232 emit widgetChanged();
233}
234
235void QgsPointClusterRendererWidget::setupBlankUi( const QString &layerName )
236{
237 QGridLayout *layout = new QGridLayout( this );
238 QLabel *label = new QLabel( tr( "The point cluster renderer only applies to (single) point layers. \n'%1' is not a (single) point layer and cannot be displayed by the point cluster renderer." ).arg( layerName ), this );
239 layout->addWidget( label );
240}
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
@ Marker
Marker symbol.
static QgsRendererRegistry * rendererRegistry()
Returns the application's renderer registry, used for managing vector layer renderers.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
static const QString EXPR_CLUSTER_COLOR
Inbuilt variable name for cluster color variable.
Abstract base class for all 2D vector feature renderers.
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
QString name
Definition qgsmaplayer.h:80
A marker symbol type, for rendering Point and MultiPoint geometries.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void widgetChanged()
Emitted when the widget state changes.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
bool dockMode()
Returns the dock mode state.
A widget which allows configuration of the properties for a QgsPointClusterRenderer.
QgsPointClusterRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor for QgsPointClusterRendererWidget.
~QgsPointClusterRendererWidget() override
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Returns a new QgsPointClusterRendererWidget.
static QgsPointClusterRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a QgsPointClusterRenderer from an existing renderer.
Stores metadata about one renderer class.
@ PointLayer
Compatible with point layers.
QIcon icon() const
Returns an icon representing the renderer.
QString visibleName() const
Returns a friendly display name of the renderer.
virtual QgsRendererWidget * createRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *oldRenderer)
Returns new instance of settings widget for the renderer.
QStringList renderersList(QgsRendererAbstractMetadata::LayerTypes layerTypes=QgsRendererAbstractMetadata::All) const
Returns a list of available renderers.
QgsRendererAbstractMetadata * rendererMetadata(const QString &rendererName)
Returns the metadata for a specified renderer.
Base class for renderer settings widgets.
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
QgsSymbolWidgetContext mContext
Context in which widget is shown.
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
virtual QgsFeatureRenderer * renderer()=0
Returns pointer to the renderer (no transfer of ownership)
virtual void disableSymbolLevels()
Disables symbol level modification on the widget.
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
QgsVectorLayer * mLayer
void changed()
Emitted when the symbol's settings are changed.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
QList< QgsExpressionContextScope * > globalProjectAtlasMapLayerScopes(const QgsMapLayer *layer) const
Returns list of scopes: global, project, atlas, map, layer.
void setAdditionalExpressionContextScopes(const QList< QgsExpressionContextScope > &scopes)
Sets a list of additional expression context scopes to show as available within the layer.
QgsExpressionContext * expressionContext() const
Returns the expression context used for the widget, if set.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
void changed()
Emitted when the selected unit is changed, or the definition of the map unit scale is changed.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Single variable definition for use within a QgsExpressionContextScope.