Skip to content

Commit b45e572

Browse files
committedJun 13, 2017
Add QgsProcessingParameterFolderOuput
1 parent be45c2f commit b45e572

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
 

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class QgsProcessingParameterDefinition
175175
sipType = sipType_QgsProcessingParameterRasterOutput;
176176
else if ( sipCpp->type() == "fileOut" )
177177
sipType = sipType_QgsProcessingParameterFileOutput;
178+
else if ( sipCpp->type() == "folderOut" )
179+
sipType = sipType_QgsProcessingParameterFolderOutput;
178180
%End
179181
public:
180182

@@ -1344,6 +1346,32 @@ class QgsProcessingParameterFileOutput : QgsProcessingParameterDefinition
13441346

13451347
};
13461348

1349+
class QgsProcessingParameterFolderOutput : QgsProcessingParameterDefinition
1350+
{
1351+
%Docstring
1352+
A folder output parameter.
1353+
.. versionadded:: 3.0
1354+
%End
1355+
1356+
%TypeHeaderCode
1357+
#include "qgsprocessingparameters.h"
1358+
%End
1359+
public:
1360+
1361+
QgsProcessingParameterFolderOutput( const QString &name, const QString &description = QString(),
1362+
const QVariant &defaultValue = QVariant(),
1363+
bool optional = false );
1364+
%Docstring
1365+
Constructor for QgsProcessingParameterFolderOutput.
1366+
%End
1367+
1368+
virtual QString type() const;
1369+
virtual bool isDestination() const;
1370+
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
1371+
1372+
1373+
};
1374+
13471375

13481376

13491377
/************************************************************************

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,3 +1912,27 @@ void QgsProcessingParameterFileOutput::setFileFilter( const QString &fileFilter
19121912
{
19131913
mFileFilter = fileFilter;
19141914
}
1915+
1916+
QgsProcessingParameterFolderOutput::QgsProcessingParameterFolderOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1917+
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1918+
{}
1919+
1920+
bool QgsProcessingParameterFolderOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
1921+
{
1922+
QVariant var = input;
1923+
if ( !var.isValid() )
1924+
return mFlags & FlagOptional;
1925+
1926+
if ( var.canConvert<QgsProperty>() )
1927+
{
1928+
return true;
1929+
}
1930+
1931+
if ( var.type() != QVariant::String )
1932+
return false;
1933+
1934+
if ( var.toString().isEmpty() )
1935+
return mFlags & FlagOptional;
1936+
1937+
return true;
1938+
}

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
210210
sipType = sipType_QgsProcessingParameterRasterOutput;
211211
else if ( sipCpp->type() == "fileOut" )
212212
sipType = sipType_QgsProcessingParameterFileOutput;
213+
else if ( sipCpp->type() == "folderOut" )
214+
sipType = sipType_QgsProcessingParameterFolderOutput;
213215
SIP_END
214216
#endif
215217

@@ -1318,6 +1320,29 @@ class CORE_EXPORT QgsProcessingParameterFileOutput : public QgsProcessingParamet
13181320
QString mFileFilter;
13191321
};
13201322

1323+
/**
1324+
* \class QgsProcessingParameterFolderOutput
1325+
* \ingroup core
1326+
* A folder output parameter.
1327+
* \since QGIS 3.0
1328+
*/
1329+
class CORE_EXPORT QgsProcessingParameterFolderOutput : public QgsProcessingParameterDefinition
1330+
{
1331+
public:
1332+
1333+
/**
1334+
* Constructor for QgsProcessingParameterFolderOutput.
1335+
*/
1336+
QgsProcessingParameterFolderOutput( const QString &name, const QString &description = QString(),
1337+
const QVariant &defaultValue = QVariant(),
1338+
bool optional = false );
1339+
1340+
QString type() const override { return QStringLiteral( "folderOut" ); }
1341+
bool isDestination() const override { return true; }
1342+
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
1343+
1344+
};
1345+
13211346
#endif // QGSPROCESSINGPARAMETERS_H
13221347

13231348

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ class TestQgsProcessing: public QObject
309309
void parameterFeatureSink();
310310
void parameterRasterOut();
311311
void parameterFileOut();
312+
void parameterFolderOut();
312313
void checkParamValues();
313314
void combineLayerExtent();
314315
void processingFeatureSource();
@@ -2651,6 +2652,48 @@ void TestQgsProcessing::parameterFileOut()
26512652
QCOMPARE( QgsProcessingParameters::parameterAsFileOutput( def.get(), params, context ), QStringLiteral( "default.txt" ) );
26522653
}
26532654

2655+
void TestQgsProcessing::parameterFolderOut()
2656+
{
2657+
// setup a context
2658+
QgsProject p;
2659+
QgsProcessingContext context;
2660+
context.setProject( &p );
2661+
2662+
// not optional!
2663+
std::unique_ptr< QgsProcessingParameterFolderOutput > def( new QgsProcessingParameterFolderOutput( "non_optional", QString(), QString(), false ) );
2664+
2665+
QVERIFY( !def->checkValueIsAcceptable( false ) );
2666+
QVERIFY( !def->checkValueIsAcceptable( true ) );
2667+
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
2668+
QVERIFY( def->checkValueIsAcceptable( "asdasd" ) );
2669+
QVERIFY( !def->checkValueIsAcceptable( "" ) );
2670+
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
2671+
2672+
// should be OK with or without context - it's an output folder!
2673+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/" ) );
2674+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/", &context ) );
2675+
2676+
QVariantMap params;
2677+
params.insert( "non_optional", "c:/mine" );
2678+
QCOMPARE( QgsProcessingParameters::parameterAsFileOutput( def.get(), params, context ), QStringLiteral( "c:/mine" ) );
2679+
2680+
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc" ), context ), QStringLiteral( "'abc'" ) );
2681+
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
2682+
2683+
// optional
2684+
def.reset( new QgsProcessingParameterFolderOutput( "optional", QString(), QString( "c:/junk" ), true ) );
2685+
QVERIFY( !def->checkValueIsAcceptable( false ) );
2686+
QVERIFY( !def->checkValueIsAcceptable( true ) );
2687+
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
2688+
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
2689+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/" ) );
2690+
QVERIFY( def->checkValueIsAcceptable( "" ) );
2691+
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
2692+
2693+
params.insert( "optional", QVariant() );
2694+
QCOMPARE( QgsProcessingParameters::parameterAsFileOutput( def.get(), params, context ), QStringLiteral( "c:/junk" ) );
2695+
}
2696+
26542697
void TestQgsProcessing::checkParamValues()
26552698
{
26562699
DummyAlgorithm a( "asd" );

0 commit comments

Comments
 (0)
Please sign in to comment.