QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 "qgslogger.h"
20#include "qgscolorrampshader.h"
21#include "qgsrastershader.h"
22#include "qgsrasterblock.h"
23#include "qgssymbollayerutils.h"
24
25#include <QDomDocument>
26#include <QDomElement>
27
28QgsRasterShader::QgsRasterShader( double minimumValue, double maximumValue )
29 : mMinimumValue( minimumValue )
30 , mMaximumValue( maximumValue )
31 , mRasterShaderFunction( new QgsRasterShaderFunction( mMinimumValue, mMaximumValue ) )
32{
33 QgsDebugMsgLevel( QStringLiteral( "called." ), 4 );
34}
35
36bool QgsRasterShader::shade( double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha )
37{
38 if ( mRasterShaderFunction )
39 {
40 return mRasterShaderFunction->shade( value, returnRedValue, returnGreenValue, returnBlueValue, returnAlpha );
41 }
42
43 return false;
44}
45
46bool QgsRasterShader::shade( double redValue, double greenValue, double blueValue, double alphaValue, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlphaValue )
47{
48 if ( mRasterShaderFunction )
49 {
50 return mRasterShaderFunction->shade( redValue, greenValue, blueValue, alphaValue, returnRedValue, returnGreenValue, returnBlueValue, returnAlphaValue );
51 }
52
53 return false;
54}
55
57{
58 QgsDebugMsgLevel( QStringLiteral( "called." ), 4 );
59
60 if ( mRasterShaderFunction.get() == function )
61 return;
62
63 if ( function )
64 {
65 mRasterShaderFunction.reset( function );
66 }
67}
68
70{
71 QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
72
73 mMaximumValue = value;
74 if ( mRasterShaderFunction )
75 {
76 mRasterShaderFunction->setMaximumValue( value );
77 }
78}
79
81{
82 QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
83
84 mMinimumValue = value;
85 if ( mRasterShaderFunction )
86 {
87 mRasterShaderFunction->setMinimumValue( value );
88 }
89}
90
91void QgsRasterShader::writeXml( QDomDocument &doc, QDomElement &parent, const QgsReadWriteContext &context ) const
92{
93 if ( parent.isNull() || !mRasterShaderFunction )
94 {
95 return;
96 }
97
98 QDomElement rasterShaderElem = doc.createElement( QStringLiteral( "rastershader" ) );
99 QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( mRasterShaderFunction.get() );
100 if ( colorRampShader )
101 {
102 rasterShaderElem.appendChild( colorRampShader->writeXml( doc, context ) );
103 }
104 parent.appendChild( rasterShaderElem );
105}
106
107void QgsRasterShader::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
108{
109 //only colorrampshader
110 const QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
111 if ( !colorRampShaderElem.isNull() )
112 {
113 QgsColorRampShader *colorRampShader = new QgsColorRampShader();
114 colorRampShader->readXml( colorRampShaderElem, context );
115 setRasterShaderFunction( colorRampShader );
116 }
117}
118
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...
void setMinimumValue(double value)
Sets the minimum value for the raster shader.
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)
bool shade(double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha)
Generates a new RGBA value based on one input value.
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.
void setMaximumValue(double value)
Sets the maximum value for the raster shader.
The class is used as a container of context for various read/write operations on other objects.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39