Skip to content

Commit

Permalink
[browser] Don't show gdal/ogr layer items for QGIS style xml files
Browse files Browse the repository at this point in the history
These aren't layers, so the extra entries are just distracting noise.
  • Loading branch information
nyalldawson committed Jan 16, 2019
1 parent 956a74f commit 2aab283
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 25 deletions.
10 changes: 10 additions & 0 deletions python/core/auto_generated/symbology/qgsstyle.sip.in
Expand Up @@ -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:
Expand Down
25 changes: 2 additions & 23 deletions src/app/qgsappbrowserproviders.cpp
Expand Up @@ -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" );
Expand All @@ -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 );
}
Expand All @@ -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 );
Expand Down
20 changes: 20 additions & 0 deletions src/core/symbology/qgsstyle.cpp
Expand Up @@ -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" ) );
Expand Down
10 changes: 10 additions & 0 deletions src/core/symbology/qgsstyle.h
Expand Up @@ -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:

/**
Expand Down
10 changes: 8 additions & 2 deletions src/providers/gdal/qgsgdaldataitems.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgsogrutils.h"
#include "qgsproject.h"
#include "qgsgdalutils.h"
#include "symbology/qgsstyle.h"

#include <QFileInfo>
#include <QAction>
Expand Down Expand Up @@ -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),
Expand All @@ -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 ) )
{
Expand Down
6 changes: 6 additions & 0 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsgeopackagedataitems.h"
#include "qgsogrutils.h"
#include "qgsproviderregistry.h"
#include "symbology/qgsstyle.h"

#include <QFileInfo>
#include <QTextStream>
Expand Down Expand Up @@ -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 ) )
Expand Down
9 changes: 9 additions & 0 deletions tests/src/core/testqgsstyle.cpp
Expand Up @@ -68,6 +68,7 @@ class TestStyle : public QObject
void testFavorites();
void testTags();
void testSmartGroup();
void testIsStyleXml();

};

Expand Down Expand Up @@ -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"

0 comments on commit 2aab283

Please sign in to comment.