QGIS API Documentation  2.8.2-Wien
 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 );
57  mRadiusUnitWidget->setUnits( QStringList() << tr( "Pixels" ) << tr( "Millimeter" ) << tr( "Map unit" ), 2 );
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  switch ( mRenderer->radiusUnit() )
82  {
83  case QgsSymbolV2::MM:
84  mRadiusUnitWidget->setUnit( 1 );
85  break;
87  mRadiusUnitWidget->setUnit( 2 );
88  break;
89  case QgsSymbolV2::Pixel:
90  default:
91  mRadiusUnitWidget->setUnit( 0 );
92  break;
93  }
94  mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
95  mRadiusUnitWidget->blockSignals( false );
96  mMaxSpinBox->blockSignals( true );
97  mMaxSpinBox->setValue( mRenderer->maximumValue() );
98  mMaxSpinBox->blockSignals( false );
99  mQualitySlider->blockSignals( true );
100  mQualitySlider->setValue( mRenderer->renderQuality() );
101  mQualitySlider->blockSignals( false );
102  mInvertCheckBox->blockSignals( true );
103  mInvertCheckBox->setChecked( mRenderer->invertRamp() );
104  mInvertCheckBox->blockSignals( false );
105 
106  mWeightExpressionWidget->setLayer( layer );
107  mWeightExpressionWidget->setField( mRenderer->weightExpression() );
108  connect( mWeightExpressionWidget, SIGNAL( fieldChanged( QString ) ), this, SLOT( weightExpressionChanged( QString ) ) );
109 }
110 
112 {
113  return mRenderer;
114 }
115 
116 void QgsHeatmapRendererWidget::applyColorRamp()
117 {
118  if ( !mRenderer )
119  {
120  return;
121  }
122 
123  QgsVectorColorRampV2* ramp = mRampComboBox->currentColorRamp();
124  if ( ramp == NULL )
125  return;
126 
127  mRenderer->setColorRamp( ramp );
128 }
129 
130 void QgsHeatmapRendererWidget::on_mButtonEditRamp_clicked()
131 {
132  if ( mRenderer && mRenderer->colorRamp()->type() == "gradient" )
133  {
135  QgsVectorGradientColorRampV2* gradRamp = static_cast<QgsVectorGradientColorRampV2*>( ramp );
136  QgsVectorGradientColorRampV2Dialog dlg( gradRamp, this );
137 
138  if ( dlg.exec() && gradRamp )
139  {
140  mRenderer->setColorRamp( gradRamp );
141  mRampComboBox->blockSignals( true );
142  mRampComboBox->setSourceColorRamp( mRenderer->colorRamp() );
143  mRampComboBox->blockSignals( false );
144  }
145  else
146  {
147  delete ramp;
148  }
149  }
150 }
151 
152 void QgsHeatmapRendererWidget::on_mRadiusUnitWidget_changed()
153 {
154  if ( !mRenderer )
155  {
156  return;
157  }
159  switch ( mRadiusUnitWidget->getUnit() )
160  {
161  case 0:
162  unit = QgsSymbolV2::Pixel;
163  break;
164  case 2:
165  unit = QgsSymbolV2::MapUnit;
166  break;
167  case 1:
168  default:
169  unit = QgsSymbolV2::MM;
170  break;
171  }
172 
173  mRenderer->setRadiusUnit( unit );
174  mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
175 }
176 
177 void QgsHeatmapRendererWidget::on_mRadiusSpinBox_valueChanged( double d )
178 {
179  if ( !mRenderer )
180  {
181  return;
182  }
183 
184  mRenderer->setRadius( d );
185 }
186 
187 void QgsHeatmapRendererWidget::on_mMaxSpinBox_valueChanged( double d )
188 {
189  if ( !mRenderer )
190  {
191  return;
192  }
193 
195 }
196 
197 void QgsHeatmapRendererWidget::on_mQualitySlider_valueChanged( int v )
198 {
199  if ( !mRenderer )
200  {
201  return;
202  }
203 
205 }
206 
207 void QgsHeatmapRendererWidget::on_mInvertCheckBox_toggled( bool v )
208 {
209  if ( !mRenderer )
210  {
211  return;
212  }
213 
214  mRenderer->setInvertRamp( v );
215 }
216 
217 void QgsHeatmapRendererWidget::weightExpressionChanged( QString expression )
218 {
219  mRenderer->setWeightExpression( expression );
220 }