Skip to content
Permalink
Browse files

[browser] Allow data item providers a chance to implement QGIS project

item creation and override default project file handling

If no provider is registered which handles QGIS project files,
then the default behavior is used as a fallback
  • Loading branch information
nyalldawson committed Oct 30, 2018
1 parent 850eae1 commit f0436df618719f1f0762df747d784019ba4c667d
Showing with 99 additions and 8 deletions.
  1. +15 −8 src/core/qgsdataitem.cpp
  2. +84 −0 tests/src/core/testqgsdataitem.cpp
@@ -878,14 +878,6 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
QString path = dir.absoluteFilePath( name );
QFileInfo fileInfo( path );

if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 ||
fileInfo.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) == 0 )
{
QgsDataItem *item = new QgsProjectItem( this, fileInfo.completeBaseName(), path );
children.append( item );
continue;
}

if ( fileInfo.suffix().compare( QLatin1String( "zip" ), Qt::CaseInsensitive ) == 0 ||
fileInfo.suffix().compare( QLatin1String( "tar" ), Qt::CaseInsensitive ) == 0 )
{
@@ -897,6 +889,7 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
}
}

bool createdItem = false;
for ( QgsDataItemProvider *provider : providers )
{
int capabilities = provider->capabilities();
@@ -911,6 +904,20 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
if ( item )
{
children.append( item );
createdItem = true;
}
}

if ( !createdItem )
{
// if item is a QGIS project, and no specific item provider has overridden handling of
// project items, then use the default project item behavior
if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 ||
fileInfo.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) == 0 )
{
QgsDataItem *item = new QgsProjectItem( this, fileInfo.completeBaseName(), path );
children.append( item );
continue;
}
}

@@ -25,6 +25,8 @@
#include "qgsmeshlayer.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsdataitemprovider.h"
#include "qgsdataitemproviderregistry.h"
#include "qgssettings.h"

/**
@@ -47,6 +49,7 @@ class TestQgsDataItem : public QObject
void testValid();
void testDirItemChildren();
void testLayerItemType();
void testProjectItemCreation();

private:
QgsDirectoryItem *mDirItem = nullptr;
@@ -218,8 +221,89 @@ void TestQgsDataItem::testLayerItemType()
QString(), QStringLiteral( "mdal" ) );
QVERIFY( layer->isValid() );
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Mesh );
}


class TestProjectDataItemProvider : public QgsDataItemProvider
{
public:
QString name() override { return QStringLiteral( "project_test" ); }
int capabilities() override { return QgsDataProvider::File; }
QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override
{
QFileInfo fileInfo( path );
if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 || fileInfo.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) == 0 )
{
return new QgsDataItem( QgsDataItem::Custom, parentItem, path, path );
}
return nullptr;
}
};

void TestQgsDataItem::testProjectItemCreation()
{
QgsDirectoryItem *dirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), mTestDataDir + QStringLiteral( "qgis_server/" ) );
QVector<QgsDataItem *> children = dirItem->createChildren();

// ensure that QgsProjectItem items were created
bool foundQgsProject = false;
bool foundQgzProject = false;
for ( QgsDataItem *child : children )
{
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgs" ) )
{
foundQgsProject = true;
continue;
}
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgz" ) )
{
foundQgzProject = true;
continue;
}
}
QVERIFY( foundQgsProject );
QVERIFY( foundQgzProject );
delete dirItem;

// now, add a specific provider which handles project files
QgsApplication::dataItemProviderRegistry()->addProvider( new TestProjectDataItemProvider() );

dirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), mTestDataDir + QStringLiteral( "qgis_server/" ) );
children = dirItem->createChildren();

// ensure that QgsProjectItem items were NOT created -- our test provider should have created custom items instead
foundQgsProject = false;
foundQgzProject = false;
bool foundCustomQgsProject = false;
bool foundCustomQgzProject = false;
for ( QgsDataItem *child : children )
{
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgs" ) )
{
foundQgsProject = true;
continue;
}
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgz" ) )
{
foundQgzProject = true;
continue;
}
if ( child->type() == QgsDataItem::Custom && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgs" ) )
{
foundCustomQgsProject = true;
continue;
}
if ( child->type() == QgsDataItem::Custom && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgz" ) )
{
foundCustomQgzProject = true;
continue;
}
}
QVERIFY( !foundQgsProject );
QVERIFY( !foundQgzProject );
QVERIFY( foundCustomQgsProject );
QVERIFY( foundCustomQgzProject );
delete dirItem;
}

QGSTEST_MAIN( TestQgsDataItem )

0 comments on commit f0436df

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