Skip to content

Commit

Permalink
sort and cleanup gdal and ogr file filter list
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed May 9, 2014
1 parent 2f74037 commit 9aff0d7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
71 changes: 49 additions & 22 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,28 @@ QGISEXTERN bool isProvider()
{
return true;
}
/**
Convenience function for readily creating file filters.
Given a long name for a file filter and a regular expression, return
a file filter string suitable for use in a QFileDialog::OpenFiles()
call. The regular express, glob, will have both all lower and upper
case versions added.
@note
Copied from qgisapp.cpp.
@todo XXX This should probably be generalized and moved to a standard
utility type thingy.
*/
static QString createFileFilter_( QString const &longName, QString const &glob )
{
// return longName + " [GDAL] (" + glob.toLower() + " " + glob.toUpper() + ");;";
return longName + " (" + glob.toLower() + " " + glob.toUpper() + ");;";
} // createFileFilter_

void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString, QStringList & theExtensions, QStringList & theWildcards )
{
Expand Down Expand Up @@ -1904,8 +1926,7 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
// driver, which will be found in DMD_LONGNAME, which will have the
// same form.

// start with the default case
theFileFiltersString = QObject::tr( "[GDAL] All files (*)" );
theFileFiltersString = "";

QgsDebugMsg( QString( "GDAL driver count: %1" ).arg( GDALGetDriverCount() ) );

Expand All @@ -1924,9 +1945,10 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
// supported; if not, we give it a miss for the next driver

myGdalDriverDescription = GDALGetDescription( myGdalDriver );

// QgsDebugMsg(QString("got driver string %1").arg(myGdalDriverDescription));

myGdalDriverExtension = myGdalDriverLongName = "";

myGdalDriverMetadata = GDALGetMetadata( myGdalDriver, NULL );

// presumably we know we've run out of metadta if either the
Expand Down Expand Up @@ -1959,6 +1981,7 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
myGdalDriverLongName.remove( QRegExp( "\\(.*\\)$" ) );
}
}

// if we have both the file name extension and the long name,
// then we've all the information we need for the current
// driver; therefore emit a file filter string and move to
Expand Down Expand Up @@ -1990,7 +2013,7 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
theExtensions << "jpeg";
}

theFileFiltersString += ";;[GDAL] " + myGdalDriverLongName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString += createFileFilter_( myGdalDriverLongName, glob );

break; // ... to next driver, if any.
}
Expand All @@ -1999,6 +2022,8 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString

} // each metadata item

//QgsDebugMsg(QString("got driver Desc=%1 LongName=%2").arg(myGdalDriverDescription).arg(myGdalDriverLongName));

if ( myGdalDriverExtension.isEmpty() && !myGdalDriverLongName.isEmpty() )
{
// Then what we have here is a driver with no corresponding
Expand All @@ -2015,8 +2040,7 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
// USGS DEMs use "*.dem"
if ( myGdalDriverDescription.startsWith( "USGSDEM" ) )
{
QString glob = "*.dem";
theFileFiltersString += ";;[GDAL] " + myGdalDriverLongName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString += createFileFilter_( myGdalDriverLongName, "*.dem" );
theExtensions << "dem";
}
else if ( myGdalDriverDescription.startsWith( "DTED" ) )
Expand All @@ -2025,33 +2049,29 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
QString glob = "*.dt0";
glob += " *.dt1";
glob += " *.dt2";
theFileFiltersString += ";;[GDAL] " + myGdalDriverLongName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString += createFileFilter_( myGdalDriverLongName, glob );
theExtensions << "dt0" << "dt1" << "dt2";
}
else if ( myGdalDriverDescription.startsWith( "MrSID" ) )
{
// MrSID use "*.sid"
QString glob = "*.sid";
theFileFiltersString += ";;[GDAL] " + myGdalDriverLongName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString += createFileFilter_( myGdalDriverLongName, "*.sid" );
theExtensions << "sid";
}
else if ( myGdalDriverDescription.startsWith( "EHdr" ) )
{
QString glob = "*.bil";
theFileFiltersString += ";;[GDAL] " + myGdalDriverLongName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString += createFileFilter_( myGdalDriverLongName, "*.bil" );
theExtensions << "bil";
}
else if ( myGdalDriverDescription.startsWith( "AIG" ) )
{
QString glob = "hdr.adf";
theFileFiltersString += ";;[GDAL] " + myGdalDriverLongName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString += createFileFilter_( myGdalDriverLongName, "hdr.adf" );
theWildcards << "hdr.adf";
}
else if ( myGdalDriverDescription == "HDF4" )
{
// HDF4 extension missing in driver metadata
QString glob = "*.hdf";
theFileFiltersString += ";;[GDAL] " + myGdalDriverLongName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString += createFileFilter_( myGdalDriverLongName, "*.hdf" );
theExtensions << "hdf";
}
else
Expand All @@ -2060,23 +2080,30 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
}
} // each loaded GDAL driver

