Skip to content
Permalink
Browse files

Merge pull request #4812 from nyalldawson/model_exp_input

[processing] Allow model child parameters to take values from an expression
  • Loading branch information
nyalldawson committed Jul 7, 2017
2 parents 52f4c5e + 515ba24 commit cb6c6f39a24193c6371a1cdd20935596aad8c50e
Showing with 1,276 additions and 471 deletions.
  1. +2 −0 doc/api_break.dox
  2. +14 −1 python/core/expression/qgsexpression.sip
  3. +104 −1 python/core/processing/qgsprocessingmodelalgorithm.sip
  4. +42 −6 python/core/processing/qgsprocessingoutputs.sip
  5. +132 −22 python/core/processing/qgsprocessingparameters.sip
  6. +27 −1 python/core/qgsexpressioncontext.sip
  7. +0 −21 python/plugins/processing/core/parameters.py
  8. +3 −3 python/plugins/processing/gui/HelpEditionDialog.py
  9. +17 −63 python/plugins/processing/gui/NumberInputPanel.py
  10. +1 −1 python/plugins/processing/gui/wrappers.py
  11. +2 −3 python/plugins/processing/modeler/AddModelFromFileAction.py
  12. +2 −3 python/plugins/processing/modeler/DeleteModelAction.py
  13. +2 −3 python/plugins/processing/modeler/EditModelAction.py
  14. +0 −178 python/plugins/processing/modeler/ModelerAlgorithm.py
  15. +2 −2 python/plugins/processing/modeler/ModelerAlgorithmProvider.py
  16. +2 −3 python/plugins/processing/modeler/ModelerDialog.py
  17. +13 −45 python/plugins/processing/modeler/ModelerParametersDialog.py
  18. +13 −8 python/plugins/processing/modeler/ModelerScene.py
  19. +10 −6 python/plugins/processing/tools/dataobjects.py
  20. +7 −2 src/core/expression/qgsexpression.cpp
  21. +13 −1 src/core/expression/qgsexpression.h
  22. +303 −4 src/core/processing/qgsprocessingmodelalgorithm.cpp
  23. +97 −2 src/core/processing/qgsprocessingmodelalgorithm.h
  24. +42 −12 src/core/processing/qgsprocessingoutputs.h
  25. +22 −22 src/core/processing/qgsprocessingparameters.cpp
  26. +132 −44 src/core/processing/qgsprocessingparameters.h
  27. +11 −0 src/core/qgsexpressioncontext.cpp
  28. +24 −1 src/core/qgsexpressioncontext.h
  29. +1 −1 src/gui/qgsexpressionbuilderwidget.cpp
  30. +27 −2 tests/src/core/testqgsexpressioncontext.cpp
  31. +209 −10 tests/src/core/testqgsprocessing.cpp
@@ -1116,6 +1116,8 @@ version instead.
- QgsExpression::Node::referencedColumns() returns QSet<QString> instead of QStringList
- `QgsExpression::Node` was renamed to `QgsExpressionNode`
- `QgsExpression::Function` was renamed to `QgsExpressionFunction`
- variableHelpText() no longer returns a formatted HTML string. It now just returns the plain text help string. Use formatVariableHelp()
to obtain the formatted version.


QgsExpression::Function {#qgis_api_break_3_0_QgsExpression_Function}
@@ -464,10 +464,11 @@ return index of the function in Functions array
Returns the help text for a specified function.
\param name function name
.. seealso:: variableHelpText()
.. seealso:: formatVariableHelp()
:rtype: str
%End

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

static QString formatVariableHelp( const QString &description, bool showValue = true, const QVariant &value = QVariant() );
%Docstring
Returns formatted help text for a variable.
\param description translated description of variable
\param showValue set to true to include current value of variable in help text
\param value current value of variable to show in help text
.. seealso:: helpText()
.. seealso:: variableHelpText()
.. versionadded:: 3.0
:rtype: str
%End

static QString group( const QString &group );
%Docstring
Returns the translated name for a function group.
@@ -39,12 +39,13 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
ModelParameter,
ChildOutput,
StaticValue,
Expression,
};

ChildParameterSource();
%Docstring
Constructor for ChildParameterSource. It is recommended that the static methods
fromStaticValue(), fromModelParameter() and fromChildOutput() are used instead.
fromStaticValue(), fromModelParameter(), fromChildOutput() and fromExpression() are used instead.
%End

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

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

static QgsProcessingModelAlgorithm::ChildParameterSource fromChildOutput( const QString &childId, const QString &outputName );
%Docstring
Returns a new ChildParameterSource which takes its value from an output generated by a child algorithm.
.. seealso:: fromStaticValue()
.. seealso:: fromModelParameter()
.. seealso:: fromExpression()
:rtype: QgsProcessingModelAlgorithm.ChildParameterSource
%End

static QgsProcessingModelAlgorithm::ChildParameterSource fromExpression( const QString &expression );
%Docstring
Returns a new ChildParameterSource which takes its value from an expression. The expression
is evaluated just before the child algorithm executes, and can use functions available
in its expression context to include results calculated from the child algorithms already
executed by the model.
.. seealso:: fromStaticValue()
.. seealso:: fromChildOutput()
.. seealso:: fromModelParameter()
:rtype: QgsProcessingModelAlgorithm.ChildParameterSource
%End
@@ -137,6 +153,22 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
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.
.. seealso:: outputName()
.. seealso:: setOutputChildId()
%End

QString expression() const;
%Docstring
Returns the source's expression. This is only used when the source() is Expression.
.. seealso:: setExpression()
:rtype: str
%End

