QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsrastershader.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrastershader.cpp - description
3 -------------------
4begin : Fri Dec 28 2007
5copyright : (C) 2007 by Peter J. Ersts
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 "qgsrastershader.h"
20
21#include "qgscolorrampshader.h"
22#include "qgslogger.h"
23#include "qgsrasterblock.h"
24#include "qgssymbollayerutils.h"
25
26#include <QDomDocument>
27#include <QDomElement>
28
30 : mMinimumValue( minimumValue )
31 , mMaximumValue( maximumValue )
32 , mRasterShaderFunction( new QgsRasterShaderFunction( mMinimumValue, mMaximumValue ) )
33{
34 QgsDebugMsgLevel( QStringLiteral( "called." ), 4 );
35}
36
37bool QgsRasterShader::shade( double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha ) const
38{
39 if ( mRasterShaderFunction )
40 {
41 return mRasterShaderFunction->shade( value, returnRedValue, returnGreenValue, returnBlueValue, returnAlpha );
42 }
43
44 return false;
45}
46
47bool QgsRasterShader::shade( double redValue, double greenValue, double blueValue, double alphaValue, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlphaValue ) const
48{
49 if ( mRasterShaderFunction )
50 {
51 return mRasterShaderFunction->shade( redValue, greenValue, blueValue, alphaValue, returnRedValue, returnGreenValue, returnBlueValue, returnAlphaValue );
52 }
53
54 return false;
55}
56
58{
59 QgsDebugMsgLevel( QStringLiteral( "called." ), 4 );
60
61 if ( mRasterShaderFunction.get() == function )
62 return;
63
64 if ( function )
65 {
66 mRasterShaderFunction.reset( function );
67 }
68}
69
71{
72 QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
73
74 mMaximumValue = value;
75 if ( mRasterShaderFunction )
76 {
77 mRasterShaderFunction->setMaximumValue( value );
78 }
79}
80
82{
83 QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
84
85 mMinimumValue = value;
86 if ( mRasterShaderFunction )
87 {
88 mRasterShaderFunction->setMinimumValue( value );
89 }
90}
91
92void QgsRasterShader::writeXml( QDomDocument &doc, QDomElement &parent, const QgsReadWriteContext &context ) const
93{
94 if ( parent.isNull() || !mRasterShaderFunction )
95 {
96 return;
97 }
98
99 QDomElement rasterShaderElem = doc.createElement( QStringLiteral( "rastershader" ) );
100 QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( mRasterShaderFunction.get() );
101 if ( colorRampShader )
102 {
103 rasterShaderElem.appendChild( colorRampShader->writeXml( doc, context ) );
104 }
105 parent.appendChild( rasterShaderElem );
106}
107
108void QgsRasterShader::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
109{
110 //only colorrampshader
111 const QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
112 if ( !colorRampShaderElem.isNull() )
113 {
114 QgsColorRampShader *colorRampShader = new QgsColorRampShader();
115 colorRampShader->readXml( colorRampShaderElem, context );
116 setRasterShaderFunction( colorRampShader );
117 }
118}
119
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes configuration to a new DOM element.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads configuration from the given DOM element.
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
double maximumValue() const
Returns the maximum value for the raster shader.
void setMinimumValue(double value)
Sets the minimum value for the raster shader.
bool shade(double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha) const
Generates a new RGBA value based on one input value.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads shader state from an XML element.
QgsRasterShader(double minimumValue=0.0, double maximumValue=255.0)
void writeXml(QDomDocument &doc, QDomElement &parent, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes shader state to an XML element.
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
double minimumValue() const
Returns the minimum value for the raster shader.
void setMaximumValue(double value)
Sets the maximum value for the raster shader.
A container for the context for various read/write operations on objects.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61