Skip to content

Commit 358d129

Browse files
committedAug 14, 2018
Read/write mesh styling from/to project files
1 parent 9fc2e3e commit 358d129

10 files changed

+355
-65
lines changed
 

‎python/core/auto_generated/mesh/qgsmeshrenderersettings.sip.in

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ Returns color used for rendering
5353
void setColor( const QColor &color );
5454
%Docstring
5555
Sets color used for rendering of the mesh
56+
%End
57+
58+
QDomElement writeXml( QDomDocument &doc ) const;
59+
%Docstring
60+
Writes configuration to a new DOM element
61+
%End
62+
void readXml( const QDomElement &elem );
63+
%Docstring
64+
Reads configuration from the given DOM element
5665
%End
5766

5867
};
@@ -81,6 +90,15 @@ Returns color ramp shader function
8190
void setColorRampShader( const QgsColorRampShader &shader );
8291
%Docstring
8392
Sets color ramp shader function
93+
%End
94+
95+
QDomElement writeXml( QDomDocument &doc ) const;
96+
%Docstring
97+
Writes configuration to a new DOM element
98+
%End
99+
void readXml( const QDomElement &elem );
100+
%Docstring
101+
Reads configuration from the given DOM element
84102
%End
85103

86104
};
@@ -242,6 +260,15 @@ Returns ratio of the head length of the arrow (range 0-1)
242260
void setArrowHeadLengthRatio( double arrowHeadLengthRatio );
243261
%Docstring
244262
Sets ratio of the head length of the arrow (range 0-1)
263+
%End
264+
265+
QDomElement writeXml( QDomDocument &doc ) const;
266+
%Docstring
267+
Writes configuration to a new DOM element
268+
%End
269+
void readXml( const QDomElement &elem );
270+
%Docstring
271+
Reads configuration from the given DOM element
245272
%End
246273

247274
};
@@ -318,6 +345,15 @@ Returns active vector dataset
318345
void setActiveVectorDataset( QgsMeshDatasetIndex index = QgsMeshDatasetIndex() );
319346
%Docstring
320347
Sets active vector dataset for rendering.
348+
%End
349+
350+
QDomElement writeXml( QDomDocument &doc ) const;
351+
%Docstring
352+
Writes configuration to a new DOM element
353+
%End
354+
void readXml( const QDomElement &elem );
355+
%Docstring
356+
Reads configuration from the given DOM element
321357
%End
322358

323359
};

‎python/core/auto_generated/raster/qgscolorrampshader.sip.in

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Returns the custom colormap.
8282
Returns the color ramp type.
8383
%End
8484

85-
QString colorRampTypeAsQString();
85+
QString colorRampTypeAsQString() const;
8686
%Docstring
8787
Returns the color ramp type as a string.
8888
%End
@@ -160,6 +160,20 @@ Generates and new RGB value based on original RGB value
160160
virtual void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems /Out/ ) const;
161161

162162

163+
QDomElement writeXml( QDomDocument &doc ) const;
164+
%Docstring
165+
Writes configuration to a new DOM element
166+
167+
.. versionadded:: 3.4
168+
%End
169+
170+
void readXml( const QDomElement &elem );
171+
%Docstring
172+
Reads configuration from the given DOM element
173+
174+
.. versionadded:: 3.4
175+
%End
176+
163177
void setClassificationMode( ClassificationMode classificationMode );
164178
%Docstring
165179
Sets classification mode

‎src/app/mesh/qgsmeshrendereractivedatasetwidget.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,13 @@ void QgsMeshRendererActiveDatasetWidget::syncToLayer()
164164
if ( mMeshLayer )
165165
{
166166
const QgsMeshRendererSettings rendererSettings = mMeshLayer->rendererSettings();
167+
mActiveDatasetGroup = mDatasetGroupTreeView->activeGroup();
167168
mActiveScalarDataset = rendererSettings.activeScalarDataset();
168169
mActiveVectorDataset = rendererSettings.activeVectorDataset();
169170
}
170171
else
171172
{
173+
mActiveDatasetGroup = -1;
172174
mActiveScalarDataset = QgsMeshDatasetIndex();
173175
mActiveVectorDataset = QgsMeshDatasetIndex();
174176
}

