Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement readXML for singleband pseudo color
  • Loading branch information
mhugent committed Apr 4, 2012
1 parent a6eaf71 commit 4da0c03
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
35 changes: 32 additions & 3 deletions src/core/raster/qgsrastershader.cpp
Expand Up @@ -129,21 +129,21 @@ void QgsRasterShader::setMinimumValue( double theValue )

void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const
{
if( parent.isNull() || !mRasterShaderFunction )
if ( parent.isNull() || !mRasterShaderFunction )
{
return;
}

QDomElement rasterShaderElem = doc.createElement( "rastershader" );
QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( mRasterShaderFunction );
if( colorRampShader )
if ( colorRampShader )
{
QDomElement colorRampShaderElem = doc.createElement( "colorrampshader" );
colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
//items
QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
for(; itemIt != itemList.constEnd(); ++itemIt )
for ( ; itemIt != itemList.constEnd(); ++itemIt )
{
QDomElement itemElem = doc.createElement( "item" );
itemElem.setAttribute( "label", itemIt->label );
Expand All @@ -155,3 +155,32 @@ void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const
}
parent.appendChild( rasterShaderElem );
}

void QgsRasterShader::readXML( const QDomElement& elem )
{
//only colorrampshader
QDomElement colorRampShaderElem = elem.firstChildElement( "colorrampshader" );
if ( !colorRampShaderElem.isNull() )
{
QgsColorRampShader* colorRampShader = new QgsColorRampShader();
colorRampShader->setColorRampType( colorRampShaderElem.attribute( "colorRampType", "INTERPOLATED" ) );

QList<QgsColorRampShader::ColorRampItem> itemList;
QDomElement itemElem;
QString itemLabel;
double itemValue;
QColor itemColor;

QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( "item" );
for ( int i = 0; i < itemNodeList.size(); ++i )
{
itemElem = itemNodeList.at( i ).toElement();
itemValue = itemElem.attribute( "value" ).toDouble();
itemLabel = itemElem.attribute( "label" );
itemColor.setNamedColor( itemElem.attribute( "color" ) );
itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
}
colorRampShader->setColorRampItemList( itemList );
setRasterShaderFunction( colorRampShader );
}
}
2 changes: 2 additions & 0 deletions src/core/raster/qgsrastershader.h
Expand Up @@ -71,6 +71,8 @@ class CORE_EXPORT QgsRasterShader

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

void readXML( const QDomElement& elem );

private:
/** \brief User defineable minimum value for the raster shader */
double mMinimumValue;
Expand Down
17 changes: 16 additions & 1 deletion src/core/raster/qgssinglebandpseudocolorrenderer.cpp
Expand Up @@ -41,7 +41,22 @@ void QgsSingleBandPseudoColorRenderer::setShader( QgsRasterShader* shader )

QgsRasterRenderer* QgsSingleBandPseudoColorRenderer::create( const QDomElement& elem, QgsRasterDataProvider* provider )
{
return 0;
if ( elem.isNull() )
{
return 0;
}

int band = elem.attribute( "band", "-1" ).toInt();
QgsRasterShader* shader = 0;
QDomElement rasterShaderElem = elem.firstChildElement( "rastershader" );
if ( !rasterShaderElem.isNull() )
{
shader = new QgsRasterShader();
shader->readXML( rasterShaderElem );
}
QgsRasterRenderer* r = new QgsSingleBandPseudoColorRenderer( provider, band, shader );
r->readXML( elem );
return r;
}

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

0 comments on commit 4da0c03

Please sign in to comment.