QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsheatmaprendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsheatmaprendererwidget.cpp
3  ----------------------------
4  begin : November 2014
5  copyright : (C) 2014 Nyall Dawson
6  email : nyall dot dawson 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  ***************************************************************************/
16 #include "qgsheatmaprenderer.h"
17 #include "qgsrendererregistry.h"
19 #include "qgstemporalcontroller.h"
20 
21 #include "qgssymbol.h"
22 
23 #include "qgslogger.h"
24 #include "qgsvectorlayer.h"
25 #include "qgscolorramp.h"
26 #include "qgscolorrampbutton.h"
27 #include "qgsstyle.h"
28 #include "qgsproject.h"
29 #include "qgsmapcanvas.h"
30 #include <QGridLayout>
31 #include <QLabel>
32 
34 {
35  return new QgsHeatmapRendererWidget( layer, style, renderer );
36 }
37 
38 QgsExpressionContext QgsHeatmapRendererWidget::createExpressionContext() const
39 {
40  QgsExpressionContext expContext;
44 
45  if ( auto *lMapCanvas = mContext.mapCanvas() )
46  {
47  expContext << QgsExpressionContextUtils::mapSettingsScope( lMapCanvas->mapSettings() )
48  << new QgsExpressionContextScope( lMapCanvas->expressionContextScope() );
49  if ( const QgsExpressionContextScopeGenerator *generator = dynamic_cast< const QgsExpressionContextScopeGenerator * >( lMapCanvas->temporalController() ) )
50  {
51  expContext << generator->createExpressionContextScope();
52  }
53  }
54  else
55  {
57  }
58 
59  if ( auto *lVectorLayer = vectorLayer() )
60  expContext << QgsExpressionContextUtils::layerScope( lVectorLayer );
61 
62  // additional scopes
63  const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
64  for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
65  {
66  expContext.appendScope( new QgsExpressionContextScope( scope ) );
67  }
68 
69  return expContext;
70 }
71 
73  : QgsRendererWidget( layer, style )
74 
75 {
76  if ( !layer )
77  {
78  return;
79  }
80  // the renderer only applies to point vector layers
81  if ( layer->geometryType() != QgsWkbTypes::PointGeometry )
82  {
83  //setup blank dialog
84  mRenderer = nullptr;
85  QLabel *label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
86  "'%1' is not a point layer and cannot be rendered as a heatmap." )
87  .arg( layer->name() ), this );
88  if ( !layout() )
89  setLayout( new QGridLayout() );
90  layout()->addWidget( label );
91  return;
92  }
93 
94  setupUi( this );
95  connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
96  connect( mRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
97  connect( mMaxSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
98  connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
99  this->layout()->setContentsMargins( 0, 0, 0, 0 );
100 
103  mWeightExpressionWidget->registerExpressionContextGenerator( this );
104  mWeightExpressionWidget->setAllowEmptyFieldName( true );
105 
106  if ( renderer )
107  {
109  }
110  if ( !mRenderer )
111  {
112  mRenderer = std::make_unique< QgsHeatmapRenderer >();
113  if ( renderer )
114  renderer->copyRendererData( mRenderer.get() );
115  }
116 
117  btnColorRamp->setShowGradientOnly( true );
118 
119  connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
120 
121  if ( mRenderer->colorRamp() )
122  {
123  btnColorRamp->blockSignals( true );
124  btnColorRamp->setColorRamp( mRenderer->colorRamp() );
125  btnColorRamp->blockSignals( false );
126  }
127  mRadiusSpinBox->blockSignals( true );
128  mRadiusSpinBox->setValue( mRenderer->radius() );
129  mRadiusSpinBox->blockSignals( false );
130  mRadiusUnitWidget->blockSignals( true );
131  mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
132  mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
133  mRadiusUnitWidget->blockSignals( false );
134  mMaxSpinBox->blockSignals( true );
135  mMaxSpinBox->setValue( mRenderer->maximumValue() );
136  mMaxSpinBox->blockSignals( false );
137  mQualitySlider->blockSignals( true );
138  mQualitySlider->setValue( mRenderer->renderQuality() );
139  mQualitySlider->blockSignals( false );
140 
141  mWeightExpressionWidget->setLayer( layer );
142  mWeightExpressionWidget->setField( mRenderer->weightExpression() );
143  connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
144 }
145 
147 
149 {
150  return mRenderer.get();
151 }
152 
154 {
156  if ( auto *lMapCanvas = context.mapCanvas() )
157  mRadiusUnitWidget->setMapCanvas( lMapCanvas );
158 }
159 
160 void QgsHeatmapRendererWidget::applyColorRamp()
161 {
162  if ( !mRenderer )
163  {
164  return;
165  }
166 
167  QgsColorRamp *ramp = btnColorRamp->colorRamp();
168  if ( !ramp )
169  return;
170 
171  mRenderer->setColorRamp( ramp );
172  emit widgetChanged();
173 }
174 
175 void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
176 {
177  if ( !mRenderer )
178  {
179  return;
180  }
181 
182  mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
183  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
184  emit widgetChanged();
185 }
186 
187 void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
188 {
189  if ( !mRenderer )
190  {
191  return;
192  }
193 
194  mRenderer->setRadius( d );
195  emit widgetChanged();
196 }
197 
198 void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
199 {
200  if ( !mRenderer )
201  {
202  return;
203  }
204 
205  mRenderer->setMaximumValue( d );
206  emit widgetChanged();
207 }
208 
209 void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
210 {
211  if ( !mRenderer )
212  {
213  return;
214  }
215 
216  mRenderer->setRenderQuality( v );
217  emit widgetChanged();
218 }
219 
220 void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
221 {
222  mRenderer->setWeightExpression( expression );
223  emit widgetChanged();
224 }
QgsFeatureRenderer::copyRendererData
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
qgsexpressioncontextutils.h
QgsSymbolWidgetContext::mapCanvas
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
Definition: qgssymbolwidgetcontext.cpp:54
QgsUnitTypes::RenderInches
@ RenderInches
Inches.
Definition: qgsunittypes.h:174
QgsExpressionContextUtils::globalScope
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Definition: qgsexpressioncontextutils.cpp:40
qgsmapcanvas.h
qgsheatmaprendererwidget.h
QgsUnitTypes::RenderPoints
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:173
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
QgsExpressionContextUtils::layerScope
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Definition: qgsexpressioncontextutils.cpp:334
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:35
QgsExpressionContextUtils::mapSettingsScope
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
Definition: qgsexpressioncontextutils.cpp:427
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsExpressionContextScopeGenerator
Abstract interface for generating an expression context scope.
Definition: qgsexpressioncontextscopegenerator.h:28
QgsHeatmapRendererWidget::create
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Static creation method.
Definition: qgsheatmaprendererwidget.cpp:33
QgsRendererWidget::vectorLayer
const QgsVectorLayer * vectorLayer() const
Returns the vector layer associated with the widget.
Definition: qgsrendererwidget.h:78
QgsHeatmapRendererWidget::~QgsHeatmapRendererWidget
~QgsHeatmapRendererWidget() override
qgstemporalcontroller.h
QgsExpressionContextUtils::projectScope
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
Definition: qgsexpressioncontextutils.cpp:291
qgscolorramp.h
QgsFieldExpressionWidget::fieldChanged
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
qgsrendererregistry.h
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
QgsUnitSelectionWidget::changed
void changed()
QgsHeatmapRendererWidget::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: qgsheatmaprendererwidget.cpp:153
qgsheatmaprenderer.h
QgsHeatmapRendererWidget::renderer
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Definition: qgsheatmaprendererwidget.cpp:148
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
qgsstyle.h
QgsUnitTypes::RenderPixels
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:171
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
QgsHeatmapRenderer::convertFromRenderer
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Definition: qgsheatmaprenderer.cpp:379
QgsExpressionContext::appendScope
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Definition: qgsexpressioncontext.cpp:494
qgsvectorlayer.h
QgsHeatmapRendererWidget::QgsHeatmapRendererWidget
QgsHeatmapRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor.
Definition: qgsheatmaprendererwidget.cpp:72
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
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
QgsExpressionContextUtils::atlasScope
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
Definition: qgsexpressioncontextutils.cpp:660
qgscolorrampbutton.h
qgslogger.h
QgsUnitTypes::RenderUnitList
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:240
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
QgsVectorLayer::geometryType
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
Definition: qgsvectorlayer.cpp:720
QgsFieldExpressionWidget
The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains ...
Definition: qgsfieldexpressionwidget.h:46
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
qgssymbol.h
QgsUnitTypes::RenderMapUnits
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:170
qgsproject.h
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
QgsColorRampButton::colorRampChanged
void colorRampChanged()
Emitted whenever a new color ramp is set for the button.