Skip to content
Permalink
Browse files

[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.

(cherry picked from commit 2aab283)
  • Loading branch information
nyalldawson committed Jan 17, 2019
1 parent 42254b4 commit 6f2daac6566d8c70388cb1e61b3e7ffe4eac4c23
@@ -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:
@@ -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" ) );
@@ -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:

/**
@@ -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 ) )
{
@@ -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 ) )
@@ -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"

0 comments on commit 6f2daac

Please sign in to comment.
You can’t perform that action at this time.