Skip to content

Commit

Permalink
Add a new source type for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Apr 26, 2018
1 parent 4168c05 commit 4f4844f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Source for the value of a parameter for a child algorithm within a model.
ChildOutput,
StaticValue,
Expression,
ExpressionText,
};

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

.. seealso:: :py:func:`fromExpression`

.. seealso:: :py:func:`fromExpressionText`
%End

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

.. seealso:: :py:func:`fromExpression`

.. seealso:: :py:func:`fromExpressionText`
%End

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

.. seealso:: :py:func:`fromExpression`

.. seealso:: :py:func:`fromExpressionText`
%End

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

.. seealso:: :py:func:`fromModelParameter`

.. seealso:: :py:func:`fromExpressionText`

.. versionadded:: 3.2
%End

static QgsProcessingModelChildParameterSource fromExpressionText( const QString &text );
%Docstring
Returns a new QgsProcessingModelChildParameterSource which takes its
value from a text with expressions. Expressions are 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:: :py:func:`fromStaticValue`

.. seealso:: :py:func:`fromChildOutput`

.. seealso:: :py:func:`fromModelParameter`

.. seealso:: :py:func:`fromExpression`

.. versionadded:: 3.2
%End

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

.. seealso:: :py:func:`expression`
%End

QString expressionText() const;
%Docstring
Returns the source's text with expressions. This is only used when the
source() is ExpressionText.

.. seealso:: :py:func:`setExpressionText`

.. versionadded:: 3.2
%End

void setExpressionText( const QString &text );
%Docstring
Sets the source's text containing expressions. Calling this will also
change the source() to ExpressionText. Expressions are 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:: :py:func:`expressionText`

.. versionadded:: 3.2
%End

QVariant toVariant() const;
Expand Down
16 changes: 15 additions & 1 deletion src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const QgsP

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

QString expressionText;
QVariantList paramParts;
Q_FOREACH ( const QgsProcessingModelChildParameterSource &source, paramSources )
{
Expand All @@ -122,9 +123,19 @@ QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const QgsP
paramParts << exp.evaluate( &expressionContext );
break;
}
case QgsProcessingModelChildParameterSource::ExpressionText:
{
expressionText = QgsExpression::replaceExpressionText( source.expressionText(), &expressionContext );
break;
}
}
}
if ( paramParts.count() == 1 )

if ( ! expressionText.isEmpty() )
{
childParams.insert( def->name(), expressionText );
}
else if ( paramParts.count() == 1 )
childParams.insert( def->name(), paramParts.at( 0 ) );
else
childParams.insert( def->name(), paramParts );
Expand Down Expand Up @@ -464,6 +475,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
}

case QgsProcessingModelChildParameterSource::Expression:
case QgsProcessingModelChildParameterSource::ExpressionText:
case QgsProcessingModelChildParameterSource::StaticValue:
continue;
};
Expand Down Expand Up @@ -508,6 +520,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
}

case QgsProcessingModelChildParameterSource::Expression:
case QgsProcessingModelChildParameterSource::ExpressionText:
case QgsProcessingModelChildParameterSource::StaticValue:
continue;

Expand Down Expand Up @@ -555,6 +568,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
}

case QgsProcessingModelChildParameterSource::Expression:
case QgsProcessingModelChildParameterSource::ExpressionText:
case QgsProcessingModelChildParameterSource::StaticValue:
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ bool QgsProcessingModelChildParameterSource::operator==( const QgsProcessingMode
return mParameterName == other.mParameterName;
case Expression:
return mExpression == other.mExpression;
case ExpressionText:
return mExpressionText == other.mExpressionText;
}
return false;
}
Expand Down Expand Up @@ -71,6 +73,14 @@ QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::f
return src;
}

QgsProcessingModelChildParameterSource QgsProcessingModelChildParameterSource::fromExpressionText( const QString &text )
{
QgsProcessingModelChildParameterSource src;
src.mSource = ExpressionText;
src.mExpressionText = text;
return src;
}

