Skip to content

Commit f332971

Browse files
committed
Fix confusing sort order of vector formats in save as dialog
We were sorting on the driver name, not the display name, so MS OpenOffice ("XLSX") was appearing after Sqlite.
1 parent 4ddfd13 commit f332971

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

src/core/qgsvectorfilewriter.cpp

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,9 +2806,6 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
28062806
QgsApplication::registerOgrDrivers();
28072807
int const drvCount = OGRGetDriverCount();
28082808

2809-
FilterFormatDetails shapeFormat;
2810-
FilterFormatDetails gpkgFormat;
2811-
28122809
for ( int i = 0; i < drvCount; ++i )
28132810
{
28142811
OGRSFDriverH drv = OGRGetDriver( i );
@@ -2846,41 +2843,27 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
28462843
details.driverName = drvName;
28472844
details.filterString = filterString;
28482845

2849-
if ( options & SortRecommended )
2850-
{
2851-
if ( drvName == QLatin1String( "ESRI Shapefile" ) )
2852-
{
2853-
shapeFormat = details;
2854-
continue;
2855-
}
2856-
else if ( drvName == QLatin1String( "GPKG" ) )
2857-
{
2858-
gpkgFormat = details;
2859-
continue;
2860-
}
2861-
}
2862-
28632846
results << details;
28642847
}
28652848
}
28662849
}
28672850

2868-
std::sort( results.begin(), results.end(), []( const FilterFormatDetails & a, const FilterFormatDetails & b ) -> bool
2851+
std::sort( results.begin(), results.end(), [options]( const FilterFormatDetails & a, const FilterFormatDetails & b ) -> bool
28692852
{
2870-
return a.driverName < b.driverName;
2871-
} );
2872-
2873-
if ( options & SortRecommended )
2874-
{
2875-
if ( !shapeFormat.filterString.isEmpty() )
2876-
{
2877-
results.insert( 0, shapeFormat );
2878-
}
2879-
if ( !gpkgFormat.filterString.isEmpty() )
2853+
if ( options & SortRecommended )
28802854
{
2881-
results.insert( 0, gpkgFormat );
2855+
if ( a.driverName == QLatin1String( "GPKG" ) )
2856+
return true; // Make https://twitter.com/shapefiIe a sad little fellow
2857+
else if ( b.driverName == QLatin1String( "GPKG" ) )
2858+
return false;
2859+
else if ( a.driverName == QLatin1String( "ESRI Shapefile" ) )
2860+
return true;
2861+
else if ( b.driverName == QLatin1String( "ESRI Shapefile" ) )
2862+
return false;
28822863
}
2883-
}
2864+
2865+
return a.driverName.toLower().localeAwareCompare( b.driverName.toLower() ) < 0;
2866+
} );
28842867

28852868
return results;
28862869
}
@@ -2966,23 +2949,8 @@ QList< QgsVectorFileWriter::DriverDetails > QgsVectorFileWriter::ogrDriverList(
29662949
}
29672950
}
29682951
}
2969-
std::sort( writableDrivers.begin(), writableDrivers.end() );
2970-
if ( options & SortRecommended )
2971-
{
2972-
// recommended order sorting, so we shift certain formats to the top
2973-
if ( writableDrivers.contains( QStringLiteral( "ESRI Shapefile" ) ) )
2974-
{
2975-
writableDrivers.removeAll( QStringLiteral( "ESRI Shapefile" ) );
2976-
writableDrivers.insert( 0, QStringLiteral( "ESRI Shapefile" ) );
2977-
}
2978-
if ( writableDrivers.contains( QStringLiteral( "GPKG" ) ) )
2979-
{
2980-
// Make https://twitter.com/shapefiIe a sad little fellow
2981-
writableDrivers.removeAll( QStringLiteral( "GPKG" ) );
2982-
writableDrivers.insert( 0, QStringLiteral( "GPKG" ) );
2983-
}
2984-
}
29852952

2953+
results.reserve( writableDrivers.count() );
29862954
for ( const QString &drvName : qgis::as_const( writableDrivers ) )
29872955
{
29882956
MetaData metadata;
@@ -2994,6 +2962,23 @@ QList< QgsVectorFileWriter::DriverDetails > QgsVectorFileWriter::ogrDriverList(
29942962
results << details;
29952963
}
29962964
}
2965+
2966+
std::sort( results.begin(), results.end(), [options]( const DriverDetails & a, const DriverDetails & b ) -> bool
2967+
{
2968+
if ( options & SortRecommended )
2969+
{
2970+
if ( a.driverName == QLatin1String( "GPKG" ) )
2971+
return true; // Make https://twitter.com/shapefiIe a sad little fellow
2972+
else if ( b.driverName == QLatin1String( "GPKG" ) )
2973+
return false;
2974+
else if ( a.driverName == QLatin1String( "ESRI Shapefile" ) )
2975+
return true;
2976+
else if ( b.driverName == QLatin1String( "ESRI Shapefile" ) )
2977+
return false;
2978+
}
2979+
2980+
return a.longName.toLower().localeAwareCompare( b.longName.toLower() ) < 0;
2981+
} );
29972982
return results;
29982983
}
29992984

tests/src/python/test_qgsvectorfilewriter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,12 @@ def testOgrDriverList(self):
791791
self.assertEqual(drivers[1].longName, 'ESRI Shapefile')
792792
self.assertEqual(drivers[1].driverName, 'ESRI Shapefile')
793793
self.assertTrue('ODS' in [f.driverName for f in drivers])
794+
795+
# ensure that XLSX comes before SQLite, because we should sort on longName, not driverName!
796+
ms_xlsx_index = next(i for i, v in enumerate(drivers) if v.driverName == 'XLSX')
797+
sqlite_index = next(i for i, v in enumerate(drivers) if v.driverName == 'SQLite')
798+
self.assertLess(ms_xlsx_index, sqlite_index)
799+
794800
# alphabetical sorting
795801
drivers2 = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.VectorFormatOptions())
796802
self.assertTrue(drivers2[0].longName < drivers2[1].longName)
@@ -822,7 +828,7 @@ def testFileFilterString(self):
822828
formats = QgsVectorFileWriter.fileFilterString()
823829
self.assertTrue('gpkg' in formats)
824830
self.assertTrue('shp' in formats)
825-
self.assertTrue(formats.index('gpkg') < formats.index('shp'))
831+
self.assertLess(formats.index('gpkg'), formats.index('shp'))
826832
self.assertTrue('ods' in formats)
827833

828834
# alphabetical sorting

0 commit comments

Comments
 (0)