QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsprocessingmodelchildparametersource.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingmodelchildparametersource.cpp
3 ------------------------------------------
4 begin : June 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30bool QgsProcessingModelChildParameterSource::operator==( const QgsProcessingModelChildParameterSource &other ) const
31{
32 if ( mSource != other.mSource )
33 return false;
34
35 switch ( mSource )
36 {
38 return mStaticValue == other.mStaticValue;
40 return mChildId == other.mChildId && mOutputName == other.mOutputName;
42 return mParameterName == other.mParameterName;
44 return mExpression == other.mExpression;
46 return mExpressionText == other.mExpressionText;
48 return true;
49 }
50 return false;
51}
52
53QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromStaticValue( const QVariant &value )
54{
55 QgsProcessingModelChildParameterSource src;
57 src.mStaticValue = value;
58 return src;
59}
60
61QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromModelParameter( const QString &parameterName )
62{
63 QgsProcessingModelChildParameterSource src;
65 src.mParameterName = parameterName;
66 return src;
67}
68
69QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromChildOutput( const QString &childId, const QString &outputName )
70{
71 QgsProcessingModelChildParameterSource src;
73 src.mChildId = childId;
74 src.mOutputName = outputName;
75 return src;
76}
77
78QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpression( const QString &expression )
79{
80 QgsProcessingModelChildParameterSource src;
82 src.mExpression = expression;
83 return src;
84}
85
86QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpressionText( const QString &text )
87{
88 QgsProcessingModelChildParameterSource src;
90 src.mExpressionText = text;
91 return src;
92}
93
94Qgis::ProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::source() const
95{
96 return mSource;
97}
98
99void QgsProcessingModelChildParameterSource::setSource( Qgis::ProcessingModelChildParameterSource source )
100{
101 mSource = source;
102}
103
104QVariant QgsProcessingModelChildParameterSource::toVariant() const
105{
106 QVariantMap map;
107 map.insert( u"source"_s, static_cast< int >( mSource ) );
108 switch ( mSource )
109 {
111 map.insert( u"parameter_name"_s, mParameterName );
112 break;
113
115 map.insert( u"child_id"_s, mChildId );
116 map.insert( u"output_name"_s, mOutputName );
117 break;
118
120 map.insert( u"static_value"_s, mStaticValue );
121 break;
122
124 map.insert( u"expression"_s, mExpression );
125 break;
126
128 map.insert( u"expression_text"_s, mExpressionText );
129 break;
130
132 break;
133 }
134 return map;
135}
136
137bool QgsProcessingModelChildParameterSource::loadVariant( const QVariantMap &map )
138{
139 mSource = static_cast< Qgis::ProcessingModelChildParameterSource >( map.value( u"source"_s ).toInt() );
140 switch ( mSource )
141 {
143 mParameterName = map.value( u"parameter_name"_s ).toString();
144 break;
145
147 mChildId = map.value( u"child_id"_s ).toString();
148 mOutputName = map.value( u"output_name"_s ).toString();
149 break;
150
152 mStaticValue = map.value( u"static_value"_s );
153 break;
154
156 mExpression = map.value( u"expression"_s ).toString();
157 break;
158
160 mExpressionText = map.value( u"expression_text"_s ).toString();
161 break;
162
164 break;
165 }
166 return true;
167}
168
169QString QgsProcessingModelChildParameterSource::asPythonCode( const QgsProcessing::PythonOutputType, const QgsProcessingParameterDefinition *definition, const QMap< QString, QString > &friendlyChildNames ) const
170{
171 switch ( mSource )
172 {
174 return u"parameters['%1']"_s.arg( mParameterName );
175
177 return u"outputs['%1']['%2']"_s.arg( friendlyChildNames.value( mChildId, mChildId ), mOutputName );
178
180 if ( definition )
181 {
183 return definition->valueAsPythonString( mStaticValue, c );
184 }
185 else
186 {
187 return QgsProcessingUtils::variantToPythonLiteral( mStaticValue );
188 }
189
191 return u"QgsExpression(%1).evaluate()"_s.arg( QgsProcessingUtils::stringToPythonLiteral( mExpression ) );
192
194 return mExpressionText;
195
197 return QString();
198 }
199 return QString();
200}
201
202QString QgsProcessingModelChildParameterSource::asPythonComment( const QgsProcessingParameterDefinition *definition ) const
203{
204 switch ( mSource )
205 {
211 return QString();
212
214 if ( definition )
215 {
217 return definition->valueAsPythonComment( mStaticValue, c );
218 }
219 else
220 {
221 return QString();
222 }
223 }
224 return QString();
225}
226
227QString QgsProcessingModelChildParameterSource::friendlyIdentifier( QgsProcessingModelAlgorithm *model ) const
228{
229 switch ( mSource )
230 {
232 {
233 if ( model )
234 {
235 if ( const QgsProcessingParameterDefinition *paramDefinition = model->parameterDefinition( mParameterName ) )
236 {
237 // A model can be valid (non null) but the parameter may not exist yet (if input has not yet been set)
238 return paramDefinition->description();
239 }
240 }
241
242 return mParameterName;
243 }
245 {
246 if ( model )
247 {
248 const QgsProcessingModelChildAlgorithm &alg = model->childAlgorithm( mChildId );
249 QString outputName = alg.algorithm() && alg.algorithm()->outputDefinition( mOutputName ) ? alg.algorithm()->outputDefinition( mOutputName )->description() : mOutputName;
250 // see if this output has been named by the model designer -- if so, we use that friendly name
251 const QMap<QString, QgsProcessingModelOutput> outputs = alg.modelOutputs();
252 for ( auto it = outputs.constBegin(); it != outputs.constEnd(); ++it )
253 {
254 if ( it.value().childOutputName() == mOutputName )
255 {
256 outputName = it.key();
257 break;
258 }
259 }
260 return QObject::tr( "'%1' from algorithm '%2'" ).arg( outputName, alg.description() );
261 }
262 else
263 {
264 return QObject::tr( "'%1' from algorithm '%2'" ).arg( mOutputName, mChildId );
265 }
266 }
267
269 return mStaticValue.toString();
270
272 return mExpression;
273
275 return mExpressionText;
276
278 return QString();
279 }
280 return QString();
281}
282
283QDataStream &operator<<( QDataStream &out, const QgsProcessingModelChildParameterSource &source )
284{
285 out << static_cast< int >( source.source() );
286 out << source.staticValue();
287 out << source.parameterName();
288 out << source.outputChildId();
289 out << source.outputName();
290 out << source.expression();
291 out << source.expressionText();
292 return out;
293}
294
295QDataStream &operator>>( QDataStream &in, QgsProcessingModelChildParameterSource &source )
296{
297 int sourceType;
298 QVariant staticValue;
299 QString parameterName;
300 QString outputChildId;
301 QString outputName;
302 QString expression;
303 QString expressionText;
304
305 in >> sourceType >> staticValue >> parameterName >> outputChildId >> outputName >> expression >> expressionText;
306
307 source.setStaticValue( staticValue );
308 source.setParameterName( parameterName );
309 source.setOutputChildId( outputChildId );
310 source.setOutputName( outputName );
311 source.setExpression( expression );
312 source.setExpressionText( expressionText );
313 source.setSource( static_cast<Qgis::ProcessingModelChildParameterSource>( sourceType ) );
314 return in;
315}
316
ProcessingModelChildParameterSource
Processing model child parameter sources.
Definition qgis.h:3906
@ ExpressionText
Parameter value is taken from a text with expressions, evaluated just before the algorithm runs.
Definition qgis.h:3911
@ ModelOutput
Parameter value is linked to an output parameter for the model.
Definition qgis.h:3912
@ ChildOutput
Parameter value is taken from an output generated by a child algorithm.
Definition qgis.h:3908
@ ModelParameter
Parameter value is taken from a parent model parameter.
Definition qgis.h:3907
@ StaticValue
Parameter value is a static value.
Definition qgis.h:3909
@ Expression
Parameter value is taken from an expression, evaluated just before the algorithm runs.
Definition qgis.h:3910
Contains information about the context in which a processing algorithm is executed.
Base class for the definition of processing parameters.
virtual QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
virtual QString valueAsPythonComment(const QVariant &value, QgsProcessingContext &context) const
Returns a Python comment explaining a parameter value, or an empty string if no comment is required.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
PythonOutputType
Available Python output types.
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
QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out. QGIS version compatibility is not guaranteed.
QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed.