QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 {
48 expContext
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(
82 tr(
83 "The heatmap renderer only applies to point and multipoint layers. \n"
84 "'%1' is not a point layer and cannot be rendered as a heatmap."
85 )
86 .arg( layer->name() ),
87 this
88 );
89 if ( !layout() )
90 setLayout( new QGridLayout() );
91 layout()->addWidget( label );
92 return;
93 }
94
95 setupUi( this );
96 connect( mRadiusUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsHeatmapRendererWidget::mRadiusUnitWidget_changed );
97 connect( mRadiusSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged );
98 connect( mMaxSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged );
99 connect( mQualitySlider, &QSlider::valueChanged, this, &QgsHeatmapRendererWidget::mQualitySlider_valueChanged );
100 this->layout()->setContentsMargins( 0, 0, 0, 0 );
101
102 mRadiusUnitWidget->setUnits(
104 );
105 mWeightExpressionWidget->registerExpressionContextGenerator( this );
106 mWeightExpressionWidget->setAllowEmptyFieldName( true );
107
108 if ( renderer )
109 {
111 }
112 if ( !mRenderer )
113 {
114 mRenderer = std::make_unique<QgsHeatmapRenderer>();
115 if ( renderer )
116 renderer->copyRendererData( mRenderer.get() );
117 }
118
119 btnColorRamp->setShowGradientOnly( true );
120
121 connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsHeatmapRendererWidget::applyColorRamp );
122 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsHeatmapRendererWidget::showLegendSettings );
123
124 if ( mRenderer->colorRamp() )
125 {
126 btnColorRamp->blockSignals( true );
127 btnColorRamp->setColorRamp( mRenderer->colorRamp() );
128 btnColorRamp->blockSignals( false );
129 }
130 mRadiusSpinBox->blockSignals( true );
131 mRadiusSpinBox->setValue( mRenderer->radius() );
132 mRadiusSpinBox->blockSignals( false );
133 mRadiusUnitWidget->blockSignals( true );
134 mRadiusUnitWidget->setUnit( mRenderer->radiusUnit() );
135 mRadiusUnitWidget->setMapUnitScale( mRenderer->radiusMapUnitScale() );
136 mRadiusUnitWidget->blockSignals( false );
137 mMaxSpinBox->blockSignals( true );
138 mMaxSpinBox->setValue( mRenderer->maximumValue() );
139 mMaxSpinBox->blockSignals( false );
140 mQualitySlider->blockSignals( true );
141 mQualitySlider->setValue( mRenderer->renderQuality() );
142 mQualitySlider->blockSignals( false );
143
144 mWeightExpressionWidget->setLayer( layer );
145 mWeightExpressionWidget->setField( mRenderer->weightExpression() );
146 connect( mWeightExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsHeatmapRendererWidget::weightExpressionChanged );
147
150}
151
153
155{
156 return mRenderer.get();
157}
158
160{
162 if ( auto *lMapCanvas = context.mapCanvas() )
163 mRadiusUnitWidget->setMapCanvas( lMapCanvas );
164}
165
166void QgsHeatmapRendererWidget::applyColorRamp()
167{
168 if ( !mRenderer )
169 {
170 return;
171 }
172
173 QgsColorRamp *ramp = btnColorRamp->colorRamp();
174 if ( !ramp )
175 return;
176
177 mRenderer->setColorRamp( ramp );
178 emit widgetChanged();
179}
180
181void QgsHeatmapRendererWidget::showLegendSettings()
182{
183 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast<QWidget *>( parent() ) );
184 if ( panel && panel->dockMode() )
185 {
186 QgsColorRampLegendNodeWidget *legendPanel = new QgsColorRampLegendNodeWidget( nullptr, QgsColorRampLegendNodeWidget::Capabilities() );
187 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
188 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
189 legendPanel->setSettings( mRenderer->legendSettings() );
190 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [this, legendPanel] {
191 mRenderer->setLegendSettings( legendPanel->settings() );
192 emit widgetChanged();
193 } );
194 panel->openPanel( legendPanel );
195 }
196 else
197 {
198 QgsColorRampLegendNodeDialog dialog( mRenderer->legendSettings(), this, QgsColorRampLegendNodeWidget::Capabilities() );
199 dialog.setUseContinuousRampCheckBoxVisibility( false );
200 dialog.setWindowTitle( tr( "Legend Settings" ) );
201 if ( dialog.exec() )
202 {
203 mRenderer->setLegendSettings( dialog.settings() );
204 emit widgetChanged();
205 }
206 }
207}
208
209void QgsHeatmapRendererWidget::mRadiusUnitWidget_changed()
210{
211 if ( !mRenderer )
212 {
213 return;
214 }
215
216 mRenderer->setRadiusUnit( mRadiusUnitWidget->unit() );
217 mRenderer->setRadiusMapUnitScale( mRadiusUnitWidget->getMapUnitScale() );
218 emit widgetChanged();
219}
220
221void QgsHeatmapRendererWidget::mRadiusSpinBox_valueChanged( double d )
222{
223 if ( !mRenderer )
224 {
225 return;
226 }
227
228 mRenderer->setRadius( d );
229 emit widgetChanged();
230}
231
232void QgsHeatmapRendererWidget::mMaxSpinBox_valueChanged( double d )
233{
234 if ( !mRenderer )
235 {
236 return;
237 }
238
239 mRenderer->setMaximumValue( d );
240 emit widgetChanged();
241}
242
243void QgsHeatmapRendererWidget::mQualitySlider_valueChanged( int v )
244{
245 if ( !mRenderer )
246 {
247 return;
248 }
249
250 mRenderer->setRenderQuality( v );
251 emit widgetChanged();
252}
253
254void QgsHeatmapRendererWidget::weightExpressionChanged( const QString &expression )
255{
256 mRenderer->setWeightExpression( expression );
257 emit widgetChanged();
258}
@ Point
Points.
Definition qgis.h:380
@ Millimeters
Millimeters.
Definition qgis.h:5341
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5345
@ MapUnits
Map units.
Definition qgis.h:5342
@ Pixels
Pixels.
Definition qgis.h:5343
@ Inches
Inches.
Definition qgis.h:5346
@ MetersInMapUnits
Meters value as Map units.
Definition qgis.h:5348
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:89
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.