Skip to content

Commit 4da0c03

Browse files
committed
Implement readXML for singleband pseudo color
1 parent a6eaf71 commit 4da0c03

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/core/raster/qgsrastershader.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,21 @@ void QgsRasterShader::setMinimumValue( double theValue )
129129

130130
void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const
131131
{
132-
if( parent.isNull() || !mRasterShaderFunction )
132+
if ( parent.isNull() || !mRasterShaderFunction )
133133
{
134134
return;
135135
}
136136

137137
QDomElement rasterShaderElem = doc.createElement( "rastershader" );
138138
QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( mRasterShaderFunction );
139-
if( colorRampShader )
139+
if ( colorRampShader )
140140
{
141141
QDomElement colorRampShaderElem = doc.createElement( "colorrampshader" );
142142
colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
143143
//items
144144
QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
145145
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
146-
for(; itemIt != itemList.constEnd(); ++itemIt )
146+
for ( ; itemIt != itemList.constEnd(); ++itemIt )
147147
{
148148
QDomElement itemElem = doc.createElement( "item" );
149149
itemElem.setAttribute( "label", itemIt->label );
@@ -155,3 +155,32 @@ void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const
155155
}
156156
parent.appendChild( rasterShaderElem );
157157
}
158+
159+
void QgsRasterShader::readXML( const QDomElement& elem )
160+
{
161+
//only colorrampshader
162+
QDomElement colorRampShaderElem = elem.firstChildElement( "colorrampshader" );
163+
if ( !colorRampShaderElem.isNull() )
164+
{
165+
QgsColorRampShader* colorRampShader = new QgsColorRampShader();
166+
colorRampShader->setColorRampType( colorRampShaderElem.attribute( "colorRampType", "INTERPOLATED" ) );
167+
168+
QList<QgsColorRampShader::ColorRampItem> itemList;
169+
QDomElement itemElem;
170+
QString itemLabel;
171+
double itemValue;
172+
QColor itemColor;
173+
174+
QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( "item" );
175+
for ( int i = 0; i < itemNodeList.size(); ++i )
176+
{
177+
itemElem = itemNodeList.at( i ).toElement();
178+
itemValue = itemElem.attribute( "value" ).toDouble();
179+
itemLabel = itemElem.attribute( "label" );
180+
itemColor.setNamedColor( itemElem.attribute( "color" ) );
181+
itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
182+
}
183+
colorRampShader->setColorRampItemList( itemList );
184+
setRasterShaderFunction( colorRampShader );
185+
}
186+
}

src/core/raster/qgsrastershader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class CORE_EXPORT QgsRasterShader
7171

7272
void writeXML( QDomDocument& doc, QDomElement& parent ) const;
7373

74+
void readXML( const QDomElement& elem );
75+
7476
private:
7577
/** \brief User defineable minimum value for the raster shader */
7678
double mMinimumValue;

src/core/raster/qgssinglebandpseudocolorrenderer.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@ void QgsSingleBandPseudoColorRenderer::setShader( QgsRasterShader* shader )
4141

4242
QgsRasterRenderer* QgsSingleBandPseudoColorRenderer::create( const QDomElement& elem, QgsRasterDataProvider* provider )
4343
{
44-
return 0;
44+
if ( elem.isNull() )
45+
{
46+
return 0;
47+
}
48+
49+
int band = elem.attribute( "band", "-1" ).toInt();
50+
QgsRasterShader* shader = 0;
51+
QDomElement rasterShaderElem = elem.firstChildElement( "rastershader" );
52+
if ( !rasterShaderElem.isNull() )
53+
{
54+
shader = new QgsRasterShader();
55+
shader->readXML( rasterShaderElem );
56+
}
57+
QgsRasterRenderer* r = new QgsSingleBandPseudoColorRenderer( provider, band, shader );
58+
r->readXML( elem );
59+
return r;
4560
}
4661

4762
void QgsSingleBandPseudoColorRenderer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )

0 commit comments

Comments
 (0)