Skip to content
Permalink
Browse files

[processing] Fix evaluation of vector layer references to compatible …

…feature sources

Refs #18545
  • Loading branch information
nyalldawson committed Jun 4, 2018
1 parent 4335130 commit a1d46de0decaf6c53f3c320ad2439edac346d270
Showing with 51 additions and 13 deletions.
  1. +33 −13 src/core/processing/qgsprocessingparameters.cpp
  2. +18 −0 tests/src/analysis/testqgsprocessing.cpp
@@ -321,25 +321,45 @@ QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const Qgs
val = fromVar.source;
}

QString layerRef;
if ( val.canConvert<QgsProperty>() )
{
layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
}
else if ( !val.isValid() || val.toString().isEmpty() )
{
// fall back to default
layerRef = definition->defaultValue().toString();
val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
}
else

QgsVectorLayer *vl = nullptr;
vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );

if ( !vl )
{
layerRef = val.toString();
}
QString layerRef;
if ( val.canConvert<QgsProperty>() )
{
layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
}
else if ( !val.isValid() || val.toString().isEmpty() )
{
// fall back to default
val = definition->defaultValue();

if ( layerRef.isEmpty() )
return QString();
// default value may be a vector layer
vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
if ( !vl )
layerRef = definition->defaultValue().toString();
}
else
{
layerRef = val.toString();
}

if ( !vl )
{
if ( layerRef.isEmpty() )
return QString();

vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context ) );
}
}

QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context ) );
if ( !vl )
return QString();

@@ -6391,6 +6391,18 @@ void TestQgsProcessing::convertCompatible()
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
QCOMPARE( out, layer->source() );

// incompatible format, will be converted
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
QVERIFY( out != layer->source() );
QVERIFY( out.endsWith( ".tab" ) );
QVERIFY( out.startsWith( QgsProcessingUtils::tempFolder() ) );

// layer as input
params.insert( QStringLiteral( "source" ), QVariant::fromValue( layer ) );
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
QCOMPARE( out, layer->source() );

// incompatible format, will be converted
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
QVERIFY( out != layer->source() );
QVERIFY( out.endsWith( ".tab" ) );
@@ -6402,6 +6414,12 @@ void TestQgsProcessing::convertCompatible()
QVERIFY( out != layer->source() );
QVERIFY( out.endsWith( ".shp" ) );
QVERIFY( out.startsWith( QgsProcessingUtils::tempFolder() ) );

// vector layer as default
def.reset( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ), QString(), QList<int>(), QVariant::fromValue( layer ) ) );
params.remove( QStringLiteral( "source" ) );
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
QCOMPARE( out, layer->source() );
}

void TestQgsProcessing::create()

0 comments on commit a1d46de

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