QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsmeshrenderervectorsettingswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshrenderervectorsettingswidget.cpp
3 ---------------------------------------
4 begin : June 2018
5 copyright : (C) 2018 by Peter Petrik
6 email : zilolv 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 ***************************************************************************/
15
17
18#include "qgis.h"
19#include "qgsmeshlayer.h"
20
22 : QWidget( parent )
23{
24 setupUi( this );
25
26 QVector<QgsDoubleSpinBox *> widgets;
27 widgets << mMinMagSpinBox << mMaxMagSpinBox
28 << mHeadWidthSpinBox << mHeadLengthSpinBox
29 << mMinimumShaftSpinBox << mMaximumShaftSpinBox
30 << mScaleShaftByFactorOfSpinBox << mShaftLengthSpinBox;
31
32 // Setup defaults and clear values for spin boxes
33 for ( const auto &widget : std::as_const( widgets ) )
34 {
35 widget->setClearValueMode( QgsDoubleSpinBox::ClearValueMode::MinimumValue );
36 widget->setSpecialValueText( QString( ) );
37 widget->setValue( widget->minimum() );
38 }
39
40 mShaftLengthComboBox->setCurrentIndex( -1 );
41
42 mColoringMethodComboBox->addItem( tr( "Single Color" ), QgsInterpolatedLineColor::SingleColor );
43 mColoringMethodComboBox->addItem( tr( "Color Ramp Shader" ), QgsInterpolatedLineColor::ColorRamp );
44
45 mXSpacingSpinBox->setClearValue( 10.0 );
46 mYSpacingSpinBox->setClearValue( 10.0 );
47 mStreamlinesDensitySpinBox->setClearValue( 15.0 );
48 mTracesParticlesCountSpinBox->setClearValue( 1000 );
49 mTracesMaxLengthSpinBox->setClearValue( 100.0 );
50
52 connect( mColoringMethodComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
53 this, &QgsMeshRendererVectorSettingsWidget::onColoringMethodChanged );
54 connect( mColorRampShaderWidget, &QgsColorRampShaderWidget::widgetChanged,
56 connect( mColorRampShaderMinimumSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ),
57 this, &QgsMeshRendererVectorSettingsWidget::onColorRampMinMaxChanged );
58 connect( mColorRampShaderMaximumSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ),
59 this, &QgsMeshRendererVectorSettingsWidget::onColorRampMinMaxChanged );
60
61 connect( mLineWidthSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ),
63
64 connect( mShaftLengthComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
66
67 connect( mShaftLengthComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
68 mShaftOptionsStackedWidget, &QStackedWidget::setCurrentIndex );
69
70 connect( mDisplayVectorsOnGridGroupBox, &QGroupBox::toggled, this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
71
72 connect( mColorRampShaderLoadButton, &QPushButton::clicked, this, &QgsMeshRendererVectorSettingsWidget::loadColorRampShader );
73
74 onColoringMethodChanged();
75
76 for ( const auto &widget : std::as_const( widgets ) )
77 {
78 connect( widget, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
79 }
80
81 connect( mXSpacingSpinBox, qOverload<int>( &QgsSpinBox::valueChanged ), this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
82 connect( mYSpacingSpinBox, qOverload<int>( &QgsSpinBox::valueChanged ), this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
83
84 connect( mSymbologyVectorComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
85 this, &QgsMeshRendererVectorSettingsWidget::onSymbologyChanged );
86 onSymbologyChanged( 0 );
87
88 connect( mSymbologyVectorComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
90
91 connect( mStreamlinesSeedingMethodComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
92 this, &QgsMeshRendererVectorSettingsWidget::onStreamLineSeedingMethodChanged );
93 onStreamLineSeedingMethodChanged( 0 );
94
95 connect( mStreamlinesSeedingMethodComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
97
98 connect( mStreamlinesDensitySpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ),
100
101 connect( mTracesMaxLengthSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ),
103
104 connect( mTracesParticlesCountSpinBox, qOverload<int>( &QgsSpinBox::valueChanged ),
106
107 mTracesTailLengthMapUnitWidget->setUnits(
108 {
109 Qgis::RenderUnit::Millimeters,
110 Qgis::RenderUnit::MetersInMapUnits,
111 Qgis::RenderUnit::Pixels,
112 Qgis::RenderUnit::Points
113 } );
114
115 connect( mTracesTailLengthMapUnitWidget, &QgsUnitSelectionWidget::changed,
117}
118
120{
121 mMeshLayer = layer;
122}
123
125{
128 static_cast<QgsMeshRendererVectorSettings::Symbology>( mSymbologyVectorComboBox->currentIndex() ) );
129
130 //Arrow settings
132
133 // basic
134 settings.setColor( mColorWidget->color() );
135 settings.setLineWidth( mLineWidthSpinBox->value() );
137 ( mColoringMethodComboBox->currentData().toInt() ) );
138 settings.setColorRampShader( mColorRampShaderWidget->shader() );
139
140 // filter by magnitude
141 double val = filterValue( mMinMagSpinBox, -1 );
142 settings.setFilterMin( val );
143
144 val = filterValue( mMaxMagSpinBox, -1 );
145 settings.setFilterMax( val );
146
147 // arrow head
148 val = filterValue( mHeadWidthSpinBox, arrowSettings.arrowHeadWidthRatio() * 100.0 );
149 arrowSettings.setArrowHeadWidthRatio( val / 100.0 );
150
151 val = filterValue( mHeadLengthSpinBox, arrowSettings.arrowHeadLengthRatio() * 100.0 );
152 arrowSettings.setArrowHeadLengthRatio( val / 100.0 );
153
154 // user grid
155 bool enabled = mDisplayVectorsOnGridGroupBox->isChecked();
157 settings.setUserGridCellWidth( mXSpacingSpinBox->value() );
158 settings.setUserGridCellHeight( mYSpacingSpinBox->value() );
159
160 // shaft length
161 auto method = static_cast<QgsMeshRendererVectorArrowSettings::ArrowScalingMethod>( mShaftLengthComboBox->currentIndex() );
162 arrowSettings.setShaftLengthMethod( method );
163
164 val = filterValue( mMinimumShaftSpinBox, arrowSettings.minShaftLength() );
165 arrowSettings.setMinShaftLength( val );
166
167 val = filterValue( mMaximumShaftSpinBox, arrowSettings.maxShaftLength() );
168 arrowSettings.setMaxShaftLength( val );
169
170 val = filterValue( mScaleShaftByFactorOfSpinBox, arrowSettings.scaleFactor() );
171 arrowSettings.setScaleFactor( val );
172
173 val = filterValue( mShaftLengthSpinBox, arrowSettings.fixedShaftLength() );
174 arrowSettings.setFixedShaftLength( val );
175
176 settings.setArrowsSettings( arrowSettings );
177
178 //Streamline setting
180 streamlineSettings.setSeedingMethod(
181 static_cast<QgsMeshRendererVectorStreamlineSettings::SeedingStartPointsMethod>( mStreamlinesSeedingMethodComboBox->currentIndex() ) );
182
183 streamlineSettings.setSeedingDensity( mStreamlinesDensitySpinBox->value() / 100 );
184
185 settings.setStreamLinesSettings( streamlineSettings );
186
187 //Traces setting
189 tracesSettings.setMaximumTailLength( mTracesMaxLengthSpinBox->value() );
190 tracesSettings.setMaximumTailLengthUnit( mTracesTailLengthMapUnitWidget->unit() );
191 tracesSettings.setParticlesCount( mTracesParticlesCountSpinBox->value() );
192 settings.setTracesSettings( tracesSettings );
193
194 return settings;
195}
196
198{
199 if ( !mMeshLayer || !mMeshLayer->dataProvider() )
200 return;
201
202 if ( mActiveDatasetGroup < 0 )
203 return;
204
205 bool hasFaces = ( mMeshLayer->dataProvider() &&
206 mMeshLayer->dataProvider()->contains( QgsMesh::ElementType::Face ) );
207
208 const QgsMeshRendererSettings rendererSettings = mMeshLayer->rendererSettings();
209 const QgsMeshRendererVectorSettings settings = rendererSettings.vectorSettings( mActiveDatasetGroup );
210
211 symbologyLabel->setVisible( hasFaces );
212 mSymbologyVectorComboBox->setVisible( hasFaces );
213 mSymbologyVectorComboBox->setCurrentIndex( hasFaces ? settings.symbology() : 0 );
214
215 // Arrow settings
217
218 // basic
219 mColorWidget->setColor( settings.color() );
220 mLineWidthSpinBox->setValue( settings.lineWidth() );
221 mColoringMethodComboBox->setCurrentIndex( mColoringMethodComboBox->findData( settings.coloringMethod() ) );
222 mColorRampShaderWidget->setFromShader( settings.colorRampShader() );
223 mColorRampShaderMinimumSpinBox->setValue( settings.colorRampShader().minimumValue() );
224 mColorRampShaderMaximumSpinBox->setValue( settings.colorRampShader().maximumValue() );
225
226 // filter by magnitude
227 if ( settings.filterMin() > 0 )
228 {
229 mMinMagSpinBox->setValue( settings.filterMin() );
230 }
231 if ( settings.filterMax() > 0 )
232 {
233 mMaxMagSpinBox->setValue( settings.filterMax() );
234 }
235
236 // arrow head
237 mHeadWidthSpinBox->setValue( arrowSettings.arrowHeadWidthRatio() * 100.0 );
238 mHeadLengthSpinBox->setValue( arrowSettings.arrowHeadLengthRatio() * 100.0 );
239
240 // user grid
241 mDisplayVectorsOnGridGroupBox->setVisible( hasFaces );
242 mDisplayVectorsOnGridGroupBox->setChecked( settings.isOnUserDefinedGrid() && hasFaces );
243 mXSpacingSpinBox->setValue( settings.userGridCellWidth() );
244 mYSpacingSpinBox->setValue( settings.userGridCellHeight() );
245
246 // shaft length
247 mShaftLengthComboBox->setCurrentIndex( arrowSettings.shaftLengthMethod() );
248
249 mMinimumShaftSpinBox->setValue( arrowSettings.minShaftLength() );
250 mMaximumShaftSpinBox->setValue( arrowSettings.maxShaftLength() );
251 mScaleShaftByFactorOfSpinBox->setValue( arrowSettings.scaleFactor() );
252 mShaftLengthSpinBox->setValue( arrowSettings.fixedShaftLength() );
253
254 //Streamlines settings
256
257 mStreamlinesSeedingMethodComboBox->setCurrentIndex( streamlinesSettings.seedingMethod() );
258 mStreamlinesDensitySpinBox->setValue( streamlinesSettings.seedingDensity() * 100 );
259
260 //Traces settings
262
263 mTracesMaxLengthSpinBox->setValue( tracesSettings.maximumTailLength() );
264 mTracesTailLengthMapUnitWidget->setUnit( tracesSettings.maximumTailLengthUnit() );
265 mTracesParticlesCountSpinBox->setValue( tracesSettings.particlesCount() );
266
267}
268
269void QgsMeshRendererVectorSettingsWidget::onSymbologyChanged( int currentIndex )
270{
271 mStreamlineWidget->setVisible( currentIndex == QgsMeshRendererVectorSettings::Streamlines );
272 mArrowLengthGroupBox->setVisible( currentIndex == QgsMeshRendererVectorSettings::Arrows );
273 mHeadOptionsGroupBox->setVisible( currentIndex == QgsMeshRendererVectorSettings::Arrows );
274 mTracesGroupBox->setVisible( currentIndex == QgsMeshRendererVectorSettings::Traces );
275
276 mDisplayVectorsOnGridGroupBox->setVisible( currentIndex != QgsMeshRendererVectorSettings::Traces );
277 filterByMagnitudeLabel->setVisible( currentIndex != QgsMeshRendererVectorSettings::Traces );
278 minimumMagLabel->setVisible( currentIndex != QgsMeshRendererVectorSettings::Traces );
279 mMinMagSpinBox->setVisible( currentIndex != QgsMeshRendererVectorSettings::Traces );
280 maximumMagLabel->setVisible( currentIndex != QgsMeshRendererVectorSettings::Traces );
281 mMaxMagSpinBox->setVisible( currentIndex != QgsMeshRendererVectorSettings::Traces );
282
283 mDisplayVectorsOnGridGroupBox->setEnabled(
286 mStreamlinesSeedingMethodComboBox->currentIndex() == QgsMeshRendererVectorStreamlineSettings::MeshGridded ) ) ;
287}
288
289void QgsMeshRendererVectorSettingsWidget::onStreamLineSeedingMethodChanged( int currentIndex )
290{
291 bool enabled = currentIndex == QgsMeshRendererVectorStreamlineSettings::Random;
292 mStreamlinesDensityLabel->setEnabled( enabled );
293 mStreamlinesDensitySpinBox->setEnabled( enabled );
294
295 mDisplayVectorsOnGridGroupBox->setEnabled( !enabled );
296}
297
298void QgsMeshRendererVectorSettingsWidget::onColoringMethodChanged()
299{
300 mColorRampShaderGroupBox->setVisible( mColoringMethodComboBox->currentData() == QgsInterpolatedLineColor::ColorRamp );
301 mColorWidget->setVisible( mColoringMethodComboBox->currentData() == QgsInterpolatedLineColor::SingleColor );
302 mSingleColorLabel->setVisible( mColoringMethodComboBox->currentData() == QgsInterpolatedLineColor::SingleColor );
303
304 if ( mColorRampShaderWidget->shader().colorRampItemList().isEmpty() )
305 loadColorRampShader();
306
307 emit widgetChanged();
308}
309
310void QgsMeshRendererVectorSettingsWidget::onColorRampMinMaxChanged()
311{
312 mColorRampShaderWidget->setMinimumMaximumAndClassify(
313 filterValue( mColorRampShaderMinimumSpinBox, 0 ),
314 filterValue( mColorRampShaderMaximumSpinBox, 0 ) );
315}
316
317void QgsMeshRendererVectorSettingsWidget::loadColorRampShader()
318{
319 if ( !mMeshLayer )
320 return;
321
322 int currentVectorDataSetGroupIndex = mMeshLayer->rendererSettings().activeVectorDatasetGroup();
323 if ( currentVectorDataSetGroupIndex < 0 ||
324 !mMeshLayer->datasetGroupMetadata( currentVectorDataSetGroupIndex ).isVector() )
325 return;
326
327 const QgsMeshDatasetGroupMetadata meta = mMeshLayer->datasetGroupMetadata( currentVectorDataSetGroupIndex );
328 double min = meta.minimum();
329 double max = meta.maximum();
330
331 mColorRampShaderWidget->setMinimumMaximumAndClassify( min, max );
332 whileBlocking( mColorRampShaderMinimumSpinBox )->setValue( min );
333 whileBlocking( mColorRampShaderMaximumSpinBox )->setValue( max );
334}
335
336double QgsMeshRendererVectorSettingsWidget::filterValue( const QgsDoubleSpinBox *spinBox, double errVal ) const
337{
338 if ( spinBox->value() == spinBox->clearValue() )
339 return errVal;
340
341 return spinBox->value();
342}
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
void widgetChanged()
Widget changed.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
ColoringMethod
Defines how the color is defined.
@ ColorRamp
Render with a color ramp.
@ SingleColor
Render with a single color.
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
bool isVector() const
Returns whether dataset group has vector data.
double minimum() const
Returns minimum scalar value/vector magnitude present for whole dataset group.
double maximum() const
Returns maximum scalar value/vector magnitude present for whole dataset group.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:100
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
Represents all mesh renderer settings.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
QgsMeshRendererVectorSettings vectorSettings(int groupIndex) const
Returns renderer settings.
Represents a mesh renderer settings for vector datasets displayed with arrows.
void setFixedShaftLength(double fixedShaftLength)
Sets fixed length (in millimeters)
void setMaxShaftLength(double maxShaftLength)
Sets maximum shaft length (in millimeters)
QgsMeshRendererVectorArrowSettings::ArrowScalingMethod shaftLengthMethod() const
Returns method used for drawing arrows.
void setMinShaftLength(double minShaftLength)
Sets mininimum shaft length (in millimeters)
double fixedShaftLength() const
Returns fixed arrow length (in millimeters)
void setArrowHeadWidthRatio(double arrowHeadWidthRatio)
Sets ratio of the head width of the arrow (range 0-1)
double scaleFactor() const
Returns scale factor.
double maxShaftLength() const
Returns maximum shaft length (in millimeters)
double arrowHeadWidthRatio() const
Returns ratio of the head width of the arrow (range 0-1)
void setArrowHeadLengthRatio(double arrowHeadLengthRatio)
Sets ratio of the head length of the arrow (range 0-1)
void setScaleFactor(double scaleFactor)
Sets scale factor.
void setShaftLengthMethod(ArrowScalingMethod shaftLengthMethod)
Sets method used for drawing arrows.
ArrowScalingMethod
Algorithm how to transform vector magnitude to length of arrow on the device in pixels.
double minShaftLength() const
Returns mininimum shaft length (in millimeters)
double arrowHeadLengthRatio() const
Returns ratio of the head length of the arrow (range 0-1)
QgsMeshRendererVectorSettingsWidget(QWidget *parent=nullptr)
A widget to hold the renderer Vector settings for a mesh layer.
QgsMeshRendererVectorSettings settings() const
Returns vector settings.
void syncToLayer()
Synchronizes widgets state with associated mesh layer.
void setLayer(QgsMeshLayer *layer)
Associates mesh layer with the widget.
void widgetChanged()
Mesh rendering settings changed.
Represents a renderer settings for vector datasets.
void setColorRampShader(const QgsColorRampShader &colorRampShader)
Returns the color ramp shader used to render vector datasets.
void setStreamLinesSettings(const QgsMeshRendererVectorStreamlineSettings &streamLinesSettings)
Sets settings for vector rendered with streamlines.
int userGridCellWidth() const
Returns width in pixels of user grid cell.
void setArrowsSettings(const QgsMeshRendererVectorArrowSettings &arrowSettings)
Sets settings for vector rendered with arrows.
void setUserGridCellWidth(int width)
Sets width of user grid cell (in pixels)
void setColor(const QColor &color)
Sets color used for drawing arrows.
QgsMeshRendererVectorTracesSettings tracesSettings() const
Returns settings for vector rendered with traces.
QColor color() const
Returns color used for drawing arrows.
int userGridCellHeight() const
Returns height in pixels of user grid cell.
void setOnUserDefinedGrid(bool enabled)
Toggles drawing of vectors on user defined grid.
double lineWidth() const
Returns line width of the arrow (in millimeters)
Symbology
Defines the symbology of vector rendering.
@ Traces
Displaying vector dataset with particle traces.
@ Arrows
Displaying vector dataset with arrows.
@ Streamlines
Displaying vector dataset with streamlines.
void setUserGridCellHeight(int height)
Sets height of user grid cell (in pixels)
Symbology symbology() const
Returns the displaying method used to render vector datasets.
double filterMax() const
Returns filter value for vector magnitudes.
QgsColorRampShader colorRampShader() const
Sets the color ramp shader used to render vector datasets.
void setSymbology(const Symbology &symbology)
Sets the displaying method used to render vector datasets.
void setFilterMin(double filterMin)
Sets filter value for vector magnitudes.
QgsMeshRendererVectorArrowSettings arrowSettings() const
Returns settings for vector rendered with arrows.
void setFilterMax(double filterMax)
Sets filter value for vector magnitudes.
void setTracesSettings(const QgsMeshRendererVectorTracesSettings &tracesSettings)
Sets settings for vector rendered with traces.
void setColoringMethod(const QgsInterpolatedLineColor::ColoringMethod &coloringMethod)
Sets the coloring method used to render vector datasets.
QgsInterpolatedLineColor::ColoringMethod coloringMethod() const
Returns the coloring method used to render vector datasets.
void setLineWidth(double lineWidth)
Sets line width of the arrow in pixels (in millimeters)
bool isOnUserDefinedGrid() const
Returns whether vectors are drawn on user-defined grid.
double filterMin() const
Returns filter value for vector magnitudes.
QgsMeshRendererVectorStreamlineSettings streamLinesSettings() const
Returns settings for vector rendered with streamlines.
Represents a streamline renderer settings for vector datasets displayed by streamlines.
void setSeedingDensity(double seedingDensity)
Sets the density used for seeding start points.
SeedingStartPointsMethod seedingMethod() const
Returns the method used for seeding start points of strealines.
void setSeedingMethod(const SeedingStartPointsMethod &seedingMethod)
Sets the method used for seeding start points of strealines.
SeedingStartPointsMethod
Method used to define start points that are used to draw streamlines.
@ Random
Seeds start points randomly on the mesh.
@ MeshGridded
Seeds start points on the vertices mesh or user regular grid.
double seedingDensity() const
Returns the density used for seeding start points.
Represents a trace renderer settings for vector datasets displayed by particle traces.
Qgis::RenderUnit maximumTailLengthUnit() const
Returns the maximum tail length unit.
void setMaximumTailLength(double maximumTailLength)
Sets the maximums tail length.
void setMaximumTailLengthUnit(Qgis::RenderUnit maximumTailLengthUnit)
Sets the maximum tail length unit.
double maximumTailLength() const
Returns the maximum tail length.
int particlesCount() const
Returns particles count.
void setParticlesCount(int value)
Sets particles count.
double maximumValue() const
Returns the minimum value for the raster shader.
double minimumValue() const
Returns the maximum value for the raster shader.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:3914