Skip to content
Permalink
Browse files

Add a new source type for parameters

  • Loading branch information
pblottiere committed Apr 26, 2018
1 parent 4168c05 commit 4f4844fa8abdc1eeaf8809dc0e74b5e7fdd1d526
@@ -29,6 +29,7 @@ Source for the value of a parameter for a child algorithm within a model.
ChildOutput,
StaticValue,
Expression,
ExpressionText,
};

QgsProcessingModelChildParameterSource();
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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;
@@ -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;
@@ -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 )
{
@@ -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 );
@@ -464,6 +475,7 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
}

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

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

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

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

@@ -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;
}
@@ -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;
@@ -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;
}
@@ -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;
}
@@ -142,6 +160,9 @@ QString QgsProcessingModelChildParameterSource::asPythonCode() const

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

case ExpressionText:
return mExpressionText;
}
return QString();
}
@@ -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
};

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

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

@@ -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 );

@@ -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.
*/
@@ -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()
@@ -185,6 +224,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
QString mChildId;
QString mOutputName;
QString mExpression;
QString mExpressionText;

};

0 comments on commit 4f4844f

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