‎src/app/mesh/qgsmeshrenderervectorsettingswidget.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ QgsMeshRendererVectorSettingsWidget::QgsMeshRendererVectorSettingsWidget( QWidge
2626
{
2727
setupUi( this );
2828

29+
mShaftLengthComboBox->setCurrentIndex( -1 );
30+
2931
connect( mColorWidget, &QgsColorButton::colorChanged, this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
3032
connect( mLineWidthSpinBox, qgis::overload<double>::of( &QgsDoubleSpinBox::valueChanged ),
3133
this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );

‎src/core/mesh/qgsmeshlayer.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,26 @@ QgsMapLayerRenderer *QgsMeshLayer::createMapRenderer( QgsRenderContext &renderer
221221

222222
bool QgsMeshLayer::readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context )
223223
{
224-
Q_UNUSED( node );
225224
Q_UNUSED( errorMessage );
226225
Q_UNUSED( context );
226+
227+
QDomElement elem = node.toElement();
228+
QDomElement elemRendererSettings = elem.firstChildElement( "mesh-renderer-settings" );
229+
if ( !elemRendererSettings.isNull() )
230+
mRendererSettings.readXml( elemRendererSettings );
231+
227232
return true;
228233
}
229234

230235
bool QgsMeshLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const
231236
{
232-
Q_UNUSED( node );
233-
Q_UNUSED( doc );
234237
Q_UNUSED( errorMessage );
235238
Q_UNUSED( context );
239+
240+
QDomElement elem = node.toElement();
241+
QDomElement elemRendererSettings = mRendererSettings.writeXml( doc );
242+
elem.appendChild( elemRendererSettings );
243+
236244
return true;
237245
}
238246

@@ -258,8 +266,6 @@ QString QgsMeshLayer::encodedSource( const QString &source, const QgsReadWriteCo
258266

259267
bool QgsMeshLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
260268
{
261-
Q_UNUSED( context );
262-
263269
QgsDebugMsgLevel( QStringLiteral( "Datasource in QgsMeshLayer::readXml: %1" ).arg( mDataSource.toLocal8Bit().data() ), 3 );
264270

265271
//process provider key
@@ -296,6 +302,9 @@ bool QgsMeshLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &con
296302
}
297303
}
298304

305+
QString errorMsg;
306+
readSymbology( layer_node, errorMsg, context );
307+
299308
return mValid; // should be true if read successfully
300309
}
301310

‎src/core/mesh/qgsmeshrenderersettings.cpp

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#include "qgsmeshrenderersettings.h"
1919

20+
#include "qgssymbollayerutils.h"
21+
22+
2023
bool QgsMeshRendererMeshSettings::isEnabled() const
2124
{
2225
return mEnabled;
@@ -47,18 +50,50 @@ void QgsMeshRendererMeshSettings::setColor( const QColor &color )
4750
mColor = color;
4851
}
4952

53+
QDomElement QgsMeshRendererMeshSettings::writeXml( QDomDocument &doc ) const
54+
{
55+
QDomElement elem = doc.createElement( "mesh-settings" );
56+
elem.setAttribute( "enabled", mEnabled ? "1" : "0" );
57+
elem.setAttribute( "line-width", mLineWidth );
58+
elem.setAttribute( "color", QgsSymbolLayerUtils::encodeColor( mColor ) );
59+
return elem;
60+
}
61+
62+
void QgsMeshRendererMeshSettings::readXml( const QDomElement &elem )
63+
{
64+
mEnabled = elem.attribute( "enabled" ).toInt();
65+
mLineWidth = elem.attribute( "line-width" ).toDouble();
66+
mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( "color" ) );
67+
}
68+
69+
// ---------------------------------------------------------------------
5070

5171
QgsColorRampShader QgsMeshRendererScalarSettings::colorRampShader() const
5272
{
5373
return mColorRampShader;
54-
5574
}
5675

5776
void QgsMeshRendererScalarSettings::setColorRampShader( const QgsColorRampShader &shader )
5877
{
5978
mColorRampShader = shader;
6079
}
6180

81+
QDomElement QgsMeshRendererScalarSettings::writeXml( QDomDocument &doc ) const
82+
{
83+
QDomElement elem = doc.createElement( "scalar-settings" );
84+
QDomElement elemShader = mColorRampShader.writeXml( doc );
85+
elem.appendChild( elemShader );
86+
return elem;
87+
}
88+
89+
void QgsMeshRendererScalarSettings::readXml( const QDomElement &elem )
90+
{
91+
QDomElement elemShader = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
92+
mColorRampShader.readXml( elemShader );
93+
}
94+
95+
// ---------------------------------------------------------------------
96+
6297
double QgsMeshRendererVectorSettings::lineWidth() const
6398
{
6499
return mLineWidth;
@@ -168,3 +203,153 @@ void QgsMeshRendererVectorSettings::setArrowHeadLengthRatio( double vectorHeadLe
168203
{
169204
mArrowHeadLengthRatio = vectorHeadLengthRatio;
170205
}
206+
207+
QDomElement QgsMeshRendererVectorSettings::writeXml( QDomDocument &doc ) const
208+
{
209+
QDomElement elem = doc.createElement( "vector-settings" );
210+
elem.setAttribute( "line-width", mLineWidth );
211+
elem.setAttribute( "color", QgsSymbolLayerUtils::encodeColor( mColor ) );
212+
elem.setAttribute( "filter-min", mFilterMin );
213+
elem.setAttribute( "filter-max", mFilterMax );
214+
elem.setAttribute( "arrow-head-width-ratio", mArrowHeadWidthRatio );
215+
elem.setAttribute( "arrow-head-length-ratio", mArrowHeadLengthRatio );
216+
217+
QDomElement elemShaft = doc.createElement( "shaft-length" );
218+
QString methodTxt;
219+
switch ( mShaftLengthMethod )
220+
{
221+
case MinMax:
222+
methodTxt = "minmax";
223+
elemShaft.setAttribute( "min", mMinShaftLength );
224+
elemShaft.setAttribute( "max", mMaxShaftLength );
225+
break;
226+
case Scaled:
227+
methodTxt = "scaled";
228+
elemShaft.setAttribute( "scale-factor", mScaleFactor );
229+
break;
230+
case Fixed:
231+
methodTxt = "fixed";
232+
elemShaft.setAttribute( "fixed-length", mFixedShaftLength );
233+
break;
234+
}
235+
elemShaft.setAttribute( "method", methodTxt );
236+
elem.appendChild( elemShaft );
237+
return elem;
238+
}
239+
240+
void QgsMeshRendererVectorSettings::readXml( const QDomElement &elem )
241+
{
242+
mLineWidth = elem.attribute( "line-width" ).toDouble();
243+
mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( "color" ) );
244+
mFilterMin = elem.attribute( "filter-min" ).toDouble();
245+
mFilterMax = elem.attribute( "filter-max" ).toDouble();
246+
mArrowHeadWidthRatio = elem.attribute( "arrow-head-width-ratio" ).toDouble();
247+
mArrowHeadLengthRatio = elem.attribute( "arrow-head-length-ratio" ).toDouble();
248+
249+
QDomElement elemShaft = elem.firstChildElement( "shaft-length" );
250+
QString methodTxt = elemShaft.attribute( "method" );
251+
if ( methodTxt == "minmax" )
252+
{
253+
mShaftLengthMethod = MinMax;
254+
mMinShaftLength = elemShaft.attribute( "min" ).toDouble();
255+
mMaxShaftLength = elemShaft.attribute( "max" ).toDouble();
256+
}
257+
else if ( methodTxt == "scaled" )
258+
{
259+
mShaftLengthMethod = Scaled;
260+
mScaleFactor = elemShaft.attribute( "scale-factor" ).toDouble();
261+
}
262+
else // fixed
263+
{
264+
mShaftLengthMethod = Fixed;
265+
mFixedShaftLength = elemShaft.attribute( "fixed-length" ).toDouble();
266+
}
267+
}
268+
269+
// ---------------------------------------------------------------------
270+
271+
QDomElement QgsMeshRendererSettings::writeXml( QDomDocument &doc ) const
272+
{
273+
QDomElement elem = doc.createElement( "mesh-renderer-settings" );
274+
275+
QDomElement elemActiveDataset = doc.createElement( "active-dataset" );
276+
if ( mActiveScalarDataset.isValid() )
277+
elemActiveDataset.setAttribute( "scalar", QString( "%1,%2" ).arg( mActiveScalarDataset.group() ).arg( mActiveScalarDataset.dataset() ) );
278+
if ( mActiveVectorDataset.isValid() )
279+
elemActiveDataset.setAttribute( "vector", QString( "%1,%2" ).arg( mActiveVectorDataset.group() ).arg( mActiveVectorDataset.dataset() ) );
280+
elem.appendChild( elemActiveDataset );
281+
282+
for ( int groupIndex : mRendererScalarSettings.keys() )
283+
{
284+
const QgsMeshRendererScalarSettings &scalarSettings = mRendererScalarSettings[groupIndex];
285+
QDomElement elemScalar = scalarSettings.writeXml( doc );
286+
elemScalar.setAttribute( "group", groupIndex );
287+
elem.appendChild( elemScalar );
288+
}
289+
290+
for ( int groupIndex : mRendererVectorSettings.keys() )
291+
{
292+
const QgsMeshRendererVectorSettings &vectorSettings = mRendererVectorSettings[groupIndex];
293+
QDomElement elemVector = vectorSettings.writeXml( doc );
294+
elemVector.setAttribute( "group", groupIndex );
295+
elem.appendChild( elemVector );
296+
}
297+
298+
QDomElement elemNativeMesh = mRendererNativeMeshSettings.writeXml( doc );
299+
elemNativeMesh.setTagName( "mesh-settings-native" );
300+
elem.appendChild( elemNativeMesh );
301+
302+
QDomElement elemTriangularMesh = mRendererTriangularMeshSettings.writeXml( doc );
303+
elemTriangularMesh.setTagName( "mesh-settings-triangular" );
304+
elem.appendChild( elemTriangularMesh );
305+
306+
return elem;
307+
}
308+
309+
void QgsMeshRendererSettings::readXml( const QDomElement &elem )
310+
{
311+
mRendererScalarSettings.clear();
312+
mRendererVectorSettings.clear();
313+
314+
QDomElement elemActiveDataset = elem.firstChildElement( "active-dataset" );
315+
if ( elemActiveDataset.hasAttribute( "scalar" ) )
316+
{
317+
QStringList lst = elemActiveDataset.attribute( "scalar" ).split( QChar( ',' ) );
318+
if ( lst.count() == 2 )
319+
mActiveScalarDataset = QgsMeshDatasetIndex( lst[0].toInt(), lst[1].toInt() );
320+
}
321+
if ( elemActiveDataset.hasAttribute( "vector" ) )
322+
{
323+
QStringList lst = elemActiveDataset.attribute( "vector" ).split( QChar( ',' ) );
324+
if ( lst.count() == 2 )
325+
mActiveVectorDataset = QgsMeshDatasetIndex( lst[0].toInt(), lst[1].toInt() );
326+
}
327+
328+
QDomElement elemScalar = elem.firstChildElement( "scalar-settings" );
329+
while ( !elemScalar.isNull() )
330+
{
331+
int groupIndex = elemScalar.attribute( "group" ).toInt();
332+
QgsMeshRendererScalarSettings scalarSettings;
333+
scalarSettings.readXml( elemScalar );
334+
mRendererScalarSettings.insert( groupIndex, scalarSettings );
335+
336+
elemScalar = elemScalar.nextSiblingElement( "scalar-settings" );
337+
}
338+
339+
QDomElement elemVector = elem.firstChildElement( "vector-settings" );
340+
while ( !elemVector.isNull() )
341+
{
342+
int groupIndex = elemVector.attribute( "group" ).toInt();
343+
QgsMeshRendererVectorSettings vectorSettings;
344+
vectorSettings.readXml( elemVector );
345+
mRendererVectorSettings.insert( groupIndex, vectorSettings );
346+
347+
elemVector = elemVector.nextSiblingElement( "vector-settings" );
348+
}
349+
350+
QDomElement elemNativeMesh = elem.firstChildElement( "mesh-settings-native" );
351+
mRendererNativeMeshSettings.readXml( elemNativeMesh );
352+
353+
QDomElement elemTriangularMesh = elem.firstChildElement( "mesh-settings-triangular" );
354+
mRendererTriangularMeshSettings.readXml( elemTriangularMesh );
355+
}

‎src/core/mesh/qgsmeshrenderersettings.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class CORE_EXPORT QgsMeshRendererMeshSettings
5353
//! Sets color used for rendering of the mesh
5454
void setColor( const QColor &color );
5555

56+
//! Writes configuration to a new DOM element
57+
QDomElement writeXml( QDomDocument &doc ) const;
58+
//! Reads configuration from the given DOM element
59+
void readXml( const QDomElement &elem );
60+
5661
private:
5762
bool mEnabled = false;
5863
double mLineWidth = DEFAULT_LINE_WIDTH;
@@ -76,6 +81,11 @@ class CORE_EXPORT QgsMeshRendererScalarSettings
7681
//! Sets color ramp shader function
7782
void setColorRampShader( const QgsColorRampShader &shader );
7883

84+
//! Writes configuration to a new DOM element
85+
QDomElement writeXml( QDomDocument &doc ) const;
86+
//! Reads configuration from the given DOM element
87+
void readXml( const QDomElement &elem );
88+
7989
private:
8090
QgsColorRampShader mColorRampShader;
8191
};
@@ -222,6 +232,11 @@ class CORE_EXPORT QgsMeshRendererVectorSettings
222232
//! Sets ratio of the head length of the arrow (range 0-1)
223233
void setArrowHeadLengthRatio( double arrowHeadLengthRatio );
224234

235+
//! Writes configuration to a new DOM element
236+
QDomElement writeXml( QDomDocument &doc ) const;
237+
//! Reads configuration from the given DOM element
238+
void readXml( const QDomElement &elem );
239+
225240
private:
226241
double mLineWidth = DEFAULT_LINE_WIDTH; //in milimeters
227242
QColor mColor = Qt::black;
@@ -282,6 +297,11 @@ class CORE_EXPORT QgsMeshRendererSettings
282297
//! Sets active vector dataset for rendering.
283298
void setActiveVectorDataset( QgsMeshDatasetIndex index = QgsMeshDatasetIndex() ) { mActiveVectorDataset = index; }
284299

300+
//! Writes configuration to a new DOM element
301+
QDomElement writeXml( QDomDocument &doc ) const;
302+
//! Reads configuration from the given DOM element
303+
void readXml( const QDomElement &elem );
304+
285305
private:
286306
QgsMeshRendererMeshSettings mRendererNativeMeshSettings;
287307
QgsMeshRendererMeshSettings mRendererTriangularMeshSettings;

‎src/core/raster/qgscolorrampshader.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ originally part of the larger QgsRasterLayer class
2828
#include "qgscolorrampshader.h"
2929
#include "qgsrasterinterface.h"
3030
#include "qgsrasterminmaxorigin.h"
31+
#include "qgssymbollayerutils.h"
3132

3233
#include <cmath>
3334
QgsColorRampShader::QgsColorRampShader( double minimumValue, double maximumValue, QgsColorRamp *colorRamp, Type type, ClassificationMode classificationMode )
@@ -73,7 +74,7 @@ QgsColorRampShader &QgsColorRampShader::operator=( const QgsColorRampShader &oth
7374
return *this;
7475
}
7576

76-
QString QgsColorRampShader::colorRampTypeAsQString()
77+
QString QgsColorRampShader::colorRampTypeAsQString() const
7778
{
7879
switch ( mColorRampType )
7980
{
@@ -491,3 +492,66 @@ void QgsColorRampShader::legendSymbologyItems( QList< QPair< QString, QColor > >
491492
symbolItems.push_back( qMakePair( colorRampIt->label, colorRampIt->color ) );
492493
}
493494
}
495+
496+
QDomElement QgsColorRampShader::writeXml( QDomDocument &doc ) const
497+
{
498+
QDomElement colorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
499+
colorRampShaderElem.setAttribute( QStringLiteral( "colorRampType" ), colorRampTypeAsQString() );
500+
colorRampShaderElem.setAttribute( QStringLiteral( "classificationMode" ), classificationMode() );
501+
colorRampShaderElem.setAttribute( QStringLiteral( "clip" ), clip() );
502+
503+
// save source color ramp
504+
if ( sourceColorRamp() )
505+
{
506+
QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), sourceColorRamp(), doc );
507+
colorRampShaderElem.appendChild( colorRampElem );
508+
}
509+
510+
//items
511+
QList<QgsColorRampShader::ColorRampItem> itemList = colorRampItemList();
512+
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
513+
for ( ; itemIt != itemList.constEnd(); ++itemIt )
514+
{
515+
QDomElement itemElem = doc.createElement( QStringLiteral( "item" ) );
516+
itemElem.setAttribute( QStringLiteral( "label" ), itemIt->label );
517+
itemElem.setAttribute( QStringLiteral( "value" ), QgsRasterBlock::printValue( itemIt->value ) );
518+
itemElem.setAttribute( QStringLiteral( "color" ), itemIt->color.name() );
519+
itemElem.setAttribute( QStringLiteral( "alpha" ), itemIt->color.alpha() );
520+
colorRampShaderElem.appendChild( itemElem );
521+
}
522+
return colorRampShaderElem;
523+
}
524+
525+
void QgsColorRampShader::readXml( const QDomElement &colorRampShaderElem )
526+
{
527+
// try to load color ramp (optional)
528+
QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral( "colorramp" ) );
529+
if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
530+
{
531+
setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
532+
}
533+
534+
setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) );
535+
setClassificationMode( static_cast< QgsColorRampShader::ClassificationMode >( colorRampShaderElem.attribute( QStringLiteral( "classificationMode" ), QStringLiteral( "1" ) ).toInt() ) );
536+
setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
537+
538+
QList<QgsColorRampShader::ColorRampItem> itemList;
539+
QDomElement itemElem;
540+
QString itemLabel;
541+
double itemValue;
542+
QColor itemColor;
543+
544+
QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral( "item" ) );
545+
itemList.reserve( itemNodeList.size() );
546+
for ( int i = 0; i < itemNodeList.size(); ++i )
547+
{
548+
itemElem = itemNodeList.at( i ).toElement();
549+
itemValue = itemElem.attribute( QStringLiteral( "value" ) ).toDouble();
550+
itemLabel = itemElem.attribute( QStringLiteral( "label" ) );
551+
itemColor.setNamedColor( itemElem.attribute( QStringLiteral( "color" ) ) );
552+
itemColor.setAlpha( itemElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt() );
553+
554+
itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
555+
}
556+
setColorRampItemList( itemList );
557+
}

