QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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"
19#include "qgsvectorlayer.h"
20#include "qgscolorramp.h"
21#include "qgscolorrampbutton.h"
22#include "qgsstyle.h"
23#include "qgsproject.h"
24#include "qgsmapcanvas.h"
25#include <QGridLayout>
26#include <QLabel>
27
29{
30 return new QgsHeatmapRendererWidget( layer, style, renderer );
31}
32
33QgsExpressionContext QgsHeatmapRendererWidget::createExpressionContext() const
34{
35 QgsExpressionContext expContext;
39
40 if ( auto *lMapCanvas = mContext.mapCanvas() )
41 {
42 expContext << QgsExpressionContextUtils::mapSettingsScope( lMapCanvas->mapSettings() )
43 << new QgsExpressionContextScope( lMapCanvas->expressionContextScope() );
44 if ( const QgsExpressionContextScopeGenerator *generator = dynamic_cast< const QgsExpressionContextScopeGenerator * >( lMapCanvas->temporalController() ) )
45 {
46 expContext << generator->createExpressionContextScope();
47 }
48 }
49 else
50 {
52 }
53
54 if ( auto *lVectorLayer = vectorLayer() )
55 expContext << QgsExpressionContextUtils::layerScope( lVectorLayer );
56
57 // additional scopes
58 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
59 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
60 {
61 expContext.appendScope( new QgsExpressionContextScope( scope ) );
62 }
63
64 return expContext;
65}
66
68 : QgsRendererWidget( layer, style )
69
70{
71 if ( !layer )
72 {
73 return;
74 }
75 // the renderer only applies to point vector layers
77 {
78 //setup blank dialog
79 mRenderer = nullptr;
80 QLabel *label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
81 "'%1' is not a point layer and cannot be rendered as a heatmap." )
82 .arg( layer->name() ), this );
83 if ( !layout() )
84 setLayout( new QGridLayout() );
85 layout()->addWidget( label );
86 return;
87 }
88
89 setupUi( this );
90 connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
91 connect( mRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
92 connect( mMaxSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
93 connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
94 this->layout()->setContentsMargins( 0, 0, 0, 0 );
95
96 mRadiusUnitWidget->setUnits( { Qgis::RenderUnit::Millimeters,
102 mWeightExpressionWidget->registerExpressionContextGenerator( this );
103 mWeightExpressionWidget->setAllowEmptyFieldName( true );
104
105 if ( renderer )
106 {
108 }
109 if ( !mRenderer )
110 {
111 mRenderer = std::make_unique< QgsHeatmapRenderer >();
112 if ( renderer )
113 renderer->copyRendererData( mRenderer.get() );
114 }
115
116 btnColorRamp->setShowGradientOnly( true );
117
118 connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
119
120 if ( mRenderer->colorRamp() )
121 {
122 btnColorRamp->blockSignals( true );
123 btnColorRamp->setColorRamp( mRenderer->colorRamp() );
124 btnColorRamp->blockSignals( false );
125 }
126 mRadiusSpinBox->blockSignals( true );
127 mRadiusSpinBox->setValue( mRenderer->radius() );
128 mRadiusSpinBox->blockSignals( false );
129 mRadiusUnitWidget->blockSignals( true );
130 mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
131 mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
132 mRadiusUnitWidget->blockSignals( false );
133 mMaxSpinBox->blockSignals( true );
134 mMaxSpinBox->setValue( mRenderer->maximumValue() );
135 mMaxSpinBox->blockSignals( false );
136 mQualitySlider->blockSignals( true );
137 mQualitySlider->setValue( mRenderer->renderQuality() );
138 mQualitySlider->blockSignals( false );
139
140 mWeightExpressionWidget->setLayer( layer );
141 mWeightExpressionWidget->setField( mRenderer->weightExpression() );
142 connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
143}
144
146
148{
149 return mRenderer.get();
150}
151
153{
155 if ( auto *lMapCanvas = context.mapCanvas() )
156 mRadiusUnitWidget->setMapCanvas( lMapCanvas );
157}
158
159void QgsHeatmapRendererWidget::applyColorRamp()
160{
161 if ( !mRenderer )
162 {
163 return;
164 }
165
166 QgsColorRamp *ramp = btnColorRamp->colorRamp();
167 if ( !ramp )
168 return;
169
170 mRenderer->setColorRamp( ramp );
171 emit widgetChanged();
172}
173
174void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
175{
176 if ( !mRenderer )
177 {
178 return;
179 }
180
181 mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
182 mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
183 emit widgetChanged();
184}
185
186void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
187{
188 if ( !mRenderer )
189 {
190 return;
191 }
192
193 mRenderer->setRadius( d );
194 emit widgetChanged();
195}
196
197void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
198{
199 if ( !mRenderer )
200 {
201 return;
202 }
203
204 mRenderer->setMaximumValue( d );
205 emit widgetChanged();
206}
207
208void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
209{
210 if ( !mRenderer )
211 {
212 return;
213 }
214
215 mRenderer->setRenderQuality( v );
216 emit widgetChanged();
217}
218
219void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
220{
221 mRenderer->setWeightExpression( expression );
222 emit widgetChanged();
223}
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
void colorRampChanged()
Emitted whenever a new color ramp is set for the button.
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
Abstract interface for generating an expression context scope.
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * atlasScope(const 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.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:46
The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains...
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Static creation method.
QgsHeatmapRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
Constructor.
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
~QgsHeatmapRendererWidget() override
static QgsHeatmapRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
QString name
Definition: qgsmaplayer.h:78
The QgsMapSettings class contains configuration for rendering of the map.
void widgetChanged()
Emitted when the widget state changes.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:481
Base class for renderer settings widgets.
QgsSymbolWidgetContext mContext
Context in which widget is shown.
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
const QgsVectorLayer * vectorLayer() const
Returns the vector layer associated with the widget.
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.