Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method to generate temporary destination parameter value
  • Loading branch information
nyalldawson committed Jun 21, 2017
1 parent a87ca09 commit 780f433
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -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

};


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

virtual bool fromVariantMap( const QVariantMap &map );

virtual QString generateTemporaryDestination() const;


};

Expand Down
13 changes: 13 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -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 )
{}
Expand Down Expand Up @@ -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 )
Expand Down
8 changes: 8 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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:

Expand Down
10 changes: 10 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -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" );
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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" ) );
Expand All @@ -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 ) );
Expand Down

0 comments on commit 780f433

Please sign in to comment.