Skip to content

Commit

Permalink
Update QgsProcessingModelChildParameterSource::asPythonCode
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 1, 2019
1 parent 37774f9 commit 946687c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Loads this source from a QVariantMap.
.. seealso:: :py:func:`toVariant`
%End

QString asPythonCode() const;
QString asPythonCode( QgsProcessing::PythonOutputType outputType, const QgsProcessingParameterDefinition *definition ) const;
%Docstring
Attempts to convert the source to executable Python code.
%End
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
***************************************************************************/

#include "qgsprocessingmodelchildparametersource.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingcontext.h"

///@cond NOT_STABLE

Expand Down Expand Up @@ -145,7 +147,7 @@ bool QgsProcessingModelChildParameterSource::loadVariant( const QVariantMap &map
return true;
}

QString QgsProcessingModelChildParameterSource::asPythonCode() const
QString QgsProcessingModelChildParameterSource::asPythonCode( const QgsProcessing::PythonOutputType, const QgsProcessingParameterDefinition *definition ) const
{
switch ( mSource )
{
Expand All @@ -156,7 +158,15 @@ QString QgsProcessingModelChildParameterSource::asPythonCode() const
return QStringLiteral( "outputs['%1']['%2']" ).arg( mChildId, mOutputName );

case StaticValue:
return mStaticValue.toString();
if ( definition )
{
QgsProcessingContext c;
return definition->valueAsPythonString( mStaticValue, c );
}
else
{
return QgsProcessingUtils::variantToPythonLiteral( mStaticValue );
}

case Expression:
return QStringLiteral( "QgsExpression('%1').evaluate()" ).arg( mExpression );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "qgis_core.h"
#include "qgis.h"
#include "qgsprocessing.h"
class QgsProcessingParameterDefinition;

///@cond NOT_STABLE

Expand Down Expand Up @@ -214,7 +216,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
/**
* Attempts to convert the source to executable Python code.
*/
QString asPythonCode() const;
QString asPythonCode( QgsProcessing::PythonOutputType outputType, const QgsProcessingParameterDefinition *definition ) const;

private:

Expand Down
16 changes: 16 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6046,44 +6046,56 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( svSource.staticValue().toInt(), 5 );
svSource.setStaticValue( 7 );
QCOMPARE( svSource.staticValue().toInt(), 7 );
QCOMPARE( svSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "7" ) );
svSource = QgsProcessingModelChildParameterSource::fromModelParameter( "a" );
// check that calling setStaticValue flips source to StaticValue
QCOMPARE( svSource.source(), QgsProcessingModelChildParameterSource::ModelParameter );
QCOMPARE( svSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['a']" ) );
svSource.setStaticValue( 7 );
QCOMPARE( svSource.staticValue().toInt(), 7 );
QCOMPARE( svSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
QCOMPARE( svSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "7" ) );

// model parameter source
QgsProcessingModelChildParameterSource mpSource = QgsProcessingModelChildParameterSource::fromModelParameter( "a" );
QCOMPARE( mpSource.source(), QgsProcessingModelChildParameterSource::ModelParameter );
QCOMPARE( mpSource.parameterName(), QStringLiteral( "a" ) );
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['a']" ) );
mpSource.setParameterName( "b" );
QCOMPARE( mpSource.parameterName(), QStringLiteral( "b" ) );
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['b']" ) );
mpSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
// check that calling setParameterName flips source to ModelParameter
QCOMPARE( mpSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
mpSource.setParameterName( "c" );
QCOMPARE( mpSource.parameterName(), QStringLiteral( "c" ) );
QCOMPARE( mpSource.source(), QgsProcessingModelChildParameterSource::ModelParameter );
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['c']" ) );

// child alg output source
QgsProcessingModelChildParameterSource oSource = QgsProcessingModelChildParameterSource::fromChildOutput( "a", "b" );
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::ChildOutput );
QCOMPARE( oSource.outputChildId(), QStringLiteral( "a" ) );
QCOMPARE( oSource.outputName(), QStringLiteral( "b" ) );
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "outputs['a']['b']" ) );
oSource.setOutputChildId( "c" );
QCOMPARE( oSource.outputChildId(), QStringLiteral( "c" ) );
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "outputs['c']['b']" ) );
oSource.setOutputName( "d" );
QCOMPARE( oSource.outputName(), QStringLiteral( "d" ) );
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "outputs['c']['d']" ) );
oSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
// check that calling setOutputChildId flips source to ChildOutput
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
oSource.setOutputChildId( "c" );
QCOMPARE( oSource.outputChildId(), QStringLiteral( "c" ) );
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::ChildOutput );
oSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
// check that calling setOutputName flips source to ChildOutput
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
oSource.setOutputName( "d" );
QCOMPARE( oSource.outputName(), QStringLiteral( "d" ) );
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::ChildOutput );
Expand All @@ -6092,14 +6104,18 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelChildParameterSource expSource = QgsProcessingModelChildParameterSource::fromExpression( "1+2" );
QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::Expression );
QCOMPARE( expSource.expression(), QStringLiteral( "1+2" ) );
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "QgsExpression('1+2').evaluate()" ) );
expSource.setExpression( "1+3" );
QCOMPARE( expSource.expression(), QStringLiteral( "1+3" ) );
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "QgsExpression('1+3').evaluate()" ) );
expSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
// check that calling setExpression flips source to Expression
QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
expSource.setExpression( "1+4" );
QCOMPARE( expSource.expression(), QStringLiteral( "1+4" ) );
QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::Expression );
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "QgsExpression('1+4').evaluate()" ) );

// source equality operator
QVERIFY( QgsProcessingModelChildParameterSource::fromStaticValue( 5 ) ==
Expand Down

0 comments on commit 946687c

Please sign in to comment.