QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 "moc_qgsheatmaprendererwidget.cpp"
17#include "qgsheatmaprenderer.h"
20#include "qgsvectorlayer.h"
21#include "qgscolorramp.h"
22#include "qgscolorrampbutton.h"
23#include "qgsstyle.h"
24#include "qgsproject.h"
25#include "qgsmapcanvas.h"
27#include <QGridLayout>
28#include <QLabel>
29
34
36{
37 QgsExpressionContext expContext;
38
39 if ( auto *lMapCanvas = mContext.mapCanvas() )
40 {
41 expContext = lMapCanvas->createExpressionContext();
42 }
43 else
44 {
49 }
50
51 if ( auto *lVectorLayer = vectorLayer() )
52 expContext << QgsExpressionContextUtils::layerScope( lVectorLayer );
53
54 // additional scopes
55 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
56 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
57 {
58 expContext.appendScope( new QgsExpressionContextScope( scope ) );
59 }
60
61 return expContext;
62}
63
65 : QgsRendererWidget( layer, style )
66
67{
68 if ( !layer )
69 {
70 return;
71 }
72 // the renderer only applies to point vector layers
74 {
75 //setup blank dialog
76 mRenderer = nullptr;
77 QLabel *label = new QLabel( tr( "The heatmap renderer only applies to point and multipoint layers. \n"
78 "'%1' is not a point layer and cannot be rendered as a heatmap." )
79 .arg( layer->name() ),
80 this );
81 if ( !layout() )
82 setLayout( new QGridLayout() );
83 layout()->addWidget( label );
84 return;
85 }
86
87 setupUi( this );
88 connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
89 connect( mRadiusSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
90 connect( mMaxSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
91 connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
92 this->layout()->setContentsMargins( 0, 0, 0, 0 );
93
95 mWeightExpressionWidget->registerExpressionContextGenerator( this );
96 mWeightExpressionWidget->setAllowEmptyFieldName( true );
97
98 if ( renderer )
99 {
101 }
102 if ( !mRenderer )
103 {
104 mRenderer = std::make_unique<QgsHeatmapRenderer>();
105 if ( renderer )
106 renderer->copyRendererData( mRenderer.get() );
107 }
108
109 btnColorRamp->setShowGradientOnly( true );
110
111 connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
112 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsHeatmapRendererWidget::showLegendSettings );
113
114 if ( mRenderer->colorRamp() )
115 {
116 btnColorRamp->blockSignals( true );
117 btnColorRamp->setColorRamp( mRenderer->colorRamp() );
118 btnColorRamp->blockSignals( false );
119 }
120 mRadiusSpinBox->blockSignals( true );
121 mRadiusSpinBox->setValue( mRenderer->radius() );
122 mRadiusSpinBox->blockSignals( false );
123 mRadiusUnitWidget->blockSignals( true );
124 mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
125 mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
126 mRadiusUnitWidget->blockSignals( false );
127 mMaxSpinBox->blockSignals( true );
128 mMaxSpinBox->setValue( mRenderer->maximumValue() );
129 mMaxSpinBox->blockSignals( false );
130 mQualitySlider->blockSignals( true );
131 mQualitySlider->setValue( mRenderer->renderQuality() );
132 mQualitySlider->blockSignals( false );
133
134 mWeightExpressionWidget->setLayer( layer );
135 mWeightExpressionWidget->setField( mRenderer->weightExpression() );
136 connect( mWeightExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
137
140}
141
143
145{
146 return mRenderer.get();
147}
148
150{
152 if ( auto *lMapCanvas = context.mapCanvas() )
153 mRadiusUnitWidget->setMapCanvas( lMapCanvas );
154}
155
156void QgsHeatmapRendererWidget::applyColorRamp()
157{
158 if ( !mRenderer )
159 {
160 return;
161 }
162
163 QgsColorRamp *ramp = btnColorRamp->colorRamp();
164 if ( !ramp )
165 return;
166
167 mRenderer->setColorRamp( ramp );
168 emit widgetChanged();
169}
170
171void QgsHeatmapRendererWidget::showLegendSettings()
172{
173 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast<QWidget *>( parent() ) );
174 if ( panel && panel->dockMode() )
175 {
177 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
178 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
179 legendPanel->setSettings( mRenderer->legendSettings() );
180 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [=] {
181 mRenderer->setLegendSettings( legendPanel->settings() );
182 emit widgetChanged();
183 } );
184 panel->openPanel( legendPanel );
185 }
186 else
187 {
188 QgsColorRampLegendNodeDialog dialog( mRenderer->legendSettings(), this, QgsColorRampLegendNodeWidget::Capabilities() );
189 dialog.setUseContinuousRampCheckBoxVisibility( false );
190 dialog.setWindowTitle( tr( "Legend Settings" ) );
191 if ( dialog.exec() )
192 {
193 mRenderer->setLegendSettings( dialog.settings() );
194 emit widgetChanged();
195 }
196 }
197}
198
199void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
200{
201 if ( !mRenderer )
202 {
203 return;
204 }
205
206 mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
207 mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
208 emit widgetChanged();
209}
210
211void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
212{
213 if ( !mRenderer )
214 {
215 return;
216 }
217
218 mRenderer->setRadius( d );
219 emit widgetChanged();
220}
221
222void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
223{
224 if ( !mRenderer )
225 {
226 return;
227 }
228
229 mRenderer->setMaximumValue( d );
230 emit widgetChanged();
231}
232
233void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
234{
235 if ( !mRenderer )
236 {
237 return;
238 }
239
240 mRenderer->setRenderQuality( v );
241 emit widgetChanged();
242}
243
244void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
245{
246 mRenderer->setWeightExpression( expression );
247 emit widgetChanged();
248}
@ 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.