Skip to content
Permalink
Browse files
Fixes #19836
Keep layer order when using different way for passing layer (layer id or layer pointer for instance)
  • Loading branch information
troopa81 authored and nyalldawson committed Oct 1, 2018
1 parent 3ecb154 commit ee4afe2db1fdeb4480a10028fa7a77aab8ef1596
Showing with 16 additions and 15 deletions.
  1. +9 −15 src/core/processing/qgsprocessingparameters.cpp
  2. +7 −0 tests/src/analysis/testqgsprocessing.cpp
@@ -1280,19 +1280,19 @@ QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsPro
{
Q_FOREACH ( const QString &s, var.toStringList() )
{
resultStringList << s;
processVariant( s );
}
}
else if ( var.canConvert<QgsProperty>() )
resultStringList << var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
processVariant( var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
else if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
{
// input is a QgsProcessingOutputLayerDefinition - get extra properties from it
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
QVariant sink = fromVar.sink;
if ( sink.canConvert<QgsProperty>() )
{
resultStringList << sink.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
processVariant( sink.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() ) );
}
}
else if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
@@ -1301,15 +1301,16 @@ QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsPro
}
else
{
resultStringList << var.toString();
QgsMapLayer *alayer = QgsProcessingUtils::mapLayerFromString( var.toString(), context );
if ( alayer )
layers << alayer;
}
};

processVariant( val );

if ( layers.isEmpty() && ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
if ( layers.isEmpty() )
{
resultStringList.clear();
// check default
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
{
@@ -1325,19 +1326,12 @@ QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsPro
}
else
{
resultStringList << var.toString();
processVariant( var );
}
}
}
else
resultStringList << definition->defaultValue().toString();
}

Q_FOREACH ( const QString &s, resultStringList )
{
QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( s, context );
if ( layer )
layers << layer;
processVariant( definition->defaultValue() );
}

return layers;
@@ -3066,6 +3066,13 @@ void TestQgsProcessing::parameterLayerList()
QCOMPARE( layers.at( 0 ), v1 );
QCOMPARE( layers.at( 1 )->publicSource(), raster2 );

// mix of existing layer and ID (and check we keep order)
params.insert( "non_optional", QVariantList() << QVariant::fromValue( v1 ) << v2->id() );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 << v2 );

params.insert( "non_optional", QVariantList() << v1->id() << QVariant::fromValue( v2 ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 << v2 );

// empty string
params.insert( "non_optional", QString( "" ) );
QVERIFY( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ).isEmpty() );

0 comments on commit ee4afe2

Please sign in to comment.