Skip to content

Commit c685ec2

Browse files
committedJun 21, 2017
Add missing tests for vector input parameter, add vector out parameter
1 parent 0da3652 commit c685ec2

File tree

4 files changed

+407
-1
lines changed

4 files changed

+407
-1
lines changed
 

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ class QgsProcessingParameterDefinition
171171
sipType = sipType_QgsProcessingParameterFeatureSource;
172172
else if ( sipCpp->type() == "sink" )
173173
sipType = sipType_QgsProcessingParameterFeatureSink;
174+
else if ( sipCpp->type() == "vectorOut" )
175+
sipType = sipType_QgsProcessingParameterVectorOutput;
174176
else if ( sipCpp->type() == "rasterOut" )
175177
sipType = sipType_QgsProcessingParameterRasterOutput;
176178
else if ( sipCpp->type() == "fileOut" )
@@ -1173,6 +1175,10 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
11731175
%End
11741176

11751177
virtual QString type() const;
1178+
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
1179+
1180+
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
1181+
11761182

11771183
};
11781184

@@ -1355,6 +1361,60 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
13551361
virtual bool fromVariantMap( const QVariantMap &map );
13561362

13571363

1364+
};
1365+
1366+
1367+
class QgsProcessingParameterVectorOutput : QgsProcessingParameterDefinition
1368+
{
1369+
%Docstring
1370+
A vector layer output parameter. Consider using the more flexible QgsProcessingParameterFeatureSink wherever
1371+
possible.
1372+
.. versionadded:: 3.0
1373+
%End
1374+
1375+
%TypeHeaderCode
1376+
#include "qgsprocessingparameters.h"
1377+
%End
1378+
public:
1379+
1380+
QgsProcessingParameterVectorOutput( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
1381+
bool optional = false );
1382+
%Docstring
1383+
Constructor for QgsProcessingParameterVectorOutput.
1384+
%End
1385+
1386+
virtual QString type() const;
1387+
virtual bool isDestination() const;
1388+
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
1389+
1390+
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
1391+
1392+
1393+
QgsProcessingParameterDefinition::LayerType dataType() const;
1394+
%Docstring
1395+
Returns the layer type for layers associated with the parameter.
1396+
.. seealso:: setDataType()
1397+
:rtype: QgsProcessingParameterDefinition.LayerType
1398+
%End
1399+
1400+
bool hasGeometry() const;
1401+
%Docstring
1402+
Returns true if the layer is likely to include geometries. In cases were presence of geometry
1403+
cannot be reliably determined in advance, this method will default to returning true.
1404+
:rtype: bool
1405+
%End
1406+
1407+
void setDataType( QgsProcessingParameterDefinition::LayerType type );
1408+
%Docstring
1409+
Sets the layer ``type`` for the layers associated with the parameter.
1410+
.. seealso:: dataType()
1411+
%End
1412+
1413+
virtual QVariantMap toVariantMap() const;
1414+
1415+
virtual bool fromVariantMap( const QVariantMap &map );
1416+
1417+
13581418
};
13591419

13601420
class QgsProcessingParameterRasterOutput : QgsProcessingParameterDefinition

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,14 +744,16 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
744744
def.reset( new QgsProcessingParameterString( name ) );
745745
else if ( type == QStringLiteral( "expression" ) )
746746
def.reset( new QgsProcessingParameterExpression( name ) );
747-
else if ( type == QStringLiteral( "table" ) )
747+
else if ( type == QStringLiteral( "vector" ) )
748748
def.reset( new QgsProcessingParameterVectorLayer( name ) );
749749
else if ( type == QStringLiteral( "field" ) )
750750
def.reset( new QgsProcessingParameterField( name ) );
751751
else if ( type == QStringLiteral( "source" ) )
752752
def.reset( new QgsProcessingParameterFeatureSource( name ) );
753753
else if ( type == QStringLiteral( "sink" ) )
754754
def.reset( new QgsProcessingParameterFeatureSink( name ) );
755+
else if ( type == QStringLiteral( "vectorOut" ) )
756+
def.reset( new QgsProcessingParameterVectorOutput( name ) );
755757
else if ( type == QStringLiteral( "rasterOut" ) )
756758
def.reset( new QgsProcessingParameterRasterOutput( name ) );
757759
else if ( type == QStringLiteral( "fileOut" ) )
@@ -1737,6 +1739,46 @@ QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QStr
17371739

