25 #include <QDomDocument> 26 #include <QDomElement> 29 : mMinimumValue( minimumValue )
30 , mMaximumValue( maximumValue )
36 bool QgsRasterShader::shade(
double value,
int *returnRedValue,
int *returnGreenValue,
int *returnBlueValue,
int *returnAlpha )
38 if ( mRasterShaderFunction )
40 return mRasterShaderFunction->shade( value, returnRedValue, returnGreenValue, returnBlueValue, returnAlpha );
46 bool QgsRasterShader::shade(
double redValue,
double greenValue,
double blueValue,
double alphaValue,
int *returnRedValue,
int *returnGreenValue,
int *returnBlueValue,
int *returnAlphaValue )
48 if ( mRasterShaderFunction )
50 return mRasterShaderFunction->shade( redValue, greenValue, blueValue, alphaValue, returnRedValue, returnGreenValue, returnBlueValue, returnAlphaValue );
60 if ( mRasterShaderFunction.get() == function )
65 mRasterShaderFunction.reset(
function );
73 mMaximumValue = value;
74 if ( mRasterShaderFunction )
76 mRasterShaderFunction->setMaximumValue( value );
84 mMinimumValue = value;
85 if ( mRasterShaderFunction )
87 mRasterShaderFunction->setMinimumValue( value );
93 if ( parent.isNull() || !mRasterShaderFunction )
98 QDomElement rasterShaderElem = doc.createElement( QStringLiteral(
"rastershader" ) );
100 if ( colorRampShader )
102 QDomElement colorRampShaderElem = doc.createElement( QStringLiteral(
"colorrampshader" ) );
103 colorRampShaderElem.setAttribute( QStringLiteral(
"colorRampType" ), colorRampShader->
colorRampTypeAsQString() );
104 colorRampShaderElem.setAttribute( QStringLiteral(
"classificationMode" ), colorRampShader->
classificationMode() );
105 colorRampShaderElem.setAttribute( QStringLiteral(
"clip" ), colorRampShader->
clip() );
111 colorRampShaderElem.appendChild( colorRampElem );
115 QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->
colorRampItemList();
116 QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
117 for ( ; itemIt != itemList.constEnd(); ++itemIt )
119 QDomElement itemElem = doc.createElement( QStringLiteral(
"item" ) );
120 itemElem.setAttribute( QStringLiteral(
"label" ), itemIt->label );
122 itemElem.setAttribute( QStringLiteral(
"color" ), itemIt->color.name() );
123 itemElem.setAttribute( QStringLiteral(
"alpha" ), itemIt->color.alpha() );
124 colorRampShaderElem.appendChild( itemElem );
126 rasterShaderElem.appendChild( colorRampShaderElem );
128 parent.appendChild( rasterShaderElem );
134 QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral(
"colorrampshader" ) );
135 if ( !colorRampShaderElem.isNull() )
140 QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral(
"colorramp" ) );
141 if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral(
"name" ) ) == QLatin1String(
"[source]" ) )
146 colorRampShader->
setColorRampType( colorRampShaderElem.attribute( QStringLiteral(
"colorRampType" ), QStringLiteral(
"INTERPOLATED" ) ) );
147 colorRampShader->
setClassificationMode( static_cast< QgsColorRampShader::ClassificationMode >( colorRampShaderElem.attribute( QStringLiteral(
"classificationMode" ), QStringLiteral(
"1" ) ).toInt() ) );
148 colorRampShader->
setClip( colorRampShaderElem.attribute( QStringLiteral(
"clip" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" ) );
150 QList<QgsColorRampShader::ColorRampItem> itemList;
151 QDomElement itemElem;
156 QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral(
"item" ) );
157 itemList.reserve( itemNodeList.size() );
158 for (
int i = 0; i < itemNodeList.size(); ++i )
160 itemElem = itemNodeList.at( i ).toElement();
161 itemValue = itemElem.attribute( QStringLiteral(
"value" ) ).toDouble();
162 itemLabel = itemElem.attribute( QStringLiteral(
"label" ) );
163 itemColor.setNamedColor( itemElem.attribute( QStringLiteral(
"color" ) ) );
164 itemColor.setAlpha( itemElem.attribute( QStringLiteral(
"alpha" ), QStringLiteral(
"255" ) ).toInt() );
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &list)
Sets a custom colormap.
static QString printValue(double value)
Print double value with all necessary significant digits.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
static QDomElement saveColorRamp(const QString &name, QgsColorRamp *ramp, QDomDocument &doc)
Encodes a color ramp's settings to an XML element.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
void setColorRampType(QgsColorRampShader::Type colorRampType)
Sets the color ramp type.
#define QgsDebugMsgLevel(str, level)
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
static QgsColorRamp * loadColorRamp(QDomElement &element)
Creates a color ramp from the settings encoded in an XML element.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
bool shade(double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha)
Generates a new RGBA value based on one input value.
QString colorRampTypeAsQString()
Returns the color ramp type as a string.
QgsRasterShader(double minimumValue=0.0, double maximumValue=255.0)
void writeXml(QDomDocument &doc, QDomElement &parent) const
Writes shader state to an XML element.
void setMinimumValue(double value)
Sets the minimum value for the raster shader.
void setMaximumValue(double value)
Sets the maximum value for the raster shader.
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
bool clip() const
Returns whether the shader will clip values which are out of range.
ClassificationMode classificationMode() const
Returns the classification mode.
QgsColorRamp * sourceColorRamp() const
Gets the source color ramp.
void readXml(const QDomElement &elem)
Reads shader state from an XML element.