QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
22
24
25bool QgsProcessingModelChildParameterSource::operator==( const QgsProcessingModelChildParameterSource &other ) const
26{
27 if ( mSource != other.mSource )
28 return false;
29
30 switch ( mSource )
31 {
32 case StaticValue:
33 return mStaticValue == other.mStaticValue;
34 case ChildOutput:
35 return mChildId == other.mChildId && mOutputName == other.mOutputName;
36 case ModelParameter:
37 return mParameterName == other.mParameterName;
38 case Expression:
39 return mExpression == other.mExpression;
40 case ExpressionText:
41 return mExpressionText == other.mExpressionText;
42 case ModelOutput:
43 return true;
44 }
45 return false;
46}
47
48QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromStaticValue( const QVariant &value )
49{
50 QgsProcessingModelChildParameterSource src;
51 src.mSource = StaticValue;
52 src.mStaticValue = value;
53 return src;
54}
55
56QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromModelParameter( const QString &parameterName )
57{
58 QgsProcessingModelChildParameterSource src;
59 src.mSource = ModelParameter;
60 src.mParameterName = parameterName;
61 return src;
62}
63
64QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromChildOutput( const QString &childId, const QString &outputName )
65{
66 QgsProcessingModelChildParameterSource src;
67 src.mSource = ChildOutput;
68 src.mChildId = childId;
69 src.mOutputName = outputName;
70 return src;
71}
72
73QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpression( const QString &expression )
74{
75 QgsProcessingModelChildParameterSource src;
76 src.mSource = Expression;
77 src.mExpression = expression;
78 return src;
79}
80
81QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpressionText( const QString &text )
82{
83 QgsProcessingModelChildParameterSource src;
84 src.mSource = ExpressionText;
85 src.mExpressionText = text;
86 return src;
87}
88
89QgsProcessingModelChildParameterSource::Source QgsProcessingModelChildParameterSource::source() const
90{
91 return mSource;
92}
93
94void QgsProcessingModelChildParameterSource::setSource( QgsProcessingModelChildParameterSource::Source source )
95{
96 mSource = source;
97}
98
99QVariant QgsProcessingModelChildParameterSource::toVariant() const
100{
101 QVariantMap map;
102 map.insert( QStringLiteral( "source" ), mSource );
103 switch ( mSource )
104 {
105 case ModelParameter:
106 map.insert( QStringLiteral( "parameter_name" ), mParameterName );
107 break;
108
109 case ChildOutput:
110 map.insert( QStringLiteral( "child_id" ), mChildId );
111 map.insert( QStringLiteral( "output_name" ), mOutputName );
112 break;
113
114 case StaticValue:
115 map.insert( QStringLiteral( "static_value" ), mStaticValue );
116 break;
117
118 case Expression:
119 map.insert( QStringLiteral( "expression" ), mExpression );
120 break;
121
122 case ExpressionText:
123 map.insert( QStringLiteral( "expression_text" ), mExpressionText );
124 break;
125
126 case ModelOutput:
127 break;
128 }
129 return map;
130}
131
132bool QgsProcessingModelChildParameterSource::loadVariant( const QVariantMap &map )
133{
134 mSource = static_cast< Source >( map.value( QStringLiteral( "source" ) ).toInt() );
135 switch ( mSource )
136 {
137 case ModelParameter:
138 mParameterName = map.value( QStringLiteral( "parameter_name" ) ).toString();
139 break;
140
141 case ChildOutput:
142 mChildId = map.value( QStringLiteral( "child_id" ) ).toString();
143 mOutputName = map.value( QStringLiteral( "output_name" ) ).toString();
144 break;
145
146 case StaticValue:
147 mStaticValue = map.value( QStringLiteral( "static_value" ) );
148 break;
149
150 case Expression:
151 mExpression = map.value( QStringLiteral( "expression" ) ).toString();
152 break;
153
154 case ExpressionText:
155 mExpressionText = map.value( QStringLiteral( "expression_text" ) ).toString();
156 break;
157
158 case ModelOutput:
159 break;
160 }
161 return true;
162}
163
164QString QgsProcessingModelChildParameterSource::asPythonCode( const QgsProcessing::PythonOutputType, const QgsProcessingParameterDefinition *definition, const QMap< QString, QString > &friendlyChildNames ) const
165{
166 switch ( mSource )
167 {
168 case ModelParameter:
169 return QStringLiteral( "parameters['%1']" ).arg( mParameterName );
170
171 case ChildOutput:
172 return QStringLiteral( "outputs['%1']['%2']" ).arg( friendlyChildNames.value( mChildId, mChildId ), mOutputName );
173
174 case StaticValue:
175 if ( definition )
176 {
178 return definition->valueAsPythonString( mStaticValue, c );
179 }
180 else
181 {
182 return QgsProcessingUtils::variantToPythonLiteral( mStaticValue );
183 }
184
185 case Expression:
186 return QStringLiteral( "QgsExpression(%1).evaluate()" ).arg( QgsProcessingUtils::stringToPythonLiteral( mExpression ) );
187
188 case ExpressionText:
189 return mExpressionText;
190
191 case ModelOutput:
192 return QString();
193 }
194 return QString();
195}
196
197QString QgsProcessingModelChildParameterSource::asPythonComment( const QgsProcessingParameterDefinition *definition ) const
198{
199 switch ( mSource )
200 {
201 case ModelParameter:
202 case ChildOutput:
203 case Expression:
204 case ExpressionText:
205 case ModelOutput:
206 return QString();
207
208 case StaticValue:
209 if ( definition )
210 {
212 return definition->valueAsPythonComment( mStaticValue, c );
213 }
214 else
215 {
216 return QString();
217 }
218 }
219 return QString();
220}
221
222QString QgsProcessingModelChildParameterSource::friendlyIdentifier( QgsProcessingModelAlgorithm *model ) const
223{
224 switch ( mSource )
225 {
226 case ModelParameter:
227 return model ? model->parameterDefinition( mParameterName )->description() : mParameterName;
228
229 case ChildOutput:
230 {
231 if ( model )
232 {
233 const QgsProcessingModelChildAlgorithm &alg = model->childAlgorithm( mChildId );
234 QString outputName = alg.algorithm() && alg.algorithm()->outputDefinition( mOutputName ) ? alg.algorithm()->outputDefinition( mOutputName )->description() : mOutputName;
235 // see if this output has been named by the model designer -- if so, we use that friendly name
236 const QMap<QString, QgsProcessingModelOutput> outputs = alg.modelOutputs();
237 for ( auto it = outputs.constBegin(); it != outputs.constEnd(); ++it )
238 {
239 if ( it.value().childOutputName() == mOutputName )
240 {
241 outputName = it.key();
242 break;
243 }
244 }
245 return QObject::tr( "'%1' from algorithm '%2'" ).arg( outputName, alg.description() );
246 }
247 else
248 {
249 return QObject::tr( "'%1' from algorithm '%2'" ).arg( mOutputName, mChildId );
250 }
251 }
252
253 case StaticValue:
254 return mStaticValue.toString();
255
256 case Expression:
257 return mExpression;
258
259 case ExpressionText:
260 return mExpressionText;
261
262 case ModelOutput:
263 return QString();
264 }
265 return QString();
266}
267
268QDataStream &operator<<( QDataStream &out, const QgsProcessingModelChildParameterSource &source )
269{
270 out << source.source();
271 out << source.staticValue();
272 out << source.parameterName();
273 out << source.outputChildId();
274 out << source.outputName();
275 out << source.expression();
276 out << source.expressionText();
277 return out;
278}
279
280QDataStream &operator>>( QDataStream &in, QgsProcessingModelChildParameterSource &source )
281{
282 int sourceType;
283 QVariant staticValue;
284 QString parameterName;
285 QString outputChildId;
286 QString outputName;
287 QString expression;
288 QString expressionText;
289
290 in >> sourceType >> staticValue >> parameterName >> outputChildId >> outputName >> expression >> expressionText;
291
292 source.setStaticValue( staticValue );
293 source.setParameterName( parameterName );
294 source.setOutputChildId( outputChildId );
295 source.setOutputName( outputName );
296 source.setExpression( expression );
297 source.setExpressionText( expressionText );
298 source.setSource( static_cast<QgsProcessingModelChildParameterSource::Source>( sourceType ) );
299 return in;
300}
301
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.
Definition: qgsprocessing.h:63
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.
Definition: qgsfeature.cpp:416
QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:433
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)