Skip to content

Commit

Permalink
Fix more sorting issues in vector format lists
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 21, 2018
1 parent 2ed5472 commit 5cfa18e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2843,7 +2843,7 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
QStringList globs;
if ( driverMetadata( drvName, metadata ) && !metadata.glob.isEmpty() )
{
globs << metadata.glob.toLower();
globs = metadata.glob.toLower().split( ' ' );
}

FilterFormatDetails details;
Expand All @@ -2870,7 +2870,7 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
return false;
}

return a.driverName.toLower().localeAwareCompare( b.driverName.toLower() ) < 0;
return a.filterString.toLower().localeAwareCompare( b.filterString.toLower() ) < 0;
} );

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

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

for ( const FilterFormatDetails &format : formats )
{
QString ext = format.filterString;
QRegularExpressionMatch match = rx.match( ext );
if ( !match.hasMatch() )
continue;
for ( const QString &glob : format.globs )
{
const QRegularExpressionMatch match = rx.match( glob );
if ( !match.hasMatch() )
continue;

QString matched = match.captured( 1 );
extensions << matched;
const QString matched = match.captured( 1 );
extensions.insert( matched );
}
}
return extensions;

QStringList extensionList = extensions.toList();

std::sort( extensionList.begin(), extensionList.end(), [options]( const QString & a, const QString & b ) -> bool
{
if ( options & SortRecommended )
{
if ( a == QLatin1String( "gpkg" ) )
return true; // Make https://twitter.com/shapefiIe a sad little fellow
else if ( b == QLatin1String( "gpkg" ) )
return false;
else if ( a == QLatin1String( "shp" ) )
return true;
else if ( b == QLatin1String( "shp" ) )
return false;
}

return a.toLower().localeAwareCompare( b.toLower() ) < 0;
} );

return extensionList;
}

QList< QgsVectorFileWriter::DriverDetails > QgsVectorFileWriter::ogrDriverList( const VectorFormatOptions options )
Expand Down
16 changes: 16 additions & 0 deletions tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -775,6 +775,9 @@ def testSupportedFiltersAndFormat(self):
self.assertEqual(formats[1].globs, ['*.shp'])
self.assertTrue('ODS' in [f.driverName for f in formats])

interlis_format = [f for f in formats if f.driverName == 'Interlis 2'][0]
self.assertEqual(interlis_format.globs, ['*.xtf', '*.xml', '*.ili'])

# alphabetical sorting
formats2 = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.VectorFormatOptions())
self.assertTrue(formats2[0].driverName < formats2[1].driverName)
Expand Down Expand Up @@ -818,12 +821,19 @@ def testSupportedFormatExtensions(self):
self.assertEqual(formats[0], 'gpkg')
self.assertEqual(formats[1], 'shp')
self.assertTrue('ods' in formats)
self.assertTrue('xtf' in formats)
self.assertTrue('ili' in formats)

for i in range(2, len(formats) - 1):
self.assertLess(formats[i].lower(), formats[i + 1].lower())

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

formats = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.SkipNonSpatialFormats)
self.assertFalse('ods' in formats)
Expand All @@ -834,10 +844,16 @@ def testFileFilterString(self):
self.assertTrue('shp' in formats)
self.assertLess(formats.index('gpkg'), formats.index('shp'))
self.assertTrue('ods' in formats)
parts = formats.split(';;')
for i in range(2, len(parts) - 1):
self.assertLess(parts[i].lower(), parts[i + 1].lower())

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

# hide non spatial
formats = QgsVectorFileWriter.fileFilterString(QgsVectorFileWriter.SkipNonSpatialFormats)
Expand Down

0 comments on commit 5cfa18e

Please sign in to comment.