Avoid use of QPair and instead use a struct

This commit is contained in:
Nyall Dawson 2017-11-06 07:19:39 +10:00
parent d855f7f3f4
commit a4ef7e42c7
6 changed files with 54 additions and 22 deletions

View File

@ -2672,6 +2672,7 @@ for consistency with other parts of the QGIS API.
- The addFeature which takes a renderer argument was renamed to addFeatureWithStyle. - The addFeature which takes a renderer argument was renamed to addFeatureWithStyle.
- static `writeAsVectorFormat` calls no longer take a errorMessage argument in - static `writeAsVectorFormat` calls no longer take a errorMessage argument in
python and instead return a `(errorCode, errorMessage)` tuple. python and instead return a `(errorCode, errorMessage)` tuple.
- ogrDriverList now returns a list of QgsVectorFileWriter.DriverDetails structs, instead of a map
QgsWMSLegendNode {#qgis_api_break_3_0_QgsWMSLegendNode} QgsWMSLegendNode {#qgis_api_break_3_0_QgsWMSLegendNode}

View File

@ -491,18 +491,31 @@ Create a new vector file writer
:rtype: list of str :rtype: list of str
%End %End
static QList< QPair< QString, QString > > ogrDriverList( VectorFormatOptions options = SortRecommended ); struct DriverDetails
{
QString longName;
%Docstring %Docstring
Returns driver list that can be used for dialogs. It contains all OGR drivers Descriptive, user friendly name for the driver
%End
QString driverName;
%Docstring
Unique driver name
%End
};
static QList< QgsVectorFileWriter::DriverDetails > ogrDriverList( VectorFormatOptions options = SortRecommended );
%Docstring
Returns the driver list that can be used for dialogs. It contains all OGR drivers
plus some additional internal QGIS driver names to distinguish between more plus some additional internal QGIS driver names to distinguish between more
supported formats of the same OGR driver. supported formats of the same OGR driver.
The returned list consists of pairs of driver long name (e.g. user-friendly The returned list consists of structs containing the driver long name (e.g. user-friendly
display name for the format) to internal driver short name. display name for the format) and internal driver short name.
The ``options`` argument can be used to control the sorting and filtering of The ``options`` argument can be used to control the sorting and filtering of
returned drivers. returned drivers.
:rtype: list of QPair< str, QString > :rtype: list of QgsVectorFileWriter.DriverDetails
%End %End
static QString driverForExtension( const QString &extension ); static QString driverForExtension( const QString &extension );

View File

@ -2762,12 +2762,12 @@ QStringList QgsVectorFileWriter::supportedFormatExtensions( const VectorFormatOp
return extensions; return extensions;
} }
QList< QPair< QString, QString> > QgsVectorFileWriter::ogrDriverList( const VectorFormatOptions options ) QList< QgsVectorFileWriter::DriverDetails > QgsVectorFileWriter::ogrDriverList( const VectorFormatOptions options )
{ {
QList< QPair< QString, QString> > resultMap; QList< QgsVectorFileWriter::DriverDetails > results;
QgsApplication::registerOgrDrivers(); QgsApplication::registerOgrDrivers();
int const drvCount = OGRGetDriverCount(); const int drvCount = OGRGetDriverCount();
QStringList writableDrivers; QStringList writableDrivers;
for ( int i = 0; i < drvCount; ++i ) for ( int i = 0; i < drvCount; ++i )
@ -2836,10 +2836,13 @@ QList< QPair< QString, QString> > QgsVectorFileWriter::ogrDriverList( const Vect
MetaData metadata; MetaData metadata;
if ( driverMetadata( drvName, metadata ) && !metadata.trLongName.isEmpty() ) if ( driverMetadata( drvName, metadata ) && !metadata.trLongName.isEmpty() )
{ {
resultMap << qMakePair( metadata.trLongName, drvName ); DriverDetails details;
details.driverName = drvName;
details.longName = metadata.trLongName;
results << details;
} }
} }
return resultMap; return results;
} }
QString QgsVectorFileWriter::driverForExtension( const QString &extension ) QString QgsVectorFileWriter::driverForExtension( const QString &extension )

View File

