Skip to content
Permalink
Browse files

Add QgsProcessingParameterFolderOuput

  • Loading branch information
nyalldawson committed Jun 13, 2017
1 parent be45c2f commit b45e5725003333f9991c8c8687b33182e9c6741a
@@ -175,6 +175,8 @@ class QgsProcessingParameterDefinition
sipType = sipType_QgsProcessingParameterRasterOutput;
else if ( sipCpp->type() == "fileOut" )
sipType = sipType_QgsProcessingParameterFileOutput;
else if ( sipCpp->type() == "folderOut" )
sipType = sipType_QgsProcessingParameterFolderOutput;
%End
public:

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

};

class QgsProcessingParameterFolderOutput : QgsProcessingParameterDefinition
{
%Docstring
A folder output parameter.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingParameterFolderOutput( const QString &name, const QString &description = QString(),
const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterFolderOutput.
%End

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


};



/************************************************************************
@@ -1912,3 +1912,27 @@ void QgsProcessingParameterFileOutput::setFileFilter( const QString &fileFilter
{
mFileFilter = fileFilter;
}

QgsProcessingParameterFolderOutput::QgsProcessingParameterFolderOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
{}

bool QgsProcessingParameterFolderOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
{
QVariant var = input;
if ( !var.isValid() )
return mFlags & FlagOptional;

if ( var.canConvert<QgsProperty>() )
{
return true;
}

if ( var.type() != QVariant::String )
return false;

if ( var.toString().isEmpty() )
return mFlags & FlagOptional;

return true;
}
@@ -210,6 +210,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
sipType = sipType_QgsProcessingParameterRasterOutput;
else if ( sipCpp->type() == "fileOut" )
sipType = sipType_QgsProcessingParameterFileOutput;
else if ( sipCpp->type() == "folderOut" )
sipType = sipType_QgsProcessingParameterFolderOutput;
SIP_END
#endif

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

/**
* \class QgsProcessingParameterFolderOutput
* \ingroup core
* A folder output parameter.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterFolderOutput : public QgsProcessingParameterDefinition
{
public:

/**
* Constructor for QgsProcessingParameterFolderOutput.
*/
QgsProcessingParameterFolderOutput( const QString &name, const QString &description = QString(),
const QVariant &defaultValue = QVariant(),
bool optional = false );

QString type() const override { return QStringLiteral( "folderOut" ); }
bool isDestination() const override { return true; }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;

};

#endif // QGSPROCESSINGPARAMETERS_H


@@ -309,6 +309,7 @@ class TestQgsProcessing: public QObject
void parameterFeatureSink();
void parameterRasterOut();
void parameterFileOut();
void parameterFolderOut();
void checkParamValues();
void combineLayerExtent();
void processingFeatureSource();
@@ -2651,6 +2652,48 @@ void TestQgsProcessing::parameterFileOut()
QCOMPARE( QgsProcessingParameters::parameterAsFileOutput( def.get(), params, context ), QStringLiteral( "default.txt" ) );
}

void TestQgsProcessing::parameterFolderOut()
{
// setup a context
QgsProject p;
QgsProcessingContext context;
context.setProject( &p );

// not optional!
std::unique_ptr< QgsProcessingParameterFolderOutput > def( new QgsProcessingParameterFolderOutput( "non_optional", QString(), QString(), false ) );

QVERIFY( !def->checkValueIsAcceptable( false ) );
QVERIFY( !def->checkValueIsAcceptable( true ) );
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "asdasd" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );

// should be OK with or without context - it's an output folder!
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/" ) );
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/", &context ) );

QVariantMap params;
params.insert( "non_optional", "c:/mine" );
QCOMPARE( QgsProcessingParameters::parameterAsFileOutput( def.get(), params, context ), QStringLiteral( "c:/mine" ) );

QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc" ), context ), QStringLiteral( "'abc'" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );

// optional
def.reset( new QgsProcessingParameterFolderOutput( "optional", QString(), QString( "c:/junk" ), true ) );
QVERIFY( !def->checkValueIsAcceptable( false ) );
QVERIFY( !def->checkValueIsAcceptable( true ) );
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/" ) );
QVERIFY( def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );

params.insert( "optional", QVariant() );
QCOMPARE( QgsProcessingParameters::parameterAsFileOutput( def.get(), params, context ), QStringLiteral( "c:/junk" ) );
}

void TestQgsProcessing::checkParamValues()
{
DummyAlgorithm a( "asd" );

0 comments on commit b45e572

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