QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsrendererv2registry.h"
18 
19 #include "qgssymbolv2.h"
20 
21 #include "qgslogger.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsvectorcolorrampv2.h"
24 #include "qgsstylev2.h"
25 #include "qgsproject.h"
26 #include "qgsmapcanvas.h"
27 #include <QGridLayout>
28 #include <QLabel>
29 
31 {
32  return new QgsHeatmapRendererWidget( layer, style, renderer );
33 }
34 
35 static QgsExpressionContext _getExpressionContext( const void* context )
36 {
37  const QgsHeatmapRendererWidget* widget = reinterpret_cast< const QgsHeatmapRendererWidget* >( context );
38 
39  QgsExpressionContext expContext;
43 
44  if ( widget->mapCanvas() )
45  {
48  }
49  else
50  {
52  }
53 
54  if ( widget->vectorLayer() )
55  expContext << QgsExpressionContextUtils::layerScope( widget->vectorLayer() );
56 
57  return expContext;
58 }
59 
61  : QgsRendererV2Widget( layer, style )
62  , mRenderer( nullptr )
63 {
64  if ( !layer )
65  {
66  return;
67  }
68  // the renderer only applies to point vector layers
69  if ( layer->geometryType() != QGis::Point )
70  {
71  //setup blank dialog
72  mRenderer = nullptr;
73  QLabel* label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
74  "'%1' is not a point layer and cannot be rendered as a heatmap." )
75  .arg( layer->name() ), this );
76  layout()->addWidget( label );
77  return;
78  }
79 
80  setupUi( this );
81 
83  mWeightExpressionWidget->registerGetExpressionContextCallback( &_getExpressionContext, this );
84 
85  if ( renderer )
86  {
88  }
89  if ( !mRenderer )
90  {
92  }
93 
94  mRampComboBox->setShowGradientOnly( true );
95  mRampComboBox->populate( QgsStyleV2::defaultStyle() );
96  connect( mRampComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( applyColorRamp() ) );
97  connect( mRampComboBox, SIGNAL( sourceRampEdited() ), this, SLOT( applyColorRamp() ) );
98  connect( mButtonEditRamp, SIGNAL( clicked() ), mRampComboBox, SLOT( editSourceRamp() ) );
99 
100  if ( mRenderer->colorRamp() )
101  {
102  mRampComboBox->blockSignals( true );
103  mRampComboBox->setSourceColorRamp( mRenderer->colorRamp() );
104  mRampComboBox->blockSignals( false );
105  }
106  mRadiusSpinBox->blockSignals( true );
107  mRadiusSpinBox->setValue( mRenderer->radius() );
108  mRadiusSpinBox->blockSignals( false );
109  mRadiusUnitWidget->blockSignals( true );
110  mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
111  mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
112  mRadiusUnitWidget->blockSignals( false );
113  mMaxSpinBox->blockSignals( true );
114  mMaxSpinBox->setValue( mRenderer->maximumValue() );
115  mMaxSpinBox->blockSignals( false );
116  mQualitySlider->blockSignals( true );
117  mQualitySlider->setValue( mRenderer->renderQuality() );
118  mQualitySlider->blockSignals( false );
119  mInvertCheckBox->blockSignals( true );
120  mInvertCheckBox->setChecked( mRenderer->invertRamp() );
121  mInvertCheckBox->blockSignals( false );
122 
123  mWeightExpressionWidget->setLayer( layer );
124  mWeightExpressionWidget->setField( mRenderer->weightExpression() );
125  connect( mWeightExpressionWidget, SIGNAL( fieldChanged( QString ) ), this, SLOT( weightExpressionChanged( QString ) ) );
126 }
127 
129 {
130  return mRenderer;
131 }
132 
134 {
136  if ( mRadiusUnitWidget )
137  mRadiusUnitWidget->setMapCanvas( canvas );
138 }
139 
140 void QgsHeatmapRendererWidget::applyColorRamp()
141 {
142  if ( !mRenderer )
143  {
144  return;
145  }
146 
147  QgsVectorColorRampV2* ramp = mRampComboBox->currentColorRamp();
148  if ( !ramp )
149  return;
150 
151  mRenderer->setColorRamp( ramp );
152  emit widgetChanged();
153 }
154 
155 void QgsHeatmapRendererWidget::on_mRadiusUnitWidget_changed()
156 {
157  if ( !mRenderer )
158  {
159  return;
160  }
161 
162  mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
163  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
164  emit widgetChanged();
165 }
166 
167 void QgsHeatmapRendererWidget::on_mRadiusSpinBox_valueChanged( double d )
168 {
169  if ( !mRenderer )
170  {
171  return;
172  }
173 
174  mRenderer->setRadius( d );
175  emit widgetChanged();
176 }
177 
178 void QgsHeatmapRendererWidget::on_mMaxSpinBox_valueChanged( double d )
179 {
180  if ( !mRenderer )
181  {
182  return;
183  }
184 
186  emit widgetChanged();
187 }
188 
189 void QgsHeatmapRendererWidget::on_mQualitySlider_valueChanged( int v )
190 {
191  if ( !mRenderer )
192  {
193  return;
194  }
195 
197  emit widgetChanged();
198 }
199 
200 void QgsHeatmapRendererWidget::on_mInvertCheckBox_toggled( bool v )
201 {
202  if ( !mRenderer )
203  {
204  return;
205  }
206 
207  mRenderer->setInvertRamp( v );
208  emit widgetChanged();
209 }
210 
211 void QgsHeatmapRendererWidget::weightExpressionChanged( const QString& expression )
212 {
213  mRenderer->setWeightExpression( expression );
214  emit widgetChanged();
215 }
QLayout * layout() const
void setInvertRamp(const bool invert)
Sets whether the ramp is inverted.
const QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
void setupUi(QWidget *widget)
double renderQuality() const
Returns the render quality used for drawing the heatmap.
static QgsExpressionContextScope * atlasScope(const QgsAtlasComposition *atlas)
Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
QStyle * style() const
The output shall be in pixels.
Definition: qgssymbolv2.h:70
static QgsExpressionContext _getExpressionContext(const void *context)
double maximumValue() const
Returns the maximum value used for shading the heatmap.
void setWeightExpression(const QString &expression)
Sets the expression used for weighting points when generating the heatmap.
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
void setRadiusUnit(const QgsSymbolV2::OutputUnit unit)
Sets the units used for the heatmap&#39;s radius.
QgsVectorColorRampV2 * colorRamp() const
Returns the color ramp used for shading the heatmap.
The QgsMapSettings class contains configuration for rendering of the map.
The output shall be in millimeters.
Definition: qgssymbolv2.h:67
bool invertRamp() const
Returns whether the ramp is inverted.
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.
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...
static QgsStyleV2 * defaultStyle()
return default application-wide style
Definition: qgsstylev2.cpp:51
The output shall be in map unitx.
Definition: qgssymbolv2.h:68
QgsSymbolV2::OutputUnit radiusUnit() const
Returns the units used for the heatmap&#39;s radius.
QGis::GeometryType geometryType() const
Returns point, line or polygon.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addWidget(QWidget *w)
void setRenderQuality(const int quality)
Sets the render quality used for drawing the heatmap.
void widgetChanged()
Emitted when the widget state changes.
void setRadius(const double radius)
Sets the radius for the heatmap.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Static creation method.
const QgsVectorLayer * vectorLayer() const
Returns the vector layer associated with the widget.
void setColorRamp(QgsVectorColorRampV2 *ramp)
Sets the color ramp to use for shading the heatmap.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:455
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
Base class for renderer settings widgets.
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
QString weightExpression() const
Returns the expression used for weighting points when generating the heatmap.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
Abstract base class for color ramps.
QgsHeatmapRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Constructor.
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
virtual QgsFeatureRendererV2 * renderer() override