QGIS API Documentation 3.39.0-Master (bca3cdb6021)
Loading...
Searching...
No Matches
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"
26#include <QGridLayout>
27#include <QLabel>
28
33
35{
36 QgsExpressionContext expContext;
37
38 if ( auto *lMapCanvas = mContext.mapCanvas() )
39 {
40 expContext = lMapCanvas->createExpressionContext();
41 }
42 else
43 {
48 }
49
50 if ( auto *lVectorLayer = vectorLayer() )
51 expContext << QgsExpressionContextUtils::layerScope( lVectorLayer );
52
53 // additional scopes
54 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
55 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
56 {
57 expContext.appendScope( new QgsExpressionContextScope( scope ) );
58 }
59
60 return expContext;
61}
62
64 : QgsRendererWidget( layer, style )
65
66{
67 if ( !layer )
68 {
69 return;
70 }
71 // the renderer only applies to point vector layers
73 {
74 //setup blank dialog
75 mRenderer = nullptr;
76 QLabel *label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
77 "'%1' is not a point layer and cannot be rendered as a heatmap." )
78 .arg( layer->name() ), this );
79 if ( !layout() )
80 setLayout( new QGridLayout() );
81 layout()->addWidget( label );
82 return;
83 }
84
85 setupUi( this );
86 connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
87 connect( mRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
88 connect( mMaxSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
89 connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
90 this->layout()->setContentsMargins( 0, 0, 0, 0 );
91
92 mRadiusUnitWidget->setUnits( { Qgis::RenderUnit::Millimeters,
98 mWeightExpressionWidget->registerExpressionContextGenerator( this );
99 mWeightExpressionWidget->setAllowEmptyFieldName( true );
100
101 if ( renderer )
102 {
104 }
105 if ( !mRenderer )
106 {
107 mRenderer = std::make_unique< QgsHeatmapRenderer >();
108 if ( renderer )
109 renderer->copyRendererData( mRenderer.get() );
110 }
111
112 btnColorRamp->setShowGradientOnly( true );
113
114 connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
115 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsHeatmapRendererWidget::showLegendSettings );
116
117 if ( mRenderer->colorRamp() )
118 {
119 btnColorRamp->blockSignals( true );
120 btnColorRamp->setColorRamp( mRenderer->colorRamp() );
121 btnColorRamp->blockSignals( false );
122 }
123 mRadiusSpinBox->blockSignals( true );
124 mRadiusSpinBox->setValue( mRenderer->radius() );
125 mRadiusSpinBox->blockSignals( false );
126 mRadiusUnitWidget->blockSignals( true );
127 mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
128 mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
129 mRadiusUnitWidget->blockSignals( false );
130 mMaxSpinBox->blockSignals( true );
131 mMaxSpinBox->setValue( mRenderer->maximumValue() );
132 mMaxSpinBox->blockSignals( false );
133 mQualitySlider->blockSignals( true );
134 mQualitySlider->setValue( mRenderer->renderQuality() );
135 mQualitySlider->blockSignals( false );
136
137 mWeightExpressionWidget->setLayer( layer );
138 mWeightExpressionWidget->setField( mRenderer->weightExpression() );
139 connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
140
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::showLegendSettings()
175{
176 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast< QWidget * >( parent() ) );
177 if ( panel && panel->dockMode() )
178 {
180 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
181 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
182 legendPanel->setSettings( mRenderer->legendSettings() );
183 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [ = ]
184 {
185 mRenderer->setLegendSettings( legendPanel->settings() );
186 emit widgetChanged();
187 } );
188 panel->openPanel( legendPanel );
189 }
190 else
191 {
192 QgsColorRampLegendNodeDialog dialog( mRenderer->legendSettings(), this, QgsColorRampLegendNodeWidget::Capabilities() );
193 dialog.setUseContinuousRampCheckBoxVisibility( false );
194 dialog.setWindowTitle( tr( "Legend Settings" ) );
195 if ( dialog.exec() )
196 {
197 mRenderer->setLegendSettings( dialog.settings() );
198 emit widgetChanged();
199 }
200 }
201}
202
203void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
204{
205 if ( !mRenderer )
206 {
207 return;
208 }
209
210 mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
211 mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
212 emit widgetChanged();
213}
214
215void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
216{
217 if ( !mRenderer )
218 {
219 return;
220 }
221
222 mRenderer->setRadius( d );
223 emit widgetChanged();
224}
225
226void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
227{
228 if ( !mRenderer )
229 {
230 return;
231 }
232
233 mRenderer->setMaximumValue( d );
234 emit widgetChanged();
235}
236
237void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
238{
239 if ( !mRenderer )
240 {
241 return;
242 }
243
244 mRenderer->setRenderQuality( v );
245 emit widgetChanged();
246}
247
248void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
249{
250 mRenderer->setWeightExpression( expression );
251 emit widgetChanged();
252}
@ 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.
A dialog for configuring a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
A widget for properties relating to a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
QFlags< Capability > Capabilities
Capabilities to expose in the widget.
QgsColorRampLegendNodeSettings settings() const
Returns the legend node settings as defined by the widget.
void setSettings(const QgsColorRampLegendNodeSettings &settings)
Sets the settings to show in the widget.
void setUseContinuousRampCheckBoxVisibility(bool visible)
Sets visibility for the "Use Continuous Legend" checkbox to visible.
Abstract base class for color ramps.
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.
Abstract base class for all 2D vector feature renderers.
@ HeatmapRadius
Heatmap renderer radius.
@ HeatmapMaximum
Heatmap maximum value.
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
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...
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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:80
The QgsMapSettings class contains configuration for rendering of the map.
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void widgetChanged()
Emitted when the widget state changes.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
bool dockMode()
Returns the dock mode state.
static QgsProject * instance()
Returns the QgsProject singleton instance.
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...
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsFeatureRenderer::Property key)
Registers a data defined override button.
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.
void changed()
Emitted when the selected unit is changed, or the definition of the map unit scale is changed.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.