myGdalDriverExtension = myGdalDriverLongName = ""; // reset for next driver

} // each loaded GDAL driver

// VSIFileHandler (see qgsogrprovider.cpp)
// sort file filters alphabetically
QgsDebugMsg( "theFileFiltersString: " + theFileFiltersString );
QStringList filters = theFileFiltersString.split( ";;", QString::SkipEmptyParts );
filters.sort();
theFileFiltersString = filters.join( ";;" ) + ";;";

// VSIFileHandler (see qgsogrprovider.cpp) - second
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1600
QSettings settings;
if ( settings.value( "/qgis/scanZipInBrowser2", "basic" ).toString() != "no" )
{
QString glob = "*.zip";
glob += " *.gz";
glob += " *.tar *.tar.gz *.tgz";
theFileFiltersString += ";;[GDAL] " + QObject::tr( "GDAL/OGR VSIFileHandler" ) + " (" + glob.toLower() + " " + glob.toUpper() + ")";
theFileFiltersString.prepend( createFileFilter_( QObject::tr( "GDAL/OGR VSIFileHandler" ), "*.zip *.gz *.tar *.tar.gz *.tgz" ) );
theExtensions << "zip" << "gz" << "tar" << "tar.gz" << "tgz";
}
#endif

// can't forget the default case - first
theFileFiltersString.prepend( QObject::tr( "All files" ) + " (*);;" );

// cleanup
if ( theFileFiltersString.endsWith( ";;" ) ) theFileFiltersString.chop( 2 );

QgsDebugMsg( "Raster filter list built: " + theFileFiltersString );
} // buildSupportedRasterFileFilter_()

Expand Down
22 changes: 17 additions & 5 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,8 @@ QString QgsOgrProvider::description() const
*/
static QString createFileFilter_( QString const &longName, QString const &glob )
{
return longName + " [OGR] (" + glob.toLower() + " " + glob.toUpper() + ");;";
// return longName + " [OGR] (" + glob.toLower() + " " + glob.toUpper() + ");;";
return longName + " (" + glob.toLower() + " " + glob.toUpper() + ");;";
} // createFileFilter_


Expand Down Expand Up @@ -1834,23 +1835,34 @@ QString createFilters( QString type )

} // each loaded OGR driver

// VSIFileHandler (.zip and .gz files)
// sort file filters alphabetically
QgsDebugMsg( "myFileFilters: " + myFileFilters );
QStringList filters = myFileFilters.split( ";;", QString::SkipEmptyParts );
filters.sort();
myFileFilters = filters.join( ";;" ) + ";;";
QgsDebugMsg( "myFileFilters: " + myFileFilters );

// VSIFileHandler (.zip and .gz files) - second
// see http://trac.osgeo.org/gdal/wiki/UserDocs/ReadInZip
// Requires GDAL>=1.6.0 with libz support, let's assume we have it.
// This does not work for some file types, see VSIFileHandler doc.
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1600
QSettings settings;
if ( settings.value( "/qgis/scanZipInBrowser2", "basic" ).toString() != "no" )
{
myFileFilters += createFileFilter_( QObject::tr( "GDAL/OGR VSIFileHandler" ), "*.zip *.gz *.tar *.tar.gz *.tgz" );
myFileFilters.prepend( createFileFilter_( QObject::tr( "GDAL/OGR VSIFileHandler" ), "*.zip *.gz *.tar *.tar.gz *.tgz" ) );
myExtensions << "zip" << "gz" << "tar" << "tar.gz" << "tgz";

}
#endif

// can't forget the default case
// can't forget the default case - first
myFileFilters.prepend( QObject::tr( "All files" ) + " (*);;" );

// cleanup
if ( myFileFilters.endsWith( ";;" ) ) myFileFilters.chop( 2 );

myFileFilters += QObject::tr( "All files" ) + " (*)";
QgsDebugMsg( "myFileFilters: " + myFileFilters );
}

if ( type == "file" )
Expand Down

0 comments on commit 9aff0d7

Please sign in to comment.