QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
17#include "qgscolorramp.h"
18#include "qgscolorrampbutton.h"
21#include "qgsheatmaprenderer.h"
22#include "qgsmapcanvas.h"
23#include "qgsproject.h"
24#include "qgsstyle.h"
26#include "qgsvectorlayer.h"
27
28#include <QGridLayout>
29#include <QLabel>
30
31#include "moc_qgsheatmaprendererwidget.cpp"
32
37
39{
40 QgsExpressionContext expContext;
41
42 if ( auto *lMapCanvas = mContext.mapCanvas() )
43 {
44 expContext = lMapCanvas->createExpressionContext();
45 }
46 else
47 {
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() ),
83 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
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 {
179 QgsColorRampLegendNodeWidget *legendPanel = new QgsColorRampLegendNodeWidget( nullptr, QgsColorRampLegendNodeWidget::Capabilities() );
180 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
181 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
182 legendPanel->setSettings( mRenderer->legendSettings() );
183 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [this, legendPanel] {
184 mRenderer->setLegendSettings( legendPanel->settings() );
185 emit widgetChanged();
186 } );
187 panel->openPanel( legendPanel );
188 }
189 else
190 {
191 QgsColorRampLegendNodeDialog dialog( mRenderer->legendSettings(), this, QgsColorRampLegendNodeWidget::Capabilities() );
192 dialog.setUseContinuousRampCheckBoxVisibility( false );
193 dialog.setWindowTitle( tr( "Legend Settings" ) );
194 if ( dialog.exec() )
195 {
196 mRenderer->setLegendSettings( dialog.settings() );
197 emit widgetChanged();
198 }
199 }
200}
201
202void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
203{
204 if ( !mRenderer )
205 {
206 return;
207 }
208
209 mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
210 mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
211 emit widgetChanged();
212}
213
214void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
215{
216 if ( !mRenderer )
217 {
218 return;
219 }
220
221 mRenderer->setRadius( d );
222 emit widgetChanged();
223}
224
225void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
226{
227 if ( !mRenderer )
228 {
229 return;
230 }
231
232 mRenderer->setMaximumValue( d );
233 emit widgetChanged();
234}
235
236void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
237{
238 if ( !mRenderer )
239 {
240 return;
241 }
242
243 mRenderer->setRenderQuality( v );
244 emit widgetChanged();
245}
246
247void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
248{
249 mRenderer->setWeightExpression( expression );
250 emit widgetChanged();
251}
@ Point
Points.
Definition qgis.h:359
@ Millimeters
Millimeters.
Definition qgis.h:5184
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5188
@ MapUnits
Map units.
Definition qgis.h:5185
@ Pixels
Pixels.
Definition qgis.h:5186
@ Inches
Inches.
Definition qgis.h:5189
@ MetersInMapUnits
Meters value as Map units.
Definition qgis.h:5191
void colorRampChanged()
Emitted whenever a new color ramp is set for the button.
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.
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.
A widget for selection of layer fields or expression creation.
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)
Contains configuration for rendering maps.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
bool dockMode() const
Returns the dock mode state.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
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.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsRendererWidget(QgsVectorLayer *layer, QgsStyle *style)
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.
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:88
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
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 dataset.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.