Skip to content

Commit

Permalink
More QPair API removal
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 5, 2017
1 parent a4ef7e4 commit fe05660
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 40 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -2673,6 +2673,7 @@ for consistency with other parts of the QGIS API.
- static `writeAsVectorFormat` calls no longer take a errorMessage argument in
python and instead return a `(errorCode, errorMessage)` tuple.
- ogrDriverList now returns a list of QgsVectorFileWriter.DriverDetails structs, instead of a map
- supportedFiltersAndFormats now returns a list of QgsVectorFileWriter.FilterFormatDetails structs, instead of a map


QgsWMSLegendNode {#qgis_api_break_3_0_QgsWMSLegendNode}
Expand Down
19 changes: 15 additions & 4 deletions python/core/qgsvectorfilewriter.sip
Expand Up @@ -10,8 +10,6 @@





class QgsVectorFileWriter : QgsFeatureSink
{
%Docstring
Expand Down Expand Up @@ -468,15 +466,28 @@ Create a new vector file writer



static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
struct FilterFormatDetails
{
QString driverName;
%Docstring
Unique driver name
%End

QString filterString;
%Docstring
Filter string for file picker dialogs
%End
};

static QList< QgsVectorFileWriter::FilterFormatDetails > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
%Docstring
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: list of QPair< str, QString >
:rtype: list of QgsVectorFileWriter.FilterFormatDetails
%End

static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended );
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -175,8 +175,8 @@ def getVectorDriverFromFileName(filename):

formats = QgsVectorFileWriter.supportedFiltersAndFormats()
for format in formats:
if ext in format[0]:
return format[1]
if ext in format.filterString:
return format.driverName
return 'ESRI Shapefile'

@staticmethod
Expand Down
44 changes: 24 additions & 20 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2681,15 +2681,15 @@ void QgsVectorFileWriter::setSymbologyScale( double d )
mRenderContext.setRendererScale( mSymbologyScale );
}

QList< QPair< QString, QString > > QgsVectorFileWriter::supportedFiltersAndFormats( const VectorFormatOptions options )
QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supportedFiltersAndFormats( const VectorFormatOptions options )
{
QList< QPair< QString, QString > > resultMap;
QList< FilterFormatDetails > results;

QgsApplication::registerOgrDrivers();
int const drvCount = OGRGetDriverCount();

QPair< QString, QString > shapeFormat;
QPair< QString, QString > gpkgFormat;
FilterFormatDetails shapeFormat;
FilterFormatDetails gpkgFormat;

for ( int i = 0; i < drvCount; ++i )
{
Expand All @@ -2703,42 +2703,47 @@ QList< QPair< QString, QString > > QgsVectorFileWriter::supportedFiltersAndForma
if ( filterString.isEmpty() )
continue;

FilterFormatDetails details;
details.driverName = drvName;
details.filterString = filterString;

if ( options & SortRecommended )
{
if ( drvName == QStringLiteral( "ESRI Shapefile" ) )
{
shapeFormat = qMakePair( filterString, drvName );
shapeFormat = details;
continue;
}
else if ( drvName == QStringLiteral( "GPKG" ) )
{
gpkgFormat = qMakePair( filterString, drvName );
gpkgFormat = details;
continue;
}
}
resultMap << qMakePair( filterString, drvName );

results << details;
}
}
}

std::sort( resultMap.begin(), resultMap.end(), []( const QPair< QString, QString > &a, const QPair< QString, QString > &b ) -> bool
std::sort( results.begin(), results.end(), []( const FilterFormatDetails & a, const FilterFormatDetails & b ) -> bool
{
return a.second < b.second;
return a.driverName < b.driverName;
} );

if ( options & SortRecommended )
{
if ( !shapeFormat.first.isEmpty() )
if ( !shapeFormat.filterString.isEmpty() )
{
resultMap.insert( 0, shapeFormat );
results.insert( 0, shapeFormat );
}
if ( !gpkgFormat.first.isEmpty() )
if ( !gpkgFormat.filterString.isEmpty() )
{
resultMap.insert( 0, gpkgFormat );
results.insert( 0, gpkgFormat );
}
}

return resultMap;
return results;
}

