QGIS API Documentation  3.6.0-Noosa (5873452)
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 #include "qgssymbollayerutils.h"
20 
21 
23 {
24  return mEnabled;
25 }
26 
28 {
29  mEnabled = on;
30 }
31 
33 {
34  return mLineWidth;
35 }
36 
38 {
39  mLineWidth = lineWidth;
40 }
41 
43 {
44  return mColor;
45 }
46 
48 {
49  mColor = color;
50 }
51 
52 QDomElement QgsMeshRendererMeshSettings::writeXml( QDomDocument &doc ) const
53 {
54  QDomElement elem = doc.createElement( QStringLiteral( "mesh-settings" ) );
55  elem.setAttribute( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
56  elem.setAttribute( QStringLiteral( "line-width" ), mLineWidth );
57  elem.setAttribute( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
58  return elem;
59 }
60 
61 void QgsMeshRendererMeshSettings::readXml( const QDomElement &elem )
62 {
63  mEnabled = elem.attribute( QStringLiteral( "enabled" ) ).toInt();
64  mLineWidth = elem.attribute( QStringLiteral( "line-width" ) ).toDouble();
65  mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "color" ) ) );
66 }
67 
68 // ---------------------------------------------------------------------
69 
71 {
72  return mColorRampShader;
73 }
74 
76 {
77  mColorRampShader = shader;
78 }
79 
80 double QgsMeshRendererScalarSettings::classificationMinimum() const { return mClassificationMinimum; }
81 
82 double QgsMeshRendererScalarSettings::classificationMaximum() const { return mClassificationMaximum; }
83 
85 {
86  mClassificationMinimum = minimum;
87  mClassificationMaximum = maximum;
88 }
89 
90 double QgsMeshRendererScalarSettings::opacity() const { return mOpacity; }
91 
92 void QgsMeshRendererScalarSettings::setOpacity( double opacity ) { mOpacity = opacity; }
93 
94 QDomElement QgsMeshRendererScalarSettings::writeXml( QDomDocument &doc ) const
95 {
96  QDomElement elem = doc.createElement( QStringLiteral( "scalar-settings" ) );
97  elem.setAttribute( QStringLiteral( "min-val" ), mClassificationMinimum );
98  elem.setAttribute( QStringLiteral( "max-val" ), mClassificationMaximum );
99  elem.setAttribute( QStringLiteral( "opacity" ), mOpacity );
100  QDomElement elemShader = mColorRampShader.writeXml( doc );
101  elem.appendChild( elemShader );
102  return elem;
103 }
104 
105 void QgsMeshRendererScalarSettings::readXml( const QDomElement &elem )
106 {
107  mClassificationMinimum = elem.attribute( QStringLiteral( "min-val" ) ).toDouble();
108  mClassificationMaximum = elem.attribute( QStringLiteral( "max-val" ) ).toDouble();
109  mOpacity = elem.attribute( QStringLiteral( "opacity" ) ).toDouble();
110  QDomElement elemShader = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
111  mColorRampShader.readXml( elemShader );
112 }
113 
114 // ---------------------------------------------------------------------
115 
117 {
118  return mLineWidth;
119 }
120 
122 {
123  mLineWidth = lineWidth;
124 }
125 
127 {
128  return mColor;
129 }
130 
131 void QgsMeshRendererVectorSettings::setColor( const QColor &vectorColor )
132 {
133  mColor = vectorColor;
134 }
135 
137 {
138  return mFilterMin;
139 }
140 
141 void QgsMeshRendererVectorSettings::setFilterMin( double vectorFilterMin )
142 {
143  mFilterMin = vectorFilterMin;
144 }
145 
147 {
148  return mFilterMax;
149 }
150 
151 void QgsMeshRendererVectorSettings::setFilterMax( double vectorFilterMax )
152 {
153  mFilterMax = vectorFilterMax;
154 }
155 
157 {
158  return mShaftLengthMethod;
159 }
160 
162 {
163  mShaftLengthMethod = shaftLengthMethod;
164 }
165 
167 {
168  return mMinShaftLength;
169 }
170 
172 {
173  mMinShaftLength = minShaftLength;
174 }
175 
177 {
178  return mMaxShaftLength;
179 }
180 
182 {
183  mMaxShaftLength = maxShaftLength;
184 }
185 
187 {
188  return mScaleFactor;
189 }
190 
192 {
193  mScaleFactor = scaleFactor;
194 }
195 
197 {
198  return mFixedShaftLength;
199 }
200 
202 {
203  mFixedShaftLength = fixedShaftLength;
204 }
205 
207 {
208  return mArrowHeadWidthRatio;
209 }
210 
211 void QgsMeshRendererVectorSettings::setArrowHeadWidthRatio( double vectorHeadWidthRatio )
212 {
213  mArrowHeadWidthRatio = vectorHeadWidthRatio;
214 }
215 
217 {
218  return mArrowHeadLengthRatio;
219 }
220 
221 void QgsMeshRendererVectorSettings::setArrowHeadLengthRatio( double vectorHeadLengthRatio )
222 {
223  mArrowHeadLengthRatio = vectorHeadLengthRatio;
224 }
225 
227 {
228  return mOnUserDefinedGrid;
229 }
230 
232 {
233  mOnUserDefinedGrid = enabled;
234 }
235 
237 {
238  return mUserGridCellWidth;
239 }
240 
242 {
243  mUserGridCellWidth = width;
244 }
245 
247 {
248  return mUserGridCellHeight;
249 }
250 
252 {
253  mUserGridCellHeight = height;
254 }
255 
256 QDomElement QgsMeshRendererVectorSettings::writeXml( QDomDocument &doc ) const
257 {
258  QDomElement elem = doc.createElement( QStringLiteral( "vector-settings" ) );
259  elem.setAttribute( QStringLiteral( "line-width" ), mLineWidth );
260  elem.setAttribute( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
261  elem.setAttribute( QStringLiteral( "filter-min" ), mFilterMin );
262  elem.setAttribute( QStringLiteral( "filter-max" ), mFilterMax );
263  elem.setAttribute( QStringLiteral( "arrow-head-width-ratio" ), mArrowHeadWidthRatio );
264  elem.setAttribute( QStringLiteral( "arrow-head-length-ratio" ), mArrowHeadLengthRatio );
265  elem.setAttribute( QStringLiteral( "user-grid-enabled" ), mOnUserDefinedGrid ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
266  elem.setAttribute( QStringLiteral( "user-grid-width" ), mUserGridCellWidth );
267  elem.setAttribute( QStringLiteral( "user-grid-height" ), mUserGridCellHeight );
268 
269  QDomElement elemShaft = doc.createElement( QStringLiteral( "shaft-length" ) );
270  QString methodTxt;
271  switch ( mShaftLengthMethod )
272  {
273  case MinMax:
274  methodTxt = QStringLiteral( "minmax" );
275  elemShaft.setAttribute( QStringLiteral( "min" ), mMinShaftLength );
276  elemShaft.setAttribute( QStringLiteral( "max" ), mMaxShaftLength );
277  break;
278  case Scaled:
279  methodTxt = QStringLiteral( "scaled" );
280  elemShaft.setAttribute( QStringLiteral( "scale-factor" ), mScaleFactor );
281  break;
282  case Fixed:
283  methodTxt = QStringLiteral( "fixed" ) ;
284  elemShaft.setAttribute( QStringLiteral( "fixed-length" ), mFixedShaftLength );
285  break;
286  }
287  elemShaft.setAttribute( QStringLiteral( "method" ), methodTxt );
288  elem.appendChild( elemShaft );
289  return elem;
290 }
291 
292 void QgsMeshRendererVectorSettings::readXml( const QDomElement &elem )
293 {
294  mLineWidth = elem.attribute( QStringLiteral( "line-width" ) ).toDouble();
295  mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "color" ) ) );
296  mFilterMin = elem.attribute( QStringLiteral( "filter-min" ) ).toDouble();
297  mFilterMax = elem.attribute( QStringLiteral( "filter-max" ) ).toDouble();
298  mArrowHeadWidthRatio = elem.attribute( QStringLiteral( "arrow-head-width-ratio" ) ).toDouble();
299  mArrowHeadLengthRatio = elem.attribute( QStringLiteral( "arrow-head-length-ratio" ) ).toDouble();
300  mOnUserDefinedGrid = elem.attribute( QStringLiteral( "user-grid-enabled" ) ).toInt(); //bool
301  mUserGridCellWidth = elem.attribute( QStringLiteral( "user-grid-width" ) ).toInt();
302  mUserGridCellHeight = elem.attribute( QStringLiteral( "user-grid-height" ) ).toInt();
303 
304  QDomElement elemShaft = elem.firstChildElement( QStringLiteral( "shaft-length" ) );
305  QString methodTxt = elemShaft.attribute( QStringLiteral( "method" ) );
306  if ( QStringLiteral( "minmax" ) == methodTxt )
307  {
308  mShaftLengthMethod = MinMax;
309  mMinShaftLength = elemShaft.attribute( QStringLiteral( "min" ) ).toDouble();
310  mMaxShaftLength = elemShaft.attribute( QStringLiteral( "max" ) ).toDouble();
311  }
312  else if ( QStringLiteral( "scaled" ) == methodTxt )
313  {
314  mShaftLengthMethod = Scaled;
315  mScaleFactor = elemShaft.attribute( QStringLiteral( "scale-factor" ) ).toDouble();
316  }
317  else // fixed
318  {
319  mShaftLengthMethod = Fixed;
320  mFixedShaftLength = elemShaft.attribute( QStringLiteral( "fixed-length" ) ).toDouble();
321  }
322 }
323 
324 // ---------------------------------------------------------------------
325 
326 QDomElement QgsMeshRendererSettings::writeXml( QDomDocument &doc ) const
327 {
328  QDomElement elem = doc.createElement( QStringLiteral( "mesh-renderer-settings" ) );
329 
330  QDomElement elemActiveDataset = doc.createElement( QStringLiteral( "active-dataset" ) );
331  if ( mActiveScalarDataset.isValid() )
332  elemActiveDataset.setAttribute( QStringLiteral( "scalar" ), QStringLiteral( "%1,%2" ).arg( mActiveScalarDataset.group() ).arg( mActiveScalarDataset.dataset() ) );
333  if ( mActiveVectorDataset.isValid() )
334  elemActiveDataset.setAttribute( QStringLiteral( "vector" ), QStringLiteral( "%1,%2" ).arg( mActiveVectorDataset.group() ).arg( mActiveVectorDataset.dataset() ) );
335  elem.appendChild( elemActiveDataset );
336 
337  for ( int groupIndex : mRendererScalarSettings.keys() )
338  {
339  const QgsMeshRendererScalarSettings &scalarSettings = mRendererScalarSettings[groupIndex];
340  QDomElement elemScalar = scalarSettings.writeXml( doc );
341  elemScalar.setAttribute( QStringLiteral( "group" ), groupIndex );
342  elem.appendChild( elemScalar );
343  }
344 
345  for ( int groupIndex : mRendererVectorSettings.keys() )
346  {
347  const QgsMeshRendererVectorSettings &vectorSettings = mRendererVectorSettings[groupIndex];
348  QDomElement elemVector = vectorSettings.writeXml( doc );
349  elemVector.setAttribute( QStringLiteral( "group" ), groupIndex );
350  elem.appendChild( elemVector );
351  }
352 
353  QDomElement elemNativeMesh = mRendererNativeMeshSettings.writeXml( doc );
354  elemNativeMesh.setTagName( QStringLiteral( "mesh-settings-native" ) );
355  elem.appendChild( elemNativeMesh );
356 
357  QDomElement elemTriangularMesh = mRendererTriangularMeshSettings.writeXml( doc );
358  elemTriangularMesh.setTagName( QStringLiteral( "mesh-settings-triangular" ) );
359  elem.appendChild( elemTriangularMesh );
360 
361  return elem;
362 }
363 
364 void QgsMeshRendererSettings::readXml( const QDomElement &elem )
365 {
366  mRendererScalarSettings.clear();
367  mRendererVectorSettings.clear();
368 
369  QDomElement elemActiveDataset = elem.firstChildElement( QStringLiteral( "active-dataset" ) );
370  if ( elemActiveDataset.hasAttribute( QStringLiteral( "scalar" ) ) )
371  {
372  QStringList lst = elemActiveDataset.attribute( QStringLiteral( "scalar" ) ).split( QChar( ',' ) );
373  if ( lst.count() == 2 )
374  mActiveScalarDataset = QgsMeshDatasetIndex( lst[0].toInt(), lst[1].toInt() );
375  }
376  if ( elemActiveDataset.hasAttribute( QStringLiteral( "vector" ) ) )
377  {
378  QStringList lst = elemActiveDataset.attribute( QStringLiteral( "vector" ) ).split( QChar( ',' ) );
379  if ( lst.count() == 2 )
380  mActiveVectorDataset = QgsMeshDatasetIndex( lst[0].toInt(), lst[1].toInt() );
381  }
382 
383  QDomElement elemScalar = elem.firstChildElement( QStringLiteral( "scalar-settings" ) );
384  while ( !elemScalar.isNull() )
385  {
386  int groupIndex = elemScalar.attribute( QStringLiteral( "group" ) ).toInt();
387  QgsMeshRendererScalarSettings scalarSettings;
388  scalarSettings.readXml( elemScalar );
389  mRendererScalarSettings.insert( groupIndex, scalarSettings );
390 
391  elemScalar = elemScalar.nextSiblingElement( QStringLiteral( "scalar-settings" ) );
392  }
393 
394  QDomElement elemVector = elem.firstChildElement( QStringLiteral( "vector-settings" ) );
395  while ( !elemVector.isNull() )
396  {
397  int groupIndex = elemVector.attribute( QStringLiteral( "group" ) ).toInt();
398  QgsMeshRendererVectorSettings vectorSettings;
399  vectorSettings.readXml( elemVector );
400  mRendererVectorSettings.insert( groupIndex, vectorSettings );
401 
402  elemVector = elemVector.nextSiblingElement( QStringLiteral( "vector-settings" ) );
403  }
404 
405  QDomElement elemNativeMesh = elem.firstChildElement( QStringLiteral( "mesh-settings-native" ) );
406  mRendererNativeMeshSettings.readXml( elemNativeMesh );
407 
408  QDomElement elemTriangularMesh = elem.firstChildElement( QStringLiteral( "mesh-settings-triangular" ) );
409  mRendererTriangularMeshSettings.readXml( elemTriangularMesh );
410 }
void setClassificationMinimumMaximum(double minimum, double maximum)
Sets min/max values used for creation of the color ramp shader.
void setFilterMax(double filterMax)
Sets filter value for vector magnitudes.
void setLineWidth(double lineWidth)
Sets line width used for rendering (in millimeters)
void setMaxShaftLength(double maxShaftLength)
Sets maximum shaft length (in millimeters)
double minShaftLength() const
Returns mininimum shaft length (in millimeters)
void setArrowHeadWidthRatio(double arrowHeadWidthRatio)
Sets ratio of the head width of the arrow (range 0-1)
double fixedShaftLength() const
Returns fixed arrow length (in millimeters)
void setUserGridCellHeight(int height)
Sets height of user grid cell (in pixels)
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
void setFilterMin(double filterMin)
Sets filter value for vector magnitudes.
QDomElement writeXml(QDomDocument &doc) const
Writes configuration to a new DOM element.
Represents a mesh renderer settings for scalar datasets.
bool isEnabled() const
Returns whether mesh structure rendering is enabled.
void readXml(const QDomElement &elem)
Reads configuration from the given DOM element.
double arrowHeadLengthRatio() const
Returns ratio of the head length of the arrow (range 0-1)
QgsMeshRendererVectorSettings::ArrowScalingMethod shaftLengthMethod() const
Returns method used for drawing arrows.
void setMinShaftLength(double minShaftLength)
Sets mininimum shaft length (in millimeters)
void setOnUserDefinedGrid(bool enabled)
Toggles drawing of vectors on user defined grid.
double filterMin() const
Returns filter value for vector magnitudes.
static QString encodeColor(const QColor &color)
double opacity() const
Returns opacity.
double lineWidth() const
Returns line width used for rendering (in millimeters)
QDomElement writeXml(QDomDocument &doc) const
Writes configuration to a new DOM element.
void readXml(const QDomElement &elem)
Reads configuration from the given DOM element.
Represents a mesh renderer settings for vector datasets.
void setColorRampShader(const QgsColorRampShader &shader)
Sets color ramp shader function.
void setColor(const QColor &color)
Sets color used for drawing arrows.
void readXml(const QDomElement &elem)
Reads configuration from the given DOM element.
QColor color() const
Returns color used for drawing arrows.
void setShaftLengthMethod(ArrowScalingMethod shaftLengthMethod)
Sets method used for drawing arrows.
double filterMax() const
Returns filter value for vector magnitudes.
void setArrowHeadLengthRatio(double arrowHeadLengthRatio)
Sets ratio of the head length of the arrow (range 0-1)
double arrowHeadWidthRatio() const
Returns ratio of the head width of the arrow (range 0-1)
void setEnabled(bool enabled)
Sets whether mesh structure rendering is enabled.
QgsColorRampShader colorRampShader() const
Returns color ramp shader function.
void setLineWidth(double lineWidth)
Sets line width of the arrow in pixels (in millimeters)
double maxShaftLength() const
Returns maximum shaft length (in millimeters)
void setScaleFactor(double scaleFactor)
Sets scale factor.
void setOpacity(double opacity)
Sets opacity.
int userGridCellWidth() const
Returns width in pixels of user grid cell.
void setFixedShaftLength(double fixedShaftLength)
Sets fixed length (in millimeters)
QDomElement writeXml(QDomDocument &doc) const
Writes configuration to a new DOM element.
bool isOnUserDefinedGrid() const
Returns whether vectors are drawn on user-defined grid.
QColor color() const
Returns color used for rendering.
QgsMeshDatasetIndex is index that identifies the dataset group (e.g.
QDomElement writeXml(QDomDocument &doc) const
Writes configuration to a new DOM element.
int userGridCellHeight() const
Returns height in pixels of user grid cell.
double scaleFactor() const
Returns scale factor.
double lineWidth() const
Returns line width of the arrow (in millimeters)
void setUserGridCellWidth(int width)
Sets width of user grid cell (in pixels)
ArrowScalingMethod
Algorithm how to transform vector magnitude to length of arrow on the device in pixels.
void setColor(const QColor &color)
Sets color used for rendering of the mesh.
void readXml(const QDomElement &elem)
Reads configuration from the given DOM element.
double classificationMaximum() const
Returns max value used for creation of the color ramp shader.
static QColor decodeColor(const QString &str)
double classificationMinimum() const
Returns min value used for creation of the color ramp shader.