QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
25
26bool QgsProcessingModelChildParameterSource::operator==( const QgsProcessingModelChildParameterSource &other ) const
27{
28 if ( mSource != other.mSource )
29 return false;
30
31 switch ( mSource )
32 {
34 return mStaticValue == other.mStaticValue;
36 return mChildId == other.mChildId && mOutputName == other.mOutputName;
38 return mParameterName == other.mParameterName;
40 return mExpression == other.mExpression;
42 return mExpressionText == other.mExpressionText;
44 return true;
45 }
46 return false;
47}
48
49QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromStaticValue( const QVariant &value )
50{
51 QgsProcessingModelChildParameterSource src;
53 src.mStaticValue = value;
54 return src;
55}
56
57QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromModelParameter( const QString &parameterName )
58{
59 QgsProcessingModelChildParameterSource src;
61 src.mParameterName = parameterName;
62 return src;
63}
64
65QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromChildOutput( const QString &childId, const QString &outputName )
66{
67 QgsProcessingModelChildParameterSource src;
69 src.mChildId = childId;
70 src.mOutputName = outputName;
71 return src;
72}
73
74QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpression( const QString &expression )
75{
76 QgsProcessingModelChildParameterSource src;
78 src.mExpression = expression;
79 return src;
80}
81
82QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpressionText( const QString &text )
83{
84 QgsProcessingModelChildParameterSource src;
86 src.mExpressionText = text;
87 return src;
88}
89
90Qgis::ProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::source() const
91{
92 return mSource;
93}
94
95void QgsProcessingModelChildParameterSource::setSource( Qgis::ProcessingModelChildParameterSource source )
96{
97 mSource = source;
98}
99
100QVariant QgsProcessingModelChildParameterSource::toVariant() const
101{
102 QVariantMap map;
103 map.insert( QStringLiteral( "source" ), static_cast< int >( mSource ) );
104 switch ( mSource )
105 {
107 map.insert( QStringLiteral( "parameter_name" ), mParameterName );
108 break;
109
111 map.insert( QStringLiteral( "child_id" ), mChildId );
112 map.insert( QStringLiteral( "output_name" ), mOutputName );
113 break;
114
116 map.insert( QStringLiteral( "static_value" ), mStaticValue );
117 break;
118
120 map.insert( QStringLiteral( "expression" ), mExpression );
121 break;
122
124 map.insert( QStringLiteral( "expression_text" ), mExpressionText );
125 break;
126
128 break;
129 }
130 return map;
131}
132
133bool QgsProcessingModelChildParameterSource::loadVariant( const QVariantMap &map )
134{
135 mSource = static_cast< Qgis::ProcessingModelChildParameterSource >( map.value( QStringLiteral( "source" ) ).toInt() );
136 switch ( mSource )
137 {
139 mParameterName = map.value( QStringLiteral( "parameter_name" ) ).toString();
140 break;
141
143 mChildId = map.value( QStringLiteral( "child_id" ) ).toString();
144 mOutputName = map.value( QStringLiteral( "output_name" ) ).toString();
145 break;
146
148 mStaticValue = map.value( QStringLiteral( "static_value" ) );
149 break;
150
152 mExpression = map.value( QStringLiteral( "expression" ) ).toString();
153 break;
154
156 mExpressionText = map.value( QStringLiteral( "expression_text" ) ).toString();
157 break;
158
160 break;
161 }
162 return true;
163}
164
165QString QgsProcessingModelChildParameterSource::asPythonCode( const QgsProcessing::PythonOutputType, const QgsProcessingParameterDefinition *definition, const QMap< QString, QString > &friendlyChildNames ) const
166{
167 switch ( mSource )
168 {
170 return QStringLiteral( "parameters['%1']" ).arg( mParameterName );
171
173 return QStringLiteral( "outputs['%1']['%2']" ).arg( friendlyChildNames.value( mChildId, mChildId ), mOutputName );
174
176 if ( definition )
177 {
179 return definition->valueAsPythonString( mStaticValue, c );
180 }
181 else
182 {
183 return QgsProcessingUtils::variantToPythonLiteral( mStaticValue );
184 }
185
187 return QStringLiteral( "QgsExpression(%1).evaluate()" ).arg( QgsProcessingUtils::stringToPythonLiteral( mExpression ) );
188
190 return mExpressionText;
191
193 return QString();
194 }
195 return QString();
196}
197
198QString QgsProcessingModelChildParameterSource::asPythonComment( const QgsProcessingParameterDefinition *definition ) const
199{
200 switch ( mSource )
201 {
207 return QString();
208
210 if ( definition )
211 {
213 return definition->valueAsPythonComment( mStaticValue, c );
214 }
215 else
216 {
217 return QString();
218 }
219 }
220 return QString();
221}
222
223QString QgsProcessingModelChildParameterSource::friendlyIdentifier( QgsProcessingModelAlgorithm *model ) const
224{
225 switch ( mSource )
226 {
228 {
229 if ( model )
230 {
231 if ( const QgsProcessingParameterDefinition *paramDefinition = model->parameterDefinition( mParameterName ) )
232 {
233 // A model can be valid (non null) but the parameter may not exist yet (if input has not yet been set)
234 return paramDefinition->description();
235 }
236 }
237
238 return mParameterName;
239 }
241 {
242 if ( model )
243 {
244 const QgsProcessingModelChildAlgorithm &alg = model->childAlgorithm( mChildId );
245 QString outputName = alg.algorithm() && alg.algorithm()->outputDefinition( mOutputName ) ? alg.algorithm()->outputDefinition( mOutputName )->description() : mOutputName;
246 // see if this output has been named by the model designer -- if so, we use that friendly name
247 const QMap<QString, QgsProcessingModelOutput> outputs = alg.modelOutputs();
248 for ( auto it = outputs.constBegin(); it != outputs.constEnd(); ++it )
249 {
250 if ( it.value().childOutputName() == mOutputName )
251 {
252 outputName = it.key();
253 break;
254 }
255 }
256 return QObject::tr( "'%1' from algorithm '%2'" ).arg( outputName, alg.description() );
257 }
258 else
259 {
260 return QObject::tr( "'%1' from algorithm '%2'" ).arg( mOutputName, mChildId );
261 }
262 }
263
265 return mStaticValue.toString();
266
268 return mExpression;
269
271 return mExpressionText;
272
274 return QString();
275 }
276 return QString();
277}
278
279QDataStream &operator<<( QDataStream &out, const QgsProcessingModelChildParameterSource &source )
280{
281 out << static_cast< int >( source.source() );
282 out << source.staticValue();
283 out << source.parameterName();
284 out << source.outputChildId();
285 out << source.outputName();
286 out << source.expression();
287 out << source.expressionText();
288 return out;
289}
290
291QDataStream &operator>>( QDataStream &in, QgsProcessingModelChildParameterSource &source )
292{
293 int sourceType;
294 QVariant staticValue;
295 QString parameterName;
296 QString outputChildId;
297 QString outputName;
298 QString expression;
299 QString expressionText;
300
301 in >> sourceType >> staticValue >> parameterName >> outputChildId >> outputName >> expression >> expressionText;
302
303 source.setStaticValue( staticValue );
304 source.setParameterName( parameterName );
305 source.setOutputChildId( outputChildId );
306 source.setOutputName( outputName );
307 source.setExpression( expression );
308 source.setExpressionText( expressionText );
309 source.setSource( static_cast<Qgis::ProcessingModelChildParameterSource>( sourceType ) );
310 return in;
311}
312
ProcessingModelChildParameterSource
Processing model child parameter sources.
Definition qgis.h:3847
@ ExpressionText
Parameter value is taken from a text with expressions, evaluated just before the algorithm runs.
Definition qgis.h:3852
@ ModelOutput
Parameter value is linked to an output parameter for the model.
Definition qgis.h:3853
@ ChildOutput
Parameter value is taken from an output generated by a child algorithm.
Definition qgis.h:3849
@ ModelParameter
Parameter value is taken from a parent model parameter.
Definition qgis.h:3848
@ StaticValue
Parameter value is a static value.
Definition qgis.h:3850
@ Expression
Parameter value is taken from an expression, evaluated just before the algorithm runs.
Definition qgis.h:3851
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.