[browser] Don't show gdal/ogr layer items for QGIS style xml files

These aren't layers, so the extra entries are just distracting noise.
This commit is contained in:
Nyall Dawson 2019-01-15 13:19:11 +10:00
parent 956a74fcb7
commit 2aab28380c
7 changed files with 65 additions and 25 deletions

View File

@ -459,6 +459,16 @@ Exports the style as a XML file
bool importXml( const QString &filename );
%Docstring
Imports the symbols and colorramps into the default style database from the given XML file
%End
static bool isXmlStyleFile( const QString &path );
%Docstring
Tests if the file at ``path`` is a QGIS style XML file.
This method samples only the first line in the file, so is safe to call on
large xml files.
.. versionadded:: 3.6
%End
signals:

View File

@ -356,27 +356,6 @@ void QgsStyleXmlDataItem::browseStyle( const QString &xmlPath )
// QgsStyleXmlDataItemProvider
//
bool isStyleFile( const QString &path )
{
QFileInfo fileInfo( path );
if ( fileInfo.suffix().compare( QLatin1String( "xml" ), Qt::CaseInsensitive ) != 0 )
return false;
// sniff the first line of the file to see if it's a style file
if ( !QFile::exists( path ) )
return false;
QFile inputFile( path );
if ( !inputFile.open( QIODevice::ReadOnly ) )
return false;
QTextStream stream( &inputFile );
const QString line = stream.readLine();
return line == QLatin1String( "<!DOCTYPE qgis_style>" );
}
QString QgsStyleXmlDataItemProvider::name()
{
return QStringLiteral( "style_xml" );
@ -389,7 +368,7 @@ int QgsStyleXmlDataItemProvider::capabilities()
QgsDataItem *QgsStyleXmlDataItemProvider::createDataItem( const QString &path, QgsDataItem *parentItem )
{
if ( isStyleFile( path ) )
if ( QgsStyle::isXmlStyleFile( path ) )
{
return new QgsStyleXmlDataItem( parentItem, QFileInfo( path ).fileName(), path );
}
@ -414,7 +393,7 @@ void QgsStyleXmlDropHandler::handleCustomUriDrop( const QgsMimeDataUtils::Uri &u
bool QgsStyleXmlDropHandler::handleFileDrop( const QString &file )
{
if ( isStyleFile( file ) )
if ( QgsStyle::isXmlStyleFile( file ) )
{
QgsStyleExportImportDialog dlg( QgsStyle::defaultStyle(), QgisApp::instance(), QgsStyleExportImportDialog::Import );
dlg.setImportFilePath( file );

View File

@ -1700,6 +1700,26 @@ bool QgsStyle::importXml( const QString &filename )
return true;
}
bool QgsStyle::isXmlStyleFile( const QString &path )
{
QFileInfo fileInfo( path );
if ( fileInfo.suffix().compare( QLatin1String( "xml" ), Qt::CaseInsensitive ) != 0 )
return false;
// sniff the first line of the file to see if it's a style file
if ( !QFile::exists( path ) )
return false;
QFile inputFile( path );
if ( !inputFile.open( QIODevice::ReadOnly ) )
return false;
QTextStream stream( &inputFile );
const QString line = stream.readLine();
return line == QLatin1String( "<!DOCTYPE qgis_style>" );
}
bool QgsStyle::updateSymbol( StyleEntity type, const QString &name )
{
QDomDocument doc( QStringLiteral( "dummy" ) );

View File

@ -441,6 +441,16 @@ class CORE_EXPORT QgsStyle : public QObject
//! Imports the symbols and colorramps into the default style database from the given XML file
bool importXml( const QString &filename );
/**
* Tests if the file at \a path is a QGIS style XML file.
*
* This method samples only the first line in the file, so is safe to call on
* large xml files.
*
* \since QGIS 3.6
*/
static bool isXmlStyleFile( const QString &path );
signals:
/**

View File

@ -20,6 +20,7 @@
#include "qgsogrutils.h"
#include "qgsproject.h"
#include "qgsgdalutils.h"
#include "symbology/qgsstyle.h"
#include <QFileInfo>
#include <QAction>
@ -257,8 +258,8 @@ QgsDataItem *QgsGdalDataItemProvider::createDataItem( const QString &pathIn, Qgs
std::call_once( initialized, [ = ]
{
buildSupportedRasterFileFilterAndExtensions( sFilterString, sExtensions, sWildcards );
QgsDebugMsgLevel( "extensions: " + sExtensions.join( " " ), 2 );
QgsDebugMsgLevel( "wildcards: " + sWildcards.join( " " ), 2 );
QgsDebugMsgLevel( QStringLiteral( "extensions: " ) + sExtensions.join( ' ' ), 2 );
QgsDebugMsgLevel( QStringLiteral( "wildcards: " ) + sWildcards.join( ' ' ), 2 );
} );
// skip *.aux.xml files (GDAL auxiliary metadata files),
@ -274,6 +275,11 @@ QgsDataItem *QgsGdalDataItemProvider::createDataItem( const QString &pathIn, Qgs
!sExtensions.contains( QStringLiteral( "tif.xml" ) ) )
return nullptr;
// skip QGIS style xml files
if ( path.endsWith( QLatin1String( ".xml" ), Qt::CaseInsensitive ) &&
QgsStyle::isXmlStyleFile( path ) )
return nullptr;
// Filter files by extension
if ( !sExtensions.contains( suffix ) )
{

View File

@ -25,6 +25,7 @@
#include "qgsgeopackagedataitems.h"
#include "qgsogrutils.h"
#include "qgsproviderregistry.h"
#include "symbology/qgsstyle.h"
#include <QFileInfo>
#include <QTextStream>
@ -667,6 +668,11 @@ QgsDataItem *QgsOgrDataItemProvider::createDataItem( const QString &pathIn, QgsD
!myExtensions.contains( QStringLiteral( "tif.xml" ) ) )
return nullptr;
// skip QGIS style xml files
if ( path.endsWith( QLatin1String( ".xml" ), Qt::CaseInsensitive ) &&
QgsStyle::isXmlStyleFile( path ) )
return nullptr;
// We have to filter by extensions, otherwise e.g. all Shapefile files are displayed
// because OGR drive can open also .dbf, .shx.
if ( myExtensions.indexOf( suffix ) < 0 && !dirExtensions.contains( suffix ) )

View File

@ -68,6 +68,7 @@ class TestStyle : public QObject
void testFavorites();
void testTags();
void testSmartGroup();
void testIsStyleXml();
};
@ -635,5 +636,13 @@ void TestStyle::testSmartGroup()
QCOMPARE( groupModifiedSpy.count(), 8 );
}
void TestStyle::testIsStyleXml()
{
QVERIFY( !QgsStyle::isXmlStyleFile( QString() ) );
QVERIFY( !QgsStyle::isXmlStyleFile( QStringLiteral( "blah" ) ) );
QVERIFY( QgsStyle::isXmlStyleFile( mTestDataDir + QStringLiteral( "categorized.xml" ) ) );
QVERIFY( !QgsStyle::isXmlStyleFile( mTestDataDir + QStringLiteral( "openstreetmap/testdata.xml" ) ) );
}
QGSTEST_MAIN( TestStyle )
#include "testqgsstyle.moc"