Skip to content

Commit

Permalink
Allow storing multiple status in field script code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 26, 2017
1 parent a72eea2 commit 46b4f24
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2207,6 +2207,9 @@ QString QgsProcessingParameterField::asScriptCode() const
break;
}

if ( mAllowMultiple )
code += QStringLiteral( "multiple " );

code += mParentLayerParameter + ' ';

code += mDefault.toString();
Expand Down Expand Up @@ -2283,6 +2286,13 @@ QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const
type = DateTime;
def = def.mid( 9 );
}

if ( def.startsWith( QStringLiteral( "multiple" ), Qt::CaseInsensitive ) )
{
allowMultiple = true;
def = def.mid( 8 ).trimmed();
}

QRegularExpression re( "(.*?)\\s+(.*)$" );
QRegularExpressionMatch m = re.match( def );
if ( m.hasMatch() )
Expand Down Expand Up @@ -2535,6 +2545,10 @@ QString QgsProcessingParameterFeatureSink::asScriptCode() const
code += QStringLiteral( "polygon " );
break;

case TypeTable:
code += QStringLiteral( "table " );
break;

default:
break;
}
Expand Down Expand Up @@ -2631,6 +2645,11 @@ QgsProcessingParameterFeatureSink *QgsProcessingParameterFeatureSink::fromScript
type = QgsProcessingParameterDefinition::TypeVectorPolygon;
def = def.mid( 8 );
}
else if ( def.startsWith( QStringLiteral( "table" ), Qt::CaseInsensitive ) )
{
type = QgsProcessingParameterDefinition::TypeTable;
def = def.mid( 6 );
}

return new QgsProcessingParameterFeatureSink( name, description, type, definition, isOptional );
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingparameters.h
Expand Up @@ -239,7 +239,7 @@ class CORE_EXPORT QgsProcessingParameterDefinition
TypeVectorPolygon = 2, //!< Vector polygon layers
TypeRaster = 3, //!< Raster layers
TypeFile = 4, //!< Files
TypeTable = 5, //!< Tables (i.e. vector layers with or without geometry)
TypeTable = 5, //!< Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink has no geometry.
};

/**
Expand Down
18 changes: 17 additions & 1 deletion tests/src/core/testqgsprocessing.cpp
Expand Up @@ -3051,7 +3051,7 @@ void TestQgsProcessing::parameterField()
QCOMPARE( fromCode->allowMultiple(), def->allowMultiple() );

// multiple
def.reset( new QgsProcessingParameterField( "non_optional", QString(), QString(), QString(), QgsProcessingParameterField::Any, true, false ) );
def.reset( new QgsProcessingParameterField( "non_optional", QString(), QVariant(), QString(), QgsProcessingParameterField::Any, true, false ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( "test" ) );
QVERIFY( def->checkValueIsAcceptable( QStringList() << "a" << "b" ) );
Expand Down Expand Up @@ -3082,6 +3082,17 @@ void TestQgsProcessing::parameterField()
def.reset( dynamic_cast< QgsProcessingParameterField *>( QgsProcessingParameters::parameterFromVariantMap( map ) ) );
QVERIFY( dynamic_cast< QgsProcessingParameterField *>( def.get() ) );

code = def->asScriptCode();
fromCode.reset( dynamic_cast< QgsProcessingParameterField * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
QCOMPARE( fromCode->parentLayerParameter(), def->parentLayerParameter() );
QCOMPARE( fromCode->dataType(), def->dataType() );
QCOMPARE( fromCode->allowMultiple(), def->allowMultiple() );

// optional
def.reset( new QgsProcessingParameterField( "optional", QString(), QString( "def" ), QString(), QgsProcessingParameterField::Any, false, true ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
Expand Down Expand Up @@ -3440,6 +3451,11 @@ void TestQgsProcessing::parameterFeatureSink()
QCOMPARE( code, QStringLiteral( "##non_optional=sink polygon" ) );
fromCode.reset( dynamic_cast< QgsProcessingParameterFeatureSink * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QCOMPARE( fromCode->dataType(), def->dataType() );
def->setDataType( QgsProcessingParameterDefinition::TypeTable );
code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##non_optional=sink table" ) );
fromCode.reset( dynamic_cast< QgsProcessingParameterFeatureSink * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QCOMPARE( fromCode->dataType(), def->dataType() );

// optional
def.reset( new QgsProcessingParameterFeatureSink( "optional", QString(), QgsProcessingParameterDefinition::TypeVectorAny, QString(), true ) );
Expand Down

0 comments on commit 46b4f24

Please sign in to comment.