Skip to content

Commit fe05660

Browse files
committedNov 5, 2017
More QPair API removal
1 parent a4ef7e4 commit fe05660

File tree

7 files changed

+68
-40
lines changed

7 files changed

+68
-40
lines changed
 

‎doc/api_break.dox

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,7 @@ for consistency with other parts of the QGIS API.
26732673
- static `writeAsVectorFormat` calls no longer take a errorMessage argument in
26742674
python and instead return a `(errorCode, errorMessage)` tuple.
26752675
- ogrDriverList now returns a list of QgsVectorFileWriter.DriverDetails structs, instead of a map
2676+
- supportedFiltersAndFormats now returns a list of QgsVectorFileWriter.FilterFormatDetails structs, instead of a map
26762677

26772678

26782679
QgsWMSLegendNode {#qgis_api_break_3_0_QgsWMSLegendNode}

‎python/core/qgsvectorfilewriter.sip

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111

1212

13-
14-
1513
class QgsVectorFileWriter : QgsFeatureSink
1614
{
1715
%Docstring
@@ -468,15 +466,28 @@ Create a new vector file writer
468466

469467

470468

471-
static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
469+
struct FilterFormatDetails
470+
{
471+
QString driverName;
472+
%Docstring
473+
Unique driver name
474+
%End
475+
476+
QString filterString;
477+
%Docstring
478+
Filter string for file picker dialogs
479+
%End
480+
};
481+
482+
static QList< QgsVectorFileWriter::FilterFormatDetails > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
472483
%Docstring
473484
Returns a list or pairs, with format filter string as first element and OGR format key as second element.
474485

475486
The ``options`` argument can be used to control the sorting and filtering of
476487
returned formats.
477488

478489
.. seealso:: supportedOutputVectorLayerExtensions()
479-
:rtype: list of QPair< str, QString >
490+
:rtype: list of QgsVectorFileWriter.FilterFormatDetails
480491
%End
481492

482493
static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended );

‎python/plugins/processing/algs/gdal/GdalUtils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ def getVectorDriverFromFileName(filename):
175175

176176
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
177177
for format in formats:
178-
if ext in format[0]:
179-
return format[1]
178+
if ext in format.filterString:
179+
return format.driverName
180180
return 'ESRI Shapefile'
181181

182182
@staticmethod

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,15 +2681,15 @@ void QgsVectorFileWriter::setSymbologyScale( double d )
26812681
mRenderContext.setRendererScale( mSymbologyScale );
26822682
}
26832683

2684-
QList< QPair< QString, QString > > QgsVectorFileWriter::supportedFiltersAndFormats( const VectorFormatOptions options )
2684+
QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supportedFiltersAndFormats( const VectorFormatOptions options )
26852685
{
2686-
QList< QPair< QString, QString > > resultMap;
2686+
QList< FilterFormatDetails > results;
26872687

26882688
QgsApplication::registerOgrDrivers();
26892689
int const drvCount = OGRGetDriverCount();
26902690

2691-
QPair< QString, QString > shapeFormat;
2692-
QPair< QString, QString > gpkgFormat;
2691+
FilterFormatDetails shapeFormat;
2692+
FilterFormatDetails gpkgFormat;
26932693

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

2706+
FilterFormatDetails details;
2707+
details.driverName = drvName;
2708+
details.filterString = filterString;
2709+
27062710
if ( options & SortRecommended )
27072711
{
27082712
if ( drvName == QStringLiteral( "ESRI Shapefile" ) )
27092713
{
2710-
shapeFormat = qMakePair( filterString, drvName );
2714+
shapeFormat = details;
27112715
continue;
27122716
}
27132717
else if ( drvName == QStringLiteral( "GPKG" ) )
27142718
{
2715-
gpkgFormat = qMakePair( filterString, drvName );
2719+
gpkgFormat = details;
27162720
continue;
27172721
}
27182722
}
2719-
resultMap << qMakePair( filterString, drvName );
2723+
2724+
results << details;
27202725
}
27212726
}
27222727
}
27232728

2724-
std::sort( resultMap.begin(), resultMap.end(), []( const QPair< QString, QString > &a, const QPair< QString, QString > &b ) -> bool
2729+
std::sort( results.begin(), results.end(), []( const FilterFormatDetails & a, const FilterFormatDetails & b ) -> bool
27252730
{
2726-
return a.second < b.second;
2731+
return a.driverName < b.driverName;
27272732
} );
27282733

27292734
if ( options & SortRecommended )
27302735
{
2731-
if ( !shapeFormat.first.isEmpty() )
2736+
if ( !shapeFormat.filterString.isEmpty() )
27322737
{
2733-
resultMap.insert( 0, shapeFormat );
2738+
results.insert( 0, shapeFormat );
27342739
}
2735-
if ( !gpkgFormat.first.isEmpty() )
2740+
if ( !gpkgFormat.filterString.isEmpty() )
27362741
{
2737-
resultMap.insert( 0, gpkgFormat );
2742+
results.insert( 0, gpkgFormat );
27382743
}
27392744
}
27402745

2741-
return resultMap;
2746+
return results;
27422747
}
27432748

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

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