17381740
}
17391741

1742+
bool QgsProcessingParameterVectorLayer::checkValueIsAcceptable( const QVariant &var, QgsProcessingContext *context ) const
1743+
{
1744+
if ( !var.isValid() )
1745+
return mFlags & FlagOptional;
1746+
1747+
if ( var.canConvert<QgsProperty>() )
1748+
{
1749+
return true;
1750+
}
1751+
1752+
if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
1753+
return true;
1754+
1755+
if ( var.type() != QVariant::String || var.toString().isEmpty() )
1756+
return mFlags & FlagOptional;
1757+
1758+
if ( !context )
1759+
{
1760+
// that's as far as we can get without a context
1761+
return true;
1762+
}
1763+
1764+
// try to load as layer
1765+
if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context ) )
1766+
return true;
1767+
1768+
return false;
1769+
}
1770+
1771+
QString QgsProcessingParameterVectorLayer::valueAsPythonString( const QVariant &val, QgsProcessingContext &context ) const
1772+
{
1773+
if ( val.canConvert<QgsProperty>() )
1774+
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
1775+
1776+
QVariantMap p;
1777+
p.insert( name(), val );
1778+
QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( this, p, context );
1779+
return layer ? QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' ) : QString();
1780+
}
1781+
17401782
QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional )
17411783
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
17421784
, mParentLayerParameter( parentLayerParameterName )
@@ -2208,3 +2250,100 @@ bool QgsProcessingParameterFolderOutput::checkValueIsAcceptable( const QVariant
22082250

22092251
return true;
22102252
}
2253+
2254+
QgsProcessingParameterVectorOutput::QgsProcessingParameterVectorOutput( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
2255+
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2256+
, mDataType( type )
2257+
{
2258+
2259+
}
2260+
2261+
bool QgsProcessingParameterVectorOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
2262+
{
2263+
QVariant var = input;
2264+
if ( !var.isValid() )
2265+
return mFlags & FlagOptional;
2266+
2267+
if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
2268+
{
2269+
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
2270+
var = fromVar.sink;
2271+
}
2272+
2273+
if ( var.canConvert<QgsProperty>() )
2274+
{
2275+
return true;
2276+
}
2277+
2278+
if ( var.type() != QVariant::String )
2279+
return false;
2280+
2281+
if ( var.toString().isEmpty() )
2282+
return mFlags & FlagOptional;
2283+
2284+
return true;
2285+
}
2286+
2287+
QString QgsProcessingParameterVectorOutput::valueAsPythonString( const QVariant &value, QgsProcessingContext & ) const
2288+
{
2289+
if ( value.canConvert<QgsProperty>() )
2290+
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2291+
2292+
if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
2293+
{
2294+
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( value );
2295+
if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
2296+
{
2297+
return QStringLiteral( "QgsProcessingOutputLayerDefinition('%1')" ).arg( fromVar.sink.staticValue().toString() );
2298+
}
2299+
else
2300+
{
2301+
return QStringLiteral( "QgsProcessingOutputLayerDefinition(QgsProperty.fromExpression('%1'))" ).arg( fromVar.sink.asExpression() );
2302+
}
2303+
}
2304+
2305+
return value.toString().prepend( '\'' ).append( '\'' );
2306+
}
2307+
2308+
QgsProcessingParameterDefinition::LayerType QgsProcessingParameterVectorOutput::dataType() const
2309+
{
2310+
return mDataType;
2311+
}
2312+
2313+
bool QgsProcessingParameterVectorOutput::hasGeometry() const
2314+
{
2315+
switch ( mDataType )
2316+
{
2317+
case TypeAny:
2318+
case TypeVectorAny:
2319+
case TypeVectorPoint:
2320+
case TypeVectorLine:
2321+
case TypeVectorPolygon:
2322+
case TypeTable:
2323+
return true;
2324+
2325+
case TypeRaster:
2326+
case TypeFile:
2327+
return false;
2328+
}
2329+
return true;
2330+
}
2331+
2332+
void QgsProcessingParameterVectorOutput::setDataType( QgsProcessingParameterDefinition::LayerType type )
2333+
{
2334+
mDataType = type;
2335+
}
2336+
2337+
QVariantMap QgsProcessingParameterVectorOutput::toVariantMap() const
2338+
{
2339+
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
2340+
map.insert( QStringLiteral( "data_type" ), mDataType );
2341+
return map;
2342+
}
2343+
2344+
bool QgsProcessingParameterVectorOutput::fromVariantMap( const QVariantMap &map )
2345+
{
2346+
QgsProcessingParameterDefinition::fromVariantMap( map );
2347+
mDataType = static_cast< QgsProcessingParameterDefinition::LayerType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2348+
return true;
2349+
}

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
206206
sipType = sipType_QgsProcessingParameterFeatureSource;
207207
else if ( sipCpp->type() == "sink" )
208208
sipType = sipType_QgsProcessingParameterFeatureSink;
209+
else if ( sipCpp->type() == "vectorOut" )
210+
sipType = sipType_QgsProcessingParameterVectorOutput;
209211
else if ( sipCpp->type() == "rasterOut" )
210212
sipType = sipType_QgsProcessingParameterRasterOutput;
211213
else if ( sipCpp->type() == "fileOut" )
@@ -1138,6 +1140,8 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
11381140
bool optional = false );
11391141

