QGIS API Documentation 4.3.0-Master (7c7bd4d7018)
Loading...
Searching...
No Matches
qgsprocessingparameterreliefcolors.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingparameterreliefcolors.cpp
3 ---------------------
4 Date : June 2026
5 Copyright : (C) 2026 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
24
26 : QgsProcessingParameterString( name, description, QVariant(), false, optional )
27 , mParentLayer( parentLayerParameter )
28{}
29
34
36{
37 return typeName();
38}
39
41{
42 switch ( outputType )
43 {
45 {
46 QString code
47 = u"QgsProcessingParameterReliefColors('%1', %2, %3"_s.arg( name(), QgsProcessingUtils::stringToPythonLiteral( description() ), QgsProcessingUtils::stringToPythonLiteral( mParentLayer ) );
49 code += ", optional=True"_L1;
50 code += ')'_L1;
51 return code;
52 }
53 }
54 return QString();
55}
56
58{
59 if ( !input.isValid() && !mDefault.isValid() )
61
62 const QString stringValue = input.toString();
63 if ( stringValue.isEmpty() || ( !input.isValid() && mDefault.userType() == QMetaType::Type::QString && mDefault.toString().isEmpty() ) )
65
66 const QStringList colorParts = stringValue.split( ';' );
67 for ( const QString &part : colorParts )
68 {
69 if ( part.trimmed().isEmpty() )
70 return false;
71
72 const QStringList v = part.split( ',' );
73 if ( v.count() < 5 )
74 {
75 return false;
76 }
77
78 bool ok = false;
79 ( void ) v.at( 0 ).toDouble( &ok );
80 if ( !ok )
81 return false;
82 ( void ) v.at( 1 ).toDouble( &ok );
83 if ( !ok )
84 return false;
85 ( void ) v.at( 2 ).toInt( &ok );
86 if ( !ok )
87 return false;
88 ( void ) v.at( 3 ).toInt( &ok );
89 if ( !ok )
90 return false;
91 ( void ) v.at( 4 ).toInt( &ok );
92 if ( !ok )
93 return false;
94 }
95
96 return true;
97}
98
100{
101 QVariantMap map = QgsProcessingParameterDefinition::toVariantMap(); // NOLINT(bugprone-parent-virtual-call) clazy:exclude=skipped-base-method
102 map.insert( u"parent"_s, mParentLayer );
103 return map;
104}
105
107{
108 QgsProcessingParameterDefinition::fromVariantMap( map ); // NOLINT(bugprone-parent-virtual-call) clazy:exclude=skipped-base-method
109 mParentLayer = map.value( u"parent"_s ).toString();
110 return true;
111}
112
113QList<QgsRasterReliefColor> QgsProcessingParameterReliefColors::valueAsReliefColors( const QVariant &value, const QgsProcessingContext &context ) const
114{
115 QList<QgsRasterReliefColor> reliefColors;
116 const QString colorsString = QgsProcessingParameters::parameterAsString( this, value, context );
117 const QStringList colorParts = colorsString.split( ';' );
118 for ( const QString &part : colorParts )
119 {
120 if ( part.trimmed().isEmpty() )
121 continue;
122
123 const QStringList v = part.split( ',' );
124 if ( v.count() >= 5 )
125 {
126 const double minElev = v.at( 0 ).toDouble();
127 const double maxElev = v.at( 1 ).toDouble();
128 const QColor color( v.at( 2 ).toInt(), v.at( 3 ).toInt(), v.at( 4 ).toInt() );
129 reliefColors.append( QgsRasterReliefColor( color, minElev, maxElev ) );
130 }
131 }
132 return reliefColors;
133}
134
135QVariant QgsProcessingParameterReliefColors::colorsAsVariant( const QList<QgsRasterReliefColor> &colors )
136{
137 QStringList parts;
138 parts.reserve( colors.size() );
139 for ( const QgsRasterReliefColor &c : colors )
140 {
141 parts << u"%1,%2,%3,%4,%5"_s.arg( qgsDoubleToString( c.minElevation ), qgsDoubleToString( c.maxElevation ) ).arg( c.color.red() ).arg( c.color.green() ).arg( c.color.blue() );
142 }
143 return parts.join( ';' );
144}
@ Optional
Parameter is optional.
Definition qgis.h:3950
Contains information about the context in which a processing algorithm is executed.
Qgis::ProcessingParameterFlags mFlags
Parameter flags.
QString description() const
Returns the description for the parameter.
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
QString name() const
Returns the name of the parameter.
QVariant mDefault
Default value for parameter.
Qgis::ProcessingParameterFlags flags() const
Returns any flags associated with the parameter.
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
QList< QgsRasterReliefColor > valueAsReliefColors(const QVariant &value, const QgsProcessingContext &context) const
Returns the parameter value interpreted as a list of raster relief colors.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonOutputType::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QVariant colorsAsVariant(const QList< QgsRasterReliefColor > &colors)
Returns a list of raster relief colors encoded as a variant value.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString type() const override
Unique parameter type name.
QString parentLayerParameter() const
Returns the linked parent layer parameter name.
QgsProcessingParameterReliefColors * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterReliefColors(const QString &name, const QString &description=QString(), const QString &parentLayerParameter=QString(), bool optional=true)
Constructor for QgsProcessingParameterReliefColors.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterString(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool multiLine=false, bool optional=false)
Constructor for QgsProcessingParameterString.
static QString parameterAsString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static string value.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
PythonOutputType
Available Python output types.
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Defines elevation range and color for raster relief coloring.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:7247