From dc341d2047176f527ed483589b379cbaee50cd0c Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 5 Nov 2017 14:18:14 +1000 Subject: [PATCH] Add flags to QgsVectorFileWriter methods which return lists of drivers Initially only flag available is whether to sort drivers by recommended order. The recommended order puts GPKG first and SHP second, then leaves the rest alphabetical. This fixes a few instances in the QGIS gui where these recommended formats are not listed first. --- python/core/qgsvectorfilewriter.sip | 47 ++++++-- .../plugins/processing/algs/gdal/GdalUtils.py | 6 +- src/core/qgsvectorfilewriter.cpp | 102 ++++++++++++------ src/core/qgsvectorfilewriter.h | 47 ++++++-- src/gui/ogr/qgsvectorlayersaveasdialog.cpp | 6 +- .../qgsgeometrycheckersetuptab.cpp | 14 +-- tests/src/python/test_qgsvectorfilewriter.py | 38 +++++++ 7 files changed, 195 insertions(+), 65 deletions(-) diff --git a/python/core/qgsvectorfilewriter.sip b/python/core/qgsvectorfilewriter.sip index 14ef303ad9f..bbfb900c721 100644 --- a/python/core/qgsvectorfilewriter.sip +++ b/python/core/qgsvectorfilewriter.sip @@ -145,6 +145,14 @@ Some formats require a compulsory encoding, typically UTF-8. If no compulsory en SymbolLayerSymbology }; + + enum VectorFormatOption + { + SortRecommended, + }; + typedef QFlags VectorFormatOptions; + + class FieldValueConverter { %Docstring @@ -460,27 +468,41 @@ Create a new vector file writer - static QMap< QString, QString> supportedFiltersAndFormats(); + static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended ); %Docstring - Returns a map with format filter string as key and OGR format key as value. + Returns a list or pairs, with format filter string as first element and OGR format key as second element. + + The ``options`` argument can be used to control the sorting and filtering of + returned formats. + .. seealso:: supportedOutputVectorLayerExtensions() - :rtype: QMap< str, QString> + :rtype: list of QPair< str, QString > %End - static QStringList supportedFormatExtensions(); + static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended ); %Docstring Returns a list of file extensions for supported formats. + + The ``options`` argument can be used to control the sorting and filtering of + returned formats. + .. versionadded:: 3.0 .. seealso:: supportedFiltersAndFormats() :rtype: list of str %End - static QMap< QString, QString> ogrDriverList(); + static QList< QPair< QString, QString > > ogrDriverList( VectorFormatOptions options = SortRecommended ); %Docstring Returns driver list that can be used for dialogs. It contains all OGR drivers - + some additional internal QGIS driver names to distinguish between more - supported formats of the same OGR driver - :rtype: QMap< str, QString> + plus some additional internal QGIS driver names to distinguish between more + supported formats of the same OGR driver. + + The returned list consists of pairs of driver long name (e.g. user-friendly + display name for the format) to internal driver short name. + + The ``options`` argument can be used to control the sorting and filtering of + returned drivers. + :rtype: list of QPair< str, QString > %End static QString driverForExtension( const QString &extension ); @@ -492,9 +514,12 @@ Create a new vector file writer :rtype: str %End - static QString fileFilterString(); + static QString fileFilterString( VectorFormatOptions options = SortRecommended ); %Docstring -Returns filter string that can be used for dialogs + Returns filter string that can be used for dialogs. + + The ``options`` argument can be used to control the sorting and filtering of + returned drivers. :rtype: str %End @@ -639,6 +664,8 @@ Close opened shapefile for writing QFlags operator|(QgsVectorFileWriter::EditionCapability f1, QFlags f2); +QFlags operator|(QgsVectorFileWriter::VectorFormatOption f1, QFlags f2); + /************************************************************************ diff --git a/python/plugins/processing/algs/gdal/GdalUtils.py b/python/plugins/processing/algs/gdal/GdalUtils.py index 9a0db1a1ad5..3d158c99c18 100644 --- a/python/plugins/processing/algs/gdal/GdalUtils.py +++ b/python/plugins/processing/algs/gdal/GdalUtils.py @@ -174,9 +174,9 @@ class GdalUtils(object): return 'ESRI Shapefile' formats = QgsVectorFileWriter.supportedFiltersAndFormats() - for k, v in list(formats.items()): - if ext in k: - return v + for format in formats: + if ext in format[0]: + return format[1] return 'ESRI Shapefile' @staticmethod diff --git a/src/core/qgsvectorfilewriter.cpp b/src/core/qgsvectorfilewriter.cpp index 14fdbe53d93..f58f1e9adf4 100644 --- a/src/core/qgsvectorfilewriter.cpp +++ b/src/core/qgsvectorfilewriter.cpp @@ -2681,13 +2681,16 @@ void QgsVectorFileWriter::setSymbologyScale( double d ) mRenderContext.setRendererScale( mSymbologyScale ); } -QMap< QString, QString> QgsVectorFileWriter::supportedFiltersAndFormats() +QList< QPair< QString, QString > > QgsVectorFileWriter::supportedFiltersAndFormats( const VectorFormatOptions options ) { - QMap resultMap; + QList< QPair< QString, QString > > resultMap; QgsApplication::registerOgrDrivers(); int const drvCount = OGRGetDriverCount(); + QPair< QString, QString > shapeFormat; + QPair< QString, QString > gpkgFormat; + for ( int i = 0; i < drvCount; ++i ) { OGRSFDriverH drv = OGRGetDriver( i ); @@ -2700,51 +2703,68 @@ QMap< QString, QString> QgsVectorFileWriter::supportedFiltersAndFormats() if ( filterString.isEmpty() ) continue; - resultMap.insert( filterString, drvName ); + if ( options & SortRecommended ) + { + if ( drvName == QStringLiteral( "ESRI Shapefile" ) ) + { + shapeFormat = qMakePair( filterString, drvName ); + continue; + } + else if ( drvName == QStringLiteral( "GPKG" ) ) + { + gpkgFormat = qMakePair( filterString, drvName ); + continue; + } + } + resultMap << qMakePair( filterString, drvName ); } } } + std::sort( resultMap.begin(), resultMap.end(), []( const QPair< QString, QString > &a, const QPair< QString, QString > &b ) -> bool + { + return a.second < b.second; + } ); + + if ( options & SortRecommended ) + { + if ( !shapeFormat.first.isEmpty() ) + { + resultMap.insert( 0, shapeFormat ); + } + if ( !gpkgFormat.first.isEmpty() ) + { + resultMap.insert( 0, gpkgFormat ); + } + } + return resultMap; } -QStringList QgsVectorFileWriter::supportedFormatExtensions() +QStringList QgsVectorFileWriter::supportedFormatExtensions( const VectorFormatOptions options ) { - QgsStringMap formats = supportedFiltersAndFormats(); + const auto formats = supportedFiltersAndFormats( options ); QStringList extensions; QRegularExpression rx( QStringLiteral( "\\*\\.([a-zA-Z0-9]*)" ) ); - QgsStringMap::const_iterator formatIt = formats.constBegin(); + auto formatIt = formats.constBegin(); for ( ; formatIt != formats.constEnd(); ++formatIt ) { - QString ext = formatIt.key(); + QString ext = formatIt->first; QRegularExpressionMatch match = rx.match( ext ); if ( !match.hasMatch() ) continue; QString matched = match.captured( 1 ); - - // special handling for the two main contenders for glory - if ( matched.compare( QStringLiteral( "gpkg" ), Qt::CaseInsensitive ) == 0 ) - continue; - if ( matched.compare( QStringLiteral( "shp" ), Qt::CaseInsensitive ) == 0 ) - continue; - extensions << matched; } - - std::sort( extensions.begin(), extensions.end() ); - - // Make https://twitter.com/shapefiIe a sad little fellow - extensions.insert( 0, QStringLiteral( "gpkg" ) ); - extensions.insert( 1, QStringLiteral( "shp" ) ); return extensions; } -QMap QgsVectorFileWriter::ogrDriverList() +QList< QPair< QString, QString> > QgsVectorFileWriter::ogrDriverList( const VectorFormatOptions options ) { - QMap resultMap; + QList< QPair< QString, QString> > resultMap; QgsApplication::registerOgrDrivers(); int const drvCount = OGRGetDriverCount(); @@ -2756,6 +2776,10 @@ QMap QgsVectorFileWriter::ogrDriverList() if ( drv ) { QString drvName = OGR_Dr_GetName( drv ); + if ( drvName == QLatin1String( "ESRI Shapefile" ) ) + { + writableDrivers << QStringLiteral( "DBF file" ); + } if ( OGR_Dr_TestCapability( drv, "CreateDataSource" ) != 0 ) { // Add separate format for Mapinfo MIF (MITAB is OGR default) @@ -2786,24 +2810,35 @@ QMap QgsVectorFileWriter::ogrDriverList() } CPLFree( options[0] ); } - else if ( drvName == QLatin1String( "ESRI Shapefile" ) ) - { - writableDrivers << QStringLiteral( "DBF file" ); - } writableDrivers << drvName; } } } + std::sort( writableDrivers.begin(), writableDrivers.end() ); + if ( options & SortRecommended ) + { + // recommended order sorting, so we shift certain formats to the top + if ( writableDrivers.contains( QStringLiteral( "ESRI Shapefile" ) ) ) + { + writableDrivers.removeAll( QStringLiteral( "ESRI Shapefile" ) ); + writableDrivers.insert( 0, QStringLiteral( "ESRI Shapefile" ) ); + } + if ( writableDrivers.contains( QStringLiteral( "GPKG" ) ) ) + { + // Make https://twitter.com/shapefiIe a sad little fellow + writableDrivers.removeAll( QStringLiteral( "GPKG" ) ); + writableDrivers.insert( 0, QStringLiteral( "GPKG" ) ); + } + } - Q_FOREACH ( const QString &drvName, writableDrivers ) + for ( const QString &drvName : qgis::as_const( writableDrivers ) ) { MetaData metadata; if ( driverMetadata( drvName, metadata ) && !metadata.trLongName.isEmpty() ) { - resultMap.insert( metadata.trLongName, drvName ); + resultMap << qMakePair( metadata.trLongName, drvName ); } } - return resultMap; } @@ -2841,17 +2876,16 @@ QString QgsVectorFileWriter::driverForExtension( const QString &extension ) return QString(); } -QString QgsVectorFileWriter::fileFilterString() +QString QgsVectorFileWriter::fileFilterString( const VectorFormatOptions options ) { QString filterString; - QMap< QString, QString> driverFormatMap = supportedFiltersAndFormats(); - QMap< QString, QString>::const_iterator it = driverFormatMap.constBegin(); - for ( ; it != driverFormatMap.constEnd(); ++it ) + const auto driverFormatMap = supportedFiltersAndFormats( options ); + for ( auto it = driverFormatMap.constBegin(); it != driverFormatMap.constEnd(); ++it ) { if ( !filterString.isEmpty() ) filterString += QLatin1String( ";;" ); - filterString += it.key(); + filterString += it->first; } return filterString; } diff --git a/src/core/qgsvectorfilewriter.h b/src/core/qgsvectorfilewriter.h index a51504eee14..ccb1f7507a4 100644 --- a/src/core/qgsvectorfilewriter.h +++ b/src/core/qgsvectorfilewriter.h @@ -186,6 +186,17 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels) }; + + /** + * Options for sorting and filtering vector formats. + * \since QGIS 3.0 + */ + enum VectorFormatOption + { + SortRecommended = 1 << 1, //!< Use recommended sort order, with extremely commonly used formats listed first + }; + Q_DECLARE_FLAGS( VectorFormatOptions, VectorFormatOption ) + /** * \ingroup core * Interface to convert raw field values to their user-friendly value. @@ -496,24 +507,38 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink QgsVectorFileWriter &operator=( const QgsVectorFileWriter &rh ) = delete; /** - * Returns a map with format filter string as key and OGR format key as value. + * Returns a list or pairs, with format filter string as first element and OGR format key as second element. + * + * The \a options argument can be used to control the sorting and filtering of + * returned formats. + * * \see supportedOutputVectorLayerExtensions() */ - static QMap< QString, QString> supportedFiltersAndFormats(); + static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended ); /** * Returns a list of file extensions for supported formats. + * + * The \a options argument can be used to control the sorting and filtering of + * returned formats. + * * \since QGIS 3.0 * \see supportedFiltersAndFormats() */ - static QStringList supportedFormatExtensions(); + static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended ); /** * Returns driver list that can be used for dialogs. It contains all OGR drivers - * + some additional internal QGIS driver names to distinguish between more - * supported formats of the same OGR driver + * plus some additional internal QGIS driver names to distinguish between more + * supported formats of the same OGR driver. + * + * The returned list consists of pairs of driver long name (e.g. user-friendly + * display name for the format) to internal driver short name. + * + * The \a options argument can be used to control the sorting and filtering of + * returned drivers. */ - static QMap< QString, QString> ogrDriverList(); + static QList< QPair< QString, QString > > ogrDriverList( VectorFormatOptions options = SortRecommended ); /** * Returns the OGR driver name for a specified file \a extension. E.g. the @@ -523,8 +548,13 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink */ static QString driverForExtension( const QString &extension ); - //! Returns filter string that can be used for dialogs - static QString fileFilterString(); + /** + * Returns filter string that can be used for dialogs. + * + * The \a options argument can be used to control the sorting and filtering of + * returned drivers. + */ + static QString fileFilterString( VectorFormatOptions options = SortRecommended ); //! Creates a filter for an OGR driver key static QString filterForDriver( const QString &driverName ); @@ -701,6 +731,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink }; Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorFileWriter::EditionCapabilities ) +Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorFileWriter::VectorFormatOptions ) // clazy:excludeall=qstring-allocations diff --git a/src/gui/ogr/qgsvectorlayersaveasdialog.cpp b/src/gui/ogr/qgsvectorlayersaveasdialog.cpp index 7a42eaae5e5..c01455c50b1 100644 --- a/src/gui/ogr/qgsvectorlayersaveasdialog.cpp +++ b/src/gui/ogr/qgsvectorlayersaveasdialog.cpp @@ -87,11 +87,11 @@ void QgsVectorLayerSaveAsDialog::setup() QgsSettings settings; restoreGeometry( settings.value( QStringLiteral( "Windows/VectorLayerSaveAs/geometry" ) ).toByteArray() ); - QMap map = QgsVectorFileWriter::ogrDriverList(); + const QList< QPair< QString, QString > > map = QgsVectorFileWriter::ogrDriverList(); mFormatComboBox->blockSignals( true ); - for ( QMap< QString, QString>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it ) + for ( auto it = map.constBegin(); it != map.constEnd(); ++it ) { - mFormatComboBox->addItem( it.key(), it.value() ); + mFormatComboBox->addItem( it->first, it->second ); } QString format = settings.value( QStringLiteral( "UI/lastVectorFormat" ), "GPKG" ).toString(); diff --git a/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp b/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp index b81cac2e118..60844665104 100644 --- a/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp +++ b/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp @@ -54,10 +54,10 @@ QgsGeometryCheckerSetupTab::QgsGeometryCheckerSetupTab( QgisInterface *iface, QD mAbortButton = new QPushButton( tr( "Abort" ) ); mRunButton->setEnabled( false ); - QMap filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats(); - for ( const QString &filter : filterFormatMap.keys() ) + const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats(); + for ( const auto &filter : filterFormatMap ) { - QString driverName = filterFormatMap.value( filter ); + QString driverName = filter.second; ui.comboBoxOutputFormat->addItem( driverName ); if ( driverName == QLatin1String( "ESRI Shapefile" ) ) { @@ -216,13 +216,13 @@ void QgsGeometryCheckerSetupTab::validateInput() void QgsGeometryCheckerSetupTab::selectOutputDirectory() { QString filterString = QgsVectorFileWriter::filterForDriver( QStringLiteral( "GPKG" ) ); - QMap filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats(); - for ( const QString &filter : filterFormatMap.keys() ) + const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats(); + for ( const auto &filter : filterFormatMap ) { - QString driverName = filterFormatMap.value( filter ); + QString driverName = filter.second; if ( driverName != QLatin1String( "ESRI Shapefile" ) ) // Default entry, first in list (see above) { - filterString += ";;" + filter; + filterString += ";;" + filter.first; } } QString initialdir = ui.lineEditOutputDirectory->text(); diff --git a/tests/src/python/test_qgsvectorfilewriter.py b/tests/src/python/test_qgsvectorfilewriter.py index 7fb9b2b5f0b..3614b254c86 100644 --- a/tests/src/python/test_qgsvectorfilewriter.py +++ b/tests/src/python/test_qgsvectorfilewriter.py @@ -723,6 +723,28 @@ class TestQgsVectorFileWriter(unittest.TestCase): gdal.Unlink(filename) + def testSupportedFiltersAndFormat(self): + # test with formats in recommended order + formats = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.SortRecommended) + self.assertEqual(formats[0], ('GeoPackage [OGR] (*.gpkg *.GPKG)', 'GPKG')) + self.assertEqual(formats[1], ('ESRI Shapefile [OGR] (*.shp *.SHP)', 'ESRI Shapefile')) + # alphabetical sorting + formats2 = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.VectorFormatOptions()) + self.assertTrue(formats2[0][0] < formats2[1][0]) + self.assertCountEqual(formats, formats2) + self.assertNotEqual(formats2[0], ('GeoPackage', 'GPKG')) + + def testOgrDriverList(self): + # test with drivers in recommended order + drivers = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.SortRecommended) + self.assertEqual(drivers[0], ('GeoPackage', 'GPKG')) + self.assertEqual(drivers[1], ('ESRI Shapefile', 'ESRI Shapefile')) + # alphabetical sorting + drivers2 = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.VectorFormatOptions()) + self.assertTrue(drivers2[0][0] < drivers2[1][0]) + self.assertCountEqual(drivers, drivers2) + self.assertNotEqual(drivers2[0], ('GeoPackage', 'GPKG')) + def testSupportedFormatExtensions(self): formats = QgsVectorFileWriter.supportedFormatExtensions() self.assertTrue('gpkg' in formats) @@ -730,6 +752,22 @@ class TestQgsVectorFileWriter(unittest.TestCase): self.assertEqual(formats[0], 'gpkg') self.assertEqual(formats[1], 'shp') + # alphabetical sorting + formats2 = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.VectorFormatOptions()) + self.assertTrue(formats2[0] < formats2[1]) + self.assertCountEqual(formats, formats2) + self.assertNotEqual(formats2[0], 'gpkg') + + def testFileFilterString(self): + formats = QgsVectorFileWriter.fileFilterString() + self.assertTrue('gpkg' in formats) + self.assertTrue('shp' in formats) + self.assertTrue(formats.index('gpkg') < formats.index('shp')) + + # alphabetical sorting + formats2 = QgsVectorFileWriter.fileFilterString(QgsVectorFileWriter.VectorFormatOptions()) + self.assertNotEqual(formats.index('gpkg'), formats2.index('gpkg')) + def testDriverForExtension(self): self.assertEqual(QgsVectorFileWriter.driverForExtension('shp'), 'ESRI Shapefile') self.assertEqual(QgsVectorFileWriter.driverForExtension('SHP'), 'ESRI Shapefile')