Skip to content

Commit

Permalink
Avoid use of QPair and instead use a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 5, 2017
1 parent d855f7f commit a4ef7e4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -2672,6 +2672,7 @@ for consistency with other parts of the QGIS API.
- The addFeature which takes a renderer argument was renamed to addFeatureWithStyle.
- 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


QgsWMSLegendNode {#qgis_api_break_3_0_QgsWMSLegendNode}
Expand Down
23 changes: 18 additions & 5 deletions python/core/qgsvectorfilewriter.sip
Expand Up @@ -491,18 +491,31 @@ Create a new vector file writer
:rtype: list of str
%End

static QList< QPair< QString, QString > > ogrDriverList( VectorFormatOptions options = SortRecommended );
struct DriverDetails
{
QString longName;
%Docstring
Descriptive, user friendly name for the driver
%End

QString driverName;
%Docstring
Returns driver list that can be used for dialogs. It contains all OGR drivers
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
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 returned list consists of structs containing the driver long name (e.g. user-friendly
display name for the format) and 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 >
:rtype: list of QgsVectorFileWriter.DriverDetails
%End

static QString driverForExtension( const QString &extension );
Expand Down
13 changes: 8 additions & 5 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2762,12 +2762,12 @@ QStringList QgsVectorFileWriter::supportedFormatExtensions( const VectorFormatOp
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();
int const drvCount = OGRGetDriverCount();
const int drvCount = OGRGetDriverCount();

QStringList writableDrivers;
for ( int i = 0; i < drvCount; ++i )
Expand Down Expand Up @@ -2836,10 +2836,13 @@ QList< QPair< QString, QString> > QgsVectorFileWriter::ogrDriverList( const Vect
MetaData metadata;
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 )
Expand Down
21 changes: 17 additions & 4 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -528,17 +528,30 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
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
* 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 returned list consists of structs containing the driver long name (e.g. user-friendly
* display name for the format) and internal driver short name.
*
* The \a options argument can be used to control the sorting and filtering of
* 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
Expand Down
6 changes: 3 additions & 3 deletions src/gui/ogr/qgsvectorlayersaveasdialog.cpp
Expand Up @@ -87,11 +87,11 @@ void QgsVectorLayerSaveAsDialog::setup()
QgsSettings settings;
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 );
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();
Expand Down
12 changes: 7 additions & 5 deletions tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -737,13 +737,15 @@ def testSupportedFiltersAndFormat(self):
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'))
self.assertEqual(drivers[0].longName, 'GeoPackage')
self.assertEqual(drivers[0].driverName, 'GPKG')
self.assertEqual(drivers[1].longName, 'ESRI Shapefile')
self.assertEqual(drivers[1].driverName, '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'))
self.assertTrue(drivers2[0].longName < drivers2[1].longName)
self.assertCountEqual([d.driverName for d in drivers], [d.driverName for d in drivers2])
self.assertNotEqual(drivers2[0].driverName, 'GPKG')

def testSupportedFormatExtensions(self):
formats = QgsVectorFileWriter.supportedFormatExtensions()
Expand Down

0 comments on commit a4ef7e4

Please sign in to comment.