Skip to content
Permalink
Browse files

Add method to generate temporary destination parameter value

  • Loading branch information
nyalldawson committed Jun 21, 2017
1 parent a87ca09 commit 780f4331ccceb687245dd399d5f53223e5875747
@@ -1361,6 +1361,14 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
virtual bool fromVariantMap( const QVariantMap &map );


virtual QString generateTemporaryDestination() const;
%Docstring
Generates a temporary destination value for this parameter. The returned
value will be a file path or QGIS data provider URI suitable for
temporary storage of created layers and files.
:rtype: str
%End

};


@@ -1414,6 +1422,8 @@ class QgsProcessingParameterVectorOutput : QgsProcessingParameterDefinition

virtual bool fromVariantMap( const QVariantMap &map );

virtual QString generateTemporaryDestination() const;


};

@@ -2114,6 +2114,14 @@ bool QgsProcessingParameterFeatureSink::fromVariantMap( const QVariantMap &map )
return true;
}

QString QgsProcessingParameterFeatureSink::generateTemporaryDestination() const
{
if ( supportsNonFileBasedOutputs() )
return QStringLiteral( "memory:" );
else
return QgsProcessingDestinationParameter::generateTemporaryDestination();
}

QgsProcessingParameterRasterOutput::QgsProcessingParameterRasterOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
{}
@@ -2331,6 +2339,11 @@ bool QgsProcessingDestinationParameter::fromVariantMap( const QVariantMap &map )
return true;
}

QString QgsProcessingDestinationParameter::generateTemporaryDestination() const
{
return QgsProcessingUtils::generateTempFilename( name() + '.' + defaultFileExtension() );
}

QgsProcessingParameterVectorOutput::QgsProcessingParameterVectorOutput( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mDataType( type )
@@ -1313,6 +1313,13 @@ class CORE_EXPORT QgsProcessingDestinationParameter : public QgsProcessingParame
*/
virtual QString defaultFileExtension() const = 0;

/**
* Generates a temporary destination value for this parameter. The returned
* value will be a file path or QGIS data provider URI suitable for
* temporary storage of created layers and files.
*/
virtual QString generateTemporaryDestination() const;

private:

bool mSupportsNonFileBasedOutputs = true;
@@ -1364,6 +1371,7 @@ class CORE_EXPORT QgsProcessingParameterFeatureSink : public QgsProcessingDestin

QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;
QString generateTemporaryDestination() const override;

private:

@@ -2812,6 +2812,10 @@ void TestQgsProcessing::parameterFeatureSink()
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );

QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
QCOMPARE( def->generateTemporaryDestination(), QStringLiteral( "memory:" ) );
def->setSupportsNonFileBasedOutputs( false );
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".shp" ) ) );
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );

QVariantMap map = def->toVariantMap();
QgsProcessingParameterFeatureSink fromMap( "x" );
@@ -2933,6 +2937,8 @@ void TestQgsProcessing::parameterRasterOut()
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif", &context ) );

QCOMPARE( def->defaultFileExtension(), QStringLiteral( "tif" ) );
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".tif" ) ) );
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );

QVariantMap params;
params.insert( "non_optional", "test.tif" );
@@ -2985,6 +2991,8 @@ void TestQgsProcessing::parameterFileOut()
std::unique_ptr< QgsProcessingParameterFileOutput > def( new QgsProcessingParameterFileOutput( "non_optional", QString(), QStringLiteral( "BMP files (*.bmp)" ), QString(), false ) );
QCOMPARE( def->fileFilter(), QStringLiteral( "BMP files (*.bmp)" ) );
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "bmp" ) );
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".bmp" ) ) );
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
def->setFileFilter( QStringLiteral( "PCX files (*.pcx)" ) );
QCOMPARE( def->fileFilter(), QStringLiteral( "PCX files (*.pcx)" ) );
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "pcx" ) );
@@ -2994,6 +3002,8 @@ void TestQgsProcessing::parameterFileOut()
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "pcx" ) );
def->setFileFilter( QString() );
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "file" ) );
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".file" ) ) );
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );

QVERIFY( !def->checkValueIsAcceptable( false ) );
QVERIFY( !def->checkValueIsAcceptable( true ) );

0 comments on commit 780f433

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