QGIS API Documentation  2.11.0-Master
 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 : ersts@amnh.org
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;
31  mRasterShaderFunction = new QgsRasterShaderFunction( mMinimumValue, mMaximumValue );
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  {
113  mRasterShaderFunction->setMaximumValue( theValue );
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  {
129  mRasterShaderFunction->setMinimumValue( theValue );
130  }
131 }
132 
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();
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 
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 
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 }
QDomNodeList elementsByTagName(const QString &tagname) const
void setMaximumValue(double)
Set the maximum value.
QDomNode appendChild(const QDomNode &newChild)
void push_back(const T &value)
void setMinimumValue(double)
Return the minimum value.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
QString attribute(const QString &name, const QString &defValue) const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual bool shade(double, int *, int *, int *, int *)
generates and new RGBA value based on one input value
void setAlpha(int alpha)
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &theList)
Set custom colormap.
void setClip(bool clip)
void setNamedColor(const QString &name)
double toDouble(bool *ok) const
void writeXML(QDomDocument &doc, QDomElement &parent) const
QDomElement toElement() const
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Get the custom colormap.
QString number(int n, int base)
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
bool shade(double, int *, int *, int *, int *)
generates and new RGBA value based on one input value
void setRasterShaderFunction(QgsRasterShaderFunction *)
A public method that allows the user to set their own shader function.
void setColorRampType(QgsColorRampShader::ColorRamp_TYPE theColorRampType)
Set the color ramp type.
QString colorRampTypeAsQString()
Get the color ramp type as a string.
bool isNull() const
QDomElement firstChildElement(const QString &tagName) const
virtual void setMinimumValue(double)
Return the minimum value.
QgsRasterShader(double theMinimumValue=0.0, double theMaximumValue=255.0)
void readXML(const QDomElement &elem)
int size() const
const_iterator constEnd() const
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
virtual void setMaximumValue(double)
Set the maximum value.
QDomNode at(int index) const