QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
20 #include "qgsrendererregistry.h"
21 #include "qgsfield.h"
22 #include "qgsstyle.h"
24 #include "qgssymbollayerutils.h"
25 #include "qgsvectorlayer.h"
26 #include "qgsguiutils.h"
27 #include "qgsapplication.h"
28 #include "qgsmarkersymbol.h"
29 
31 {
32  return new QgsPointClusterRendererWidget( layer, style, renderer );
33 }
34 
36  : QgsRendererWidget( layer, style )
37 
38 {
39  if ( !layer )
40  {
41  return;
42  }
43 
44  //the renderer only applies to point vector layers
46  {
47  //setup blank dialog
48  mRenderer = nullptr;
49  setupBlankUi( layer->name() );
50  return;
51  }
52  setupUi( this );
53  connect( mRendererComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPointClusterRendererWidget::mRendererComboBox_currentIndexChanged );
54  connect( mDistanceSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsPointClusterRendererWidget::mDistanceSpinBox_valueChanged );
55  connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsPointClusterRendererWidget::mDistanceUnitWidget_changed );
56  connect( mRendererSettingsButton, &QPushButton::clicked, this, &QgsPointClusterRendererWidget::mRendererSettingsButton_clicked );
57  this->layout()->setContentsMargins( 0, 0, 0, 0 );
58 
61 
62  mCenterSymbolToolButton->setSymbolType( Qgis::SymbolType::Marker );
63 
64  if ( renderer )
65  {
67  }
68  if ( !mRenderer )
69  {
70  mRenderer = std::make_unique< QgsPointClusterRenderer >();
71  if ( renderer )
72  renderer->copyRendererData( mRenderer.get() );
73  }
74 
75  blockAllSignals( true );
76 
77  //insert possible renderer types
79  QStringList::const_iterator it = rendererList.constBegin();
80  for ( ; it != rendererList.constEnd(); ++it )
81  {
82  if ( *it != QLatin1String( "pointDisplacement" ) && *it != QLatin1String( "pointCluster" ) && *it != QLatin1String( "heatmapRenderer" ) )
83  {
85  mRendererComboBox->addItem( m->icon(), m->visibleName(), *it );
86  }
87  }
88 
89  mDistanceSpinBox->setValue( mRenderer->tolerance() );
90  mDistanceUnitWidget->setUnit( mRenderer->toleranceUnit() );
91  mDistanceUnitWidget->setMapUnitScale( mRenderer->toleranceMapUnitScale() );
92  mCenterSymbolToolButton->setSymbol( mRenderer->clusterSymbol()->clone() );
93 
94  blockAllSignals( false );
95 
96  //set the appropriate renderer dialog
97  if ( mRenderer->embeddedRenderer() )
98  {
99  const QString rendererName = mRenderer->embeddedRenderer()->type();
100  const int rendererIndex = mRendererComboBox->findData( rendererName );
101  if ( rendererIndex != -1 )
102  {
103  mRendererComboBox->setCurrentIndex( rendererIndex );
104  mRendererComboBox_currentIndexChanged( rendererIndex );
105  }
106  }
107 
108  connect( mCenterSymbolToolButton, &QgsSymbolButton::changed, this, &QgsPointClusterRendererWidget::centerSymbolChanged );
109  mCenterSymbolToolButton->setDialogTitle( tr( "Cluster symbol" ) );
110  mCenterSymbolToolButton->setLayer( mLayer );
111  mCenterSymbolToolButton->registerExpressionContextGenerator( this );
112 }
113 
115 
117 {
118  return mRenderer.get();
119 }
120 
122 {
124  if ( mDistanceUnitWidget )
125  mDistanceUnitWidget->setMapCanvas( context.mapCanvas() );
126  if ( mCenterSymbolToolButton )
127  {
128  mCenterSymbolToolButton->setMapCanvas( context.mapCanvas() );
129  mCenterSymbolToolButton->setMessageBar( context.messageBar() );
130  }
131 }
132 
133 void QgsPointClusterRendererWidget::mRendererComboBox_currentIndexChanged( int index )
134 {
135  const QString rendererId = mRendererComboBox->itemData( index ).toString();
137  if ( m )
138  {
139  // unfortunately renderer conversion is only available through the creation of a widget...
140  const std::unique_ptr< QgsFeatureRenderer > oldRenderer( mRenderer->embeddedRenderer()->clone() );
141  QgsRendererWidget *tempRenderWidget = m->createRendererWidget( mLayer, mStyle, oldRenderer.get() );
142  mRenderer->setEmbeddedRenderer( tempRenderWidget->renderer()->clone() );
143  delete tempRenderWidget;
144  }
145  emit widgetChanged();
146 }
147 
148 void QgsPointClusterRendererWidget::mRendererSettingsButton_clicked()
149 {
150  if ( !mRenderer )
151  return;
152 
153  QgsRendererAbstractMetadata *m = QgsApplication::rendererRegistry()->rendererMetadata( mRenderer->embeddedRenderer()->type() );
154  if ( m )
155  {
156  QgsRendererWidget *w = m->createRendererWidget( mLayer, mStyle, mRenderer->embeddedRenderer()->clone() );
157  w->setPanelTitle( tr( "Renderer Settings" ) );
158 
162  QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
163  scopes << scope;
166  w->setContext( context );
167  w->disableSymbolLevels();
168  connect( w, &QgsPanelWidget::widgetChanged, this, &QgsPointClusterRendererWidget::updateRendererFromWidget );
169  w->setDockMode( this->dockMode() );
170  openPanel( w );
171  }
172 }
173 
174 void QgsPointClusterRendererWidget::mDistanceSpinBox_valueChanged( double d )
175 {
176  if ( mRenderer )
177  {
178  mRenderer->setTolerance( d );
179  }
180  emit widgetChanged();
181 }
182 
183 void QgsPointClusterRendererWidget::mDistanceUnitWidget_changed()
184 {
185  if ( mRenderer )
186  {
187  mRenderer->setToleranceUnit( mDistanceUnitWidget->unit() );
188  mRenderer->setToleranceMapUnitScale( mDistanceUnitWidget->getMapUnitScale() );
189  }
190  emit widgetChanged();
191 }
192 
193 void QgsPointClusterRendererWidget::blockAllSignals( bool block )
194 {
195  mRendererComboBox->blockSignals( block );
196  mCenterSymbolToolButton->blockSignals( block );
197  mDistanceSpinBox->blockSignals( block );
198  mDistanceUnitWidget->blockSignals( block );
199 }
200 
202 {
204  if ( auto *lExpressionContext = mContext.expressionContext() )
205  context = *lExpressionContext;
206  else
211  QList< QgsExpressionContextScope > scopes = mContext.additionalExpressionContextScopes();
212  scopes << scope;
213  const auto constScopes = scopes;
214  for ( const QgsExpressionContextScope &s : constScopes )
215  {
217  }
218  return context;
219 }
220 
221 void QgsPointClusterRendererWidget::centerSymbolChanged()
222 {
223  mRenderer->setClusterSymbol( mCenterSymbolToolButton->clonedSymbol< QgsMarkerSymbol >() );
224  emit widgetChanged();
225 }
226 
227 void QgsPointClusterRendererWidget::updateRendererFromWidget()
228 {
229  QgsRendererWidget *w = qobject_cast<QgsRendererWidget *>( sender() );
230  if ( !w )
231  return;
232 
233  mRenderer->setEmbeddedRenderer( w->renderer()->clone() );
234  emit widgetChanged();
235 }
236 
237 void QgsPointClusterRendererWidget::setupBlankUi( const QString &layerName )
238 {
239  QGridLayout *layout = new QGridLayout( this );
240  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 );
241  layout->addWidget( label );
242 }
QgsRendererWidget::renderer
virtual QgsFeatureRenderer * renderer()=0
Returns pointer to the renderer (no transfer of ownership)
QgsFeatureRenderer::copyRendererData
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
QgsSymbolWidgetContext::globalProjectAtlasMapLayerScopes
QList< QgsExpressionContextScope * > globalProjectAtlasMapLayerScopes(const QgsMapLayer *layer) const
Returns list of scopes: global, project, atlas, map, layer.
Definition: qgssymbolwidgetcontext.cpp:92
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsSymbolWidgetContext::mapCanvas
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
Definition: qgssymbolwidgetcontext.cpp:54
QgsRendererWidget::mStyle
QgsStyle * mStyle
Definition: qgsrendererwidget.h:113
QgsApplication::rendererRegistry
static QgsRendererRegistry * rendererRegistry()
Returns the application's renderer registry, used for managing vector layer renderers.
Definition: qgsapplication.cpp:2320
QgsUnitTypes::RenderInches
@ RenderInches
Inches.
Definition: qgsunittypes.h:174
QgsSymbolButton::changed
void changed()
Emitted when the symbol's settings are changed.
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:725
QgsExpressionContextScope::addVariable
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Definition: qgsexpressioncontext.cpp:97
QgsPointClusterRendererWidget::create
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Returns a new QgsPointClusterRendererWidget.
Definition: qgspointclusterrendererwidget.cpp:30
QgsRendererWidget::mLayer
QgsVectorLayer * mLayer
Definition: qgsrendererwidget.h:112
QgsExpressionContext::EXPR_CLUSTER_COLOR
static const QString EXPR_CLUSTER_COLOR
Inbuilt variable name for cluster color variable.
Definition: qgsexpressioncontext.h:827
QgsRendererAbstractMetadata::PointLayer
@ PointLayer
Compatible with point layers.
Definition: qgsrendererregistry.h:51
QgsUnitTypes::RenderPoints
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:173
QgsRendererAbstractMetadata::visibleName
QString visibleName() const
Returns a friendly display name of the renderer.
Definition: qgsrendererregistry.h:82
qgssymbollayerutils.h
QgsRendererWidget::context
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
Definition: qgsrendererwidget.cpp:346
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:35
QgsPointClusterRendererWidget::renderer
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Definition: qgspointclusterrendererwidget.cpp:116
QgsPanelWidget::openPanel
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Definition: qgspanelwidget.cpp:84
QgsPointClusterRenderer::convertFromRenderer
static QgsPointClusterRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a QgsPointClusterRenderer from an existing renderer.
Definition: qgspointclusterrenderer.cpp:179
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsPanelWidget::dockMode
bool dockMode()
Returns the dock mode state.
Definition: qgspanelwidget.h:93
QgsRendererWidget::disableSymbolLevels
virtual void disableSymbolLevels()
Disables symbol level modification on the widget.
Definition: qgsrendererwidget.cpp:369
qgsapplication.h
QgsSymbolWidgetContext::setAdditionalExpressionContextScopes
void setAdditionalExpressionContextScopes(const QList< QgsExpressionContextScope > &scopes)
Sets a list of additional expression context scopes to show as available within the layer.
Definition: qgssymbolwidgetcontext.cpp:82
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgsmarkersymbol.h:30
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
qgspointclusterrendererwidget.h
qgsrendererregistry.h
QgsRendererWidget::setDockMode
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Definition: qgsrendererwidget.cpp:356
QgsPointClusterRendererWidget::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgspointclusterrendererwidget.cpp:201
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
QgsUnitSelectionWidget::changed
void changed()
QgsPointClusterRendererWidget::QgsPointClusterRendererWidget
QgsPointClusterRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor for QgsPointClusterRendererWidget.
Definition: qgspointclusterrendererwidget.cpp:35
QgsSymbolWidgetContext::expressionContext
QgsExpressionContext * expressionContext() const
Returns the expression context used for the widget, if set.
Definition: qgssymbolwidgetcontext.cpp:77
QgsRendererRegistry::renderersList
QStringList renderersList(QgsRendererAbstractMetadata::LayerTypes layerTypes=QgsRendererAbstractMetadata::All) const
Returns a list of available renderers.
Definition: qgsrendererregistry.cpp:137
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
qgssymbolselectordialog.h
qgsstyle.h
QgsUnitTypes::RenderPixels
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:171
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
qgsvectorlayer.h
QgsExpressionContext::EXPR_CLUSTER_SIZE
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
Definition: qgsexpressioncontext.h:825
QgsPointClusterRendererWidget::~QgsPointClusterRendererWidget
~QgsPointClusterRendererWidget() override
QgsWkbTypes::PointGeometry
@ PointGeometry
Definition: qgswkbtypes.h:142
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsStyle
Definition: qgsstyle.h:159
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
qgsmarkersymbol.h
qgsfield.h
QgsRendererWidget::mContext
QgsSymbolWidgetContext mContext
Context in which widget is shown.
Definition: qgsrendererwidget.h:131
QgsUnitTypes::RenderMetersInMapUnits
@ RenderMetersInMapUnits
Meters value as Map units.
Definition: qgsunittypes.h:176
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:76
QgsWkbTypes::geometryType
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
QgsSymbolWidgetContext::messageBar
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
Definition: qgssymbolwidgetcontext.cpp:64
QgsRendererAbstractMetadata::icon
QIcon icon() const
Returns an icon representing the renderer.
Definition: qgsrendererregistry.h:88
qgspointclusterrenderer.h
QgsUnitTypes::RenderUnitList
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:240
qgsguiutils.h
QgsRendererAbstractMetadata::createRendererWidget
virtual QgsRendererWidget * createRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *oldRenderer)
Returns new instance of settings widget for the renderer.
Definition: qgsrendererregistry.h:115
QgsExpressionContextScope::StaticVariable
Single variable definition for use within a QgsExpressionContextScope.
Definition: qgsexpressioncontext.h:120
QgsRendererRegistry::rendererMetadata
QgsRendererAbstractMetadata * rendererMetadata(const QString &rendererName)
Returns the metadata for a specified renderer.
Definition: qgsrendererregistry.cpp:132
QgsRendererAbstractMetadata
Stores metadata about one renderer class.
Definition: qgsrendererregistry.h:41
QgsRendererWidget::setContext
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
Definition: qgsrendererwidget.cpp:341
QgsUnitTypes::RenderMapUnits
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:170
Qgis::SymbolType::Marker
@ Marker
Marker symbol.
QgsSymbolWidgetContext::additionalExpressionContextScopes
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
Definition: qgssymbolwidgetcontext.cpp:87
QgsPointClusterRendererWidget::setContext
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
Definition: qgspointclusterrendererwidget.cpp:121