|
Quantum GIS API Documentation
master-ce49b66
|
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 00048 bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue ) 00049 { 00050 if ( 0 != mRasterShaderFunction ) 00051 { 00052 return mRasterShaderFunction->shade( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue ); 00053 } 00054 00055 return false; 00056 } 00069 bool QgsRasterShader::shade( double theRedValue, double theGreenValue, double theBlueValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue ) 00070 { 00071 if ( 0 != mRasterShaderFunction ) 00072 { 00073 return mRasterShaderFunction->shade( theRedValue, theGreenValue, theBlueValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue ); 00074 } 00075 00076 return false; 00077 } 00078 00084 void QgsRasterShader::setRasterShaderFunction( QgsRasterShaderFunction* theFunction ) 00085 { 00086 QgsDebugMsg( "called." ); 00087 00088 if ( mRasterShaderFunction == theFunction ) 00089 return; 00090 00091 if ( 0 != theFunction ) 00092 { 00093 delete mRasterShaderFunction; 00094 mRasterShaderFunction = theFunction; 00095 } 00096 } 00097 00103 void QgsRasterShader::setMaximumValue( double theValue ) 00104 { 00105 QgsDebugMsg( "Value = " + QString::number( theValue ) ); 00106 00107 mMaximumValue = theValue; 00108 if ( 0 != mRasterShaderFunction ) 00109 { 00110 mRasterShaderFunction->setMaximumValue( theValue ); 00111 } 00112 } 00113 00119 void QgsRasterShader::setMinimumValue( double theValue ) 00120 { 00121 QgsDebugMsg( "Value = " + QString::number( theValue ) ); 00122 00123 mMinimumValue = theValue; 00124 if ( 0 != mRasterShaderFunction ) 00125 { 00126 mRasterShaderFunction->setMinimumValue( theValue ); 00127 } 00128 } 00129 00130 void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const 00131 { 00132 if ( parent.isNull() || !mRasterShaderFunction ) 00133 { 00134 return; 00135 } 00136 00137 QDomElement rasterShaderElem = doc.createElement( "rastershader" ); 00138 QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( mRasterShaderFunction ); 00139 if ( colorRampShader ) 00140 { 00141 QDomElement colorRampShaderElem = doc.createElement( "colorrampshader" ); 00142 colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() ); 00143 colorRampShaderElem.setAttribute( "clip", colorRampShader->clip() ); 00144 //items 00145 QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList(); 00146 QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin(); 00147 for ( ; itemIt != itemList.constEnd(); ++itemIt ) 00148 { 00149 QDomElement itemElem = doc.createElement( "item" ); 00150 itemElem.setAttribute( "label", itemIt->label ); 00151 itemElem.setAttribute( "value", QString::number( itemIt->value ) ); 00152 itemElem.setAttribute( "color", itemIt->color.name() ); 00153 colorRampShaderElem.appendChild( itemElem ); 00154 } 00155 rasterShaderElem.appendChild( colorRampShaderElem ); 00156 } 00157 parent.appendChild( rasterShaderElem ); 00158 } 00159 00160 void QgsRasterShader::readXML( const QDomElement& elem ) 00161 { 00162 //only colorrampshader 00163 QDomElement colorRampShaderElem = elem.firstChildElement( "colorrampshader" ); 00164 if ( !colorRampShaderElem.isNull() ) 00165 { 00166 QgsColorRampShader* colorRampShader = new QgsColorRampShader(); 00167 colorRampShader->setColorRampType( colorRampShaderElem.attribute( "colorRampType", "INTERPOLATED" ) ); 00168 colorRampShader->setClip( colorRampShaderElem.attribute( "clip", "0" ) == "1" ); 00169 00170 QList<QgsColorRampShader::ColorRampItem> itemList; 00171 QDomElement itemElem; 00172 QString itemLabel; 00173 double itemValue; 00174 QColor itemColor; 00175 00176 QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( "item" ); 00177 for ( int i = 0; i < itemNodeList.size(); ++i ) 00178 { 00179 itemElem = itemNodeList.at( i ).toElement(); 00180 itemValue = itemElem.attribute( "value" ).toDouble(); 00181 itemLabel = itemElem.attribute( "label" ); 00182 itemColor.setNamedColor( itemElem.attribute( "color" ) ); 00183 itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) ); 00184 } 00185 colorRampShader->setColorRampItemList( itemList ); 00186 setRasterShaderFunction( colorRampShader ); 00187 } 00188 }