Skip to content

Commit

Permalink
[processing] Use user-set default file formats if supported by
Browse files Browse the repository at this point in the history
provider, otherwise fall back to a format which IS supported
by that provider
  • Loading branch information
nyalldawson committed Nov 7, 2017
1 parent 4e3b082 commit bf75747
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
16 changes: 4 additions & 12 deletions python/core/processing/qgsprocessingprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class QgsProcessingProvider : QObject
%Docstring
Returns a list of the raster format file extensions supported by this provider.
.. seealso:: supportedOutputVectorLayerExtensions()
.. seealso:: supportedOutputTableExtensions()
:rtype: list of str
%End

Expand All @@ -103,19 +102,10 @@ class QgsProcessingProvider : QObject
Returns a list of the vector format file extensions supported by this provider.
.. seealso:: defaultVectorFileExtension()
.. seealso:: supportedOutputRasterLayerExtensions()
.. seealso:: supportedOutputTableExtensions()
.. seealso:: supportsNonFileBasedOutput()
:rtype: list of str
%End

virtual QStringList supportedOutputTableExtensions() const;
%Docstring
Returns a list of the table format file extensions supported by this provider.
.. seealso:: supportedOutputRasterLayerExtensions()
.. seealso:: supportedOutputVectorLayerExtensions()
:rtype: list of str
%End

virtual QString defaultVectorFileExtension( bool hasGeometry = true ) const;
%Docstring
Returns the default file extension to use for vector outputs created by the
Expand All @@ -125,7 +115,8 @@ class QgsProcessingProvider : QObject
geometry. If ``hasGeometry`` is false then non-spatial formats can be used.

The default implementation returns the user's default Processing vector output format
setting.
setting, if it's supported by the provider (see supportedOutputVectorLayerExtensions()).
Otherwise the first reported supported vector format will be used.

.. seealso:: supportedOutputVectorLayerExtensions()
.. seealso:: defaultRasterFileExtension()
Expand All @@ -138,7 +129,8 @@ class QgsProcessingProvider : QObject
provider.

The default implementation returns the user's default Processing raster output format
setting.
setting, if it's supported by the provider (see supportedOutputRasterLayerExtensions()).
Otherwise the first reported supported raster format will be used.

.. seealso:: supportedOutputRasterLayerExtensions()
.. seealso:: defaultVectorFileExtension()
Expand Down
36 changes: 32 additions & 4 deletions src/core/processing/qgsprocessingprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,47 @@ QStringList QgsProcessingProvider::supportedOutputVectorLayerExtensions() const
QString QgsProcessingProvider::defaultVectorFileExtension( bool hasGeometry ) const
{
QgsSettings settings;
if ( hasGeometry )
const QString defaultExtension = hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), defaultExtension, QgsSettings::Core ).toString();

const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
{
// user set default is supported by provider, use that
return userDefault;
}
else if ( !supportedExtensions.empty() )
{
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
return supportedExtensions.at( 0 );
}
else
{
return QStringLiteral( "dbf" );
// who knows? provider says it has no file support at all...
// let's say shp. even MapInfo supports shapefiles.
return defaultExtension;
}
}

QString QgsProcessingProvider::defaultRasterFileExtension() const
{
QgsSettings settings;
return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
const QString defaultExtension = QStringLiteral( "tif" );
const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), defaultExtension, QgsSettings::Core ).toString();

const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
{
// user set default is supported by provider, use that
return userDefault;
}
else if ( !supportedExtensions.empty() )
{
return supportedExtensions.at( 0 );
}
else
{
// who knows? provider says it has no file support at all...
return defaultExtension;
}
}

15 changes: 4 additions & 11 deletions src/core/processing/qgsprocessingprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,17 @@ class CORE_EXPORT QgsProcessingProvider : public QObject
/**
* Returns a list of the raster format file extensions supported by this provider.
* \see supportedOutputVectorLayerExtensions()
* \see supportedOutputTableExtensions()
*/
virtual QStringList supportedOutputRasterLayerExtensions() const;

/**
* Returns a list of the vector format file extensions supported by this provider.
* \see defaultVectorFileExtension()
* \see supportedOutputRasterLayerExtensions()
* \see supportedOutputTableExtensions()
* \see supportsNonFileBasedOutput()
*/
virtual QStringList supportedOutputVectorLayerExtensions() const;

