QGIS API Documentation  2.14.0-Essen
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  QGridLayout* layout = new QGridLayout( this );
74  QLabel* label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
75  "'%1' is not a point layer and cannot be rendered as a heatmap." )
76  .arg( layer->name() ), this );
77  layout->addWidget( label );
78  return;
79  }
80 
81  setupUi( this );
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 }
153 
154 void QgsHeatmapRendererWidget::on_mRadiusUnitWidget_changed()
155 {
156  if ( !mRenderer )
157  {
158  return;
159  }
160 
161  mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
162  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
163 }
164 
165 void QgsHeatmapRendererWidget::on_mRadiusSpinBox_valueChanged( double d )
166 {
167  if ( !mRenderer )
168  {
169  return;
170  }
171 
172  mRenderer->setRadius( d );
173 }
174 
175 void QgsHeatmapRendererWidget::on_mMaxSpinBox_valueChanged( double d )
176 {
177  if ( !mRenderer )
178  {
179  return;
180  }
181 
183 }
184 
185 void QgsHeatmapRendererWidget::on_mQualitySlider_valueChanged( int v )
186 {
187  if ( !mRenderer )
188  {
189  return;
190  }
191 
193 }
194 
195 void QgsHeatmapRendererWidget::on_mInvertCheckBox_toggled( bool v )
196 {
197  if ( !mRenderer )
198  {
199  return;
200  }
201 
202  mRenderer->setInvertRamp( v );
203 }
204 
205 void QgsHeatmapRendererWidget::weightExpressionChanged( const QString& expression )
206 {
207  mRenderer->setWeightExpression( expression );
208 }
QLayout * layout() const
void setInvertRamp(const bool invert)
Sets whether the ramp is inverted.
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
void setupUi(QWidget *widget)
QString name() const
Get the display name of the layer.
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
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:67
static QgsExpressionContext _getExpressionContext(const void *context)
QgsSymbolV2::OutputUnit radiusUnit() const
Returns the units used for the heatmap&#39;s radius.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
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.
QgsVectorColorRampV2 * colorRamp() const
Returns the color ramp used for shading 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:105
void setRadiusUnit(const QgsSymbolV2::OutputUnit unit)
Sets the units used for the heatmap&#39;s radius.
The QgsMapSettings class contains configuration for rendering of the map.
The output shall be in millimeters.
Definition: qgssymbolv2.h:64
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.
const QgsMapUnitScale & radiusMapUnitScale() const
Returns the map unit scale used for the heatmap&#39;s radius.
double radius() const
Returns the radius for the heatmap.
void setRadiusMapUnitScale(const QgsMapUnitScale &scale)
Sets 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
const QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
const QgsVectorLayer * vectorLayer() const
Returns the vector layer associated with the widget.
The output shall be in map unitx.
Definition: qgssymbolv2.h:65
QGis::GeometryType geometryType() const
Returns point, line or polygon.
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 setRadius(const double radius)
Sets the radius for the heatmap.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Static creation method.
double renderQuality() const
Returns the render quality used for drawing the heatmap.
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:443
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
Base class for renderer settings widgets.
bool invertRamp() const
Returns whether the ramp is inverted.
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.
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.
QString weightExpression() const
Returns the expression used for weighting points when generating the heatmap.
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