Fix more sorting issues in vector format lists

This commit is contained in:
Nyall Dawson 2018-06-21 10:25:41 +10:00
parent 2ed547219a
commit 5cfa18ee9f
2 changed files with 49 additions and 11 deletions

View File

@ -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;
@ -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;
@ -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 )

View File

@ -775,6 +775,9 @@ class TestQgsVectorFileWriter(unittest.TestCase):
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)
@ -818,12 +821,19 @@ class TestQgsVectorFileWriter(unittest.TestCase):
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)
@ -834,10 +844,16 @@ class TestQgsVectorFileWriter(unittest.TestCase):
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)