Skip to content

Commit 4f4844f

Browse files
committedApr 26, 2018
Add a new source type for parameters
1 parent 4168c05 commit 4f4844f

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed
 

‎python/core/processing/models/qgsprocessingmodelchildparametersource.sip.in

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Source for the value of a parameter for a child algorithm within a model.
2929
ChildOutput,
3030
StaticValue,
3131
Expression,
32+
ExpressionText,
3233
};
3334

3435
QgsProcessingModelChildParameterSource();
@@ -49,6 +50,8 @@ Returns a new QgsProcessingModelChildParameterSource which takes its value from
4950
.. seealso:: :py:func:`fromChildOutput`
5051

5152
.. seealso:: :py:func:`fromExpression`
53+
54+
.. seealso:: :py:func:`fromExpressionText`
5255
%End
5356

5457
static QgsProcessingModelChildParameterSource fromModelParameter( const QString &parameterName );
@@ -60,6 +63,8 @@ Returns a new QgsProcessingModelChildParameterSource which takes its value from
6063
.. seealso:: :py:func:`fromChildOutput`
6164

6265
.. seealso:: :py:func:`fromExpression`
66+
67+
.. seealso:: :py:func:`fromExpressionText`
6368
%End
6469

6570
static QgsProcessingModelChildParameterSource fromChildOutput( const QString &childId, const QString &outputName );
@@ -71,6 +76,8 @@ Returns a new QgsProcessingModelChildParameterSource which takes its value from
7176
.. seealso:: :py:func:`fromModelParameter`
7277

7378
.. seealso:: :py:func:`fromExpression`
79+
80+
.. seealso:: :py:func:`fromExpressionText`
7481
%End
7582

7683
static QgsProcessingModelChildParameterSource fromExpression( const QString &expression );
@@ -85,6 +92,29 @@ executed by the model.
8592
.. seealso:: :py:func:`fromChildOutput`
8693

8794
.. seealso:: :py:func:`fromModelParameter`
95+
96+
.. seealso:: :py:func:`fromExpressionText`
97+
98+
.. versionadded:: 3.2
99+
%End
100+
101+
static QgsProcessingModelChildParameterSource fromExpressionText( const QString &text );
102+
%Docstring
103+
Returns a new QgsProcessingModelChildParameterSource which takes its
104+
value from a text with expressions. Expressions are evaluated just before
105+
the child algorithm executes, and can use functions available
106+
in its expression context to include results calculated from the child
107+
algorithms already executed by the model.
108+
109+
.. seealso:: :py:func:`fromStaticValue`
110+
111+
.. seealso:: :py:func:`fromChildOutput`
112+
113+
.. seealso:: :py:func:`fromModelParameter`
114+
115+
.. seealso:: :py:func:`fromExpression`
116+
117+
.. versionadded:: 3.2
88118
%End
89119

90120
Source source() const;
@@ -171,6 +201,29 @@ in its expression context to include results calculated from the child algorithm
171201
executed by the model.
172202

173203
.. seealso:: :py:func:`expression`
204+
%End
205+
206+
QString expressionText() const;
207+
%Docstring
208+
Returns the source's text with expressions. This is only used when the
209+
source() is ExpressionText.
210+
211+
.. seealso:: :py:func:`setExpressionText`
212+
213+
.. versionadded:: 3.2
214+
%End
215+
216+
void setExpressionText( const QString &text );
217+
%Docstring
218+
Sets the source's text containing expressions. Calling this will also
219+
change the source() to ExpressionText. Expressions are evaluated just
220+
before the child algorithm executes, and can use functions available
221+
in its expression context to include results calculated from the child
222+
algorithms already executed by the model.
223+
224+
.. seealso:: :py:func:`expressionText`
225+
226+
.. versionadded:: 3.2
174227
%End
175228

176229
QVariant toVariant() const;

‎src/core/processing/models/qgsprocessingmodelalgorithm.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const QgsP
9696

9797
QgsProcessingModelChildParameterSources paramSources = child.parameterSources().value( def->name() );
9898

99+
QString expressionText;
99100
QVariantList paramParts;
100101
Q_FOREACH ( const QgsProcessingModelChildParameterSource &source, paramSources )
101102
{
@@ -122,9 +123,19 @@ QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const QgsP
122123
paramParts << exp.evaluate( &expressionContext );
123124
break;
124125
}
126+
case QgsProcessingModelChildParameterSource::ExpressionText:
127+
{
128+
expressionText = QgsExpression::replaceExpressionText( source.expressionText(), &expressionContext );
129+
break;
130+
}
125131
}
126132
}
127-
if ( paramParts.count() == 1 )
133+
134+
if ( ! expressionText.isEmpty() )
135+
{
136+
childParams.insert( def->name(), expressionText );
137+
}
138+
else if ( paramParts.count() == 1 )
128139
childParams.insert( def->name(), paramParts.at( 0 ) );
129140
else
130141
childParams.insert( def->name(), paramParts );
@@ -464,6 +475,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
464475
}
465476

466477
case QgsProcessingModelChildParameterSource::Expression:
478+
case QgsProcessingModelChildParameterSource::ExpressionText:
467479
case QgsProcessingModelChildParameterSource::StaticValue:
468480
continue;
469481
};
@@ -508,6 +520,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
508520
}
509521

510522
case QgsProcessingModelChildParameterSource::Expression:
523+
case QgsProcessingModelChildParameterSource::ExpressionText:
511524
case QgsProcessingModelChildParameterSource::StaticValue:
512525
continue;
513526

@@ -555,6 +568,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
555568
}
556569

557570
case QgsProcessingModelChildParameterSource::Expression:
571+
case QgsProcessingModelChildParameterSource::ExpressionText:
558572
case QgsProcessingModelChildParameterSource::StaticValue:
559573
continue;
560574

