Skip to content
Permalink
Browse files
use variantToPythonLiteral
  • Loading branch information
Koyaani authored and nyalldawson committed Nov 9, 2021
1 parent ffadd34 commit 58f9095d3ed97fb023835d93c5713f62850fa899
Showing with 16 additions and 41 deletions.
  1. +7 −32 src/core/processing/qgsprocessingparameters.cpp
  2. +9 −9 tests/src/analysis/testqgsprocessing.cpp
@@ -1737,8 +1737,13 @@ QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingPara

QVariantList result;
const auto constSplit = resultString.split( ',' );
bool ok;
double number;
for ( const QString &s : constSplit )
result << s;
{
number = s.toDouble( &ok );
result << ( ok ? QVariant( number ) : s );
}

return result;
}
@@ -3582,37 +3587,7 @@ QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value
p.insert( name(), value );
const QVariantList list = QgsProcessingParameters::parameterAsMatrix( this, p, context );

QStringList parts;
const auto constList = list;
for ( const QVariant &v : constList )
{
if ( v.type() == QVariant::List )
{
QStringList parts2;
const auto constToList = v.toList();
for ( const QVariant &v2 : constToList )
{
if ( v2.isNull() || !v2.isValid() )
parts2 << QStringLiteral( "None" );
else if ( v2.toString().isEmpty() )
parts2 << QStringLiteral( "''" );
else
parts2 << QgsProcessingUtils::stringToPythonLiteral( v2.toString() );
}
parts << parts2.join( ',' ).prepend( '[' ).append( ']' );
}
else
{
if ( v.isNull() || !v.isValid() )
parts << QStringLiteral( "None" );
else if ( v.toString().isEmpty() )
parts << QStringLiteral( "''" );
else
parts << QgsProcessingUtils::stringToPythonLiteral( v.toString() );
}
}

return parts.join( ',' ).prepend( '[' ).append( ']' );
return QgsProcessingUtils::variantToPythonLiteral( list );
}

QString QgsProcessingParameterMatrix::asPythonString( const QgsProcessing::PythonOutputType outputType ) const
@@ -4020,13 +4020,13 @@ void TestQgsProcessing::parameterMatrix()
QCOMPARE( QgsProcessingParameters::parameterAsMatrix( def.get(), params, context ), QVariantList() << 4 << 5 << 6 );

QCOMPARE( def->valueAsPythonString( QVariant(), context ), QStringLiteral( "None" ) );
QCOMPARE( def->valueAsPythonString( 5, context ), QStringLiteral( "['5']" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << 1 << 2 << 3, context ), QStringLiteral( "['1','2','3']" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << 2 << 3 ) << ( QVariantList() << 1 << 2 << 3 ), context ), QStringLiteral( "['1','2','3','1','2','3']" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << QStringLiteral( "value" ) << 3 ) << ( QVariantList() << 1 << 2 << QStringLiteral( "it's a value" ) ), context ), QStringLiteral( "['1','value','3','1','2',\"it's a value\"]" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << QVariant() << 3 ) << ( QVariantList() << QVariant() << 2 << 3 ), context ), QStringLiteral( "['1',None,'3',None,'2','3']" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << QString( "" ) << 3 ) << ( QVariantList() << 1 << 2 << QString( "" ) ), context ), QStringLiteral( "['1','','3','1','2','']" ) );
QCOMPARE( def->valueAsPythonString( "1,2,3", context ), QStringLiteral( "['1','2','3']" ) );
QCOMPARE( def->valueAsPythonString( 5, context ), QStringLiteral( "[5]" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << 1 << 2 << 3, context ), QStringLiteral( "[1,2,3]" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << 2 << 3 ) << ( QVariantList() << 1 << 2 << 3 ), context ), QStringLiteral( "[1,2,3,1,2,3]" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << QStringLiteral( "value" ) << 3 ) << ( QVariantList() << 1 << 2 << QStringLiteral( "it's a value" ) ), context ), QStringLiteral( "[1,'value',3,1,2,\"it's a value\"]" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << QVariant() << 3 ) << ( QVariantList() << QVariant() << 2 << 3 ), context ), QStringLiteral( "[1,None,3,None,2,3]" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << ( QVariantList() << 1 << QString( "" ) << 3 ) << ( QVariantList() << 1 << 2 << QString( "" ) ), context ), QStringLiteral( "[1,'',3,1,2,'']" ) );
QCOMPARE( def->valueAsPythonString( "1,2,3", context ), QStringLiteral( "[1,2,3]" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );

QString pythonCode = def->asPythonString();
@@ -4064,7 +4064,7 @@ void TestQgsProcessing::parameterMatrix()
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );

pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterMatrix('optional', '', optional=True, numberRows=3, hasFixedNumberRows=False, headers=[], defaultValue=['4','5','6'])" ) );
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterMatrix('optional', '', optional=True, numberRows=3, hasFixedNumberRows=False, headers=[], defaultValue=[4,5,6])" ) );

code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##optional=optional matrix" ) );
@@ -4080,7 +4080,7 @@ void TestQgsProcessing::parameterMatrix()
def.reset( new QgsProcessingParameterMatrix( "optional", QString(), 3, false, QStringList(), QString( "1,2,3" ), true ) );

pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterMatrix('optional', '', optional=True, numberRows=3, hasFixedNumberRows=False, headers=[], defaultValue=['1','2','3'])" ) );
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterMatrix('optional', '', optional=True, numberRows=3, hasFixedNumberRows=False, headers=[], defaultValue=[1,2,3])" ) );

code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##optional=optional matrix 1,2,3" ) );

0 comments on commit 58f9095

Please sign in to comment.