QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
25 #include "qgsstylev2.h"
26 #include "qgsproject.h"
27 #include <QGridLayout>
28 #include <QLabel>
29 
31 {
32  return new QgsHeatmapRendererWidget( layer, style, renderer );
33 }
34 
36  : QgsRendererV2Widget( layer, style )
37  , mRenderer( NULL )
38 {
39  if ( !layer )
40  {
41  return;
42  }
43  // the renderer only applies to point vector layers
44  if ( layer->geometryType() != QGis::Point )
45  {
46  //setup blank dialog
47  mRenderer = NULL;
48  QGridLayout* layout = new QGridLayout( this );
49  QLabel* label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
50  "'%1' is not a point layer and cannot be rendered as a heatmap." )
51  .arg( layer->name() ), this );
52  layout->addWidget( label );
53  return;
54  }
55 
56  setupUi( this );
58 
59  if ( renderer )
60  {
62  }
63  if ( !mRenderer )
64  {
66  }
67 
68  mRampComboBox->setShowGradientOnly( true );
69  mRampComboBox->populate( QgsStyleV2::defaultStyle() );
70  connect( mRampComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( applyColorRamp() ) );
71  if ( mRenderer->colorRamp() )
72  {
73  mRampComboBox->blockSignals( true );
74  mRampComboBox->setSourceColorRamp( mRenderer->colorRamp() );
75  mRampComboBox->blockSignals( false );
76  }
77  mRadiusSpinBox->blockSignals( true );
78  mRadiusSpinBox->setValue( mRenderer->radius() );
79  mRadiusSpinBox->blockSignals( false );
80  mRadiusUnitWidget->blockSignals( true );
81  mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
82  mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
83  mRadiusUnitWidget->blockSignals( false );
84  mMaxSpinBox->blockSignals( true );
85  mMaxSpinBox->setValue( mRenderer->maximumValue() );
86  mMaxSpinBox->blockSignals( false );
87  mQualitySlider->blockSignals( true );
88  mQualitySlider->setValue( mRenderer->renderQuality() );
89  mQualitySlider->blockSignals( false );
90  mInvertCheckBox->blockSignals( true );
91  mInvertCheckBox->setChecked( mRenderer->invertRamp() );
92  mInvertCheckBox->blockSignals( false );
93 
94  mWeightExpressionWidget->setLayer( layer );
95  mWeightExpressionWidget->setField( mRenderer->weightExpression() );
96  connect( mWeightExpressionWidget, SIGNAL( fieldChanged( QString ) ), this, SLOT( weightExpressionChanged( QString ) ) );
97 }
98 
100 {
101  return mRenderer;
102 }
103 
104 void QgsHeatmapRendererWidget::applyColorRamp()
105 {
106  if ( !mRenderer )
107  {
108  return;
109  }
110 
111  QgsVectorColorRampV2* ramp = mRampComboBox->currentColorRamp();
112  if ( ramp == NULL )
113  return;
114 
115  mRenderer->setColorRamp( ramp );
116 }
117 
118 void QgsHeatmapRendererWidget::on_mButtonEditRamp_clicked()
119 {
120  if ( mRenderer && mRenderer->colorRamp()->type() == "gradient" )
121  {
123  QgsVectorGradientColorRampV2* gradRamp = static_cast<QgsVectorGradientColorRampV2*>( ramp );
124  QgsVectorGradientColorRampV2Dialog dlg( gradRamp, this );
125 
126  if ( dlg.exec() && gradRamp )
127  {
128  mRenderer->setColorRamp( gradRamp );
129  mRampComboBox->blockSignals( true );
130  mRampComboBox->setSourceColorRamp( mRenderer->colorRamp() );
131  mRampComboBox->blockSignals( false );
132  }
133  else
134  {
135  delete ramp;
136  }
137  }
138 }
139 
140 void QgsHeatmapRendererWidget::on_mRadiusUnitWidget_changed()
141 {
142  if ( !mRenderer )
143  {
144  return;
145  }
146 
147  mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
148  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
149 }
150 
151 void QgsHeatmapRendererWidget::on_mRadiusSpinBox_valueChanged( double d )
152 {
153  if ( !mRenderer )
154  {
155  return;
156  }
157 
158  mRenderer->setRadius( d );
159 }
160 
161 void QgsHeatmapRendererWidget::on_mMaxSpinBox_valueChanged( double d )
162 {
163  if ( !mRenderer )
164  {
165  return;
166  }
167 
169 }
170 
171 void QgsHeatmapRendererWidget::on_mQualitySlider_valueChanged( int v )
172 {
173  if ( !mRenderer )
174  {
175  return;
176  }
177 
179 }
180 
181 void QgsHeatmapRendererWidget::on_mInvertCheckBox_toggled( bool v )
182 {
183  if ( !mRenderer )
184  {
185  return;
186  }
187 
188  mRenderer->setInvertRamp( v );
189 }
190 
191 void QgsHeatmapRendererWidget::weightExpressionChanged( QString expression )
192 {
193  mRenderer->setWeightExpression( expression );
194 }
QLayout * layout() const
void setInvertRamp(const bool invert)
Sets whether the ramp is inverted.
void setupUi(QWidget *widget)
virtual QString type() const =0
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QgsSymbolV2::OutputUnit radiusUnit() const
Returns the units used for the heatmap's radius.
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)
void setRadiusUnit(const QgsSymbolV2::OutputUnit unit)
Sets the units used for the heatmap's radius.
const QString & name() const
Get the display name of the layer.
A renderer which draws points as a live heatmap.
const QgsMapUnitScale & radiusMapUnitScale() const
Returns the map unit scale used for the heatmap'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's radius.
virtual QgsVectorColorRampV2 * clone() const =0
static QgsStyleV2 * defaultStyle()
return default application-wide style
Definition: qgsstylev2.cpp:51
QGis::GeometryType geometryType() const
Returns point, line or polygon.
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 QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
Base class for renderer settings widgets.
double invertRamp() const
Returns whether the ramp is inverted.
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.
void setMaximumValue(const double value)
Sets the maximum value used for shading the heatmap.
virtual QgsFeatureRendererV2 * renderer() override