QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmeshrendererscalarsettingswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshrendererscalarsettingswidget.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 "QDialogButtonBox"
19 
20 #include "qgis.h"
21 #include "qgsmeshlayer.h"
22 #include "qgsmeshlayerutils.h"
23 #include "qgsmessagelog.h"
25 #include "qgssettings.h"
26 #include <QPointer>
27 
29  : QWidget( parent )
30 
31 {
32  setupUi( this );
33 
34  mScalarMinSpinBox->setClearValueMode( QgsDoubleSpinBox::ClearValueMode::MinimumValue );
35  mScalarMinSpinBox->setSpecialValueText( QString( ) );
36  mScalarMaxSpinBox->setClearValueMode( QgsDoubleSpinBox::ClearValueMode::MinimumValue );
37  mScalarMaxSpinBox->setSpecialValueText( QString( ) );
38 
39  // add items to data interpolation combo box
40  mScalarInterpolationTypeComboBox->addItem( tr( "None" ), QgsMeshRendererScalarSettings::None );
41  mScalarInterpolationTypeComboBox->addItem( tr( "Neighbour Average" ), QgsMeshRendererScalarSettings::NeighbourAverage );
42  mScalarInterpolationTypeComboBox->setCurrentIndex( 0 );
43 
44  mScalarEdgeStrokeWidthUnitSelectionWidget->setUnits( QgsUnitTypes::RenderUnitList()
49 
50  // connect
51  connect( mScalarRecalculateMinMaxButton, &QPushButton::clicked, this, &QgsMeshRendererScalarSettingsWidget::recalculateMinMaxButtonClicked );
52  connect( mScalarMinSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, [ = ]( double ) { minMaxChanged(); } );
53  connect( mScalarMaxSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, [ = ]( double ) { minMaxChanged(); } );
54  connect( mScalarEdgeStrokeWidthVariableRadioButton, &QRadioButton::toggled, this, &QgsMeshRendererScalarSettingsWidget::onEdgeStrokeWidthMethodChanged );
55 
58  connect( mScalarInterpolationTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsMeshRendererScalarSettingsWidget::widgetChanged );
59 
60  connect( mScalarEdgeStrokeWidthUnitSelectionWidget, &QgsUnitSelectionWidget::changed,
62  connect( mScalarEdgeStrokeWidthSpinBox, qOverload<double>( &QgsDoubleSpinBox::valueChanged ),
64  connect( mScalarEdgeStrokeWidthVariableRadioButton, &QCheckBox::toggled, this, &QgsMeshRendererScalarSettingsWidget::widgetChanged );
65  connect( mScalarEdgeStrokeWidthFixedRadioButton, &QCheckBox::toggled, this, &QgsMeshRendererScalarSettingsWidget::widgetChanged );
66  connect( mScalarEdgeStrokeWidthVariablePushButton, &QgsMeshVariableStrokeWidthButton::widgetChanged, this, &QgsMeshRendererScalarSettingsWidget::widgetChanged );
67 
68 }
69 
71 {
72  mMeshLayer = layer;
73  mScalarInterpolationTypeComboBox->setEnabled( !dataIsDefinedOnEdges() );
74 }
75 
77 {
78  mActiveDatasetGroup = groupIndex;
79  mScalarInterpolationTypeComboBox->setEnabled( !dataIsDefinedOnEdges() );
80 }
81 
83 {
85  settings.setColorRampShader( mScalarColorRampShaderWidget->shader() );
86  settings.setClassificationMinimumMaximum( spinBoxValue( mScalarMinSpinBox ), spinBoxValue( mScalarMaxSpinBox ) );
87  settings.setOpacity( mOpacityWidget->opacity() );
88  settings.setDataResamplingMethod( dataIntepolationMethod() );
89 
90  const bool hasEdges = ( mMeshLayer->contains( QgsMesh::ElementType::Edge ) );
91  if ( hasEdges )
92  {
93 
94  QgsInterpolatedLineWidth edgeStrokeWidth = mScalarEdgeStrokeWidthVariablePushButton->variableStrokeWidth();
95  edgeStrokeWidth.setIsVariableWidth( mScalarEdgeStrokeWidthVariableRadioButton->isChecked() );
96  edgeStrokeWidth.setFixedStrokeWidth( mScalarEdgeStrokeWidthSpinBox->value() );
97  settings.setEdgeStrokeWidth( edgeStrokeWidth );
98  settings.setEdgeStrokeWidthUnit( mScalarEdgeStrokeWidthUnitSelectionWidget->unit() );
99  }
100 
101  return settings;
102 }
103 
105 {
106  if ( !mMeshLayer )
107  return;
108 
109  if ( mActiveDatasetGroup < 0 )
110  return;
111 
112  const QgsMeshRendererSettings rendererSettings = mMeshLayer->rendererSettings();
113  const QgsMeshRendererScalarSettings settings = rendererSettings.scalarSettings( mActiveDatasetGroup );
115  const double min = settings.classificationMinimum();
116  const double max = settings.classificationMaximum();
117 
118  whileBlocking( mScalarMinSpinBox )->setValue( min );
119  whileBlocking( mScalarMaxSpinBox )->setValue( max );
120  whileBlocking( mScalarColorRampShaderWidget )->setFromShader( shader );
121  whileBlocking( mScalarColorRampShaderWidget )->setMinimumMaximum( min, max );
122  whileBlocking( mOpacityWidget )->setOpacity( settings.opacity() );
123  const int index = mScalarInterpolationTypeComboBox->findData( settings.dataResamplingMethod() );
124  whileBlocking( mScalarInterpolationTypeComboBox )->setCurrentIndex( index );
125 
126  const bool hasEdges = ( mMeshLayer->contains( QgsMesh::ElementType::Edge ) );
127  const bool hasFaces = ( mMeshLayer->contains( QgsMesh::ElementType::Face ) );
128 
129  mScalarResamplingWidget->setVisible( hasFaces );
130 
131  mEdgeWidthGroupBox->setVisible( hasEdges );
132 
133  if ( hasEdges )
134  {
135  const QgsInterpolatedLineWidth edgeStrokeWidth = settings.edgeStrokeWidth();
136  whileBlocking( mScalarEdgeStrokeWidthVariablePushButton )->setVariableStrokeWidth( edgeStrokeWidth );
137  whileBlocking( mScalarEdgeStrokeWidthSpinBox )->setValue( edgeStrokeWidth.fixedStrokeWidth() );
138  whileBlocking( mScalarEdgeStrokeWidthVariableRadioButton )->setChecked( edgeStrokeWidth.isVariableWidth() );
139  whileBlocking( mScalarEdgeStrokeWidthUnitSelectionWidget )->setUnit( settings.edgeStrokeWidthUnit() );
140  if ( !hasFaces )
141  mOpacityContainerWidget->setVisible( false );
142 
143  const QgsMeshDatasetGroupMetadata metadata = mMeshLayer->datasetGroupMetadata( mActiveDatasetGroup );
144  const double min = metadata.minimum();
145  const double max = metadata.maximum();
146  mScalarEdgeStrokeWidthVariablePushButton->setDefaultMinMaxValue( min, max );
147  }
148 
149  onEdgeStrokeWidthMethodChanged();
150 }
151 
152 double QgsMeshRendererScalarSettingsWidget::spinBoxValue( const QgsDoubleSpinBox *spinBox ) const
153 {
154  if ( spinBox->value() == spinBox->clearValue() )
155  {
156  return std::numeric_limits<double>::quiet_NaN();
157  }
158 
159  return spinBox->value();
160 }
161 
162 void QgsMeshRendererScalarSettingsWidget::minMaxChanged()
163 {
164  const double min = spinBoxValue( mScalarMinSpinBox );
165  const double max = spinBoxValue( mScalarMaxSpinBox );
166  mScalarColorRampShaderWidget->setMinimumMaximumAndClassify( min, max );
167 }
168 
169 void QgsMeshRendererScalarSettingsWidget::recalculateMinMaxButtonClicked()
170 {
171  const QgsMeshDatasetGroupMetadata metadata = mMeshLayer->datasetGroupMetadata( mActiveDatasetGroup );
172  const double min = metadata.minimum();
173  const double max = metadata.maximum();
174  whileBlocking( mScalarMinSpinBox )->setValue( min );
175  whileBlocking( mScalarMaxSpinBox )->setValue( max );
176  mScalarColorRampShaderWidget->setMinimumMaximumAndClassify( min, max );
177 }
178 
179 void QgsMeshRendererScalarSettingsWidget::onEdgeStrokeWidthMethodChanged()
180 {
181  const bool variableWidth = mScalarEdgeStrokeWidthVariableRadioButton->isChecked();
182  mScalarEdgeStrokeWidthVariablePushButton->setVisible( variableWidth );
183  mScalarEdgeStrokeWidthSpinBox->setVisible( !variableWidth );
184 }
185 
186 QgsMeshRendererScalarSettings::DataResamplingMethod QgsMeshRendererScalarSettingsWidget::dataIntepolationMethod() const
187 {
188  const int data = mScalarInterpolationTypeComboBox->currentData().toInt();
190  return method;
191 }
192 
193 bool QgsMeshRendererScalarSettingsWidget::dataIsDefinedOnFaces() const
194 {
195  if ( !mMeshLayer )
196  return false;
197 
198  if ( mActiveDatasetGroup < 0 )
199  return false;
200 
201  const QgsMeshDatasetGroupMetadata meta = mMeshLayer->datasetGroupMetadata( mActiveDatasetGroup );
202  const bool onFaces = ( meta.dataType() == QgsMeshDatasetGroupMetadata::DataOnFaces );
203  return onFaces;
204 }
205 
206 bool QgsMeshRendererScalarSettingsWidget::dataIsDefinedOnEdges() const
207 {
208  if ( !mMeshLayer )
209  return false;
210 
211  if ( mActiveDatasetGroup < 0 )
212  return false;
213 
214  const QgsMeshDatasetGroupMetadata meta = mMeshLayer->datasetGroupMetadata( mActiveDatasetGroup );
215  const bool onEdges = ( meta.dataType() == QgsMeshDatasetGroupMetadata::DataOnEdges );
216  return onEdges;
217 }
218 
219 
QgsMeshDatasetGroupMetadata::minimum
double minimum() const
Returns minimum scalar value/vector magnitude present for whole dataset group.
Definition: qgsmeshdataset.cpp:177
qgsmeshlayerutils.h
QgsOpacityWidget::opacityChanged
void opacityChanged(double opacity)
Emitted when the opacity is changed in the widget, where opacity ranges from 0.0 (transparent) to 1....
QgsMeshVariableStrokeWidthButton::widgetChanged
void widgetChanged()
QgsMeshRendererScalarSettings::setClassificationMinimumMaximum
void setClassificationMinimumMaximum(double minimum, double maximum)
Sets min/max values used for creation of the color ramp shader.
Definition: qgsmeshrenderersettings.cpp:95
QgsMeshRendererScalarSettings::dataResamplingMethod
DataResamplingMethod dataResamplingMethod() const
Returns the type of interpolation to use to convert face defined datasets to values on vertices.
Definition: qgsmeshrenderersettings.cpp:105
QgsMeshRendererSettings
Represents all mesh renderer settings.
Definition: qgsmeshrenderersettings.h:590
QgsMeshLayer::contains
bool contains(const QgsMesh::ElementType &type) const
Returns whether the mesh contains at mesh elements of given type.
Definition: qgsmeshlayer.cpp:1134
QgsMeshRendererScalarSettings::classificationMinimum
double classificationMinimum() const
Returns min value used for creation of the color ramp shader.
Definition: qgsmeshrenderersettings.cpp:91
QgsUnitTypes::RenderPoints
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:173
QgsMeshRendererScalarSettingsWidget::setLayer
void setLayer(QgsMeshLayer *layer)
Associates mesh layer with the widget.
Definition: qgsmeshrendererscalarsettingswidget.cpp:70
qgis.h
qgsmeshvariablestrokewidthwidget.h
QgsDoubleSpinBox::clearValue
bool clearValue
Definition: qgsdoublespinbox.h:56
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsInterpolatedLineWidth::fixedStrokeWidth
double fixedStrokeWidth() const
Returns the fixed width.
Definition: qgsinterpolatedlinerenderer.cpp:398
QgsInterpolatedLineWidth::setIsVariableWidth
void setIsVariableWidth(bool isVariableWidth)
Returns whether the width is variable.
Definition: qgsinterpolatedlinerenderer.cpp:418
QgsColorRampShader
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Definition: qgscolorrampshader.h:42
QgsMeshRendererScalarSettings::setEdgeStrokeWidth
void setEdgeStrokeWidth(const QgsInterpolatedLineWidth &strokeWidth)
Sets the stroke width used to render edges scalar dataset.
Definition: qgsmeshrenderersettings.cpp:174
QgsMeshLayer::rendererSettings
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
Definition: qgsmeshlayer.cpp:344
QgsMeshRendererScalarSettings::NeighbourAverage
@ NeighbourAverage
Does a simple average of values defined for all surrounding faces/vertices.
Definition: qgsmeshrenderersettings.h:116
QgsMeshDatasetGroupMetadata::DataOnFaces
@ DataOnFaces
Data is defined on faces.
Definition: qgsmeshdataset.h:358
QgsMeshRendererScalarSettingsWidget::syncToLayer
void syncToLayer()
Synchronizes widgets state with associated mesh layer.
Definition: qgsmeshrendererscalarsettingswidget.cpp:104
QgsMeshRendererScalarSettings::opacity
double opacity() const
Returns opacity.
Definition: qgsmeshrenderersettings.cpp:101
QgsMeshRendererSettings::scalarSettings
QgsMeshRendererScalarSettings scalarSettings(int groupIndex) const
Returns renderer settings.
Definition: qgsmeshrenderersettings.h:624
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2191
QgsInterpolatedLineWidth
Represents a width than can vary depending on values.
Definition: qgsinterpolatedlinerenderer.h:126
QgsMeshRendererScalarSettings::colorRampShader
QgsColorRampShader colorRampShader() const
Returns color ramp shader function.
Definition: qgsmeshrenderersettings.cpp:81
QgsMeshLayer
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:98
QgsMeshRendererScalarSettings::None
@ None
Does not use resampling.
Definition: qgsmeshrenderersettings.h:111
QgsMeshRendererScalarSettingsWidget::QgsMeshRendererScalarSettingsWidget
QgsMeshRendererScalarSettingsWidget(QWidget *parent=nullptr)
A widget to hold the renderer scalar settings for a mesh layer.
Definition: qgsmeshrendererscalarsettingswidget.cpp:28
QgsUnitSelectionWidget::changed
void changed()
QgsMeshRendererScalarSettings::DataResamplingMethod
DataResamplingMethod
Resampling of value from dataset.
Definition: qgsmeshrenderersettings.h:105
QgsMeshDatasetGroupMetadata::DataOnEdges
@ DataOnEdges
Data is defined on edges.
Definition: qgsmeshdataset.h:361
QgsMeshRendererScalarSettings::setColorRampShader
void setColorRampShader(const QgsColorRampShader &shader)
Sets color ramp shader function.
Definition: qgsmeshrenderersettings.cpp:86
QgsMeshRendererScalarSettingsWidget::settings
QgsMeshRendererScalarSettings settings() const
Returns scalar settings.
Definition: qgsmeshrendererscalarsettingswidget.cpp:82
QgsUnitTypes::RenderPixels
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:171
qgsmeshlayer.h
QgsMeshRendererScalarSettings::setOpacity
void setOpacity(double opacity)
Sets opacity.
Definition: qgsmeshrenderersettings.cpp:103
QgsMeshRendererScalarSettings::edgeStrokeWidthUnit
QgsUnitTypes::RenderUnit edgeStrokeWidthUnit() const
Returns the stroke width unit used to render edges scalar dataset.
Definition: qgsmeshrenderersettings.cpp:179
QgsMeshDatasetGroupMetadata::dataType
DataType dataType() const
Returns whether dataset group data is defined on vertices or faces or volumes.
Definition: qgsmeshdataset.cpp:172
QgsMeshRendererScalarSettings
Represents a mesh renderer settings for scalar datasets.
Definition: qgsmeshrenderersettings.h:94
QgsMeshDatasetGroupMetadata
QgsMeshDatasetGroupMetadata is a collection of dataset group metadata such as whether the data is vec...
Definition: qgsmeshdataset.h:351
QgsMeshRendererScalarSettings::setDataResamplingMethod
void setDataResamplingMethod(const DataResamplingMethod &dataResamplingMethod)
Sets data interpolation method.
Definition: qgsmeshrenderersettings.cpp:110
QgsColorRampShaderWidget::widgetChanged
void widgetChanged()
Widget changed.
QgsMeshDatasetGroupMetadata::maximum
double maximum() const
Returns maximum scalar value/vector magnitude present for whole dataset group.
Definition: qgsmeshdataset.cpp:182
QgsMeshRendererScalarSettingsWidget::widgetChanged
void widgetChanged()
Mesh rendering settings changed.
QgsMeshRendererScalarSettingsWidget::setActiveDatasetGroup
void setActiveDatasetGroup(int groupIndex)
Associates a dataset group with the widget (should be set before syncToLayer())
Definition: qgsmeshrendererscalarsettingswidget.cpp:76
qgssettings.h
QgsUnitTypes::RenderMetersInMapUnits
@ RenderMetersInMapUnits
Meters value as Map units.
Definition: qgsunittypes.h:176
QgsDoubleSpinBox
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value....
Definition: qgsdoublespinbox.h:42
QgsMeshLayer::datasetGroupMetadata
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
Definition: qgsmeshlayer.cpp:404
QgsUnitTypes::RenderUnitList
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:240
QgsMeshRendererScalarSettings::edgeStrokeWidth
QgsInterpolatedLineWidth edgeStrokeWidth() const
Returns the stroke width used to render edges scalar dataset.
Definition: qgsmeshrenderersettings.cpp:169
QgsMeshRendererScalarSettings::classificationMaximum
double classificationMaximum() const
Returns max value used for creation of the color ramp shader.
Definition: qgsmeshrenderersettings.cpp:93
qgsmeshrendererscalarsettingswidget.h
QgsMeshRendererScalarSettings::setEdgeStrokeWidthUnit
void setEdgeStrokeWidthUnit(const QgsUnitTypes::RenderUnit &edgeStrokeWidthUnit)
Sets the stroke width unit used to render edges scalar dataset.
Definition: qgsmeshrenderersettings.cpp:184
QgsInterpolatedLineWidth::setFixedStrokeWidth
void setFixedStrokeWidth(double fixedWidth)
Sets the fixed width.
Definition: qgsinterpolatedlinerenderer.cpp:423
qgsmessagelog.h
QgsInterpolatedLineWidth::isVariableWidth
bool isVariableWidth() const
Returns whether the width is variable.
Definition: qgsinterpolatedlinerenderer.cpp:413