Skip to content

Commit 946687c

Browse files
committedFeb 1, 2019
Update QgsProcessingModelChildParameterSource::asPythonCode
1 parent 37774f9 commit 946687c

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed
 

‎python/core/auto_generated/processing/models/qgsprocessingmodelchildparametersource.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Loads this source from a QVariantMap.
240240
.. seealso:: :py:func:`toVariant`
241241
%End
242242

243-
QString asPythonCode() const;
243+
QString asPythonCode( QgsProcessing::PythonOutputType outputType, const QgsProcessingParameterDefinition *definition ) const;
244244
%Docstring
245245
Attempts to convert the source to executable Python code.
246246
%End

‎src/core/processing/models/qgsprocessingmodelchildparametersource.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
***************************************************************************/
1717

1818
#include "qgsprocessingmodelchildparametersource.h"
19+
#include "qgsprocessingparameters.h"
20+
#include "qgsprocessingcontext.h"
1921

2022
///@cond NOT_STABLE
2123

@@ -145,7 +147,7 @@ bool QgsProcessingModelChildParameterSource::loadVariant( const QVariantMap &map
145147
return true;
146148
}
147149

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

158160
case StaticValue:
159-
return mStaticValue.toString();
161+
if ( definition )
162+
{
163+
QgsProcessingContext c;
164+
return definition->valueAsPythonString( mStaticValue, c );
165+
}
166+
else
167+
{
168+
return QgsProcessingUtils::variantToPythonLiteral( mStaticValue );
169+
}
160170

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

‎src/core/processing/models/qgsprocessingmodelchildparametersource.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include "qgis_core.h"
2222
#include "qgis.h"
23+
#include "qgsprocessing.h"
24+
class QgsProcessingParameterDefinition;
2325

2426
///@cond NOT_STABLE
2527

@@ -214,7 +216,7 @@ class CORE_EXPORT QgsProcessingModelChildParameterSource
214216
/**
215217
* Attempts to convert the source to executable Python code.
216218
*/
217-
QString asPythonCode() const;
219+
QString asPythonCode( QgsProcessing::PythonOutputType outputType, const QgsProcessingParameterDefinition *definition ) const;
218220

219221
private:
220222

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6046,44 +6046,56 @@ void TestQgsProcessing::modelerAlgorithm()
60466046
QCOMPARE( svSource.staticValue().toInt(), 5 );
60476047
svSource.setStaticValue( 7 );
60486048
QCOMPARE( svSource.staticValue().toInt(), 7 );
6049+
QCOMPARE( svSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "7" ) );
60496050
svSource = QgsProcessingModelChildParameterSource::fromModelParameter( "a" );
60506051
// check that calling setStaticValue flips source to StaticValue
60516052
QCOMPARE( svSource.source(), QgsProcessingModelChildParameterSource::ModelParameter );
6053+
QCOMPARE( svSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['a']" ) );
60526054
svSource.setStaticValue( 7 );
60536055
QCOMPARE( svSource.staticValue().toInt(), 7 );
60546056
QCOMPARE( svSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
6057+
QCOMPARE( svSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "7" ) );
60556058

60566059
// model parameter source
60576060
QgsProcessingModelChildParameterSource mpSource = QgsProcessingModelChildParameterSource::fromModelParameter( "a" );
60586061
QCOMPARE( mpSource.source(), QgsProcessingModelChildParameterSource::ModelParameter );
60596062
QCOMPARE( mpSource.parameterName(), QStringLiteral( "a" ) );
6063+
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['a']" ) );
60606064
mpSource.setParameterName( "b" );
60616065
QCOMPARE( mpSource.parameterName(), QStringLiteral( "b" ) );
6066+
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['b']" ) );
60626067
mpSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
60636068
// check that calling setParameterName flips source to ModelParameter
60646069
QCOMPARE( mpSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
6070+
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
60656071
mpSource.setParameterName( "c" );
60666072
QCOMPARE( mpSource.parameterName(), QStringLiteral( "c" ) );
60676073
QCOMPARE( mpSource.source(), QgsProcessingModelChildParameterSource::ModelParameter );
6074+
QCOMPARE( mpSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "parameters['c']" ) );
60686075

60696076
// child alg output source
60706077
QgsProcessingModelChildParameterSource oSource = QgsProcessingModelChildParameterSource::fromChildOutput( "a", "b" );
60716078
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::ChildOutput );
60726079
QCOMPARE( oSource.outputChildId(), QStringLiteral( "a" ) );
60736080
QCOMPARE( oSource.outputName(), QStringLiteral( "b" ) );
6081+
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "outputs['a']['b']" ) );
60746082
oSource.setOutputChildId( "c" );
60756083
QCOMPARE( oSource.outputChildId(), QStringLiteral( "c" ) );
6084+
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "outputs['c']['b']" ) );
60766085
oSource.setOutputName( "d" );
60776086
QCOMPARE( oSource.outputName(), QStringLiteral( "d" ) );
6087+
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "outputs['c']['d']" ) );
60786088
oSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
60796089
// check that calling setOutputChildId flips source to ChildOutput
60806090
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
6091+
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
60816092
oSource.setOutputChildId( "c" );
60826093
QCOMPARE( oSource.outputChildId(), QStringLiteral( "c" ) );
60836094
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::ChildOutput );
60846095
oSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
60856096
// check that calling setOutputName flips source to ChildOutput
60866097
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
6098+
QCOMPARE( oSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
60876099
oSource.setOutputName( "d" );
60886100
QCOMPARE( oSource.outputName(), QStringLiteral( "d" ) );
60896101
QCOMPARE( oSource.source(), QgsProcessingModelChildParameterSource::ChildOutput );
@@ -6092,14 +6104,18 @@ void TestQgsProcessing::modelerAlgorithm()
60926104
QgsProcessingModelChildParameterSource expSource = QgsProcessingModelChildParameterSource::fromExpression( "1+2" );
60936105
QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::Expression );
60946106
QCOMPARE( expSource.expression(), QStringLiteral( "1+2" ) );
6107+
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "QgsExpression('1+2').evaluate()" ) );
60956108
expSource.setExpression( "1+3" );
60966109
QCOMPARE( expSource.expression(), QStringLiteral( "1+3" ) );
6110+
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "QgsExpression('1+3').evaluate()" ) );
60976111
expSource = QgsProcessingModelChildParameterSource::fromStaticValue( 5 );
60986112
// check that calling setExpression flips source to Expression
60996113
QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::StaticValue );
6114+
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "5" ) );
61006115
expSource.setExpression( "1+4" );
61016116
QCOMPARE( expSource.expression(), QStringLiteral( "1+4" ) );
61026117
QCOMPARE( expSource.source(), QgsProcessingModelChildParameterSource::Expression );
6118+
QCOMPARE( expSource.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, nullptr ), QStringLiteral( "QgsExpression('1+4').evaluate()" ) );
61036119

61046120
// source equality operator
61056121
QVERIFY( QgsProcessingModelChildParameterSource::fromStaticValue( 5 ) ==

0 commit comments

Comments
 (0)
Please sign in to comment.