Skip to content

Commit e8a03b9

Browse files
committed
Move default file extension code to c++
1 parent b1879df commit e8a03b9

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
lines changed

python/plugins/processing/tools/system.py

-8
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ def tempFolder():
7474
return str(os.path.abspath(tempDir))
7575

7676

77-
def setTempOutput(out, alg):
78-
if hasattr(out, 'directory'):
79-
out.value = getTempDirInTempFolder()
80-
else:
81-
ext = out.getDefaultFileExtension(alg)
82-
out.value = getTempFilenameInTempFolder(out.name + '.' + ext)
83-
84-
8577
def getTempFilename(ext=None):
8678
tmpPath = tempFolder()
8779
t = time.time()

src/core/processing/qgsprocessingalgorithm.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ QgsProcessingProvider *QgsProcessingAlgorithm::provider() const
9595
void QgsProcessingAlgorithm::setProvider( QgsProcessingProvider *provider )
9696
{
9797
mProvider = provider;
98+
99+
// need to update all destination parameters to set whether the provider supports non file based outputs
100+
Q_FOREACH ( const QgsProcessingParameterDefinition *definition, mParameters )
101+
{
102+
if ( definition->isDestination() && mProvider )
103+
{
104+
const QgsProcessingDestinationParameter *destParam = static_cast< const QgsProcessingDestinationParameter *>( definition );
105+
const_cast< QgsProcessingDestinationParameter *>( destParam )->setSupportsNonFileBasedOutputs( mProvider->supportsNonFileBasedOutput() );
106+
}
107+
}
98108
}
99109

100110
QWidget *QgsProcessingAlgorithm::createCustomParametersWidget( QWidget * ) const
@@ -214,7 +224,7 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def
214224
if ( QgsProcessingAlgorithm::parameterDefinition( definition->name() ) )
215225
return false;
216226

217-
if ( definition->isDestination() )
227+
if ( definition->isDestination() && mProvider )
218228
{
219229
QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition );
220230
destParam->setSupportsNonFileBasedOutputs( mProvider->supportsNonFileBasedOutput() );

src/core/processing/qgsprocessingparameters.cpp

+40-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgsprocessingutils.h"
2121
#include "qgsvectorlayerfeatureiterator.h"
2222
#include "qgsprocessingoutputs.h"
23+
#include "qgssettings.h"
2324

2425
bool QgsProcessingParameters::isDynamic( const QVariantMap &parameters, const QString &name )
2526
{
@@ -2057,6 +2058,19 @@ QgsProcessingOutputDefinition *QgsProcessingParameterFeatureSink::toOutputDefini
20572058
return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
20582059
}
20592060

2061+
QString QgsProcessingParameterFeatureSink::defaultFileExtension() const
2062+
{
2063+
QgsSettings settings;
2064+
if ( hasGeometry() )
2065+
{
2066+
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
2067+
}
2068+
else
2069+
{
2070+
return QStringLiteral( "dbf" );
2071+
}
2072+
}
2073+
20602074
QgsProcessingParameterDefinition::LayerType QgsProcessingParameterFeatureSink::dataType() const
20612075
{
20622076
return mDataType;
@@ -2156,6 +2170,12 @@ QgsProcessingOutputDefinition *QgsProcessingParameterRasterOutput::toOutputDefin
21562170
return new QgsProcessingOutputRasterLayer( name(), description() );
21572171
}
21582172

2173+
QString QgsProcessingParameterRasterOutput::defaultFileExtension() const
2174+
{
2175+
QgsSettings settings;
2176+
return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
2177+
}
2178+
21592179

21602180
QgsProcessingParameterFileOutput::QgsProcessingParameterFileOutput( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional )
21612181
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
@@ -2218,6 +2238,20 @@ QgsProcessingOutputDefinition *QgsProcessingParameterFileOutput::toOutputDefinit
22182238
return nullptr;
22192239
}
22202240

2241+
QString QgsProcessingParameterFileOutput::defaultFileExtension() const
2242+
{
2243+
if ( mFileFilter.isEmpty() || mFileFilter == QObject::tr( "All files (*.*)" ) )
2244+
return QStringLiteral( "file" );
2245+
2246+
// get first extension from filter
2247+
QRegularExpression rx( ".*?\\(\\*\\.([a-zA-Z0-9._]+).*" );
2248+
QRegularExpressionMatch match = rx.match( mFileFilter );
2249+
if ( !match.hasMatch() )
2250+
return QStringLiteral( "file" );
2251+
2252+
return match.captured( 1 );
2253+
}
2254+
22212255
QString QgsProcessingParameterFileOutput::fileFilter() const
22222256
{
22232257
return mFileFilter;
@@ -2272,6 +2306,11 @@ QgsProcessingOutputDefinition *QgsProcessingParameterFolderOutput::toOutputDefin
22722306
return nullptr;
22732307
}
22742308

2309+
QString QgsProcessingParameterFolderOutput::defaultFileExtension() const
2310+
{
2311+
return QString();
2312+
}
2313+
22752314
QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
22762315
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
22772316
{
@@ -2281,7 +2320,7 @@ QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QStr
22812320
QVariantMap QgsProcessingDestinationParameter::toVariantMap() const
22822321
{
22832322
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
2284-
map.insert( QStringLiteral( "support_non_file_outputs" ), mSupportsNonFileBasedOutputs );
2323+
map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
22852324
return map;
22862325
}
22872326

src/core/processing/qgsprocessingparameters.h

+10
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,12 @@ class CORE_EXPORT QgsProcessingDestinationParameter : public QgsProcessingParame
13071307
*/
13081308
void setSupportsNonFileBasedOutputs( bool supportsNonFileBasedOutputs ) { mSupportsNonFileBasedOutputs = supportsNonFileBasedOutputs; }
13091309

1310+
/**
1311+
* Returns the default file extension for destination file paths
1312+
* associated with this parameter.
1313+
*/
1314+
virtual QString defaultFileExtension() const = 0;
1315+
13101316
private:
13111317

13121318
bool mSupportsNonFileBasedOutputs = true;
@@ -1336,6 +1342,7 @@ class CORE_EXPORT QgsProcessingParameterFeatureSink : public QgsProcessingDestin
13361342
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
13371343
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
13381344
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;
1345+
QString defaultFileExtension() const override;
13391346

13401347
/**
13411348
* Returns the layer type for sinks associated with the parameter.
@@ -1433,6 +1440,7 @@ class CORE_EXPORT QgsProcessingParameterRasterOutput : public QgsProcessingDesti
14331440
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
14341441
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
14351442
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;
1443+
QString defaultFileExtension() const override;
14361444
};
14371445

14381446
/**
@@ -1457,6 +1465,7 @@ class CORE_EXPORT QgsProcessingParameterFileOutput : public QgsProcessingDestina
14571465
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
14581466
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
14591467
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;
1468+
QString defaultFileExtension() const override;
14601469

14611470
/**
14621471
* Returns the file filter string for files compatible with this output.
@@ -1498,6 +1507,7 @@ class CORE_EXPORT QgsProcessingParameterFolderOutput : public QgsProcessingDesti
14981507
QString type() const override { return QStringLiteral( "folderOut" ); }
14991508
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
15001509
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;
1510+
QString defaultFileExtension() const override;
15011511

15021512
};
15031513

tests/src/core/testqgsprocessing.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,6 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
106106
QVERIFY( addParameter( p6 ) );
107107
QCOMPARE( destinationParameterDefinitions(), QgsProcessingParameterDefinitions() << p5 << p6 );
108108

109-
// check that supportsNonFileBasedOutputs flags is set automatically to match provider
110-
// when adding a destination parameter
111-
QgsProcessingParameterFeatureSink *p7 = new QgsProcessingParameterFeatureSink( "p7" );
112-
p7->setSupportsNonFileBasedOutputs( false );
113-
QVERIFY( addParameter( p7 ) );
114-
QVERIFY( destinationParameterDefinitions().at( 2 )->supportsNonFileBasedOutputs() );
115-
116109
// remove parameter
117110
removeParameter( "non existent" );
118111
removeParameter( "p6" );
@@ -346,6 +339,11 @@ void TestQgsProcessing::initTestCase()
346339
{
347340
QgsApplication::init();
348341
QgsApplication::initQgis();
342+
343+
// Set up the QgsSettings environment
344+
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
345+
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
346+
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
349347
}
350348

351349
void TestQgsProcessing::cleanupTestCase()
@@ -2812,6 +2810,8 @@ void TestQgsProcessing::parameterFeatureSink()
28122810
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProcessingOutputLayerDefinition( QgsProperty::fromExpression( "\"abc\" || \"def\"" ) ) ), context ), QStringLiteral( "QgsProcessingOutputLayerDefinition(QgsProperty.fromExpression('\"abc\" || \"def\"'))" ) );
28132811
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
28142812

2813+
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "shp" ) );
2814+
28152815
QVariantMap map = def->toVariantMap();
28162816
QgsProcessingParameterFeatureSink fromMap( "x" );
28172817
QVERIFY( fromMap.fromVariantMap( map ) );
@@ -2931,6 +2931,8 @@ void TestQgsProcessing::parameterRasterOut()
29312931
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif" ) );
29322932
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif", &context ) );
29332933

2934+
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "tif" ) );
2935+
29342936
QVariantMap params;
29352937
params.insert( "non_optional", "test.tif" );
29362938
QCOMPARE( QgsProcessingParameters::parameterAsRasterOutputLayer( def.get(), params, context ), QStringLiteral( "test.tif" ) );
@@ -2981,8 +2983,16 @@ void TestQgsProcessing::parameterFileOut()
29812983
// not optional!
29822984
std::unique_ptr< QgsProcessingParameterFileOutput > def( new QgsProcessingParameterFileOutput( "non_optional", QString(), QStringLiteral( "BMP files (*.bmp)" ), QString(), false ) );
29832985
QCOMPARE( def->fileFilter(), QStringLiteral( "BMP files (*.bmp)" ) );
2986+
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "bmp" ) );
29842987
def->setFileFilter( QStringLiteral( "PCX files (*.pcx)" ) );
29852988
QCOMPARE( def->fileFilter(), QStringLiteral( "PCX files (*.pcx)" ) );
2989+
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "pcx" ) );
2990+
def->setFileFilter( QStringLiteral( "PCX files (*.pcx *.picx)" ) );
2991+
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "pcx" ) );
2992+
def->setFileFilter( QStringLiteral( "PCX files (*.pcx *.picx);;BMP files (*.bmp)" ) );
2993+
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "pcx" ) );
2994+
def->setFileFilter( QString() );
2995+
QCOMPARE( def->defaultFileExtension(), QStringLiteral( "file" ) );
29862996

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

0 commit comments

Comments
 (0)