QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrastershader.cpp
Go to the documentation of this file.
1 /* **************************************************************************
2  qgsrastershader.cpp - description
3  -------------------
4 begin : Fri Dec 28 2007
5 copyright : (C) 2007 by Peter J. Ersts
6 email : [email protected]
7 
8 ****************************************************************************/
9 
10 /* **************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "qgslogger.h"
20 #include "qgscolorrampshader.h"
21 #include "qgsrastershader.h"
22 #include <QDomDocument>
23 #include <QDomElement>
24 
25 QgsRasterShader::QgsRasterShader( double theMinimumValue, double theMaximumValue )
26 {
27  QgsDebugMsg( "called." );
28 
29  mMinimumValue = theMinimumValue;
30  mMaximumValue = theMaximumValue;
32 }
33 
35 {
36  delete mRasterShaderFunction;
37 }
38 
49 bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue , int *theReturnAlpha )
50 {
51  if ( 0 != mRasterShaderFunction )
52  {
53  return mRasterShaderFunction->shade( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlpha );
54  }
55 
56  return false;
57 }
72 bool QgsRasterShader::shade( double theRedValue, double theGreenValue, double theBlueValue, double theAlphaValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int* theReturnAlphaValue )
73 {
74  if ( 0 != mRasterShaderFunction )
75  {
76  return mRasterShaderFunction->shade( theRedValue, theGreenValue, theBlueValue, theAlphaValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
77  }
78 
79  return false;
80 }
81 
88 {
89  QgsDebugMsg( "called." );
90 
91  if ( mRasterShaderFunction == theFunction )
92  return;
93 
94  if ( 0 != theFunction )
95  {
96  delete mRasterShaderFunction;
97  mRasterShaderFunction = theFunction;
98  }
99 }
100 
106 void QgsRasterShader::setMaximumValue( double theValue )
107 {
108  QgsDebugMsg( "Value = " + QString::number( theValue ) );
109 
110  mMaximumValue = theValue;
111  if ( 0 != mRasterShaderFunction )
112  {
114  }
115 }
116 
122 void QgsRasterShader::setMinimumValue( double theValue )
123 {
124  QgsDebugMsg( "Value = " + QString::number( theValue ) );
125 
126  mMinimumValue = theValue;
127  if ( 0 != mRasterShaderFunction )
128  {
130  }
131 }
132 
133 void QgsRasterShader::writeXML( QDomDocument& doc, QDomElement& parent ) const
134 {
135  if ( parent.isNull() || !mRasterShaderFunction )
136  {
137  return;
138  }
139 
140  QDomElement rasterShaderElem = doc.createElement( "rastershader" );
141  QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( mRasterShaderFunction );
142  if ( colorRampShader )
143  {
144  QDomElement colorRampShaderElem = doc.createElement( "colorrampshader" );
145  colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
146  colorRampShaderElem.setAttribute( "clip", colorRampShader->clip() );
147  //items
148  QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
149  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
150  for ( ; itemIt != itemList.constEnd(); ++itemIt )
151  {
152  QDomElement itemElem = doc.createElement( "item" );
153  itemElem.setAttribute( "label", itemIt->label );
154  itemElem.setAttribute( "value", QString::number( itemIt->value ) );
155  itemElem.setAttribute( "color", itemIt->color.name() );
156  itemElem.setAttribute( "alpha", itemIt->color.alpha() );
157  colorRampShaderElem.appendChild( itemElem );
158  }
159  rasterShaderElem.appendChild( colorRampShaderElem );
160  }
161  parent.appendChild( rasterShaderElem );
162 }
163 
164 void QgsRasterShader::readXML( const QDomElement& elem )
165 {
166  //only colorrampshader
167  QDomElement colorRampShaderElem = elem.firstChildElement( "colorrampshader" );
168  if ( !colorRampShaderElem.isNull() )
169  {
170  QgsColorRampShader* colorRampShader = new QgsColorRampShader();
171  colorRampShader->setColorRampType( colorRampShaderElem.attribute( "colorRampType", "INTERPOLATED" ) );
172  colorRampShader->setClip( colorRampShaderElem.attribute( "clip", "0" ) == "1" );
173 
174  QList<QgsColorRampShader::ColorRampItem> itemList;
175  QDomElement itemElem;
176  QString itemLabel;
177  double itemValue;
178  QColor itemColor;
179 
180  QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( "item" );
181  for ( int i = 0; i < itemNodeList.size(); ++i )
182  {
183  itemElem = itemNodeList.at( i ).toElement();
184  itemValue = itemElem.attribute( "value" ).toDouble();
185  itemLabel = itemElem.attribute( "label" );
186  itemColor.setNamedColor( itemElem.attribute( "color" ) );
187  itemColor.setAlpha( itemElem.attribute( "alpha", "255" ).toInt() );
188 
189  itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
190  }
191  colorRampShader->setColorRampItemList( itemList );
192  setRasterShaderFunction( colorRampShader );
193  }
194 }