void setExpression( const QString &expression );
%Docstring
Sets the source's expression. Calling this will also change the source() to Expression.
The expression is evaluated just before the child algorithm executes, and can use functions available
in its expression context to include results calculated from the child algorithms already
executed by the model.
.. seealso:: expression()
%End

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

QList< QgsProcessingModelAlgorithm::ChildParameterSource > availableSourcesForChild( const QString &childId, const QStringList &parameterTypes = QStringList(),
const QStringList &outputTypes = QStringList(), const QList< int > dataTypes = QList< int >() ) const;
%Docstring
Returns a list of possible sources which can be used for the parameters for a child
algorithm in the model. Returned sources are those which match either one of the
specified ``parameterTypes`` (see QgsProcessingParameterDefinition.type() ) or
on of the specified ``outputTypes`` (see QgsProcessingOutputDefinition.type() ).
If specified, an optional list of ``dataTypes`` can be used to filter the returned
sources to those with compatible data types for the parameter/outputs.
:rtype: list of QgsProcessingModelAlgorithm.ChildParameterSource
%End

class VariableDefinition
{
%Docstring
Definition of a expression context variable available during model execution.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingmodelalgorithm.h"
%End
public:

VariableDefinition( const QVariant &value = QVariant(), const QgsProcessingModelAlgorithm::ChildParameterSource &source = QgsProcessingModelAlgorithm::ChildParameterSource::fromStaticValue( QVariant() ), const QString &description = QString() );
%Docstring
Constructor for a new VariableDefinition with the specified ``value`` and original
parameter ``source``, and ``description``.
%End

QVariant value;
%Docstring
Value of variable
%End

QgsProcessingModelAlgorithm::ChildParameterSource source;
%Docstring
Original source of variable's value
%End

QString description;
%Docstring
Translated description of variable
%End
};

QMap< QString, QgsProcessingModelAlgorithm::VariableDefinition > variablesForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
const QVariantMap &results = QVariantMap() ) const;
%Docstring
Returns a map of variable name to variable definition for expression context variables which are available
for use by child algorithm during model execution.

The child algorithm ``childId`` and processing ``context``
are manadatory. If ``modelParameters`` and ``results`` are not specified, then only the variable names and sources
will be returned, but all variable values will be null. This can be used to determine in advance which variables
will be available for a specific child algorithm, e.g. for use in expression builder widgets.

In order to calculate the actual variable value, the input model ``modelParameters`` and already executed child
algorithm ``results`` must be passed.
.. seealso:: createExpressionContextScopeForChildAlgorithm()
:rtype: QMap< str, QgsProcessingModelAlgorithm.VariableDefinition >
%End

QgsExpressionContextScope *createExpressionContextScopeForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
const QVariantMap &results = QVariantMap() ) const /Factory/;
%Docstring
Creates a new expression context scope for a child algorithm within the model.
.. seealso:: variablesForChildAlgorithm()
:rtype: QgsExpressionContextScope
%End

protected:

virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
@@ -28,17 +28,17 @@ class QgsProcessingOutputDefinition
%End

%ConvertToSubClassCode
if ( sipCpp->type() == "outputVector" )
if ( sipCpp->type() == QgsProcessingOutputVectorLayer::typeName() )
sipType = sipType_QgsProcessingOutputVectorLayer;
else if ( sipCpp->type() == "outputRaster" )
else if ( sipCpp->type() == QgsProcessingOutputRasterLayer::typeName() )
sipType = sipType_QgsProcessingOutputRasterLayer;
else if ( sipCpp->type() == "outputHtml" )
else if ( sipCpp->type() == QgsProcessingOutputHtml::typeName() )
sipType = sipType_QgsProcessingOutputHtml;
else if ( sipCpp->type() == "outputNumber" )
else if ( sipCpp->type() == QgsProcessingOutputNumber::typeName() )
sipType = sipType_QgsProcessingOutputNumber;
else if ( sipCpp->type() == "outputString" )
else if ( sipCpp->type() == QgsProcessingOutputString::typeName() )
sipType = sipType_QgsProcessingOutputString;
else if ( sipCpp->type() == "outputFolder" )
else if ( sipCpp->type() == QgsProcessingOutputFolder::typeName() )
sipType = sipType_QgsProcessingOutputFolder;
%End
public:
@@ -111,6 +111,11 @@ class QgsProcessingOutputVectorLayer : QgsProcessingOutputDefinition
Constructor for QgsProcessingOutputVectorLayer.
%End

static QString typeName();
%Docstring
Returns the type name for the output class.
:rtype: str
%End
virtual QString type() const;

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

static QString typeName();
%Docstring
Returns the type name for the output class.
:rtype: str
%End
virtual QString type() const;


};

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

static QString typeName();
%Docstring
Returns the type name for the output class.
:rtype: str
%End
virtual QString type() const;

};

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

static QString typeName();
%Docstring
Returns the type name for the output class.
:rtype: str
%End
virtual QString type() const;
};

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

static QString typeName();
%Docstring
Returns the type name for the output class.
:rtype: str
%End
virtual QString type() const;

};

class QgsProcessingOutputFolder : QgsProcessingOutputDefinition
@@ -220,12 +249,19 @@ class QgsProcessingOutputFolder : QgsProcessingOutputDefinition
%End
public:


QgsProcessingOutputFolder( const QString &name, const QString &description = QString() );
%Docstring
Constructor for QgsProcessingOutputFolder.
%End

static QString typeName();
%Docstring
Returns the type name for the output class.
:rtype: str
%End
virtual QString type() const;

};


0 comments on commit cb6c6f3

Please sign in to comment.
You can’t perform that action at this time.