QGIS API Documentation 4.3.0-Master (a9c95da348f)
Loading...
Searching...
No Matches
qgsmeshrenderersettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshrenderersettings.cpp
3 ---------------------------
4 begin : May 2018
5 copyright : (C) 2018 by Peter Petrik
6 email : zilolv at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgscolorramp.h"
21#include "qgscolorutils.h"
22#include "qgsunittypes.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29{
30 return mEnabled;
31}
32
34{
35 mEnabled = on;
36}
37
39{
40 return mLineWidth;
41}
42
44{
45 mLineWidth = lineWidth;
46}
47
49{
50 return mColor;
51}
52
54{
55 mColor = color;
56}
57
59{
60 return mLineWidthUnit;
61}
62
67
68QDomElement QgsMeshRendererMeshSettings::writeXml( QDomDocument &doc ) const
69{
70 QDomElement elem = doc.createElement( u"mesh-settings"_s );
71 elem.setAttribute( u"enabled"_s, mEnabled ? u"1"_s : u"0"_s );
72 elem.setAttribute( u"line-width"_s, mLineWidth );
73 elem.setAttribute( u"color"_s, QgsColorUtils::colorToString( mColor ) );
74 elem.setAttribute( u"line-width-unit"_s, QgsUnitTypes::encodeUnit( mLineWidthUnit ) );
75 return elem;
76}
77
78void QgsMeshRendererMeshSettings::readXml( const QDomElement &elem )
79{
80 mEnabled = elem.attribute( u"enabled"_s ).toInt();
81 mLineWidth = elem.attribute( u"line-width"_s ).toDouble();
82 mColor = QgsColorUtils::colorFromString( elem.attribute( u"color"_s ) );
83 mLineWidthUnit = QgsUnitTypes::decodeRenderUnit( elem.attribute( u"line-width-unit"_s ) );
84}
85
86// ---------------------------------------------------------------------
87
89{
90 return mColorRampShader;
91}
92
94{
95 mColorRampShader = shader;
96}
97
99{
100 return mClassificationMinimum;
101}
102
104{
105 return mClassificationMaximum;
106}
107
109{
110 mClassificationMinimum = minimum;
111 mClassificationMaximum = maximum;
112 updateShader();
113}
114
116{
117 return mOpacity;
118}
119
121{
122 mOpacity = opacity;
123}
124
129
131{
132 mDataResamplingMethod = dataInterpolationMethod;
133}
134
135QDomElement QgsMeshRendererScalarSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
136{
137 QDomElement elem = doc.createElement( u"scalar-settings"_s );
138 elem.setAttribute( u"min-val"_s, mClassificationMinimum );
139 elem.setAttribute( u"max-val"_s, mClassificationMaximum );
140 elem.setAttribute( u"opacity"_s, mOpacity );
141
142 QString methodTxt;
143 switch ( mDataResamplingMethod )
144 {
145 case NoResampling:
146 methodTxt = u"no-resampling"_s;
147 break;
148 case NeighbourAverage:
149 methodTxt = u"neighbour-average"_s;
150 break;
151 }
152 elem.setAttribute( u"interpolation-method"_s, methodTxt );
153
154 if ( mRangeExtent != Qgis::MeshRangeExtent::WholeMesh )
155 elem.setAttribute( u"range-extent"_s, qgsEnumValueToKey( mRangeExtent ) );
156 if ( mRangeLimit != Qgis::MeshRangeLimit::NotSet )
157 elem.setAttribute( u"range-limit"_s, qgsEnumValueToKey( mRangeLimit ) );
158
159 const QDomElement elemShader = mColorRampShader.writeXml( doc, context );
160 elem.appendChild( elemShader );
161
162 QDomElement elemEdge = doc.createElement( u"edge-settings"_s );
163 elemEdge.appendChild( mEdgeStrokeWidth.writeXml( doc, context ) );
164 elemEdge.setAttribute( u"stroke-width-unit"_s, static_cast< int >( mEdgeStrokeWidthUnit ) );
165 elem.appendChild( elemEdge );
166
167 return elem;
168}
169
170void QgsMeshRendererScalarSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
171{
172 mClassificationMinimum = elem.attribute( u"min-val"_s ).toDouble();
173 mClassificationMaximum = elem.attribute( u"max-val"_s ).toDouble();
174 mOpacity = elem.attribute( u"opacity"_s ).toDouble();
175
176 const QString methodTxt = elem.attribute( u"interpolation-method"_s );
177 if ( u"neighbour-average"_s == methodTxt )
178 {
179 mDataResamplingMethod = DataResamplingMethod::NeighbourAverage;
180 }
181 else
182 {
183 mDataResamplingMethod = DataResamplingMethod::NoResampling;
184 }
185
186 mRangeExtent = qgsEnumKeyToValue( elem.attribute( "range-extent" ), Qgis::MeshRangeExtent::WholeMesh );
187 mRangeLimit = qgsEnumKeyToValue( elem.attribute( "range-limit" ), Qgis::MeshRangeLimit::NotSet );
188
189 const QDomElement elemShader = elem.firstChildElement( u"colorrampshader"_s );
190 mColorRampShader.readXml( elemShader, context );
191
192 const QDomElement elemEdge = elem.firstChildElement( u"edge-settings"_s );
193 const QDomElement elemEdgeStrokeWidth = elemEdge.firstChildElement( u"mesh-stroke-width"_s );
194 mEdgeStrokeWidth.readXml( elemEdgeStrokeWidth, context );
195 mEdgeStrokeWidthUnit = static_cast<Qgis::RenderUnit>( elemEdge.attribute( u"stroke-width-unit"_s ).toInt() );
196}
197
202
204{
205 mEdgeStrokeWidth = strokeWidth;
206}
207
209{
210 return mEdgeStrokeWidthUnit;
211}
212
217
218void QgsMeshRendererScalarSettings::updateShader()
219{
220 mColorRampShader.setMinimumValue( mClassificationMinimum );
221 mColorRampShader.setMaximumValue( mClassificationMaximum );
222
223 if ( !mColorRampShader.isEmpty() )
224 mColorRampShader.classifyColorRamp( mColorRampShader.sourceColorRamp()->count(), 1, QgsRectangle(), nullptr );
225}
226
227// ---------------------------------------------------------------------
228
232
234 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
235 : mRendererNativeMeshSettings( other.mRendererNativeMeshSettings )
236 , mRendererTriangularMeshSettings( other.mRendererTriangularMeshSettings )
237 , mRendererEdgeMeshSettings( other.mRendererEdgeMeshSettings )
238 , mRendererScalarSettings( other.mRendererScalarSettings )
239 , mRendererVectorSettings( other.mRendererVectorSettings )
240 , mActiveScalarDatasetGroup( other.mActiveScalarDatasetGroup )
241 , mActiveVectorDatasetGroup( other.mActiveVectorDatasetGroup )
242 , mAveragingMethod( other.mAveragingMethod )
243//****** IMPORTANT! editing this? make sure you update the move constructor too! *****
244{}
245
247 : mRendererNativeMeshSettings( std::move( other.mRendererNativeMeshSettings ) )
248 , mRendererTriangularMeshSettings( std::move( other.mRendererTriangularMeshSettings ) )
249 , mRendererEdgeMeshSettings( std::move( other.mRendererEdgeMeshSettings ) )
250 , mRendererScalarSettings( std::move( other.mRendererScalarSettings ) )
251 , mRendererVectorSettings( std::move( other.mRendererVectorSettings ) )
252 , mActiveScalarDatasetGroup( other.mActiveScalarDatasetGroup )
253 , mActiveVectorDatasetGroup( other.mActiveVectorDatasetGroup )
254 , mAveragingMethod( std::move( other.mAveragingMethod ) )
255{}
256
258{
259 if ( &other == this )
260 return *this;
261
262 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
263 mRendererNativeMeshSettings = other.mRendererNativeMeshSettings;
264 mRendererTriangularMeshSettings = other.mRendererTriangularMeshSettings;
265 mRendererEdgeMeshSettings = other.mRendererEdgeMeshSettings;
266 mRendererScalarSettings = other.mRendererScalarSettings;
267 mRendererVectorSettings = other.mRendererVectorSettings;
268 mActiveScalarDatasetGroup = other.mActiveScalarDatasetGroup;
269 mActiveVectorDatasetGroup = other.mActiveVectorDatasetGroup;
270 mAveragingMethod = other.mAveragingMethod;
271 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
272 return *this;
273}
274
276{
277 if ( &other == this )
278 return *this;
279
280 mRendererNativeMeshSettings = std::move( other.mRendererNativeMeshSettings );
281 mRendererTriangularMeshSettings = std::move( other.mRendererTriangularMeshSettings );
282 mRendererEdgeMeshSettings = std::move( other.mRendererEdgeMeshSettings );
283 mRendererScalarSettings = std::move( other.mRendererScalarSettings );
284 mRendererVectorSettings = std::move( other.mRendererVectorSettings );
285 mActiveScalarDatasetGroup = other.mActiveScalarDatasetGroup;
286 mActiveVectorDatasetGroup = other.mActiveVectorDatasetGroup;
287 mAveragingMethod = std::move( other.mAveragingMethod );
288 return *this;
289}
290
292
294{
295 return mAveragingMethod.get();
296}
297
299{
300 if ( method )
301 mAveragingMethod.reset( method->clone() );
302 else
303 mAveragingMethod.reset();
304}
305
306QDomElement QgsMeshRendererSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
307{
308 QDomElement elem = doc.createElement( u"mesh-renderer-settings"_s );
309
310 QDomElement elemActiveDatasetGroup = doc.createElement( u"active-dataset-group"_s );
311 elemActiveDatasetGroup.setAttribute( u"scalar"_s, mActiveScalarDatasetGroup );
312 elemActiveDatasetGroup.setAttribute( u"vector"_s, mActiveVectorDatasetGroup );
313 elem.appendChild( elemActiveDatasetGroup );
314
315 for ( auto groupIndex = mRendererScalarSettings.keyBegin(); groupIndex != mRendererScalarSettings.keyEnd(); groupIndex++ )
316 {
317 const QgsMeshRendererScalarSettings &scalarSettings = mRendererScalarSettings[*groupIndex];
318 QDomElement elemScalar = scalarSettings.writeXml( doc, context );
319 elemScalar.setAttribute( u"group"_s, *groupIndex );
320 elem.appendChild( elemScalar );
321 }
322
323 for ( auto groupIndex = mRendererVectorSettings.keyBegin(); groupIndex != mRendererVectorSettings.keyEnd(); groupIndex++ )
324 {
325 const QgsVectorFieldSettings &vectorSettings = mRendererVectorSettings[*groupIndex];
326 QDomElement elemVector = vectorSettings.writeXml( doc, context );
327 elemVector.setAttribute( u"group"_s, *groupIndex );
328 elem.appendChild( elemVector );
329 }
330
331 QDomElement elemNativeMesh = mRendererNativeMeshSettings.writeXml( doc );
332 elemNativeMesh.setTagName( u"mesh-settings-native"_s );
333 elem.appendChild( elemNativeMesh );
334
335 QDomElement elemEdgeMesh = mRendererEdgeMeshSettings.writeXml( doc );
336 elemEdgeMesh.setTagName( u"mesh-settings-edge"_s );
337 elem.appendChild( elemEdgeMesh );
338
339 QDomElement elemTriangularMesh = mRendererTriangularMeshSettings.writeXml( doc );
340 elemTriangularMesh.setTagName( u"mesh-settings-triangular"_s );
341 elem.appendChild( elemTriangularMesh );
342
343 if ( mAveragingMethod )
344 {
345 QDomElement elemAveraging = doc.createElement( u"averaging-3d"_s );
346 elemAveraging.setAttribute( u"method"_s, QString::number( mAveragingMethod->method() ) );
347 const QDomElement elemAveragingParams = mAveragingMethod->writeXml( doc );
348 elemAveraging.appendChild( elemAveragingParams );
349 elem.appendChild( elemAveraging );
350 }
351
352 return elem;
353}
354
355void QgsMeshRendererSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
356{
357 mRendererScalarSettings.clear();
358 mRendererVectorSettings.clear();
359 mAveragingMethod.reset();
360
361 const QDomElement elemActiveDataset = elem.firstChildElement( u"active-dataset-group"_s );
362 if ( elemActiveDataset.hasAttribute( u"scalar"_s ) )
363 mActiveScalarDatasetGroup = elemActiveDataset.attribute( u"scalar"_s ).toInt();
364
365 if ( elemActiveDataset.hasAttribute( u"vector"_s ) )
366 mActiveVectorDatasetGroup = elemActiveDataset.attribute( u"vector"_s ).toInt();
367
368 QDomElement elemScalar = elem.firstChildElement( u"scalar-settings"_s );
369 while ( !elemScalar.isNull() )
370 {
371 const int groupIndex = elemScalar.attribute( u"group"_s ).toInt();
373 scalarSettings.readXml( elemScalar, context );
374 mRendererScalarSettings.insert( groupIndex, scalarSettings );
375
376 elemScalar = elemScalar.nextSiblingElement( u"scalar-settings"_s );
377 }
378
379 QDomElement elemVector = elem.firstChildElement( u"vector-settings"_s );
380 while ( !elemVector.isNull() )
381 {
382 const int groupIndex = elemVector.attribute( u"group"_s ).toInt();
384 vectorSettings.readXml( elemVector, context );
385 mRendererVectorSettings.insert( groupIndex, vectorSettings );
386
387 elemVector = elemVector.nextSiblingElement( u"vector-settings"_s );
388 }
389
390 const QDomElement elemNativeMesh = elem.firstChildElement( u"mesh-settings-native"_s );
391 mRendererNativeMeshSettings.readXml( elemNativeMesh );
392
393 const QDomElement elemEdgeMesh = elem.firstChildElement( u"mesh-settings-edge"_s );
394 mRendererEdgeMeshSettings.readXml( elemEdgeMesh );
395
396 const QDomElement elemTriangularMesh = elem.firstChildElement( u"mesh-settings-triangular"_s );
397 mRendererTriangularMeshSettings.readXml( elemTriangularMesh );
398
399 const QDomElement elemAveraging = elem.firstChildElement( u"averaging-3d"_s );
400 if ( !elemAveraging.isNull() )
401 {
402 mAveragingMethod.reset( QgsMesh3DAveragingMethod::createFromXml( elemAveraging ) );
403 }
404}
405
407{
408 return mActiveScalarDatasetGroup;
409}
410
415
417{
418 return mActiveVectorDatasetGroup;
419}
420
425
426bool QgsMeshRendererSettings::hasSettings( int datasetGroupIndex ) const
427{
428 return mRendererScalarSettings.contains( datasetGroupIndex ) || mRendererVectorSettings.contains( datasetGroupIndex );
429}
@ NotSet
User defined.
Definition qgis.h:6858
@ WholeMesh
Whole mesh is used to compute statistics.
Definition qgis.h:6870
RenderUnit
Rendering size units.
Definition qgis.h:5716
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
bool isEmpty() const
Whether the color ramp contains any items.
QgsColorRamp * sourceColorRamp() const
Returns the source color ramp.
void classifyColorRamp(int classes=0, int band=-1, const QgsRectangle &extent=QgsRectangle(), QgsRasterInterface *input=nullptr)
Classify color ramp shader.
virtual int count() const =0
Returns number of defined colors, or -1 if undefined.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Represents a width that can vary depending on values.
Abstract class for interpolating 3d stacked mesh data to 2d data.
static QgsMesh3DAveragingMethod * createFromXml(const QDomElement &elem)
Creates the instance from XML by calling readXml of derived classes.
virtual QDomElement writeXml(QDomDocument &doc) const =0
Writes configuration to a new DOM element.
virtual QgsMesh3DAveragingMethod * clone() const =0
Clone the instance.
void setLineWidthUnit(Qgis::RenderUnit lineWidthUnit)
Sets units of the width of the mesh frame.
void setEnabled(bool enabled)
Sets whether mesh structure rendering is enabled.
QColor color() const
Returns color used for rendering.
QDomElement writeXml(QDomDocument &doc) const
Writes configuration to a new DOM element.
double lineWidth() const
Returns line width used for rendering (in millimeters).
void setLineWidth(double lineWidth)
Sets line width used for rendering (in millimeters).
void readXml(const QDomElement &elem)
Reads configuration from the given DOM element.
Qgis::RenderUnit lineWidthUnit() const
Returns units of the width of the mesh frame.
bool isEnabled() const
Returns whether mesh structure rendering is enabled.
void setColor(const QColor &color)
Sets color used for rendering of the mesh.
Represents a mesh renderer settings for scalar datasets.
void setClassificationMinimumMaximum(double minimum, double maximum)
Sets min/max values used for creation of the color ramp shader.
double opacity() const
Returns opacity.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads configuration from the given DOM element.
void setEdgeStrokeWidthUnit(Qgis::RenderUnit edgeStrokeWidthUnit)
Sets the stroke width unit used to render edges scalar dataset.
void setColorRampShader(const QgsColorRampShader &shader)
Sets color ramp shader function.
QgsColorRampShader colorRampShader() const
Returns color ramp shader function.
double classificationMinimum() const
Returns min value used for creation of the color ramp shader.
void setOpacity(double opacity)
Sets opacity.
DataResamplingMethod
Resampling of value from dataset.
@ NoResampling
Does not use resampling.
@ NeighbourAverage
Does a simple average of values defined for all surrounding faces/vertices.
Qgis::RenderUnit edgeStrokeWidthUnit() const
Returns the stroke width unit used to render edges scalar dataset.
DataResamplingMethod dataResamplingMethod() const
Returns the type of interpolation to use to convert face defined datasets to values on vertices.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes configuration to a new DOM element.
void setEdgeStrokeWidth(const QgsInterpolatedLineWidth &strokeWidth)
Sets the stroke width used to render edges scalar dataset.
double classificationMaximum() const
Returns max value used for creation of the color ramp shader.
QgsInterpolatedLineWidth edgeStrokeWidth() const
Returns the stroke width used to render edges scalar dataset.
void setDataResamplingMethod(const DataResamplingMethod &dataResamplingMethod)
Sets data interpolation method.
void setAveragingMethod(QgsMesh3DAveragingMethod *method)
Sets averaging method for conversion of 3d stacked mesh data to 2d data.
void setActiveVectorDatasetGroup(int activeVectorDatasetGroup)
Sets the active vector dataset group.
QgsMeshRendererScalarSettings scalarSettings(int groupIndex) const
Returns renderer settings.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
QgsMesh3DAveragingMethod * averagingMethod() const
Returns averaging method for conversion of 3d stacked mesh data to 2d data.
bool hasSettings(int datasetGroupIndex) const
Returns whether the group with index has render settings (scalar or vector).
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
void setActiveScalarDatasetGroup(int activeScalarDatasetGroup)
Sets the active scalar dataset group.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads configuration from the given DOM element.
QgsMeshRendererSettings()
Constructs renderer with default single layer averaging method.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes configuration to a new DOM element.
QgsVectorFieldSettings vectorSettings(int groupIndex) const
Returns renderer settings.
QgsMeshRendererSettings & operator=(const QgsMeshRendererSettings &other)
Sigma averages over the values between 0 (bed level) and 1 (surface).
virtual void setMaximumValue(double value)
Sets the maximum value for the raster shader.
virtual void setMinimumValue(double value)
Sets the minimum value for the raster shader.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
Represents a renderer settings for vector datasets.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:7929
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7910