QGIS API Documentation 3.37.0-Master (684a802617f)
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;
40
41 if ( auto *lMapCanvas = mContext.mapCanvas() )
42 {
43 expContext << QgsExpressionContextUtils::mapSettingsScope( lMapCanvas->mapSettings() )
44 << new QgsExpressionContextScope( lMapCanvas->expressionContextScope() );
45 if ( const QgsExpressionContextScopeGenerator *generator = dynamic_cast< const QgsExpressionContextScopeGenerator * >( lMapCanvas->temporalController() ) )
46 {
47 expContext << generator->createExpressionContextScope();
48 }
49 }
50 else
51 {
53 }
54
55 if ( auto *lVectorLayer = vectorLayer() )
56 expContext << QgsExpressionContextUtils::layerScope( lVectorLayer );
57
58 // additional scopes
59 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
60 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
61 {
62 expContext.appendScope( new QgsExpressionContextScope( scope ) );
63 }
64
65 return expContext;
66}
67
69 : QgsRendererWidget( layer, style )
70
71{
72 if ( !layer )
73 {
74 return;
75 }
76 // the renderer only applies to point vector layers
78 {
79 //setup blank dialog
80 mRenderer = nullptr;
81 QLabel *label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
82 "'%1' is not a point layer and cannot be rendered as a heatmap." )
83 .arg( layer->name() ), this );
84 if ( !layout() )
85 setLayout( new QGridLayout() );
86 layout()->addWidget( label );
87 return;
88 }
89
90 setupUi( this );
91 connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
92 connect( mRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
93 connect( mMaxSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
94 connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
95 this->layout()->setContentsMargins( 0, 0, 0, 0 );
96
97 mRadiusUnitWidget->setUnits( { Qgis::RenderUnit::Millimeters,
103 mWeightExpressionWidget->registerExpressionContextGenerator( this );
104 mWeightExpressionWidget->setAllowEmptyFieldName( true );
105
106 if ( renderer )
107 {
109 }
110 if ( !mRenderer )
111 {
112 mRenderer = std::make_unique< QgsHeatmapRenderer >();
113 if ( renderer )
114 renderer->copyRendererData( mRenderer.get() );
115 }
116
117 btnColorRamp->setShowGradientOnly( true );
118
119 connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
120 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsHeatmapRendererWidget::showLegendSettings );
121
122 if ( mRenderer->colorRamp() )
123 {
124 btnColorRamp->blockSignals( true );
125 btnColorRamp->setColorRamp( mRenderer->colorRamp() );
126 btnColorRamp->blockSignals( false );
127 }
128 mRadiusSpinBox->blockSignals( true );
129 mRadiusSpinBox->setValue( mRenderer->radius() );
130 mRadiusSpinBox->blockSignals( false );
131 mRadiusUnitWidget->blockSignals( true );
132 mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
133 mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
134 mRadiusUnitWidget->blockSignals( false );
135 mMaxSpinBox->blockSignals( true );
136 mMaxSpinBox->setValue( mRenderer->maximumValue() );
137 mMaxSpinBox->blockSignals( false );
138 mQualitySlider->blockSignals( true );
139 mQualitySlider->setValue( mRenderer->renderQuality() );
140 mQualitySlider->blockSignals( false );
141
142 mWeightExpressionWidget->setLayer( layer );
143 mWeightExpressionWidget->setField( mRenderer->weightExpression() );
144 connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
145
148}
149
151
153{
154 return mRenderer.get();
155}
156
158{
160 if ( auto *lMapCanvas = context.mapCanvas() )
161 mRadiusUnitWidget->setMapCanvas( lMapCanvas );
162}
163
164void QgsHeatmapRendererWidget::applyColorRamp()
165{
166 if ( !mRenderer )
167 {
168 return;
169 }
170
171 QgsColorRamp *ramp = btnColorRamp->colorRamp();
172 if ( !ramp )
173 return;
174
175 mRenderer->setColorRamp( ramp );
176 emit widgetChanged();
177}
178
179void QgsHeatmapRendererWidget::showLegendSettings()
180{
181 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast< QWidget * >( parent() ) );
182 if ( panel && panel->dockMode() )
183 {
185 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
186 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
187 legendPanel->setSettings( mRenderer->legendSettings() );
188 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [ = ]
189 {
190 mRenderer->setLegendSettings( legendPanel->settings() );
191 emit widgetChanged();
192 } );
193 panel->openPanel( legendPanel );
194 }
195 else
196 {
197 QgsColorRampLegendNodeDialog dialog( mRenderer->legendSettings(), this, QgsColorRampLegendNodeWidget::Capabilities() );
198 dialog.setUseContinuousRampCheckBoxVisibility( false );
199 dialog.setWindowTitle( tr( "Legend Settings" ) );
200 if ( dialog.exec() )
201 {
202 mRenderer->setLegendSettings( dialog.settings() );
203 emit widgetChanged();
204 }
205 }
206}
207
208void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
209{
210 if ( !mRenderer )
211 {
212 return;
213 }
214
215 mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
216 mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
217 emit widgetChanged();
218}
219
220void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
221{
222 if ( !mRenderer )
223 {
224 return;
225 }
226
227 mRenderer->setRadius( d );
228 emit widgetChanged();
229}
230
231void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
232{
233 if ( !mRenderer )
234 {
235 return;
236 }
237
238 mRenderer->setMaximumValue( d );
239 emit widgetChanged();
240}
241
242void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
243{
244 if ( !mRenderer )
245 {
246 return;
247 }
248
249 mRenderer->setRenderQuality( v );
250 emit widgetChanged();
251}
252
253void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
254{
255 mRenderer->setWeightExpression( expression );
256 emit widgetChanged();
257}
@ 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.
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.
@ 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:79
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.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.