mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
Update QgsProcessingModelChildParameterSource::asPythonCode
This commit is contained in:
parent
37774f9aa9
commit
946687c87b
@ -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
|
||||
|
@ -16,6 +16,8 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include "qgsprocessingmodelchildparametersource.h"
|
||||
#include "qgsprocessingparameters.h"
|
||||
#include "qgsprocessingcontext.h"
|
||||
|
||||
///@cond NOT_STABLE
|
||||
|
||||
@ -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 )
|
||||
{
|
||||
@ -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 );
|
||||
|
@ -20,6 +20,8 @@
|
||||
|
||||
#include "qgis_core.h"
|
||||
#include "qgis.h"
|
||||
#include "qgsprocessing.h"
|
||||
class QgsProcessingParameterDefinition;
|
||||
|
||||
///@cond NOT_STABLE
|
||||
|
||||
@ -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:
|
||||
|
||||
|
@ -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 );
|
||||
@ -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 ) ==
|
||||
|
Loading…
x
Reference in New Issue
Block a user