@ -528,17 +528,30 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended ); static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended );
/** /**
* Returns driver list that can be used for dialogs. It contains all OGR drivers * Details of available driver formats.
* \since QGIS 3.0
*/
struct DriverDetails
{
//! Descriptive, user friendly name for the driver
QString longName;
//! Unique driver name
QString driverName;
};
/**
* Returns the driver list that can be used for dialogs. It contains all OGR drivers
* plus some additional internal QGIS driver names to distinguish between more * plus some additional internal QGIS driver names to distinguish between more
* supported formats of the same OGR driver. * supported formats of the same OGR driver.
* *
* The returned list consists of pairs of driver long name (e.g. user-friendly * The returned list consists of structs containing the driver long name (e.g. user-friendly
* display name for the format) to internal driver short name. * display name for the format) and internal driver short name.
* *
* The \a options argument can be used to control the sorting and filtering of * The \a options argument can be used to control the sorting and filtering of
* returned drivers. * returned drivers.
*/ */
static QList< QPair< QString, QString > > ogrDriverList( VectorFormatOptions options = SortRecommended ); static QList< QgsVectorFileWriter::DriverDetails > ogrDriverList( VectorFormatOptions options = SortRecommended );
/** /**
* Returns the OGR driver name for a specified file \a extension. E.g. the * Returns the OGR driver name for a specified file \a extension. E.g. the

View File

@ -87,11 +87,11 @@ void QgsVectorLayerSaveAsDialog::setup()
QgsSettings settings; QgsSettings settings;
restoreGeometry( settings.value( QStringLiteral( "Windows/VectorLayerSaveAs/geometry" ) ).toByteArray() ); restoreGeometry( settings.value( QStringLiteral( "Windows/VectorLayerSaveAs/geometry" ) ).toByteArray() );
const QList< QPair< QString, QString > > map = QgsVectorFileWriter::ogrDriverList(); const QList< QgsVectorFileWriter::DriverDetails > drivers = QgsVectorFileWriter::ogrDriverList();
mFormatComboBox->blockSignals( true ); mFormatComboBox->blockSignals( true );
for ( auto it = map.constBegin(); it != map.constEnd(); ++it ) for ( const QgsVectorFileWriter::DriverDetails &driver : drivers )
{ {
mFormatComboBox->addItem( it->first, it->second ); mFormatComboBox->addItem( driver.longName, driver.driverName );
} }
QString format = settings.value( QStringLiteral( "UI/lastVectorFormat" ), "GPKG" ).toString(); QString format = settings.value( QStringLiteral( "UI/lastVectorFormat" ), "GPKG" ).toString();

View File

@ -737,13 +737,15 @@ class TestQgsVectorFileWriter(unittest.TestCase):
def testOgrDriverList(self): def testOgrDriverList(self):
# test with drivers in recommended order # test with drivers in recommended order
drivers = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.SortRecommended) drivers = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.SortRecommended)
self.assertEqual(drivers[0], ('GeoPackage', 'GPKG')) self.assertEqual(drivers[0].longName, 'GeoPackage')
self.assertEqual(drivers[1], ('ESRI Shapefile', 'ESRI Shapefile')) self.assertEqual(drivers[0].driverName, 'GPKG')
self.assertEqual(drivers[1].longName, 'ESRI Shapefile')
self.assertEqual(drivers[1].driverName, 'ESRI Shapefile')
# alphabetical sorting # alphabetical sorting
drivers2 = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.VectorFormatOptions()) drivers2 = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.VectorFormatOptions())
self.assertTrue(drivers2[0][0] < drivers2[1][0]) self.assertTrue(drivers2[0].longName < drivers2[1].longName)
self.assertCountEqual(drivers, drivers2) self.assertCountEqual([d.driverName for d in drivers], [d.driverName for d in drivers2])
self.assertNotEqual(drivers2[0], ('GeoPackage', 'GPKG')) self.assertNotEqual(drivers2[0].driverName, 'GPKG')
def testSupportedFormatExtensions(self): def testSupportedFormatExtensions(self):
formats = QgsVectorFileWriter.supportedFormatExtensions() formats = QgsVectorFileWriter.supportedFormatExtensions()