QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
29
30using namespace Qt::StringLiterals;
31
33 : mMinimumValue( minimumValue )
34 , mMaximumValue( maximumValue )
35 , mRasterShaderFunction( new QgsRasterShaderFunction( mMinimumValue, mMaximumValue ) )
36{
37 QgsDebugMsgLevel( u"called."_s, 4 );
38}
39
40bool QgsRasterShader::shade( double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha ) const
41{
42 if ( mRasterShaderFunction )
43 {
44 return mRasterShaderFunction->shade( value, returnRedValue, returnGreenValue, returnBlueValue, returnAlpha );
45 }
46
47 return false;
48}
49
50bool QgsRasterShader::shade( double redValue, double greenValue, double blueValue, double alphaValue, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlphaValue ) const
51{
52 if ( mRasterShaderFunction )
53 {
54 return mRasterShaderFunction->shade( redValue, greenValue, blueValue, alphaValue, returnRedValue, returnGreenValue, returnBlueValue, returnAlphaValue );
55 }
56
57 return false;
58}
59
61{
62 QgsDebugMsgLevel( u"called."_s, 4 );
63
64 if ( mRasterShaderFunction.get() == function )
65 return;
66
67 if ( function )
68 {
69 mRasterShaderFunction.reset( function );
70 }
71}
72
74{
75 QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
76
77 mMaximumValue = value;
78 if ( mRasterShaderFunction )
79 {
80 mRasterShaderFunction->setMaximumValue( value );
81 }
82}
83
85{
86 QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
87
88 mMinimumValue = value;
89 if ( mRasterShaderFunction )
90 {
91 mRasterShaderFunction->setMinimumValue( value );
92 }
93}
94
95void QgsRasterShader::writeXml( QDomDocument &doc, QDomElement &parent, const QgsReadWriteContext &context ) const
96{
97 if ( parent.isNull() || !mRasterShaderFunction )
98 {
99 return;
100 }
101
102 QDomElement rasterShaderElem = doc.createElement( u"rastershader"_s );
103 QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( mRasterShaderFunction.get() );
104 if ( colorRampShader )
105 {
106 rasterShaderElem.appendChild( colorRampShader->writeXml( doc, context ) );
107 }
108 parent.appendChild( rasterShaderElem );
109}
110
111void QgsRasterShader::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
112{
113 //only colorrampshader
114 const QDomElement colorRampShaderElem = elem.firstChildElement( u"colorrampshader"_s );
115 if ( !colorRampShaderElem.isNull() )
116 {
117 QgsColorRampShader *colorRampShader = new QgsColorRampShader();
118 colorRampShader->readXml( colorRampShaderElem, context );
119 setRasterShaderFunction( colorRampShader );
120 }
121}
122
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:63