11401142
QString type() const override { return QStringLiteral( "vector" ); }
1143+
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
1144+
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
11411145

11421146
};
11431147

@@ -1312,6 +1316,55 @@ class CORE_EXPORT QgsProcessingParameterFeatureSink : public QgsProcessingParame
13121316
QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
13131317
};
13141318

1319+
1320+
/**
1321+
* \class QgsProcessingParameterVectorOutput
1322+
* \ingroup core
1323+
* A vector layer output parameter. Consider using the more flexible QgsProcessingParameterFeatureSink wherever
1324+
* possible.
1325+
* \since QGIS 3.0
1326+
*/
1327+
class CORE_EXPORT QgsProcessingParameterVectorOutput : public QgsProcessingParameterDefinition
1328+
{
1329+
public:
1330+
1331+
/**
1332+
* Constructor for QgsProcessingParameterVectorOutput.
1333+
*/
1334+
QgsProcessingParameterVectorOutput( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
1335+
bool optional = false );
1336+
1337+
QString type() const override { return QStringLiteral( "vectorOut" ); }
1338+
bool isDestination() const override { return true; }
1339+
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
1340+
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
1341+
1342+
/**
1343+
* Returns the layer type for layers associated with the parameter.
1344+
* \see setDataType()
1345+
*/
1346+
QgsProcessingParameterDefinition::LayerType dataType() const;
1347+
1348+
/**
1349+
* Returns true if the layer is likely to include geometries. In cases were presence of geometry
1350+
* cannot be reliably determined in advance, this method will default to returning true.
1351+
*/
1352+
bool hasGeometry() const;
1353+
1354+
/**
1355+
* Sets the layer \a type for the layers associated with the parameter.
1356+
* \see dataType()
1357+
*/
1358+
void setDataType( QgsProcessingParameterDefinition::LayerType type );
1359+
1360+
QVariantMap toVariantMap() const override;
1361+
bool fromVariantMap( const QVariantMap &map ) override;
1362+
1363+
private:
1364+
1365+
QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
1366+
};
1367+
13151368
/**
13161369
* \class QgsProcessingParameterRasterOutput
13171370
* \ingroup core

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,10 @@ class TestQgsProcessing: public QObject
314314
void parameterString();
315315
void parameterExpression();
316316
void parameterField();
317+
void parameterVectorLayer();
317318
void parameterFeatureSource();
318319
void parameterFeatureSink();
320+
void parameterVectorOut();
319321
void parameterRasterOut();
320322
void parameterFileOut();
321323
void parameterFolderOut();
@@ -2593,6 +2595,96 @@ void TestQgsProcessing::parameterField()
25932595
QCOMPARE( fields, QStringList() << "abc" << "def" );
25942596
}
25952597

2598+
void TestQgsProcessing::parameterVectorLayer()
2599+
{
2600+
// setup a context
2601+
QgsProject p;
2602+
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 28353 ) );
2603+
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
2604+
QString vector1 = testDataDir + "multipoint.shp";
2605+
QString raster = testDataDir + "landsat.tif";
2606+
QFileInfo fi1( raster );
2607+
QFileInfo fi2( vector1 );
2608+
QgsRasterLayer *r1 = new QgsRasterLayer( fi1.filePath(), "R1" );
2609+
QgsVectorLayer *v1 = new QgsVectorLayer( fi2.filePath(), "V4", "ogr" );
2610+
p.addMapLayers( QList<QgsMapLayer *>() << v1 << r1 );
2611+
QgsProcessingContext context;
2612+
context.setProject( &p );
2613+
2614+
// not optional!
2615+
std::unique_ptr< QgsProcessingParameterVectorLayer > def( new QgsProcessingParameterVectorLayer( "non_optional", QString(), QString( "somelayer" ), false ) );
2616+
QVERIFY( !def->checkValueIsAcceptable( false ) );
2617+
QVERIFY( !def->checkValueIsAcceptable( true ) );
2618+
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
2619+
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
2620+
QVERIFY( !def->checkValueIsAcceptable( "" ) );
2621+
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
2622+
QVERIFY( !def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( "layer1231123" ) ) );
2623+
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );
2624+
QVERIFY( !def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
2625+
2626+
// should be OK
2627+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2628+
// ... unless we use context, when the check that the layer actually exists is performed
2629+
QVERIFY( !def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp", &context ) );
2630+
2631+
// using existing map layer ID
2632+
QVariantMap params;
2633+
params.insert( "non_optional", v1->id() );
2634+
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );
2635+
2636+
// using existing layer
2637+
params.insert( "non_optional", QVariant::fromValue( v1 ) );
2638+
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );
2639+
2640+
// not vector layer
2641+
params.insert( "non_optional", r1->id() );
2642+
QVERIFY( !QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context ) );
2643+
2644+
// using existing non-vector layer
2645+
params.insert( "non_optional", QVariant::fromValue( r1 ) );
2646+
QVERIFY( !QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context ) );
2647+
2648+
// string representing a layer source
2649+
params.insert( "non_optional", vector1 );
2650+
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->publicSource(), vector1 );
2651+
2652+
// nonsense string
2653+
params.insert( "non_optional", QString( "i'm not a layer, and nothing you can do will make me one" ) );
2654+
QVERIFY( !QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context ) );
2655+
2656+
QCOMPARE( def->valueAsPythonString( vector1, context ), QString( "'" ) + testDataDir + QStringLiteral( "multipoint.shp'" ) );
2657+
QCOMPARE( def->valueAsPythonString( v1->id(), context ), QString( "'" ) + testDataDir + QStringLiteral( "multipoint.shp'" ) );
2658+
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( v1 ), context ), QString( "'" ) + testDataDir + QStringLiteral( "multipoint.shp'" ) );
2659+
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
2660+
QVariantMap map = def->toVariantMap();
2661+
QgsProcessingParameterVectorLayer fromMap( "x" );
2662+
QVERIFY( fromMap.fromVariantMap( map ) );
2663+
QCOMPARE( fromMap.name(), def->name() );
2664+
QCOMPARE( fromMap.description(), def->description() );
2665+
QCOMPARE( fromMap.flags(), def->flags() );
2666+
QCOMPARE( fromMap.defaultValue(), def->defaultValue() );
2667+
def.reset( dynamic_cast< QgsProcessingParameterVectorLayer *>( QgsProcessingParameters::parameterFromVariantMap( map ) ) );
2668+
QVERIFY( dynamic_cast< QgsProcessingParameterVectorLayer *>( def.get() ) );
2669+
2670+
// optional
2671+
def.reset( new QgsProcessingParameterVectorLayer( "optional", QString(), v1->id(), true ) );
2672+
params.insert( "optional", QVariant() );
2673+
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );
2674+
QVERIFY( def->checkValueIsAcceptable( false ) );
2675+
QVERIFY( def->checkValueIsAcceptable( true ) );
2676+
QVERIFY( def->checkValueIsAcceptable( 5 ) );
2677+
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
2678+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2679+
QVERIFY( def->checkValueIsAcceptable( "" ) );
2680+
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
2681+
QVERIFY( def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( "layer1231123" ) ) );
2682+
2683+
//optional with direct layer default
2684+
def.reset( new QgsProcessingParameterVectorLayer( "optional", QString(), QVariant::fromValue( v1 ), true ) );
2685+
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );
2686+
}
2687+
25962688
void TestQgsProcessing::parameterFeatureSource()
25972689
{
25982690
// setup a context
@@ -2747,6 +2839,68 @@ void TestQgsProcessing::parameterFeatureSink()
27472839

27482840
}
27492841

2842+
void TestQgsProcessing::parameterVectorOut()
2843+
{
2844+
// setup a context
2845+
QgsProject p;
2846+
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 28353 ) );
2847+
QgsProcessingContext context;
2848+
context.setProject( &p );
2849+
2850+
// not optional!
2851+
std::unique_ptr< QgsProcessingParameterVectorOutput > def( new QgsProcessingParameterVectorOutput( "non_optional", QString(), QgsProcessingParameterDefinition::TypeVectorAny, QString( "EPSG:3113" ), false ) );
2852+
QVERIFY( !def->checkValueIsAcceptable( false ) );
2853+
QVERIFY( !def->checkValueIsAcceptable( true ) );
2854+
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
2855+
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
2856+
QVERIFY( !def->checkValueIsAcceptable( "" ) );
2857+
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
2858+
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
2859+
2860+
// should be OK with or without context - it's an output layer!
2861+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2862+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp", &context ) );
2863+
2864+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc" ), context ), QStringLiteral( "'abc'" ) );
2865+
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( "abc" ) ), context ), QStringLiteral( "QgsProcessingOutputLayerDefinition('abc')" ) );
2866+
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromValue( "abc" ) ) ), context ), QStringLiteral( "QgsProcessingOutputLayerDefinition('abc')" ) );
2867+
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProcessingOutputLayerDefinition(QgsProperty.fromExpression('\"abc\" || \"def\"'))" ) );
2868+
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
2869+
2870+
QVariantMap map = def->toVariantMap();
2871+
QgsProcessingParameterVectorOutput fromMap( "x" );
2872+
QVERIFY( fromMap.fromVariantMap( map ) );
2873+
QCOMPARE( fromMap.name(), def->name() );
2874+
QCOMPARE( fromMap.description(), def->description() );
2875+
QCOMPARE( fromMap.flags(), def->flags() );
2876+
QCOMPARE( fromMap.defaultValue(), def->defaultValue() );
2877+
QCOMPARE( fromMap.dataType(), def->dataType() );
2878+
def.reset( dynamic_cast< QgsProcessingParameterVectorOutput *>( QgsProcessingParameters::parameterFromVariantMap( map ) ) );
2879+
QVERIFY( dynamic_cast< QgsProcessingParameterVectorOutput *>( def.get() ) );
2880+
2881+
// optional
2882+
def.reset( new QgsProcessingParameterVectorOutput( "optional", QString(), QgsProcessingParameterDefinition::TypeVectorAny, QString(), true ) );
2883+
QVERIFY( !def->checkValueIsAcceptable( false ) );
2884+
QVERIFY( !def->checkValueIsAcceptable( true ) );
2885+
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
2886+
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
2887+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2888+
QVERIFY( def->checkValueIsAcceptable( "" ) );
2889+
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
2890+
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
2891+
2892+
// test hasGeometry
2893+
QVERIFY( QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeAny ).hasGeometry() );
2894+
QVERIFY( QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeVectorAny ).hasGeometry() );
2895+
QVERIFY( QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeVectorPoint ).hasGeometry() );
2896+
QVERIFY( QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeVectorLine ).hasGeometry() );
2897+
QVERIFY( QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeVectorPolygon ).hasGeometry() );
2898+
QVERIFY( !QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeRaster ).hasGeometry() );
2899+
QVERIFY( !QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeFile ).hasGeometry() );
2900+
QVERIFY( QgsProcessingParameterVectorOutput( "test", QString(), QgsProcessingParameterDefinition::TypeTable ).hasGeometry() );
2901+
2902+
}
2903+
27502904
void TestQgsProcessing::parameterRasterOut()
27512905
{
27522906
// setup a context

0 commit comments

Comments
 (0)
Please sign in to comment.