Skip to content
Permalink
Browse files

[processing] Use user-set default file formats if supported by

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 bf75747f18034687415af21e8e1a545bff82f593
@@ -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

@@ -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
@@ -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()
@@ -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()
@@ -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;
}
}

@@ -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.
@@ -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()
@@ -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()
@@ -34,6 +34,7 @@
#include "qgsexpressioncontext.h"
#include "qgsxmlutils.h"
#include "qgsreferencedgeometry.h"
#include "qgssettings.h"

class DummyAlgorithm : public QgsProcessingAlgorithm
{
@@ -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
@@ -401,6 +424,7 @@ class TestQgsProcessing: public QObject
void create();
void combineFields();
void stringToPythonLiteral();
void defaultExtensionsForProvider();

private:

@@ -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.
You can’t perform that action at this time.