Skip to content

Commit

Permalink
Merge pull request #4812 from nyalldawson/model_exp_input
Browse files Browse the repository at this point in the history
[processing] Allow model child parameters to take values from an expression
  • Loading branch information
nyalldawson authored Jul 7, 2017
2 parents 52f4c5e + 515ba24 commit cb6c6f3
Show file tree
Hide file tree
Showing 31 changed files with 1,276 additions and 471 deletions.
2 changes: 2 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
15 changes: 14 additions & 1 deletion python/core/expression/qgsexpression.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
105 changes: 104 additions & 1 deletion python/core/processing/qgsprocessingmodelalgorithm.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
48 changes: 42 additions & 6 deletions python/core/processing/qgsprocessingoutputs.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
};

Expand All @@ -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
Expand All @@ -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;

};


Expand Down
Loading

0 comments on commit cb6c6f3

Please sign in to comment.