Skip to content

Commit 780f433

Browse files
committed
Add method to generate temporary destination parameter value
1 parent a87ca09 commit 780f433

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

python/core/processing/qgsprocessingparameters.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,14 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
13611361
virtual bool fromVariantMap( const QVariantMap &map );
13621362

13631363

1364+
virtual QString generateTemporaryDestination() const;
1365+
%Docstring
1366+
Generates a temporary destination value for this parameter. The returned
1367+
value will be a file path or QGIS data provider URI suitable for
1368+
temporary storage of created layers and files.
1369+
:rtype: str
1370+
%End
1371+
13641372
};
13651373

13661374

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

14151423
virtual bool fromVariantMap( const QVariantMap &map );
14161424

1425+
virtual QString generateTemporaryDestination() const;
1426+
14171427

14181428
};
14191429

src/core/processing/qgsprocessingparameters.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,14 @@ bool QgsProcessingParameterFeatureSink::fromVariantMap( const QVariantMap &map )
21142114
return true;
21152115
}
21162116

2117+
QString QgsProcessingParameterFeatureSink::generateTemporaryDestination() const
2118+
{
2119+
if ( supportsNonFileBasedOutputs() )
2120+
return QStringLiteral( "memory:" );
2121+
else
2122+
return QgsProcessingDestinationParameter::generateTemporaryDestination();
2123+
}
2124+
21172125
QgsProcessingParameterRasterOutput::QgsProcessingParameterRasterOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
21182126
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
21192127
{}
@@ -2331,6 +2339,11 @@ bool QgsProcessingDestinationParameter::fromVariantMap( const QVariantMap &map )
23312339
return true;
23322340
}
23332341

2342+
QString QgsProcessingDestinationParameter::generateTemporaryDestination() const
2343+
{
2344+
return QgsProcessingUtils::generateTempFilename( name() + '.' + defaultFileExtension() );
2345+
}
2346+
23342347
QgsProcessingParameterVectorOutput::QgsProcessingParameterVectorOutput( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
23352348
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
23362349
, mDataType( type )

src/core/processing/qgsprocessingparameters.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,13 @@ class CORE_EXPORT QgsProcessingDestinationParameter : public QgsProcessingParame
13131313
*/
13141314
virtual QString defaultFileExtension() const = 0;
13151315

1316+
/**
1317+
* Generates a temporary destination value for this parameter. The returned
1318+
* value will be a file path or QGIS data provider URI suitable for
1319+
* temporary storage of created layers and files.
1320+
*/
1321+
virtual QString generateTemporaryDestination() const;
1322+
13161323
private:
13171324

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

13651372
QVariantMap toVariantMap() const override;
13661373
bool fromVariantMap( const QVariantMap &map ) override;
1374+
QString generateTemporaryDestination() const override;
13671375

13681376
private:
13691377

tests/src/core/testqgsprocessing.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,10 @@ void TestQgsProcessing::parameterFeatureSink()
28122812
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
28132813

28142814
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
2815+
QCOMPARE( def->generateTemporaryDestination(), QStringLiteral( "memory:" ) );
2816+
def->setSupportsNonFileBasedOutputs( false );
2817+
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".shp" ) ) );
2818+
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
28152819

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

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

29372943
QVariantMap params;
29382944
params.insert( "non_optional", "test.tif" );
@@ -2985,6 +2991,8 @@ void TestQgsProcessing::parameterFileOut()
29852991
std::unique_ptr< QgsProcessingParameterFileOutput > def( new QgsProcessingParameterFileOutput( "non_optional", QString(), QStringLiteral( "BMP files (*.bmp)" ), QString(), false ) );
29862992
QCOMPARE( def->fileFilter(), QStringLiteral( "BMP files (*.bmp)" ) );
29872993
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "bmp" ) );
2994+
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".bmp" ) ) );
2995+
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
29882996
def->setFileFilter( QStringLiteral( "PCX files (*.pcx)" ) );
29892997
QCOMPARE( def->fileFilter(), QStringLiteral( "PCX files (*.pcx)" ) );
29902998
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "pcx" ) );
@@ -2994,6 +3002,8 @@ void TestQgsProcessing::parameterFileOut()
29943002
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "pcx" ) );
29953003
def->setFileFilter( QString() );
29963004
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "file" ) );
3005+
QVERIFY( def->generateTemporaryDestination().endsWith( QStringLiteral( ".file" ) ) );
3006+
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
29973007

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

0 commit comments

Comments
 (0)