QgsProcessingModelChildParameterSource::Source QgsProcessingModelChildParameterSource::source() const
{
return mSource;
Expand Down Expand Up @@ -98,6 +108,10 @@ QVariant QgsProcessingModelChildParameterSource::toVariant() const
case Expression:
map.insert( QStringLiteral( "expression" ), mExpression );
break;

case ExpressionText:
map.insert( QStringLiteral( "expression_text" ), mExpressionText );
break;
}
return map;
}
Expand All @@ -123,6 +137,10 @@ bool QgsProcessingModelChildParameterSource::loadVariant( const QVariantMap &map
case Expression:
mExpression = map.value( QStringLiteral( "expression" ) ).toString();
break;

case ExpressionText:
mExpressionText = map.value( QStringLiteral( "expression_text" ) ).toString();
break;
}
return true;
}
Expand All @@ -142,6 +160,9 @@ QString QgsProcessingModelChildParameterSource::asPythonCode() const

case Expression:
return QStringLiteral( "QgsExpression('%1').evaluate()" ).arg( mExpression );

case ExpressionText:
return mExpressionText;
}
return QString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
ChildOutput, //!< Parameter value is taken from an output generated by a child algorithm
StaticValue, //!< Parameter value is a static value
Expression, //!< Parameter value is taken from an expression, evaluated just before the algorithm runs
ExpressionText, //!< Parameter value is taken from a text with expressions, evaluated just before the algorithm runs
};

/**
Expand All @@ -58,6 +59,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
* \see fromModelParameter()
* \see fromChildOutput()
* \see fromExpression()
* \see fromExpressionText()
*/
static QgsProcessingModelChildParameterSource fromStaticValue( const QVariant &value );

Expand All @@ -66,6 +68,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
* \see fromStaticValue()
* \see fromChildOutput()
* \see fromExpression()
* \see fromExpressionText()
*/
static QgsProcessingModelChildParameterSource fromModelParameter( const QString &parameterName );

Expand All @@ -74,6 +77,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
* \see fromStaticValue()
* \see fromModelParameter()
* \see fromExpression()
* \see fromExpressionText()
*/
static QgsProcessingModelChildParameterSource fromChildOutput( const QString &childId, const QString &outputName );

Expand All @@ -85,9 +89,25 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
* \see fromStaticValue()
* \see fromChildOutput()
* \see fromModelParameter()
* \see fromExpressionText()
* \since QGIS 3.2
*/
static QgsProcessingModelChildParameterSource fromExpression( const QString &expression );

/**
* Returns a new QgsProcessingModelChildParameterSource which takes its
* value from a text with expressions. Expressions are 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.
* \see fromStaticValue()
* \see fromChildOutput()
* \see fromModelParameter()
* \see fromExpression()
* \since QGIS 3.2
*/
static QgsProcessingModelChildParameterSource fromExpressionText( const QString &text );

/**
* Returns the parameter value's source.
*/
Expand Down Expand Up @@ -160,6 +180,25 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
*/
void setExpression( const QString &expression ) { mExpression = expression; mSource = Expression; }

/**
* Returns the source's text with expressions. This is only used when the
* source() is ExpressionText.
* \see setExpressionText()
* \since QGIS 3.2
*/
QString expressionText() const { return mExpressionText; }

/**
* Sets the source's text containing expressions. Calling this will also
* change the source() to ExpressionText. Expressions are 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.
* \see expressionText()
* \since QGIS 3.2
*/
void setExpressionText( const QString &text ) { mExpressionText = text; mSource = ExpressionText; }

/**
* Saves this source to a QVariant.
* \see loadVariant()
Expand All @@ -185,6 +224,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
QString mChildId;
QString mOutputName;
QString mExpression;
QString mExpressionText;

};

Expand Down

0 comments on commit 4f4844f

Please sign in to comment.