25
25
#include " qgsmeshlayer.h"
26
26
#include " qgsapplication.h"
27
27
#include " qgslogger.h"
28
+ #include " qgsdataitemprovider.h"
29
+ #include " qgsdataitemproviderregistry.h"
28
30
#include " qgssettings.h"
29
31
30
32
/* *
@@ -47,6 +49,7 @@ class TestQgsDataItem : public QObject
47
49
void testValid ();
48
50
void testDirItemChildren ();
49
51
void testLayerItemType ();
52
+ void testProjectItemCreation ();
50
53
51
54
private:
52
55
QgsDirectoryItem *mDirItem = nullptr ;
@@ -218,8 +221,89 @@ void TestQgsDataItem::testLayerItemType()
218
221
QString (), QStringLiteral ( " mdal" ) );
219
222
QVERIFY ( layer->isValid () );
220
223
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
+ };
221
242
243
+ void TestQgsDataItem::testProjectItemCreation ()
244
+ {
245
+ QgsDirectoryItem *dirItem = new QgsDirectoryItem ( nullptr , QStringLiteral ( " Test" ), mTestDataDir + QStringLiteral ( " qgis_server/" ) );
246
+ QVector<QgsDataItem *> children = dirItem->createChildren ();
222
247
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;
223
307
}
224
308
225
309
QGSTEST_MAIN ( TestQgsDataItem )
0 commit comments