QGIS API Documentation 3.41.0-Master (3440c17df1d)
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() ), this );
80 if ( !layout() )
81 setLayout( new QGridLayout() );
82 layout()->addWidget( label );
83 return;
84 }
85
86 setupUi( this );
87 connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
88 connect( mRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
89 connect( mMaxSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
90 connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
91 this->layout()->setContentsMargins( 0, 0, 0, 0 );
92
93 mRadiusUnitWidget->setUnits( { Qgis::RenderUnit::Millimeters,
99 mWeightExpressionWidget->registerExpressionContextGenerator( this );
100 mWeightExpressionWidget->setAllowEmptyFieldName( true );
101
102 if ( renderer )
103 {
105 }
106 if ( !mRenderer )
107 {
108 mRenderer = std::make_unique< QgsHeatmapRenderer >();
109 if ( renderer )
110 renderer->copyRendererData( mRenderer.get() );
111 }
112
113 btnColorRamp->setShowGradientOnly( true );
114
115 connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
116 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsHeatmapRendererWidget::showLegendSettings );
117
118 if ( mRenderer->colorRamp() )
119 {
120 btnColorRamp->blockSignals( true );
121 btnColorRamp->setColorRamp( mRenderer->colorRamp() );
122 btnColorRamp->blockSignals( false );
123 }
124 mRadiusSpinBox->blockSignals( true );
125 mRadiusSpinBox->setValue( mRenderer->radius() );
126 mRadiusSpinBox->blockSignals( false );
127 mRadiusUnitWidget->blockSignals( true );
128 mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
129 mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
130 mRadiusUnitWidget->blockSignals( false );
131 mMaxSpinBox->blockSignals( true );
132 mMaxSpinBox->setValue( mRenderer->maximumValue() );
133 mMaxSpinBox->blockSignals( false );
134 mQualitySlider->blockSignals( true );
135 mQualitySlider->setValue( mRenderer->renderQuality() );
136 mQualitySlider->blockSignals( false );
137
138 mWeightExpressionWidget->setLayer( layer );
139 mWeightExpressionWidget->setField( mRenderer->weightExpression() );
140 connect( mWeightExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
141
144}
145
147
149{
150 return mRenderer.get();
151}
152
154{
156 if ( auto *lMapCanvas = context.mapCanvas() )
157 mRadiusUnitWidget->setMapCanvas( lMapCanvas );
158}
159
160void QgsHeatmapRendererWidget::applyColorRamp()
161{
162 if ( !mRenderer )
163 {
164 return;
165 }
166
167 QgsColorRamp *ramp = btnColorRamp->colorRamp();
168 if ( !ramp )
169 return;
170
171 mRenderer->setColorRamp( ramp );
172 emit widgetChanged();
173}
174
175void QgsHeatmapRendererWidget::showLegendSettings()
176{
177 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast< QWidget * >( parent() ) );
178 if ( panel && panel->dockMode() )
179 {
181 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
182 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
183 legendPanel->setSettings( mRenderer->legendSettings() );
184 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [ = ]
185 {
186 mRenderer->setLegendSettings( legendPanel->settings() );
187 emit widgetChanged();
188 } );
189 panel->openPanel( legendPanel );
190 }
191 else
192 {
193 QgsColorRampLegendNodeDialog dialog( mRenderer->legendSettings(), this, QgsColorRampLegendNodeWidget::Capabilities() );
194 dialog.setUseContinuousRampCheckBoxVisibility( false );
195 dialog.setWindowTitle( tr( "Legend Settings" ) );
196 if ( dialog.exec() )
197 {
198 mRenderer->setLegendSettings( dialog.settings() );
199 emit widgetChanged();
200 }
201 }
202}
203
204void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
205{
206 if ( !mRenderer )
207 {
208 return;
209 }
210
211 mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
212 mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
213 emit widgetChanged();
214}
215
216void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
217{
218 if ( !mRenderer )
219 {
220 return;
221 }
222
223 mRenderer->setRadius( d );
224 emit widgetChanged();
225}
226
227void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
228{
229 if ( !mRenderer )
230 {
231 return;
232 }
233
234 mRenderer->setMaximumValue( d );
235 emit widgetChanged();
236}
237
238void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
239{
240 if ( !mRenderer )
241 {
242 return;
243 }
244
245 mRenderer->setRenderQuality( v );
246 emit widgetChanged();
247}
248
249void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
250{
251 mRenderer->setWeightExpression( expression );
252 emit widgetChanged();
253}
@ 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.