QGIS API Documentation  master-6227475
src/core/raster/qgsrastershader.cpp
Go to the documentation of this file.
00001 /* **************************************************************************
00002                 qgsrastershader.cpp -  description
00003                        -------------------
00004 begin                : Fri Dec 28 2007
00005 copyright            : (C) 2007 by Peter J. Ersts
00006 email                : ersts@amnh.org
00007 
00008 ****************************************************************************/
00009 
00010 /* **************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "qgslogger.h"
00020 #include "qgscolorrampshader.h"
00021 #include "qgsrastershader.h"
00022 #include <QDomDocument>
00023 #include <QDomElement>
00024 
00025 QgsRasterShader::QgsRasterShader( double theMinimumValue, double theMaximumValue )
00026 {
00027   QgsDebugMsg( "called." );
00028 
00029   mMinimumValue = theMinimumValue;
00030   mMaximumValue = theMaximumValue;
00031   mRasterShaderFunction = new QgsRasterShaderFunction( mMinimumValue, mMaximumValue );
00032 }
00033 
00034 QgsRasterShader::~QgsRasterShader()
00035 {
00036   delete mRasterShaderFunction;
00037 }
00038 
00049 bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue , int *theReturnAlpha )
00050 {
00051   if ( 0 != mRasterShaderFunction )
00052   {
00053     return mRasterShaderFunction->shade( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlpha );
00054   }
00055 
00056   return false;
00057 }
00072 bool QgsRasterShader::shade( double theRedValue, double theGreenValue, double theBlueValue, double theAlphaValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int* theReturnAlphaValue )
00073 {
00074   if ( 0 != mRasterShaderFunction )
00075   {
00076     return mRasterShaderFunction->shade( theRedValue, theGreenValue, theBlueValue, theAlphaValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
00077   }
00078 
00079   return false;
00080 }
00081 
00087 void QgsRasterShader::setRasterShaderFunction( QgsRasterShaderFunction* theFunction )
00088 {
00089   QgsDebugMsg( "called." );
00090 
00091   if ( mRasterShaderFunction == theFunction )
00092     return;
00093 
00094   if ( 0 != theFunction )
00095   {
00096     delete mRasterShaderFunction;
00097     mRasterShaderFunction = theFunction;
00098   }
00099 }
00100 
00106 void QgsRasterShader::setMaximumValue( double theValue )
00107 {
00108   QgsDebugMsg( "Value = " + QString::number( theValue ) );
00109 
00110   mMaximumValue = theValue;
00111   if ( 0 != mRasterShaderFunction )
00112   {
00113     mRasterShaderFunction->setMaximumValue( theValue );
00114   }
00115 }
00116 
00122 void QgsRasterShader::setMinimumValue( double theValue )
00123 {
00124   QgsDebugMsg( "Value = " + QString::number( theValue ) );
00125 
00126   mMinimumValue = theValue;
00127   if ( 0 != mRasterShaderFunction )
00128   {
00129     mRasterShaderFunction->setMinimumValue( theValue );
00130   }
00131 }
00132 
00133 void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const
00134 {
00135   if ( parent.isNull() || !mRasterShaderFunction )
00136   {
00137     return;
00138   }
00139 
00140   QDomElement rasterShaderElem = doc.createElement( "rastershader" );
00141   QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( mRasterShaderFunction );
00142   if ( colorRampShader )
00143   {
00144     QDomElement colorRampShaderElem = doc.createElement( "colorrampshader" );
00145     colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
00146     colorRampShaderElem.setAttribute( "clip", colorRampShader->clip() );
00147     //items
00148     QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
00149     QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
00150     for ( ; itemIt != itemList.constEnd(); ++itemIt )
00151     {
00152       QDomElement itemElem = doc.createElement( "item" );
00153       itemElem.setAttribute( "label", itemIt->label );
00154       itemElem.setAttribute( "value", QString::number( itemIt->value ) );
00155       itemElem.setAttribute( "color", itemIt->color.name() );
00156       itemElem.setAttribute( "alpha", itemIt->color.alpha() );
00157       colorRampShaderElem.appendChild( itemElem );
00158     }
00159     rasterShaderElem.appendChild( colorRampShaderElem );
00160   }
00161   parent.appendChild( rasterShaderElem );
00162 }
00163 
00164 void QgsRasterShader::readXML( const QDomElement& elem )
00165 {
00166   //only colorrampshader
00167   QDomElement colorRampShaderElem = elem.firstChildElement( "colorrampshader" );
00168   if ( !colorRampShaderElem.isNull() )
00169   {
00170     QgsColorRampShader* colorRampShader = new QgsColorRampShader();
00171     colorRampShader->setColorRampType( colorRampShaderElem.attribute( "colorRampType", "INTERPOLATED" ) );
00172     colorRampShader->setClip( colorRampShaderElem.attribute( "clip", "0" ) == "1" );
00173 
00174     QList<QgsColorRampShader::ColorRampItem> itemList;
00175     QDomElement itemElem;
00176     QString itemLabel;
00177     double itemValue;
00178     QColor itemColor;
00179 
00180     QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( "item" );
00181     for ( int i = 0; i < itemNodeList.size(); ++i )
00182     {
00183       itemElem = itemNodeList.at( i ).toElement();
00184       itemValue = itemElem.attribute( "value" ).toDouble();
00185       itemLabel = itemElem.attribute( "label" );
00186       itemColor.setNamedColor( itemElem.attribute( "color" ) );
00187       itemColor.setAlpha( itemElem.attribute( "alpha", "255" ).toInt() );
00188 
00189       itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
00190     }
00191     colorRampShader->setColorRampItemList( itemList );
00192     setRasterShaderFunction( colorRampShader );
00193   }
00194 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines