Skip to content
Permalink
Browse files

Allow direct map layer parameter/default values were appropriate

  • Loading branch information
nyalldawson committed Jun 6, 2017
1 parent b75a174 commit e6a71ab5fc25d90d83e9e924628ab42e157575c9
Showing with 134 additions and 7 deletions.
  1. +77 −6 src/core/processing/qgsprocessingparameters.cpp
  2. +57 −1 tests/src/core/testqgsprocessing.cpp
@@ -301,7 +301,29 @@ QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParam
if ( !definition )
return nullptr;

QString layerRef = parameterAsString( definition, parameters, context );
QVariant val = parameters.value( definition->name() );
if ( val.canConvert<QgsProperty>() )
{
val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
}

if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return layer;
}

if ( !val.isValid() || val.toString().isEmpty() )
{
// fall back to default
val = definition->defaultValue();
}

if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return layer;
}

QString layerRef = val.toString();
if ( layerRef.isEmpty() )
layerRef = definition->defaultValue().toString();

@@ -456,14 +478,31 @@ QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsPro
if ( !definition )
return QList<QgsMapLayer *>();

QStringList resultStringList;
QVariant val = parameters.value( definition->name() );
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return QList<QgsMapLayer *>() << layer;
}

QList<QgsMapLayer *> layers;

QStringList resultStringList;

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();
{
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
{
layers << layer;
}
else
{
resultStringList << var.toString();
}
}
}
else
resultStringList << val.toString();
@@ -472,16 +511,28 @@ QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsPro
{
resultStringList.clear();
// check default
if ( definition->defaultValue().type() == QVariant::List )
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
{
layers << layer;
}
else if ( definition->defaultValue().type() == QVariant::List )
{
Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
resultStringList << var.toString();
{
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
{
layers << layer;
}
else
{
resultStringList << var.toString();
}
}
}
else
resultStringList << definition->defaultValue().toString();
}

QList<QgsMapLayer *> layers;
Q_FOREACH ( const QString &s, resultStringList )
{
QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( s, context );
@@ -662,6 +713,11 @@ bool QgsProcessingParameterExtent::checkValueIsAcceptable( const QVariant &input
return true;
}

if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
{
return true;
}

if ( input.type() != QVariant::String || input.toString().isEmpty() )
return mFlags & FlagOptional;

@@ -842,6 +898,11 @@ bool QgsProcessingParameterMultipleLayers::checkValueIsAcceptable( const QVarian
if ( !input.isValid() )
return mFlags & FlagOptional;

if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
{
return true;
}

if ( input.type() == QVariant::String )
{
if ( input.toString().isEmpty() )
@@ -868,6 +929,9 @@ bool QgsProcessingParameterMultipleLayers::checkValueIsAcceptable( const QVarian

Q_FOREACH ( const QVariant &v, input.toList() )
{
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
continue;

if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
return false;
}
@@ -1048,6 +1112,9 @@ bool QgsProcessingParameterRasterLayer::checkValueIsAcceptable( const QVariant &
return true;
}

if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
return true;

if ( input.type() != QVariant::String || input.toString().isEmpty() )
return mFlags & FlagOptional;

@@ -1282,6 +1349,10 @@ bool QgsProcessingParameterFeatureSource::checkValueIsAcceptable( const QVariant
{
return true;
}
if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
{
return true;
}

if ( var.type() != QVariant::String || var.toString().isEmpty() )
return mFlags & FlagOptional;
@@ -1409,6 +1409,8 @@ void TestQgsProcessing::parameterLayer()
QVERIFY( !def->checkValueIsAcceptable( true ) );
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );

@@ -1437,6 +1439,13 @@ void TestQgsProcessing::parameterLayer()
params.insert( "non_optional", QString( "i'm not a layer, and nothing you can do will make me one" ) );
QVERIFY( !QgsProcessingParameters::parameterAsLayer( def.get(), params, context ) );

// layer
params.insert( "non_optional", QVariant::fromValue( r1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayer( def.get(), params, context ), r1 );
params.insert( "non_optional", QVariant::fromValue( v1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayer( def.get(), params, context ), v1 );


// optional
def.reset( new QgsProcessingParameterMapLayer( "optional", QString(), v1->id(), true ) );
params.insert( "optional", QVariant() );
@@ -1448,6 +1457,8 @@ void TestQgsProcessing::parameterLayer()
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
QVERIFY( def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );
}

void TestQgsProcessing::parameterExtent()
@@ -1674,6 +1685,8 @@ void TestQgsProcessing::parameterLayerList()
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );

// should be OK
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
@@ -1690,11 +1703,18 @@ void TestQgsProcessing::parameterLayerList()
QVariantMap params;
params.insert( "non_optional", v1->id() );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 );
// using existing map layer
params.insert( "non_optional", QVariant::fromValue( v1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 );

// using two existing map layer ID
params.insert( "non_optional", QVariantList() << v1->id() << r1->id() );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 << r1 );

// using two existing map layers
params.insert( "non_optional", QVariantList() << QVariant::fromValue( v1 ) << QVariant::fromValue( r1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 << r1 );

// mix of existing layers and non project layer string
params.insert( "non_optional", QVariantList() << v1->id() << raster2 );
QList< QgsMapLayer *> layers = QgsProcessingParameters::parameterAsLayerList( def.get(), params, context );
@@ -1756,6 +1776,14 @@ void TestQgsProcessing::parameterLayerList()
def.reset( new QgsProcessingParameterMultipleLayers( "optional", QString(), QgsProcessingParameterDefinition::TypeAny, QVariantList() << v1->id() << r1->publicSource(), true ) );
params.insert( "optional", QVariant() );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 << r1 );

// optional with one default direct layer
def.reset( new QgsProcessingParameterMultipleLayers( "optional", QString(), QgsProcessingParameterDefinition::TypeAny, QVariant::fromValue( v1 ), true ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 );

// optional with two default direct layers
def.reset( new QgsProcessingParameterMultipleLayers( "optional", QString(), QgsProcessingParameterDefinition::TypeAny, QVariantList() << QVariant::fromValue( v1 ) << QVariant::fromValue( r1 ), true ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayerList( def.get(), params, context ), QList< QgsMapLayer *>() << v1 << r1 );
}

void TestQgsProcessing::parameterNumber()
@@ -1915,6 +1943,8 @@ void TestQgsProcessing::parameterRasterLayer()
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );

// should be OK
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif" ) );
@@ -1926,10 +1956,18 @@ void TestQgsProcessing::parameterRasterLayer()
params.insert( "non_optional", r1->id() );
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );

// using existing map layer
params.insert( "non_optional", QVariant::fromValue( r1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );

// not raster layer
params.insert( "non_optional", v1->id() );
QVERIFY( !QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context ) );

// using existing vector layer
params.insert( "non_optional", QVariant::fromValue( v1 ) );
QVERIFY( !QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context ) );

// string representing a project layer source
params.insert( "non_optional", raster1 );
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );
@@ -1943,7 +1981,7 @@ void TestQgsProcessing::parameterRasterLayer()

