Skip to content

Commit cb6c6f3

Browse files
authoredJul 7, 2017
Merge pull request #4812 from nyalldawson/model_exp_input
[processing] Allow model child parameters to take values from an expression
2 parents 52f4c5e + 515ba24 commit cb6c6f3

31 files changed

+1276
-471
lines changed
 

‎doc/api_break.dox

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,8 @@ version instead.
11161116
- QgsExpression::Node::referencedColumns() returns QSet<QString> instead of QStringList
11171117
- `QgsExpression::Node` was renamed to `QgsExpressionNode`
11181118
- `QgsExpression::Function` was renamed to `QgsExpressionFunction`
1119+
- variableHelpText() no longer returns a formatted HTML string. It now just returns the plain text help string. Use formatVariableHelp()
1120+
to obtain the formatted version.
11191121

11201122

11211123
QgsExpression::Function {#qgis_api_break_3_0_QgsExpression_Function}

‎python/core/expression/qgsexpression.sip

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,11 @@ return index of the function in Functions array
464464
Returns the help text for a specified function.
465465
\param name function name
466466
.. seealso:: variableHelpText()
467+
.. seealso:: formatVariableHelp()
467468
:rtype: str
468469
%End
469470

470-
static QString variableHelpText( const QString &variableName, bool showValue = true, const QVariant &value = QVariant() );
471+
static QString variableHelpText( const QString &variableName );
471472
%Docstring
472473
Returns the help text for a specified variable.
473474
\param variableName name of variable
@@ -478,6 +479,18 @@ return index of the function in Functions array
478479
:rtype: str
479480
%End
480481

482+
static QString formatVariableHelp( const QString &description, bool showValue = true, const QVariant &value = QVariant() );
483+
%Docstring
484+
Returns formatted help text for a variable.
485+
\param description translated description of variable
486+
\param showValue set to true to include current value of variable in help text
487+
\param value current value of variable to show in help text
488+
.. seealso:: helpText()
489+
.. seealso:: variableHelpText()
490+
.. versionadded:: 3.0
491+
:rtype: str
492+
%End
493+
481494
static QString group( const QString &group );
482495
%Docstring
483496
Returns the translated name for a function group.

‎python/core/processing/qgsprocessingmodelalgorithm.sip

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
3939
ModelParameter,
4040
ChildOutput,
4141
StaticValue,
42+
Expression,
4243
};
4344

4445
ChildParameterSource();
4546
%Docstring
4647
Constructor for ChildParameterSource. It is recommended that the static methods
47-
fromStaticValue(), fromModelParameter() and fromChildOutput() are used instead.
48+
fromStaticValue(), fromModelParameter(), fromChildOutput() and fromExpression() are used instead.
4849
%End
4950

5051
bool operator==( const QgsProcessingModelAlgorithm::ChildParameterSource &other ) const;
@@ -58,6 +59,7 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
5859
Returns a new ChildParameterSource which takes its value from a static ``value``.
5960
.. seealso:: fromModelParameter()
6061
.. seealso:: fromChildOutput()
62+
.. seealso:: fromExpression()
6163
:rtype: QgsProcessingModelAlgorithm.ChildParameterSource
6264
%End
6365

@@ -66,13 +68,27 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
6668
Returns a new ChildParameterSource which takes its value from a parent model parameter.
6769
.. seealso:: fromStaticValue()
6870
.. seealso:: fromChildOutput()
71+
.. seealso:: fromExpression()
6972
:rtype: QgsProcessingModelAlgorithm.ChildParameterSource
7073
%End
7174