2751-
auto formatIt = formats.constBegin();
2752-
for ( ; formatIt != formats.constEnd(); ++formatIt )
2756+
for ( const FilterFormatDetails &format : formats )
27532757
{
2754-
QString ext = formatIt->first;
2758+
QString ext = format.filterString;
27552759
QRegularExpressionMatch match = rx.match( ext );
27562760
if ( !match.hasMatch() )
27572761
continue;
@@ -2882,13 +2886,13 @@ QString QgsVectorFileWriter::driverForExtension( const QString &extension )
28822886
QString QgsVectorFileWriter::fileFilterString( const VectorFormatOptions options )
28832887
{
28842888
QString filterString;
2885-
const auto driverFormatMap = supportedFiltersAndFormats( options );
2886-
for ( auto it = driverFormatMap.constBegin(); it != driverFormatMap.constEnd(); ++it )
2889+
const auto driverFormats = supportedFiltersAndFormats( options );
2890+
for ( const FilterFormatDetails &details : driverFormats )
28872891
{
28882892
if ( !filterString.isEmpty() )
28892893
filterString += QLatin1String( ";;" );
28902894

2891-
filterString += it->first;
2895+
filterString += details.filterString;
28922896
}
28932897
return filterString;
28942898
}

‎src/core/qgsvectorfilewriter.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
#include "qgsogrutils.h"
3030
#include <ogr_api.h>
3131

32-
#include <QPair>
33-
34-
3532
class QgsSymbolLayer;
3633
class QTextCodec;
3734
class QgsFeatureIterator;
@@ -506,6 +503,19 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
506503
//! QgsVectorFileWriter cannot be copied.
507504
QgsVectorFileWriter &operator=( const QgsVectorFileWriter &rh ) = delete;
508505

506+
/**
507+
* Details of available filters and formats.
508+
* \since QGIS 3.0
509+
*/
510+
struct FilterFormatDetails
511+
{
512+
//! Unique driver name
513+
QString driverName;
514+
515+
//! Filter string for file picker dialogs
516+
QString filterString;
517+
};
518+
509519
/**
510520
* Returns a list or pairs, with format filter string as first element and OGR format key as second element.
511521
*
@@ -514,7 +524,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
514524
*
515525
* \see supportedOutputVectorLayerExtensions()
516526
*/
517-
static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
527+
static QList< QgsVectorFileWriter::FilterFormatDetails > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
518528

519529
/**
520530
* Returns a list of file extensions for supported formats.

‎src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ QgsGeometryCheckerSetupTab::QgsGeometryCheckerSetupTab( QgisInterface *iface, QD
5555
mRunButton->setEnabled( false );
5656

5757
const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
58-
for ( const auto &filter : filterFormatMap )
58+
for ( const QgsVectorFileWriter::FilterFormatDetails &filter : filterFormatMap )
5959
{
60-
QString driverName = filter.second;
60+
QString driverName = filter.driverName;
6161
ui.comboBoxOutputFormat->addItem( driverName );
6262
if ( driverName == QLatin1String( "ESRI Shapefile" ) )
6363
{
@@ -217,12 +217,12 @@ void QgsGeometryCheckerSetupTab::selectOutputDirectory()
217217
{
218218
QString filterString = QgsVectorFileWriter::filterForDriver( QStringLiteral( "GPKG" ) );
219219
const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
220-
for ( const auto &filter : filterFormatMap )
220+
for ( const QgsVectorFileWriter::FilterFormatDetails &filter : filterFormatMap )
221221
{
222-
QString driverName = filter.second;
222+
QString driverName = filter.driverName;
223223
if ( driverName != QLatin1String( "ESRI Shapefile" ) ) // Default entry, first in list (see above)
224224
{
225-
filterString += ";;" + filter.first;
225+
filterString += ";;" + filter.filterString;
226226
}
227227
}
228228
QString initialdir = ui.lineEditOutputDirectory->text();

‎tests/src/python/test_qgsvectorfilewriter.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,15 @@ def testOverwriteLayer(self):
726726
def testSupportedFiltersAndFormat(self):
727727
# test with formats in recommended order
728728
formats = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.SortRecommended)
729-
self.assertEqual(formats[0], ('GeoPackage (*.gpkg *.GPKG)', 'GPKG'))
730-
self.assertEqual(formats[1], ('ESRI Shapefile (*.shp *.SHP)', 'ESRI Shapefile'))
729+
self.assertEqual(formats[0].filterString, 'GeoPackage (*.gpkg *.GPKG)')
730+
self.assertEqual(formats[0].driverName, 'GPKG')
731+
self.assertEqual(formats[1].filterString, 'ESRI Shapefile (*.shp *.SHP)')
732+
self.assertEqual(formats[1].driverName, 'ESRI Shapefile')
731733
# alphabetical sorting
732734
formats2 = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.VectorFormatOptions())
733-
self.assertTrue(formats2[0][0] < formats2[1][0])
734-
self.assertCountEqual(formats, formats2)
735-
self.assertNotEqual(formats2[0], ('GeoPackage', 'GPKG'))
735+
self.assertTrue(formats2[0].driverName < formats2[1].driverName)
736+
self.assertCountEqual([f.driverName for f in formats], [f.driverName for f in formats2])
737+
self.assertNotEqual(formats2[0].driverName, 'GeoPackage')
736738

737739
def testOgrDriverList(self):
738740
# test with drivers in recommended order

0 commit comments

Comments
 (0)
Please sign in to comment.