‎src/core/processing/models/qgsprocessingmodelchildparametersource.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ bool QgsProcessingModelChildParameterSource::operator==( const QgsProcessingMode
3434
return mParameterName == other.mParameterName;
3535
case Expression:
3636
return mExpression == other.mExpression;
37+
case ExpressionText:
38+
return mExpressionText == other.mExpressionText;
3739
}
3840
return false;
3941
}
@@ -71,6 +73,14 @@ QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::f
7173
return src;
7274
}
7375

76+
QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpressionText( const QString &text )
77+
{
78+
QgsProcessingModelChildParameterSource src;
79+
src.mSource = ExpressionText;
80+
src.mExpressionText = text;
81+
return src;
82+
}
83+
7484
QgsProcessingModelChildParameterSource::Source QgsProcessingModelChildParameterSource::source() const
7585
{
7686
return mSource;
@@ -98,6 +108,10 @@ QVariant QgsProcessingModelChildParameterSource::toVariant() const
98108
case Expression:
99109
map.insert( QStringLiteral( "expression" ), mExpression );
100110
break;
111+
112+
case ExpressionText:
113+
map.insert( QStringLiteral( "expression_text" ), mExpressionText );
114+
break;
101115
}
102116
return map;
103117
}
@@ -123,6 +137,10 @@ bool QgsProcessingModelChildParameterSource::loadVariant( const QVariantMap &map
123137
case Expression:
124138
mExpression = map.value( QStringLiteral( "expression" ) ).toString();
125139
break;
140+
141+
case ExpressionText:
142+
mExpressionText = map.value( QStringLiteral( "expression_text" ) ).toString();
143+
break;
126144
}
127145
return true;
128146
}
@@ -142,6 +160,9 @@ QString QgsProcessingModelChildParameterSource::asPythonCode() const
142160

143161
case Expression:
144162
return QStringLiteral( "QgsExpression('%1').evaluate()" ).arg( mExpression );
163+
164+
case ExpressionText:
165+
return mExpressionText;
145166
}
146167
return QString();
147168
}

‎src/core/processing/models/qgsprocessingmodelchildparametersource.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
3939
ChildOutput, //!< Parameter value is taken from an output generated by a child algorithm
4040
StaticValue, //!< Parameter value is a static value
4141
Expression, //!< Parameter value is taken from an expression, evaluated just before the algorithm runs
42+
ExpressionText, //!< Parameter value is taken from a text with expressions, evaluated just before the algorithm runs
4243
};
4344

4445
/**
@@ -58,6 +59,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
5859
* \see fromModelParameter()
5960
* \see fromChildOutput()
6061
* \see fromExpression()
62+
* \see fromExpressionText()
6163
*/
6264
static QgsProcessingModelChildParameterSource fromStaticValue( const QVariant &value );
6365

@@ -66,6 +68,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
6668
* \see fromStaticValue()
6769
* \see fromChildOutput()
6870
* \see fromExpression()
71+
* \see fromExpressionText()
6972
*/
7073
static QgsProcessingModelChildParameterSource fromModelParameter( const QString &parameterName );
7174

@@ -74,6 +77,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
7477
* \see fromStaticValue()
7578
* \see fromModelParameter()
7679
* \see fromExpression()
80+
* \see fromExpressionText()
7781
*/
7882
static QgsProcessingModelChildParameterSource fromChildOutput( const QString &childId, const QString &outputName );
7983

@@ -85,9 +89,25 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
8589
* \see fromStaticValue()
8690
* \see fromChildOutput()
8791
* \see fromModelParameter()
92+
* \see fromExpressionText()
93+
* \since QGIS 3.2
8894
*/
8995
static QgsProcessingModelChildParameterSource fromExpression( const QString &expression );
9096

97+
/**
98+
* Returns a new QgsProcessingModelChildParameterSource which takes its
99+
* value from a text with expressions. Expressions are evaluated just before
100+
* the child algorithm executes, and can use functions available
101+
* in its expression context to include results calculated from the child
102+
* algorithms already executed by the model.
103+
* \see fromStaticValue()
104+
* \see fromChildOutput()
105+
* \see fromModelParameter()
106+
* \see fromExpression()
107+
* \since QGIS 3.2
108+
*/
109+
static QgsProcessingModelChildParameterSource fromExpressionText( const QString &text );
110+
91111
/**
92112
* Returns the parameter value's source.
93113
*/
@@ -160,6 +180,25 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
160180
*/
161181
void setExpression( const QString &expression ) { mExpression = expression; mSource = Expression; }
162182

183+
/**
184+
* Returns the source's text with expressions. This is only used when the
185+
* source() is ExpressionText.
186+
* \see setExpressionText()
187+
* \since QGIS 3.2
188+
*/
189+
QString expressionText() const { return mExpressionText; }
190+
191+
/**
192+
* Sets the source's text containing expressions. Calling this will also
193+
* change the source() to ExpressionText. Expressions are evaluated just
194+
* before the child algorithm executes, and can use functions available
195+
* in its expression context to include results calculated from the child
196+
* algorithms already executed by the model.
197+
* \see expressionText()
198+
* \since QGIS 3.2
199+
*/
200+
void setExpressionText( const QString &text ) { mExpressionText = text; mSource = ExpressionText; }
201+
163202
/**
164203
* Saves this source to a QVariant.
165204
* \see loadVariant()
@@ -185,6 +224,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
185224
QString mChildId;
186225
QString mOutputName;
187226
QString mExpression;
227+
QString mExpressionText;
188228

189229
};
190230

0 commit comments

Comments
 (0)
Please sign in to comment.