7275
static QgsProcessingModelAlgorithm::ChildParameterSource fromChildOutput( const QString &childId, const QString &outputName );
7376
%Docstring
7477
Returns a new ChildParameterSource which takes its value from an output generated by a child algorithm.
7578
.. seealso:: fromStaticValue()
79+
.. seealso:: fromModelParameter()
80+
.. seealso:: fromExpression()
81+
:rtype: QgsProcessingModelAlgorithm.ChildParameterSource
82+
%End
83+
84+
static QgsProcessingModelAlgorithm::ChildParameterSource fromExpression( const QString &expression );
85+
%Docstring
86+
Returns a new ChildParameterSource which takes its value from an expression. The expression
87+
is evaluated just before the child algorithm executes, and can use functions available
88+
in its expression context to include results calculated from the child algorithms already
89+
executed by the model.
90+
.. seealso:: fromStaticValue()
91+
.. seealso:: fromChildOutput()
7692
.. seealso:: fromModelParameter()
7793
:rtype: QgsProcessingModelAlgorithm.ChildParameterSource
7894
%End
@@ -137,6 +153,22 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
137153
Sets the source's child algorithm output ``name`` from which the output value will be taken. Calling this will also change the source() to ChildOutput.
138154
.. seealso:: outputName()
139155
.. seealso:: setOutputChildId()
156+
%End
157+
158+
QString expression() const;
159+
%Docstring
160+
Returns the source's expression. This is only used when the source() is Expression.
161+
.. seealso:: setExpression()
162+
:rtype: str
163+
%End
164+
165+
void setExpression( const QString &expression );
166+
%Docstring
167+
Sets the source's expression. Calling this will also change the source() to Expression.
168+
The expression is evaluated just before the child algorithm executes, and can use functions available
169+
in its expression context to include results calculated from the child algorithms already
170+
executed by the model.
171+
.. seealso:: expression()
140172
%End
141173

142174
QVariant toVariant() const;
@@ -833,6 +865,77 @@ Copies are protected to avoid slicing
833865
:rtype: str
834866
%End
835867

868+
QList< QgsProcessingModelAlgorithm::ChildParameterSource > availableSourcesForChild( const QString &childId, const QStringList &parameterTypes = QStringList(),
869+
const QStringList &outputTypes = QStringList(), const QList< int > dataTypes = QList< int >() ) const;
870+
%Docstring
871+
Returns a list of possible sources which can be used for the parameters for a child
872+
algorithm in the model. Returned sources are those which match either one of the
873+
specified ``parameterTypes`` (see QgsProcessingParameterDefinition.type() ) or
874+
on of the specified ``outputTypes`` (see QgsProcessingOutputDefinition.type() ).
875+
If specified, an optional list of ``dataTypes`` can be used to filter the returned
876+
sources to those with compatible data types for the parameter/outputs.
877+
:rtype: list of QgsProcessingModelAlgorithm.ChildParameterSource
878+
%End
879+
880+
class VariableDefinition
881+
{
882+
%Docstring
883+
Definition of a expression context variable available during model execution.
884+
.. versionadded:: 3.0
885+
%End
886+
887+
%TypeHeaderCode
888+
#include "qgsprocessingmodelalgorithm.h"
889+
%End
890+
public:
891+
892+
VariableDefinition( const QVariant &value = QVariant(), const QgsProcessingModelAlgorithm::ChildParameterSource &source = QgsProcessingModelAlgorithm::ChildParameterSource::fromStaticValue( QVariant() ), const QString &description = QString() );
893+
%Docstring
894+
Constructor for a new VariableDefinition with the specified ``value`` and original
895+
parameter ``source``, and ``description``.
896+
%End
897+
898+
QVariant value;
899+
%Docstring
900+
Value of variable
901+
%End
902+
903+
QgsProcessingModelAlgorithm::ChildParameterSource source;
904+
%Docstring
905+
Original source of variable's value
906+
%End
907+
908+
QString description;
909+
%Docstring
910+
Translated description of variable
911+
%End
912+
};
913+
914+
QMap< QString, QgsProcessingModelAlgorithm::VariableDefinition > variablesForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
915+
const QVariantMap &results = QVariantMap() ) const;
916+
%Docstring
917+
Returns a map of variable name to variable definition for expression context variables which are available
918+
for use by child algorithm during model execution.
919+
920+
The child algorithm ``childId`` and processing ``context``
921+
are manadatory. If ``modelParameters`` and ``results`` are not specified, then only the variable names and sources
922+
will be returned, but all variable values will be null. This can be used to determine in advance which variables
923+
will be available for a specific child algorithm, e.g. for use in expression builder widgets.
924+
925+
In order to calculate the actual variable value, the input model ``modelParameters`` and already executed child
926+
algorithm ``results`` must be passed.
927+
.. seealso:: createExpressionContextScopeForChildAlgorithm()
928+
:rtype: QMap< str, QgsProcessingModelAlgorithm.VariableDefinition >
929+
%End
930+
931+
QgsExpressionContextScope *createExpressionContextScopeForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
932+
const QVariantMap &results = QVariantMap() ) const /Factory/;
933+
%Docstring
934+
Creates a new expression context scope for a child algorithm within the model.
935+
.. seealso:: variablesForChildAlgorithm()
936+
:rtype: QgsExpressionContextScope
937+
%End
938+
836939
protected:
837940

