Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use variantToPythonLiteral
  • Loading branch information
Koyaani authored and nyalldawson committed Nov 11, 2021
1 parent 46d823d commit 18b4e12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
39 changes: 7 additions & 32 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -1649,8 +1649,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;
}
Expand Down Expand Up @@ -3420,37 +3425,7 @@ QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value
p.insert( name(), value );
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
Expand Down
18 changes: 9 additions & 9 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -3810,13 +3810,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();
Expand Down Expand Up @@ -3854,7 +3854,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" ) );
Expand All @@ -3870,7 +3870,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" ) );
Expand Down

0 comments on commit 18b4e12

Please sign in to comment.