‎src/core/raster/qgscolorrampshader.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
108108
Type colorRampType() const { return mColorRampType; }
109109

110110
//! Returns the color ramp type as a string.
111-
QString colorRampTypeAsQString();
111+
QString colorRampTypeAsQString() const;
112112

113113
//! Sets a custom colormap
114114
void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem> &list ); //TODO: sort on set
@@ -167,6 +167,18 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
167167

168168
void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems SIP_OUT ) const override;
169169

170+
/**
171+
* Writes configuration to a new DOM element
172+
* \since QGIS 3.4
173+
*/
174+
QDomElement writeXml( QDomDocument &doc ) const;
175+
176+
/**
177+
* Reads configuration from the given DOM element
178+
* \since QGIS 3.4
179+
*/
180+
void readXml( const QDomElement &elem );
181+
170182
//! Sets classification mode
171183
void setClassificationMode( ClassificationMode classificationMode ) { mClassificationMode = classificationMode; }
172184

‎src/core/raster/qgsrastershader.cpp

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,7 @@ void QgsRasterShader::writeXml( QDomDocument &doc, QDomElement &parent ) const
9999
QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( mRasterShaderFunction.get() );
100100
if ( colorRampShader )
101101
{
102-
QDomElement colorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
103-
colorRampShaderElem.setAttribute( QStringLiteral( "colorRampType" ), colorRampShader->colorRampTypeAsQString() );
104-
colorRampShaderElem.setAttribute( QStringLiteral( "classificationMode" ), colorRampShader->classificationMode() );
105-
colorRampShaderElem.setAttribute( QStringLiteral( "clip" ), colorRampShader->clip() );
106-
107-
// save source color ramp
108-
if ( colorRampShader->sourceColorRamp() )
109-
{
110-
QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), colorRampShader->sourceColorRamp(), doc );
111-
colorRampShaderElem.appendChild( colorRampElem );
112-
}
113-
114-
//items
115-
QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
116-
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
117-
for ( ; itemIt != itemList.constEnd(); ++itemIt )
118-
{
119-
QDomElement itemElem = doc.createElement( QStringLiteral( "item" ) );
120-
itemElem.setAttribute( QStringLiteral( "label" ), itemIt->label );
121-
itemElem.setAttribute( QStringLiteral( "value" ), QgsRasterBlock::printValue( itemIt->value ) );
122-
itemElem.setAttribute( QStringLiteral( "color" ), itemIt->color.name() );
123-
itemElem.setAttribute( QStringLiteral( "alpha" ), itemIt->color.alpha() );
124-
colorRampShaderElem.appendChild( itemElem );
125-
}
126-
rasterShaderElem.appendChild( colorRampShaderElem );
102+
rasterShaderElem.appendChild( colorRampShader->writeXml( doc ) );
127103
}
128104
parent.appendChild( rasterShaderElem );
129105
}
@@ -135,37 +111,7 @@ void QgsRasterShader::readXml( const QDomElement &elem )
135111
if ( !colorRampShaderElem.isNull() )
136112
{
137113
QgsColorRampShader *colorRampShader = new QgsColorRampShader();
138-
139-
// try to load color ramp (optional)
140-
QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral( "colorramp" ) );
141-
if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
142-
{
143-
colorRampShader->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
144-
}
145-
146-
colorRampShader->setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) );
147-
colorRampShader->setClassificationMode( static_cast< QgsColorRampShader::ClassificationMode >( colorRampShaderElem.attribute( QStringLiteral( "classificationMode" ), QStringLiteral( "1" ) ).toInt() ) );
148-
colorRampShader->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
149-
150-
QList<QgsColorRampShader::ColorRampItem> itemList;
151-
QDomElement itemElem;
152-
QString itemLabel;
153-
double itemValue;
154-
QColor itemColor;
155-
156-
QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral( "item" ) );
157-
itemList.reserve( itemNodeList.size() );
158-
for ( int i = 0; i < itemNodeList.size(); ++i )
159-
{
160-
itemElem = itemNodeList.at( i ).toElement();
161-
itemValue = itemElem.attribute( QStringLiteral( "value" ) ).toDouble();
162-
itemLabel = itemElem.attribute( QStringLiteral( "label" ) );
163-
itemColor.setNamedColor( itemElem.attribute( QStringLiteral( "color" ) ) );
164-
itemColor.setAlpha( itemElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt() );
165-
166-
itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
167-
}
168-
colorRampShader->setColorRampItemList( itemList );
114+
colorRampShader->readXml( colorRampShaderElem );
169115
setRasterShaderFunction( colorRampShader );
170116
}
171117
}

0 commit comments

Comments
 (0)
Please sign in to comment.