QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 = new QgsHeatmapRenderer();
113  }
114 
115  btnColorRamp->setShowGradientOnly( true );
116 
117  connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
118 
119  if ( mRenderer->colorRamp() )
120  {
121  btnColorRamp->blockSignals( true );
122  btnColorRamp->setColorRamp( mRenderer->colorRamp() );
123  btnColorRamp->blockSignals( false );
124  }
125  mRadiusSpinBox->blockSignals( true );
126  mRadiusSpinBox->setValue( mRenderer->radius() );
127  mRadiusSpinBox->blockSignals( false );
128  mRadiusUnitWidget->blockSignals( true );
129  mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
130  mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
131  mRadiusUnitWidget->blockSignals( false );
132  mMaxSpinBox->blockSignals( true );
133  mMaxSpinBox->setValue( mRenderer->maximumValue() );
134  mMaxSpinBox->blockSignals( false );
135  mQualitySlider->blockSignals( true );
136  mQualitySlider->setValue( mRenderer->renderQuality() );
137  mQualitySlider->blockSignals( false );
138 
139  mWeightExpressionWidget->setLayer( layer );
140  mWeightExpressionWidget->setField( mRenderer->weightExpression() );
141  connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
142 }
143 
145 {
146  return mRenderer;
147 }
148 
150 {
152  if ( auto *lMapCanvas = context.mapCanvas() )
153  mRadiusUnitWidget->setMapCanvas( lMapCanvas );
154 }
155 
156 void QgsHeatmapRendererWidget::applyColorRamp()
157 {
158  if ( !mRenderer )
159  {
160  return;
161  }
162 
163  QgsColorRamp *ramp = btnColorRamp->colorRamp();
164  if ( !ramp )
165  return;
166 
167  mRenderer->setColorRamp( ramp );
168  emit widgetChanged();
169 }
170 
171 void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
172 {
173  if ( !mRenderer )
174  {
175  return;
176  }
177 
178  mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
179  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
180  emit widgetChanged();
181 }
182 
183 void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
184 {
185  if ( !mRenderer )
186  {
187  return;
188  }
189 
190  mRenderer->setRadius( d );
191  emit widgetChanged();
192 }
193 
194 void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
195 {
196  if ( !mRenderer )
197  {
198  return;
199  }
200 
201  mRenderer->setMaximumValue( d );
202  emit widgetChanged();
203 }
204 
205 void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
206 {
207  if ( !mRenderer )
208  {
209  return;
210  }
211 
212  mRenderer->setRenderQuality( v );
213  emit widgetChanged();
214 }
215 
216 void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
217 {
218  mRenderer->setWeightExpression( expression );
219  emit widgetChanged();
220 }
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
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:173
QgsExpressionContextUtils::globalScope
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Definition: qgsexpressioncontextutils.cpp:34
QgsHeatmapRenderer::weightExpression
QString weightExpression() const
Returns the expression used for weighting points when generating the heatmap.
Definition: qgsheatmaprenderer.h:173
qgsmapcanvas.h
QgsHeatmapRenderer::radiusUnit
QgsUnitTypes::RenderUnit radiusUnit() const
Returns the units used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:107
qgsheatmaprendererwidget.h
QgsUnitTypes::RenderPoints
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:172
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:342
QgsExpressionContextUtils::layerScope
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Definition: qgsexpressioncontextutils.cpp:265
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:36
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:357
QgsHeatmapRenderer
A renderer which draws points as a live heatmap.
Definition: qgsheatmaprenderer.h:34
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:468
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
QgsExpressionContextScopeGenerator
Abstract interface for generating an expression context scope.
Definition: qgsexpressioncontextscopegenerator.h:29
QgsHeatmapRenderer::setRadius
void setRadius(const double radius)
Sets the radius for the heatmap.
Definition: qgsheatmaprenderer.h:98
QgsHeatmapRenderer::setRadiusUnit
void setRadiusUnit(const QgsUnitTypes::RenderUnit unit)
Sets the units used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:116
QgsHeatmapRenderer::setMaximumValue
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
Definition: qgsheatmaprenderer.h:150
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:75
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:222
QgsHeatmapRenderer::renderQuality
double renderQuality() const
Returns the render quality used for drawing the heatmap.
Definition: qgsheatmaprenderer.h:158
QgsHeatmapRenderer::setWeightExpression
void setWeightExpression(const QString &expression)
Sets the expression used for weighting points when generating the heatmap.
Definition: qgsheatmaprenderer.h:180
qgscolorramp.h
QgsHeatmapRenderer::colorRamp
QgsColorRamp * colorRamp() const
Returns the color ramp used for shading the heatmap.
Definition: qgsheatmaprenderer.h:73
QgsHeatmapRenderer::setRenderQuality
void setRenderQuality(const int quality)
Sets the render quality used for drawing the heatmap.
Definition: qgsheatmaprenderer.h:166
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.
QgsHeatmapRenderer::maximumValue
double maximumValue() const
Returns the maximum value used for shading the heatmap.
Definition: qgsheatmaprenderer.h:142
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:149
qgsheatmaprenderer.h
QgsHeatmapRendererWidget::renderer
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
Definition: qgsheatmaprendererwidget.cpp:144
QgsHeatmapRenderer::setColorRamp
void setColorRamp(QgsColorRamp *ramp)
Sets the color ramp to use for shading the heatmap.
Definition: qgsheatmaprenderer.cpp:410
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext.
Definition: qgsexpressioncontext.h:112
qgsstyle.h
QgsUnitTypes::RenderPixels
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:170
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
QgsHeatmapRenderer::convertFromRenderer
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Definition: qgsheatmaprenderer.cpp:387
QgsExpressionContext::appendScope
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Definition: qgsexpressioncontext.cpp:490
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:103
QgsStyle
Definition: qgsstyle.h:160
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsRendererWidget::mContext
QgsSymbolWidgetContext mContext
Context in which widget is shown.
Definition: qgsrendererwidget.h:118
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:86
QgsExpressionContextUtils::atlasScope
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
Definition: qgsexpressioncontextutils.cpp:580
QgsHeatmapRenderer::setRadiusMapUnitScale
void setRadiusMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:134
qgscolorrampbutton.h
qgslogger.h
QgsHeatmapRenderer::radius
double radius() const
Returns the radius for the heatmap.
Definition: qgsheatmaprenderer.h:89
QgsUnitTypes::RenderUnitList
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:239
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsVectorLayer::geometryType
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
Definition: qgsvectorlayer.cpp:659
QgsFieldExpressionWidget
The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains ...
Definition: qgsfieldexpressionwidget.h:47
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:337
qgssymbol.h
QgsUnitTypes::RenderMapUnits
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:169
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.
QgsHeatmapRenderer::radiusMapUnitScale
const QgsMapUnitScale & radiusMapUnitScale() const
Returns the map unit scale used for the heatmap's radius.
Definition: qgsheatmaprenderer.h:125