Skip to content

Commit f0436df

Browse files
committed
[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
1 parent 850eae1 commit f0436df

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

src/core/qgsdataitem.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -878,14 +878,6 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
878878
QString path = dir.absoluteFilePath( name );
879879
QFileInfo fileInfo( path );
880880

881-
if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 ||
882-
fileInfo.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) == 0 )
883-
{
884-
QgsDataItem *item = new QgsProjectItem( this, fileInfo.completeBaseName(), path );
885-
children.append( item );
886-
continue;
887-
}
888-
889881
if ( fileInfo.suffix().compare( QLatin1String( "zip" ), Qt::CaseInsensitive ) == 0 ||
890882
fileInfo.suffix().compare( QLatin1String( "tar" ), Qt::CaseInsensitive ) == 0 )
891883
{
@@ -897,6 +889,7 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
897889
}
898890
}
899891

892+
bool createdItem = false;
900893
for ( QgsDataItemProvider *provider : providers )
901894
{
902895
int capabilities = provider->capabilities();
@@ -911,6 +904,20 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
911904
if ( item )
912905
{
913906
children.append( item );
907+
createdItem = true;
908+
}
909+
}
910+
911+
if ( !createdItem )
912+
{
913+
// if item is a QGIS project, and no specific item provider has overridden handling of
914+
// project items, then use the default project item behavior
915+
if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 ||
916+
fileInfo.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) == 0 )
917+
{
918+
QgsDataItem *item = new QgsProjectItem( this, fileInfo.completeBaseName(), path );
919+
children.append( item );
920+
continue;
914921
}
915922
}
916923

tests/src/core/testqgsdataitem.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "qgsmeshlayer.h"
2626
#include "qgsapplication.h"
2727
#include "qgslogger.h"
28+
#include "qgsdataitemprovider.h"
29+
#include "qgsdataitemproviderregistry.h"
2830
#include "qgssettings.h"
2931

3032
/**
@@ -47,6 +49,7 @@ class TestQgsDataItem : public QObject
4749
void testValid();
4850
void testDirItemChildren();
4951
void testLayerItemType();
52+
void testProjectItemCreation();
5053

5154
private:
5255
QgsDirectoryItem *mDirItem = nullptr;
@@ -218,8 +221,89 @@ void TestQgsDataItem::testLayerItemType()
218221
QString(), QStringLiteral( "mdal" ) );
219222
QVERIFY( layer->isValid() );
220223
QCOMPARE( QgsLayerItem::typeFromMapLayer( layer.get() ), QgsLayerItem::Mesh );
224+
}
225+
226+
227+
class TestProjectDataItemProvider : public QgsDataItemProvider
228+
{
229+
public:
230+
QString name() override { return QStringLiteral( "project_test" ); }
231+
int capabilities() override { return QgsDataProvider::File; }
232+
QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override
233+
{
234+
QFileInfo fileInfo( path );
235+
if ( fileInfo.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) == 0 || fileInfo.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) == 0 )
236+
{
237+
return new QgsDataItem( QgsDataItem::Custom, parentItem, path, path );
238+
}
239+
return nullptr;
240+
}
241+
};
221242

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

248+
// ensure that QgsProjectItem items were created
249+
bool foundQgsProject = false;
250+
bool foundQgzProject = false;
251+
for ( QgsDataItem *child : children )
252+
{
253+
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgs" ) )
254+
{
255+
foundQgsProject = true;
256+
continue;
257+
}
258+
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgz" ) )
259+
{
260+
foundQgzProject = true;
261+
continue;
262+
}
263+
}
264+
QVERIFY( foundQgsProject );
265+
QVERIFY( foundQgzProject );
266+
delete dirItem;
267+
268+
// now, add a specific provider which handles project files
269+
QgsApplication::dataItemProviderRegistry()->addProvider( new TestProjectDataItemProvider() );
270+
271+
dirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), mTestDataDir + QStringLiteral( "qgis_server/" ) );
272+
children = dirItem->createChildren();
273+
274+
// ensure that QgsProjectItem items were NOT created -- our test provider should have created custom items instead
275+
foundQgsProject = false;
276+
foundQgzProject = false;
277+
bool foundCustomQgsProject = false;
278+
bool foundCustomQgzProject = false;
279+
for ( QgsDataItem *child : children )
280+
{
281+
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgs" ) )
282+
{
283+
foundQgsProject = true;
284+
continue;
285+
}
286+
if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgz" ) )
287+
{
288+
foundQgzProject = true;
289+
continue;
290+
}
291+
if ( child->type() == QgsDataItem::Custom && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgs" ) )
292+
{
293+
foundCustomQgsProject = true;
294+
continue;
295+
}
296+
if ( child->type() == QgsDataItem::Custom && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgz" ) )
297+
{
298+
foundCustomQgzProject = true;
299+
continue;
300+
}
301+
}
302+
QVERIFY( !foundQgsProject );
303+
QVERIFY( !foundQgzProject );
304+
QVERIFY( foundCustomQgsProject );
305+
QVERIFY( foundCustomQgzProject );
306+
delete dirItem;
223307
}
224308

225309
QGSTEST_MAIN( TestQgsDataItem )

0 commit comments

Comments
 (0)