/**
* Returns a list of the table format file extensions supported by this provider.
* \see supportedOutputRasterLayerExtensions()
* \see supportedOutputVectorLayerExtensions()
*/
virtual QStringList supportedOutputTableExtensions() const { return QStringList() << QStringLiteral( "csv" ); }

/**
* Returns the default file extension to use for vector outputs created by the
* provider.
Expand All @@ -132,7 +123,8 @@ class CORE_EXPORT QgsProcessingProvider : public QObject
* geometry. If \a hasGeometry is false then non-spatial formats can be used.
*
* The default implementation returns the user's default Processing vector output format
* setting.
* setting, if it's supported by the provider (see supportedOutputVectorLayerExtensions()).
* Otherwise the first reported supported vector format will be used.
*
* \see supportedOutputVectorLayerExtensions()
* \see defaultRasterFileExtension()
Expand All @@ -144,7 +136,8 @@ class CORE_EXPORT QgsProcessingProvider : public QObject
* provider.
*
* The default implementation returns the user's default Processing raster output format
* setting.
* setting, if it's supported by the provider (see supportedOutputRasterLayerExtensions()).
* Otherwise the first reported supported raster format will be used.
*
* \see supportedOutputRasterLayerExtensions()
* \see defaultVectorFileExtension()
Expand Down
44 changes: 44 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "qgsexpressioncontext.h"
#include "qgsxmlutils.h"
#include "qgsreferencedgeometry.h"
#include "qgssettings.h"

class DummyAlgorithm : public QgsProcessingAlgorithm
{
Expand Down Expand Up @@ -334,6 +335,28 @@ class DummyProviderNoLoad : public DummyProvider

};

class DummyProvider3 : public QgsProcessingProvider
{
public:

DummyProvider3() = default;
QString id() const override { return QStringLiteral( "dummy3" ); }
QString name() const override { return QStringLiteral( "dummy3" ); }

QStringList supportedOutputVectorLayerExtensions() const override
{
return QStringList() << QStringLiteral( "mif" ) << QStringLiteral( "tab" );
}

QStringList supportedOutputRasterLayerExtensions() const override
{
return QStringList() << QStringLiteral( "mig" ) << QStringLiteral( "asc" );
}

void loadAlgorithms() override {}

};

class TestQgsProcessing: public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -401,6 +424,7 @@ class TestQgsProcessing: public QObject
void create();
void combineFields();
void stringToPythonLiteral();
void defaultExtensionsForProvider();

private:

Expand Down Expand Up @@ -5678,5 +5702,25 @@ void TestQgsProcessing::stringToPythonLiteral()
QCOMPARE( QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "a \"string\"" ) ), QStringLiteral( "'a \\\"string\\\"'" ) );
}

void TestQgsProcessing::defaultExtensionsForProvider()
{
DummyProvider3 provider;
// default implementation should return first supported format for provider
QCOMPARE( provider.defaultVectorFileExtension( true ), QStringLiteral( "mif" ) );
QCOMPARE( provider.defaultRasterFileExtension(), QStringLiteral( "mig" ) );
// unless the user has set a default format, which IS supported by that provider
QgsSettings settings;
settings.setValue( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "tab" ), QgsSettings::Core );
settings.setValue( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "asc" ), QgsSettings::Core );
QCOMPARE( provider.defaultVectorFileExtension( true ), QStringLiteral( "tab" ) );
QCOMPARE( provider.defaultRasterFileExtension(), QStringLiteral( "asc" ) );

// but if default is not supported by provider, we use a supported format
settings.setValue( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "gpkg" ), QgsSettings::Core );
settings.setValue( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "ecw" ), QgsSettings::Core );
QCOMPARE( provider.defaultVectorFileExtension( true ), QStringLiteral( "mif" ) );
QCOMPARE( provider.defaultRasterFileExtension(), QStringLiteral( "mig" ) );
}

QGSTEST_MAIN( TestQgsProcessing )
#include "testqgsprocessing.moc"

0 comments on commit bf75747

Please sign in to comment.