QGIS API Documentation  3.2.0-Bonn (bc43194)
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"
18 
19 #include "qgssymbol.h"
20 
21 #include "qgslogger.h"
22 #include "qgsvectorlayer.h"
23 #include "qgscolorramp.h"
24 #include "qgscolorrampbutton.h"
25 #include "qgsstyle.h"
26 #include "qgsproject.h"
27 #include "qgsmapcanvas.h"
28 #include <QGridLayout>
29 #include <QLabel>
30 
32 {
33  return new QgsHeatmapRendererWidget( layer, style, renderer );
34 }
35 
36 QgsExpressionContext QgsHeatmapRendererWidget::createExpressionContext() const
37 {
38  QgsExpressionContext expContext;
42 
43  if ( mContext.mapCanvas() )
44  {
47  }
48  else
49  {
51  }
52 
53  if ( vectorLayer() )
55 
56  // additional scopes
58  {
59  expContext.appendScope( new QgsExpressionContextScope( scope ) );
60  }
61 
62  return expContext;
63 }
64 
66  : QgsRendererWidget( layer, style )
67 
68 {
69  if ( !layer )
70  {
71  return;
72  }
73  // the renderer only applies to point vector layers
74  if ( layer->geometryType() != QgsWkbTypes::PointGeometry )
75  {
76  //setup blank dialog
77  mRenderer = nullptr;
78  QLabel *label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
79  "'%1' is not a point layer and cannot be rendered as a heatmap." )
80  .arg( layer->name() ), this );
81  layout()->addWidget( label );
82  return;
83  }
84 
85  setupUi( this );
86  connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
87  connect( mRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
88  connect( mMaxSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
89  connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
90  this->layout()->setContentsMargins( 0, 0, 0, 0 );
91 
94  mWeightExpressionWidget->registerExpressionContextGenerator( this );
95 
96  if ( renderer )
97  {
98  mRenderer = QgsHeatmapRenderer::convertFromRenderer( renderer );
99  }
100  if ( !mRenderer )
101  {
102  mRenderer = new QgsHeatmapRenderer();
103  }
104 
105  btnColorRamp->setShowGradientOnly( true );
106 
107  connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
108 
109  if ( mRenderer->colorRamp() )
110  {
111  btnColorRamp->blockSignals( true );
112  btnColorRamp->setColorRamp( mRenderer->colorRamp() );
113  btnColorRamp->blockSignals( false );
114  }
115  mRadiusSpinBox->blockSignals( true );
116  mRadiusSpinBox->setValue( mRenderer->radius() );
117  mRadiusSpinBox->blockSignals( false );
118  mRadiusUnitWidget->blockSignals( true );
119  mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
120  mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
121  mRadiusUnitWidget->blockSignals( false );
122  mMaxSpinBox->blockSignals( true );
123  mMaxSpinBox->setValue( mRenderer->maximumValue() );
124  mMaxSpinBox->blockSignals( false );
125  mQualitySlider->blockSignals( true );
126  mQualitySlider->setValue( mRenderer->renderQuality() );
127  mQualitySlider->blockSignals( false );
128 
129  mWeightExpressionWidget->setLayer( layer );
130  mWeightExpressionWidget->setField( mRenderer->weightExpression() );
131  connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
132 }
133 
135 {
136  return mRenderer;
137 }
138 
140 {
142  if ( context.mapCanvas() )
143  mRadiusUnitWidget->setMapCanvas( context.mapCanvas() );
144 }
145 
146 void QgsHeatmapRendererWidget::applyColorRamp()
147 {
148  if ( !mRenderer )
149  {
150  return;
151  }
152 
153  QgsColorRamp *ramp = btnColorRamp->colorRamp();
154  if ( !ramp )
155  return;
156 
157  mRenderer->setColorRamp( ramp );
158  emit widgetChanged();
159 }
160 
161 void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
162 {
163  if ( !mRenderer )
164  {
165  return;
166  }
167 
168  mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
169  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
170  emit widgetChanged();
171 }
172 
173 void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
174 {
175  if ( !mRenderer )
176  {
177  return;
178  }
179 
180  mRenderer->setRadius( d );
181  emit widgetChanged();
182 }
183 
184 void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
185 {
186  if ( !mRenderer )
187  {
188  return;
189  }
190 
191  mRenderer->setMaximumValue( d );
192  emit widgetChanged();
193 }
194 
195 void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
196 {
197  if ( !mRenderer )
198  {
199  return;
200  }
201 
202  mRenderer->setRenderQuality( v );
203  emit widgetChanged();
204 }
205 
206 void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
207 {
208  mRenderer->setWeightExpression( expression );
209  emit widgetChanged();
210 }
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.
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:173
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.
points (e.g., for font sizes)
Definition: qgsunittypes.h:107
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)
the signal is 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 * atlasScope(QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
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:558
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:391
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
QString name
Definition: qgsmaplayer.h:65
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...