Skip to content
Permalink
Browse files

Fix handling optional multiple table field parameter with no fields set

  • Loading branch information
nyalldawson committed Jun 11, 2017
1 parent b40f409 commit 9f018e67e22eaac10491ea2353d4185fe1dd00ab
Showing with 24 additions and 12 deletions.
  1. +18 −12 src/core/processing/qgsprocessingparameters.cpp
  2. +6 −0 tests/src/core/testqgsprocessing.cpp
@@ -672,27 +672,33 @@ QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParam

QStringList resultStringList;
QVariant val = parameters.value( definition->name() );
if ( val.canConvert<QgsProperty>() )
resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
else if ( val.type() == QVariant::List )
if ( val.isValid() )
{
Q_FOREACH ( const QVariant &var, val.toList() )
resultStringList << var.toString();
if ( val.canConvert<QgsProperty>() )
resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
else if ( val.type() == QVariant::List )
{
Q_FOREACH ( const QVariant &var, val.toList() )
resultStringList << var.toString();
}
else
resultStringList.append( val.toString().split( ';' ) );
}
else
resultStringList.append( val.toString().split( ';' ) );

if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
{
resultStringList.clear();
// check default
if ( definition->defaultValue().type() == QVariant::List )
if ( definition->defaultValue().isValid() )
{
Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
resultStringList << var.toString();
if ( definition->defaultValue().type() == QVariant::List )
{
Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
resultStringList << var.toString();
}
else
resultStringList.append( definition->defaultValue().toString().split( ';' ) );
}
else
resultStringList.append( definition->defaultValue().toString().split( ';' ) );
}

return resultStringList;
@@ -2288,6 +2288,12 @@ void TestQgsProcessing::parameterField()
fields = QgsProcessingParameters::parameterAsFields( def.get(), params, context );
QCOMPARE( fields, QStringList() << "def" );

// optional, no default
def.reset( new QgsProcessingParameterTableField( "optional", QString(), QVariant(), QString(), QgsProcessingParameterTableField::Any, false, true ) );
params.insert( "optional", QVariant() );
fields = QgsProcessingParameters::parameterAsFields( def.get(), params, context );
QVERIFY( fields.isEmpty() );

//optional with multiples
def.reset( new QgsProcessingParameterTableField( "optional", QString(), QString( "abc;def" ), QString(), QgsProcessingParameterTableField::Any, true, true ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );

0 comments on commit 9f018e6

Please sign in to comment.
You can’t perform that action at this time.