Skip to content

Commit 5cfa18e

Browse files
committedJun 21, 2018
Fix more sorting issues in vector format lists
1 parent 2ed5472 commit 5cfa18e

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed
 

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,7 +2843,7 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
28432843
QStringList globs;
28442844
if ( driverMetadata( drvName, metadata ) && !metadata.glob.isEmpty() )
28452845
{
2846-
globs << metadata.glob.toLower();
2846+
globs = metadata.glob.toLower().split( ' ' );
28472847
}
28482848

28492849
FilterFormatDetails details;
@@ -2870,7 +2870,7 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
28702870
return false;
28712871
}
28722872

2873-
return a.driverName.toLower().localeAwareCompare( b.driverName.toLower() ) < 0;
2873+
return a.filterString.toLower().localeAwareCompare( b.filterString.toLower() ) < 0;
28742874
} );
28752875

28762876
return results;
@@ -2879,21 +2879,43 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
28792879
QStringList QgsVectorFileWriter::supportedFormatExtensions( const VectorFormatOptions options )
28802880
{
28812881
const auto formats = supportedFiltersAndFormats( options );
2882-
QStringList extensions;
2882+
QSet< QString > extensions;
28832883

2884-
QRegularExpression rx( QStringLiteral( "\\*\\.([a-zA-Z0-9]*)" ) );
2884+
const QRegularExpression rx( QStringLiteral( "\\*\\.(.*)$" ) );
28852885

28862886
for ( const FilterFormatDetails &format : formats )
28872887
{
2888-
QString ext = format.filterString;
2889-
QRegularExpressionMatch match = rx.match( ext );
2890-
if ( !match.hasMatch() )
2891-
continue;
2888+
for ( const QString &glob : format.globs )
2889+
{
2890+
const QRegularExpressionMatch match = rx.match( glob );
2891+
if ( !match.hasMatch() )
2892+
continue;
28922893

2893-
QString matched = match.captured( 1 );
2894-
extensions << matched;
2894+
const QString matched = match.captured( 1 );
2895+
extensions.insert( matched );
2896+
}
28952897
}
2896-
return extensions;
2898+
2899+
QStringList extensionList = extensions.toList();
2900+
2901+
std::sort( extensionList.begin(), extensionList.end(), [options]( const QString & a, const QString & b ) -> bool
2902+
{
2903+
if ( options & SortRecommended )
2904+
{
2905+
if ( a == QLatin1String( "gpkg" ) )
2906+
return true; // Make https://twitter.com/shapefiIe a sad little fellow
2907+
else if ( b == QLatin1String( "gpkg" ) )
2908+
return false;
2909+
else if ( a == QLatin1String( "shp" ) )
2910+
return true;
2911+
else if ( b == QLatin1String( "shp" ) )
2912+
return false;
2913+
}
2914+
2915+
return a.toLower().localeAwareCompare( b.toLower() ) < 0;
2916+
} );
2917+
2918+
return extensionList;
28972919
}
28982920

28992921
QList< QgsVectorFileWriter::DriverDetails > QgsVectorFileWriter::ogrDriverList( const VectorFormatOptions options )

‎tests/src/python/test_qgsvectorfilewriter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ def testSupportedFiltersAndFormat(self):
775775
self.assertEqual(formats[1].globs, ['*.shp'])
776776
self.assertTrue('ODS' in [f.driverName for f in formats])
777777

778+
interlis_format = [f for f in formats if f.driverName == 'Interlis 2'][0]
779+
self.assertEqual(interlis_format.globs, ['*.xtf', '*.xml', '*.ili'])
780+
778781
# alphabetical sorting
779782
formats2 = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.VectorFormatOptions())
780783
self.assertTrue(formats2[0].driverName < formats2[1].driverName)
@@ -818,12 +821,19 @@ def testSupportedFormatExtensions(self):
818821
self.assertEqual(formats[0], 'gpkg')
819822
self.assertEqual(formats[1], 'shp')
820823
self.assertTrue('ods' in formats)
824+
self.assertTrue('xtf' in formats)
825+
self.assertTrue('ili' in formats)
826+
827+
for i in range(2, len(formats) - 1):
828+
self.assertLess(formats[i].lower(), formats[i + 1].lower())
821829

822830
# alphabetical sorting
823831
formats2 = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.VectorFormatOptions())
824832
self.assertTrue(formats2[0] < formats2[1])
825833
self.assertCountEqual(formats, formats2)
826834
self.assertNotEqual(formats2[0], 'gpkg')
835+
for i in range(0, len(formats2) - 1):
836+
self.assertLess(formats2[i].lower(), formats2[i + 1].lower())
827837

828838
formats = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.SkipNonSpatialFormats)
829839
self.assertFalse('ods' in formats)
@@ -834,10 +844,16 @@ def testFileFilterString(self):
834844
self.assertTrue('shp' in formats)
835845
self.assertLess(formats.index('gpkg'), formats.index('shp'))
836846
self.assertTrue('ods' in formats)
847+
parts = formats.split(';;')
848+
for i in range(2, len(parts) - 1):
849+
self.assertLess(parts[i].lower(), parts[i + 1].lower())
837850

838851
# alphabetical sorting
839852
formats2 = QgsVectorFileWriter.fileFilterString(QgsVectorFileWriter.VectorFormatOptions())
840853
self.assertNotEqual(formats.index('gpkg'), formats2.index('gpkg'))
854+
parts = formats2.split(';;')
855+
for i in range(len(parts) - 1):
856+
self.assertLess(parts[i].lower(), parts[i + 1].lower())
841857

842858
# hide non spatial
843859
formats = QgsVectorFileWriter.fileFilterString(QgsVectorFileWriter.SkipNonSpatialFormats)

0 commit comments

Comments
 (0)
Please sign in to comment.