QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
20 #include "qgssymbol.h"
21 
22 #include "qgslogger.h"
23 #include "qgsvectorlayer.h"
24 #include "qgscolorramp.h"
25 #include "qgscolorrampbutton.h"
26 #include "qgsstyle.h"
27 #include "qgsproject.h"
28 #include "qgsmapcanvas.h"
29 #include <QGridLayout>
30 #include <QLabel>
31 
33 {
34  return new QgsHeatmapRendererWidget( layer, style, renderer );
35 }
36 
37 QgsExpressionContext QgsHeatmapRendererWidget::createExpressionContext() const
38 {
39  QgsExpressionContext expContext;
43 
44  if ( mContext.mapCanvas() )
45  {
48  }
49  else
50  {
52  }
53 
54  if ( vectorLayer() )
56 
57  // additional scopes
58  const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
59  for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
60  {
61  expContext.appendScope( new QgsExpressionContextScope( scope ) );
62  }
63 
64  return expContext;
65 }
66 
68  : QgsRendererWidget( layer, style )
69 
70 {
71  if ( !layer )
72  {
73  return;
74  }
75  // the renderer only applies to point vector layers
76  if ( layer->geometryType() != QgsWkbTypes::PointGeometry )
77  {
78  //setup blank dialog
79  mRenderer = nullptr;
80  QLabel *label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
81  "'%1' is not a point layer and cannot be rendered as a heatmap." )
82  .arg( layer->name() ), this );
83  layout()->addWidget( label );
84  return;
85  }
86 
87  setupUi( this );
88  connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
89  connect( mRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
90  connect( mMaxSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
91  connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
92  this->layout()->setContentsMargins( 0, 0, 0, 0 );
93 
96  mWeightExpressionWidget->registerExpressionContextGenerator( this );
97 
98  if ( renderer )
99  {
100  mRenderer = QgsHeatmapRenderer::convertFromRenderer( renderer );
101  }
102  if ( !mRenderer )
103  {
104  mRenderer = new QgsHeatmapRenderer();
105  }
106 
107  btnColorRamp->setShowGradientOnly( true );
108 
109  connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
110 
111  if ( mRenderer->colorRamp() )
112  {
113  btnColorRamp->blockSignals( true );
114  btnColorRamp->setColorRamp( mRenderer->colorRamp() );
115  btnColorRamp->blockSignals( false );
116  }
117  mRadiusSpinBox->blockSignals( true );
118  mRadiusSpinBox->setValue( mRenderer->radius() );
119  mRadiusSpinBox->blockSignals( false );
120  mRadiusUnitWidget->blockSignals( true );
121  mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
122  mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
123  mRadiusUnitWidget->blockSignals( false );
124  mMaxSpinBox->blockSignals( true );
125  mMaxSpinBox->setValue( mRenderer->maximumValue() );
126  mMaxSpinBox->blockSignals( false );
127  mQualitySlider->blockSignals( true );
128  mQualitySlider->setValue( mRenderer->renderQuality() );
129  mQualitySlider->blockSignals( false );
130 
131  mWeightExpressionWidget->setLayer( layer );
132  mWeightExpressionWidget->setField( mRenderer->weightExpression() );
133  connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
134 }
135 
137 {
138  return mRenderer;
139 }
140 
142 {
144  if ( context.mapCanvas() )
145  mRadiusUnitWidget->setMapCanvas( context.mapCanvas() );
146 }
147 
148 void QgsHeatmapRendererWidget::applyColorRamp()
149 {
150  if ( !mRenderer )
151  {
152  return;
153  }
154 
155  QgsColorRamp *ramp = btnColorRamp->colorRamp();
156  if ( !ramp )
157  return;
158 
159  mRenderer->setColorRamp( ramp );
160  emit widgetChanged();
161 }
162 
163 void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
164 {
165  if ( !mRenderer )
166  {
167  return;
168  }
169 
170  mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
171  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
172  emit widgetChanged();
173 }
174 
175 void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
176 {
177  if ( !mRenderer )
178  {
179  return;
180  }
181 
182  mRenderer->setRadius( d );
183  emit widgetChanged();
184 }
185 
186 void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
187 {
188  if ( !mRenderer )
189  {
190  return;
191  }
192 
193  mRenderer->setMaximumValue( d );
194  emit widgetChanged();
195 }
196 
197 void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
198 {
199  if ( !mRenderer )
200  {
201  return;
202  }
203 
204  mRenderer->setRenderQuality( v );
205  emit widgetChanged();
206 }
207 
208 void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
209 {
210  mRenderer->setWeightExpression( expression );
211  emit widgetChanged();
212 }
The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains ...
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
double renderQuality() const
Returns the render quality used for drawing the heatmap.
void setColorRamp(QgsColorRamp *ramp)
Sets the color ramp to use for shading the heatmap.
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
void colorRampChanged()
Emitted whenever a new color ramp is set for the button.
Base class for renderer settings widgets.
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Static creation method.
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
double maximumValue() const
Returns the maximum value used for shading the heatmap.
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
void setWeightExpression(const QString &expression)
Sets the expression used for weighting points when generating the heatmap.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:218
The QgsMapSettings class contains configuration for rendering of the map.
QgsHeatmapRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
A renderer which draws points as a live heatmap.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown...
void setRadiusMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale used for the heatmap&#39;s radius.
double radius() const
Returns the radius for the heatmap.
const QgsMapUnitScale & radiusMapUnitScale() const
Returns the map unit scale used for the heatmap&#39;s radius.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
const QgsVectorLayer * vectorLayer() const
Returns the vector layer associated with the widget.
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
QgsColorRamp * colorRamp() const
Returns the color ramp used for shading the heatmap.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setRenderQuality(const int quality)
Sets the render quality used for drawing the heatmap.
void widgetChanged()
Emitted when the widget state changes.
void setRadiusUnit(const QgsUnitTypes::RenderUnit unit)
Sets the units used for the heatmap&#39;s radius.
void setRadius(const double radius)
Sets the radius for the heatmap.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
QgsSymbolWidgetContext mContext
Context in which widget is shown.
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:573
Points (e.g., for font sizes)
Definition: qgsunittypes.h:151
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:442
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
QString name
Definition: qgsmaplayer.h:83
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QString weightExpression() const
Returns the expression used for weighting points when generating the heatmap.
Represents a vector layer which manages a vector based data sets.
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
QgsUnitTypes::RenderUnit radiusUnit() const
Returns the units used for the heatmap&#39;s radius.
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer...