Skip to content

Commit e9e7740

Browse files
authored
Merge pull request #5544 from nyalldawson/proc_api
[processing] Improve api handling for provider format support
2 parents edea38f + bf75747 commit e9e7740

File tree

9 files changed

+293
-32
lines changed

9 files changed

+293
-32
lines changed

python/core/processing/qgsprocessingparameters.sip

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,31 @@ class QgsProcessingParameterDefinition
353353
:rtype: list of str
354354
%End
355355

356+
QgsProcessingAlgorithm *algorithm() const;
357+
%Docstring
358+
Returns a pointer to the algorithm which owns this parameter. May be None
359+
for non-owned parameters.
360+
.. seealso:: provider()
361+
:rtype: QgsProcessingAlgorithm
362+
%End
363+
364+
QgsProcessingProvider *provider() const;
365+
%Docstring
366+
Returns a pointer to the provider for the algorithm which owns this parameter. May be None
367+
for non-owned parameters or algorithms.
368+
.. seealso:: algorithm()
369+
:rtype: QgsProcessingProvider
370+
%End
371+
356372
protected:
357373

358374

359375

360376

361377

362378

379+
380+
363381
};
364382

365383
QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);

python/core/processing/qgsprocessingprovider.sip

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,47 @@ class QgsProcessingProvider : QObject
9494
%Docstring
9595
Returns a list of the raster format file extensions supported by this provider.
9696
.. seealso:: supportedOutputVectorLayerExtensions()
97-
.. seealso:: supportedOutputTableExtensions()
9897
:rtype: list of str
9998
%End
10099

101100
virtual QStringList supportedOutputVectorLayerExtensions() const;
102101
%Docstring
103102
Returns a list of the vector format file extensions supported by this provider.
103+
.. seealso:: defaultVectorFileExtension()
104104
.. seealso:: supportedOutputRasterLayerExtensions()
105-
.. seealso:: supportedOutputTableExtensions()
106105
.. seealso:: supportsNonFileBasedOutput()
107106
:rtype: list of str
108107
%End
109108

110-
virtual QStringList supportedOutputTableExtensions() const;
109+
virtual QString defaultVectorFileExtension( bool hasGeometry = true ) const;
111110
%Docstring
112-
Returns a list of the table format file extensions supported by this provider.
113-
.. seealso:: supportedOutputRasterLayerExtensions()
111+
Returns the default file extension to use for vector outputs created by the
112+
provider.
113+
114+
If ``hasGeometry`` is true then the output file format must have support for
115+
geometry. If ``hasGeometry`` is false then non-spatial formats can be used.
116+
117+
The default implementation returns the user's default Processing vector output format
118+
setting, if it's supported by the provider (see supportedOutputVectorLayerExtensions()).
119+
Otherwise the first reported supported vector format will be used.
120+
114121
.. seealso:: supportedOutputVectorLayerExtensions()
115-
:rtype: list of str
122+
.. seealso:: defaultRasterFileExtension()
123+
:rtype: str
124+
%End
125+
126+
virtual QString defaultRasterFileExtension() const;
127+
%Docstring
128+
Returns the default file extension to use for raster outputs created by the
129+
provider.
130+
131+
The default implementation returns the user's default Processing raster output format
132+
setting, if it's supported by the provider (see supportedOutputRasterLayerExtensions()).
133+
Otherwise the first reported supported raster format will be used.
134+
135+
.. seealso:: supportedOutputRasterLayerExtensions()
136+
.. seealso:: defaultVectorFileExtension()
137+
:rtype: str
116138
%End
117139

118140
virtual bool supportsNonFileBasedOutput() const;

python/plugins/processing/gui/ParameterGuiUtils.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ def getFileFilter(param):
6363
for i in range(len(exts)):
6464
exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterRasterDestination').format(exts[i].upper(), exts[i].lower())
6565
return ';;'.join(exts) + ';;' + tr('All files (*.*)')
66-
elif param.type() == 'table':
67-
exts = ['csv', 'dbf']
68-
for i in range(len(exts)):
69-
exts[i] = tr('{0} files (*.{1})', 'ParameterTable').format(exts[i].upper(), exts[i].lower())
70-
return tr('All files (*.*)') + ';;' + ';;'.join(exts)
71-
elif param.type() == 'sink':
72-
exts = QgsVectorFileWriter.supportedFormatExtensions()
66+
elif param.type() in ('sink', 'vectorDestination'):
67+
if param.provider() is not None:
68+
exts = param.provider().supportedOutputVectorLayerExtensions()
69+
else:
70+
exts = QgsVectorFileWriter.supportedFormatExtensions()
7371
for i in range(len(exts)):
7472
exts[i] = tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
7573
return ';;'.join(exts) + ';;' + tr('All files (*.*)')

src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def
242242
}
243243

244244
mParameters << definition;
245+
definition->mAlgorithm = this;
245246

246247
if ( createOutput )
247248
return createAutoOutputForParameter( definition );

src/core/processing/qgsprocessingparameters.cpp

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
***************************************************************************/
1717

1818
#include "qgsprocessingparameters.h"
19+
#include "qgsprocessingprovider.h"
1920
#include "qgsprocessingcontext.h"
2021
#include "qgsprocessingutils.h"
22+
#include "qgsprocessingalgorithm.h"
2123
#include "qgsvectorlayerfeatureiterator.h"
2224
#include "qgsprocessingoutputs.h"
2325
#include "qgssettings.h"
@@ -1237,6 +1239,16 @@ bool QgsProcessingParameterDefinition::fromVariantMap( const QVariantMap &map )
12371239
return true;
12381240
}
12391241

1242+
QgsProcessingAlgorithm *QgsProcessingParameterDefinition::algorithm() const
1243+
{
1244+
return mAlgorithm;
1245+
}
1246+
1247+
QgsProcessingProvider *QgsProcessingParameterDefinition::provider() const
1248+
{
1249+
return mAlgorithm ? mAlgorithm->provider() : nullptr;
1250+
}
1251+
12401252
QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
12411253
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
12421254
{}
@@ -3087,14 +3099,21 @@ QgsProcessingOutputDefinition *QgsProcessingParameterFeatureSink::toOutputDefini
30873099

30883100
QString QgsProcessingParameterFeatureSink::defaultFileExtension() const
30893101
{
3090-
QgsSettings settings;
3091-
if ( hasGeometry() )
3102+
if ( QgsProcessingProvider *p = provider() )
30923103
{
3093-
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3104+
return p->defaultVectorFileExtension( hasGeometry() );
30943105
}
30953106
else
30963107
{
3097-
return QStringLiteral( "dbf" );
3108+
QgsSettings settings;
3109+
if ( hasGeometry() )
3110+
{
3111+
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3112+
}
3113+
else
3114+
{
3115+
return QStringLiteral( "dbf" );
3116+
}
30983117
}
30993118
}
31003119

@@ -3240,8 +3259,15 @@ QgsProcessingOutputDefinition *QgsProcessingParameterRasterDestination::toOutput
32403259

32413260
QString QgsProcessingParameterRasterDestination::defaultFileExtension() const
32423261
{
3243-
QgsSettings settings;
3244-
return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
3262+
if ( QgsProcessingProvider *p = provider() )
3263+
{
3264+
return p->defaultRasterFileExtension();
3265+
}
3266+
else
3267+
{
3268+
QgsSettings settings;
3269+
return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
3270+
}
32453271
}
32463272

32473273
QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
@@ -3536,14 +3562,21 @@ QgsProcessingOutputDefinition *QgsProcessingParameterVectorDestination::toOutput
35363562

35373563
QString QgsProcessingParameterVectorDestination::defaultFileExtension() const
35383564
{
3539-
QgsSettings settings;
3540-
if ( hasGeometry() )
3565+
if ( QgsProcessingProvider *p = provider() )
35413566
{
3542-
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3567+
return p->defaultVectorFileExtension( hasGeometry() );
35433568
}
35443569
else
35453570
{
3546-
return QStringLiteral( "dbf" );
3571+
QgsSettings settings;
3572+
if ( hasGeometry() )
3573+
{
3574+
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3575+
}
3576+
else
3577+
{
3578+
return QStringLiteral( "dbf" );
3579+
}
35473580
}
35483581
}
35493582

src/core/processing/qgsprocessingparameters.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class QgsFeatureSink;
3434
class QgsProcessingFeatureSource;
3535
class QgsProcessingOutputDefinition;
3636
class QgsProcessingFeedback;
37+
class QgsProcessingProvider;
3738

3839
/**
3940
* \class QgsProcessingFeatureSourceDefinition
@@ -391,6 +392,20 @@ class CORE_EXPORT QgsProcessingParameterDefinition
391392
*/
392393
virtual QStringList dependsOnOtherParameters() const { return QStringList(); }
393394

395+
/**
396+
* Returns a pointer to the algorithm which owns this parameter. May be nullptr
397+
* for non-owned parameters.
398+
* \see provider()
399+
*/
400+
QgsProcessingAlgorithm *algorithm() const;
401+
402+
/**
403+
* Returns a pointer to the provider for the algorithm which owns this parameter. May be nullptr
404+
* for non-owned parameters or algorithms.
405+
* \see algorithm()
406+
*/
407+
QgsProcessingProvider *provider() const;
408+
394409
protected:
395410

396411
//! Parameter name
@@ -408,6 +423,12 @@ class CORE_EXPORT QgsProcessingParameterDefinition
408423
//! Freeform metadata for parameter. Mostly used by widget wrappers to customise their appearance and behavior.
409424
QVariantMap mMetadata;
410425

426+
//! Pointer to algorithm which owns this parameter
427+
QgsProcessingAlgorithm *mAlgorithm = nullptr;
428+
429+
// To allow access to mAlgorithm. We don't want a public setter for this!
430+
friend class QgsProcessingAlgorithm;
431+
411432
};
412433

413434
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingParameterDefinition::Flags )

src/core/processing/qgsprocessingprovider.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsprocessingprovider.h"
1919
#include "qgsapplication.h"
2020
#include "qgsvectorfilewriter.h"
21+
#include "qgssettings.h"
2122

2223
QgsProcessingProvider::QgsProcessingProvider( QObject *parent SIP_TRANSFERTHIS )
2324
: QObject( parent )
@@ -44,6 +45,11 @@ QString QgsProcessingProvider::longName() const
4445
return name();
4546
}
4647

48+
QStringList QgsProcessingProvider::supportedOutputRasterLayerExtensions() const
49+
{
50+
return QStringList() << QStringLiteral( "tif" );
51+
}
52+
4753
void QgsProcessingProvider::refreshAlgorithms()
4854
{
4955
qDeleteAll( mAlgorithms );
@@ -84,3 +90,50 @@ QStringList QgsProcessingProvider::supportedOutputVectorLayerExtensions() const
8490
return QgsVectorFileWriter::supportedFormatExtensions();
8591
}
8692

93+
QString QgsProcessingProvider::defaultVectorFileExtension( bool hasGeometry ) const
94+
{
95+
QgsSettings settings;
96+
const QString defaultExtension = hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
97+
const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), defaultExtension, QgsSettings::Core ).toString();
98+
99+
const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
100+
if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
101+
{
102+
// user set default is supported by provider, use that
103+
return userDefault;
104+
}
105+
else if ( !supportedExtensions.empty() )
106+
{
107+
return supportedExtensions.at( 0 );
108+
}
109+
else
110+
{
111+
// who knows? provider says it has no file support at all...
112+
// let's say shp. even MapInfo supports shapefiles.
113+
return defaultExtension;
114+
}
115+
}
116+
117+
QString QgsProcessingProvider::defaultRasterFileExtension() const
118+
{
119+
QgsSettings settings;
120+
const QString defaultExtension = QStringLiteral( "tif" );
121+
const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), defaultExtension, QgsSettings::Core ).toString();
122+
123+
const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
124+
if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
125+
{
126+
// user set default is supported by provider, use that
127+
return userDefault;
128+
}
129+
else if ( !supportedExtensions.empty() )
130+
{
131+
return supportedExtensions.at( 0 );
132+
}
133+
else
134+
{
135+
// who knows? provider says it has no file support at all...
136+
return defaultExtension;
137+
}
138+
}
139+

src/core/processing/qgsprocessingprovider.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,45 @@ class CORE_EXPORT QgsProcessingProvider : public QObject
104104
/**
105105
* Returns a list of the raster format file extensions supported by this provider.
106106
* \see supportedOutputVectorLayerExtensions()
107-
* \see supportedOutputTableExtensions()
108107
*/
109-
virtual QStringList supportedOutputRasterLayerExtensions() const { return QStringList() << QStringLiteral( "tif" ); }
108+
virtual QStringList supportedOutputRasterLayerExtensions() const;
110109

111110
/**
112111
* Returns a list of the vector format file extensions supported by this provider.
112+
* \see defaultVectorFileExtension()
113113
* \see supportedOutputRasterLayerExtensions()
114-
* \see supportedOutputTableExtensions()
115114
* \see supportsNonFileBasedOutput()
116115
*/
117116
virtual QStringList supportedOutputVectorLayerExtensions() const;
118117

119118
/**
120-
* Returns a list of the table format file extensions supported by this provider.
121-
* \see supportedOutputRasterLayerExtensions()
119+
* Returns the default file extension to use for vector outputs created by the
120+
* provider.
121+
*
122+
* If \a hasGeometry is true then the output file format must have support for
123+
* geometry. If \a hasGeometry is false then non-spatial formats can be used.
124+
*
125+
* The default implementation returns the user's default Processing vector output format
126+
* setting, if it's supported by the provider (see supportedOutputVectorLayerExtensions()).
127+
* Otherwise the first reported supported vector format will be used.
128+
*
122129
* \see supportedOutputVectorLayerExtensions()
130+
* \see defaultRasterFileExtension()
131+
*/
132+
virtual QString defaultVectorFileExtension( bool hasGeometry = true ) const;
133+
134+
/**
135+
* Returns the default file extension to use for raster outputs created by the
136+
* provider.
137+
*
138+
* The default implementation returns the user's default Processing raster output format
139+
* setting, if it's supported by the provider (see supportedOutputRasterLayerExtensions()).
140+
* Otherwise the first reported supported raster format will be used.
141+
*
142+
* \see supportedOutputRasterLayerExtensions()
143+
* \see defaultVectorFileExtension()
123144
*/
124-
virtual QStringList supportedOutputTableExtensions() const { return QStringList() << QStringLiteral( "csv" ); }
145+
virtual QString defaultRasterFileExtension() const;
125146

126147
/**
127148
* Returns true if the provider supports non-file based outputs (such as memory layers

0 commit comments

Comments
 (0)