QStringList QgsVectorFileWriter::supportedFormatExtensions( const VectorFormatOptions options )
Expand All @@ -2748,10 +2753,9 @@ QStringList QgsVectorFileWriter::supportedFormatExtensions( const VectorFormatOp

QRegularExpression rx( QStringLiteral( "\\*\\.([a-zA-Z0-9]*)" ) );

auto formatIt = formats.constBegin();
for ( ; formatIt != formats.constEnd(); ++formatIt )
for ( const FilterFormatDetails &format : formats )
{
QString ext = formatIt->first;
QString ext = format.filterString;
QRegularExpressionMatch match = rx.match( ext );
if ( !match.hasMatch() )
continue;
Expand Down Expand Up @@ -2882,13 +2886,13 @@ QString QgsVectorFileWriter::driverForExtension( const QString &extension )
QString QgsVectorFileWriter::fileFilterString( const VectorFormatOptions options )
{
QString filterString;
const auto driverFormatMap = supportedFiltersAndFormats( options );
for ( auto it = driverFormatMap.constBegin(); it != driverFormatMap.constEnd(); ++it )
const auto driverFormats = supportedFiltersAndFormats( options );
for ( const FilterFormatDetails &details : driverFormats )
{
if ( !filterString.isEmpty() )
filterString += QLatin1String( ";;" );

filterString += it->first;
filterString += details.filterString;
}
return filterString;
}
Expand Down
18 changes: 14 additions & 4 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -29,9 +29,6 @@
#include "qgsogrutils.h"
#include <ogr_api.h>

#include <QPair>


class QgsSymbolLayer;
class QTextCodec;
class QgsFeatureIterator;
Expand Down Expand Up @@ -506,6 +503,19 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
//! QgsVectorFileWriter cannot be copied.
QgsVectorFileWriter &operator=( const QgsVectorFileWriter &rh ) = delete;

/**
* Details of available filters and formats.
* \since QGIS 3.0
*/
struct FilterFormatDetails
{
//! Unique driver name
QString driverName;

//! Filter string for file picker dialogs
QString filterString;
};

/**
* Returns a list or pairs, with format filter string as first element and OGR format key as second element.
*
Expand All @@ -514,7 +524,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
*
* \see supportedOutputVectorLayerExtensions()
*/
static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
static QList< QgsVectorFileWriter::FilterFormatDetails > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );

/**
* Returns a list of file extensions for supported formats.
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp
Expand Up @@ -55,9 +55,9 @@ QgsGeometryCheckerSetupTab::QgsGeometryCheckerSetupTab( QgisInterface *iface, QD
mRunButton->setEnabled( false );

const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
for ( const auto &filter : filterFormatMap )
for ( const QgsVectorFileWriter::FilterFormatDetails &filter : filterFormatMap )
{
QString driverName = filter.second;
QString driverName = filter.driverName;
ui.comboBoxOutputFormat->addItem( driverName );
if ( driverName == QLatin1String( "ESRI Shapefile" ) )
{
Expand Down Expand Up @@ -217,12 +217,12 @@ void QgsGeometryCheckerSetupTab::selectOutputDirectory()
{
QString filterString = QgsVectorFileWriter::filterForDriver( QStringLiteral( "GPKG" ) );
const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
for ( const auto &filter : filterFormatMap )
for ( const QgsVectorFileWriter::FilterFormatDetails &filter : filterFormatMap )
{
QString driverName = filter.second;
QString driverName = filter.driverName;
if ( driverName != QLatin1String( "ESRI Shapefile" ) ) // Default entry, first in list (see above)
{
filterString += ";;" + filter.first;
filterString += ";;" + filter.filterString;
}
}
QString initialdir = ui.lineEditOutputDirectory->text();
Expand Down
12 changes: 7 additions & 5 deletions tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -726,13 +726,15 @@ def testOverwriteLayer(self):
def testSupportedFiltersAndFormat(self):
# test with formats in recommended order
formats = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.SortRecommended)
self.assertEqual(formats[0], ('GeoPackage (*.gpkg *.GPKG)', 'GPKG'))
self.assertEqual(formats[1], ('ESRI Shapefile (*.shp *.SHP)', 'ESRI Shapefile'))
self.assertEqual(formats[0].filterString, 'GeoPackage (*.gpkg *.GPKG)')
self.assertEqual(formats[0].driverName, 'GPKG')
self.assertEqual(formats[1].filterString, 'ESRI Shapefile (*.shp *.SHP)')
self.assertEqual(formats[1].driverName, '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'))
self.assertTrue(formats2[0].driverName < formats2[1].driverName)
self.assertCountEqual([f.driverName for f in formats], [f.driverName for f in formats2])
self.assertNotEqual(formats2[0].driverName, 'GeoPackage')

def testOgrDriverList(self):
# test with drivers in recommended order
Expand Down

0 comments on commit fe05660

Please sign in to comment.