838941
virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );

‎python/core/processing/qgsprocessingoutputs.sip

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ class QgsProcessingOutputDefinition
2828
%End
2929

3030
%ConvertToSubClassCode
31-
if ( sipCpp->type() == "outputVector" )
31+
if ( sipCpp->type() == QgsProcessingOutputVectorLayer::typeName() )
3232
sipType = sipType_QgsProcessingOutputVectorLayer;
33-
else if ( sipCpp->type() == "outputRaster" )
33+
else if ( sipCpp->type() == QgsProcessingOutputRasterLayer::typeName() )
3434
sipType = sipType_QgsProcessingOutputRasterLayer;
35-
else if ( sipCpp->type() == "outputHtml" )
35+
else if ( sipCpp->type() == QgsProcessingOutputHtml::typeName() )
3636
sipType = sipType_QgsProcessingOutputHtml;
37-
else if ( sipCpp->type() == "outputNumber" )
37+
else if ( sipCpp->type() == QgsProcessingOutputNumber::typeName() )
3838
sipType = sipType_QgsProcessingOutputNumber;
39-
else if ( sipCpp->type() == "outputString" )
39+
else if ( sipCpp->type() == QgsProcessingOutputString::typeName() )
4040
sipType = sipType_QgsProcessingOutputString;
41-
else if ( sipCpp->type() == "outputFolder" )
41+
else if ( sipCpp->type() == QgsProcessingOutputFolder::typeName() )
4242
sipType = sipType_QgsProcessingOutputFolder;
4343
%End
4444
public:
@@ -111,6 +111,11 @@ class QgsProcessingOutputVectorLayer : QgsProcessingOutputDefinition
111111
Constructor for QgsProcessingOutputVectorLayer.
112112
%End
113113

114+
static QString typeName();
115+
%Docstring
116+
Returns the type name for the output class.
117+
:rtype: str
118+
%End
114119
virtual QString type() const;
115120

116121
QgsProcessingParameterDefinition::LayerType dataType() const;
@@ -145,7 +150,14 @@ class QgsProcessingOutputRasterLayer : QgsProcessingOutputDefinition
145150
Constructor for QgsProcessingOutputRasterLayer.
146151
%End
147152

153+
static QString typeName();
154+
%Docstring
155+
Returns the type name for the output class.
156+
:rtype: str
157+
%End
148158
virtual QString type() const;
159+
160+
149161
};
150162

151163
class QgsProcessingOutputHtml : QgsProcessingOutputDefinition
@@ -165,7 +177,13 @@ class QgsProcessingOutputHtml : QgsProcessingOutputDefinition
165177
Constructor for QgsProcessingOutputHtml.
166178
%End
167179

180+
static QString typeName();
181+
%Docstring
182+
Returns the type name for the output class.
183+
:rtype: str
184+
%End
168185
virtual QString type() const;
186+
169187
};
170188

171189
class QgsProcessingOutputNumber : QgsProcessingOutputDefinition
@@ -185,6 +203,11 @@ class QgsProcessingOutputNumber : QgsProcessingOutputDefinition
185203
Constructor for QgsProcessingOutputNumber.
186204
%End
187205

206+
static QString typeName();
207+
%Docstring
208+
Returns the type name for the output class.
209+
:rtype: str
210+
%End
188211
virtual QString type() const;
189212
};
190213

@@ -205,7 +228,13 @@ class QgsProcessingOutputString : QgsProcessingOutputDefinition
205228
Constructor for QgsProcessingOutputString.
206229
%End
207230

231+
static QString typeName();
232+
%Docstring
233+
Returns the type name for the output class.
234+
:rtype: str
235+
%End
208236
virtual QString type() const;
237+
209238
};
210239

211240
class QgsProcessingOutputFolder : QgsProcessingOutputDefinition
@@ -220,12 +249,19 @@ class QgsProcessingOutputFolder : QgsProcessingOutputDefinition
220249
%End
221250
public:
222251

252+
223253
QgsProcessingOutputFolder( const QString &name, const QString &description = QString() );
224254
%Docstring
225255
Constructor for QgsProcessingOutputFolder.
226256
%End
227257

258+
static QString typeName();
259+
%Docstring
260+
Returns the type name for the output class.
261+
:rtype: str
262+
%End
228263
virtual QString type() const;
264+
229265
};
230266

231267

0 commit comments

Comments
 (0)
Please sign in to comment.