// optional
def.reset( new QgsProcessingParameterRasterLayer( "optional", QString(), r1->id(), true ) );
QCOMPARE( QgsProcessingParameters::parameterAsLayer( def.get(), params, context )->id(), r1->id() );
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );
QVERIFY( def->checkValueIsAcceptable( false ) );
QVERIFY( def->checkValueIsAcceptable( true ) );
QVERIFY( def->checkValueIsAcceptable( 5 ) );
@@ -1954,6 +1992,10 @@ void TestQgsProcessing::parameterRasterLayer()

params.insert( "optional", QVariant() );
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );

// optional with direct layer
def.reset( new QgsProcessingParameterRasterLayer( "optional", QString(), QVariant::fromValue( r1 ), true ) );
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );
}

void TestQgsProcessing::parameterEnum()
@@ -2219,6 +2261,8 @@ void TestQgsProcessing::parameterFeatureSource()
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( "layer1231123" ) ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );

// should be OK
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
@@ -2230,10 +2274,18 @@ void TestQgsProcessing::parameterFeatureSource()
params.insert( "non_optional", v1->id() );
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );

// using existing layer
params.insert( "non_optional", QVariant::fromValue( v1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );

// not vector layer
params.insert( "non_optional", r1->id() );
QVERIFY( !QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context ) );

// using existing non-vector layer
params.insert( "non_optional", QVariant::fromValue( r1 ) );
QVERIFY( !QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context ) );

// string representing a layer source
params.insert( "non_optional", vector1 );
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->publicSource(), vector1 );
@@ -2254,6 +2306,10 @@ void TestQgsProcessing::parameterFeatureSource()
QVERIFY( def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( "layer1231123" ) ) );

//optional with direct layer default
def.reset( new QgsProcessingParameterFeatureSource( "optional", QString(), QList< int >() << QgsProcessingParameterDefinition::TypeVectorAny, QVariant::fromValue( v1 ), true ) );
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );
}

void TestQgsProcessing::parameterFeatureSink()

0 comments on commit e6a71ab

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