From 5963028cafc5aef640122ed4abea96dbd2af2363 Mon Sep 17 00:00:00 2001 From: Martin Dobias Date: Wed, 21 Mar 2018 14:36:51 +0100 Subject: [PATCH] Extend project storage interface: remove/rename projects, GUI support --- python/core/qgsprojectstorage.sip.in | 30 +++++++++++++++++++++ src/core/qgsprojectstorage.h | 34 +++++++++++++++++++++--- tests/src/core/testqgsprojectstorage.cpp | 26 +++++++++++++++++- 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/python/core/qgsprojectstorage.sip.in b/python/core/qgsprojectstorage.sip.in index a6bd417d56b2..3cf2c3dd4daf 100644 --- a/python/core/qgsprojectstorage.sip.in +++ b/python/core/qgsprojectstorage.sip.in @@ -10,6 +10,7 @@ + class QgsProjectStorage { %Docstring @@ -52,7 +53,36 @@ and having position at the start of the content when passed to writeProject() so can read all data from it until it reaches its end. %End + virtual bool removeProject( const QString &uri ) = 0; +%Docstring +Removes and existing project at the given URI. Returns true if the removal +was successful. +%End + + virtual bool renameProject( const QString &uri, const QString &uriNew ); +%Docstring +Rename an existing project at the given URI to a different URI. Returns true if renaming +was successful. +%End + + virtual QString visibleName(); +%Docstring +Returns human-readable name of the storage. Used as the menu item text in QGIS. Empty name +indicates that the storage does not implement GUI support (showLoadGui() and showSaveGui()). +The name may be translatable and ideally unique as well. +%End + virtual QString showLoadGui(); +%Docstring +Opens GUI to allow user to select a project to be loaded (GUI specific to this storage type). +Returns project URI if user has picked a project or empty string if the GUI was canceled. +%End + + virtual QString showSaveGui(); +%Docstring +Opens GUI to allow user to select where a project should be saved (GUI specific to this storage type). +Returns project URI if user has picked a destination or empty string if the GUI was canceled. +%End }; /************************************************************************ diff --git a/src/core/qgsprojectstorage.h b/src/core/qgsprojectstorage.h index a0b4783d6351..7ea80fca7b15 100644 --- a/src/core/qgsprojectstorage.h +++ b/src/core/qgsprojectstorage.h @@ -18,8 +18,9 @@ #include "qgis_core.h" +#include + class QIODevice; -class QString; class QStringList; class QgsReadWriteContext; @@ -60,9 +61,36 @@ class CORE_EXPORT QgsProjectStorage */ virtual bool writeProject( const QString &uri, QIODevice *device, QgsReadWriteContext &context ) = 0; - // TODO: rename / remove ? + /** + * Removes and existing project at the given URI. Returns true if the removal + * was successful. + */ + virtual bool removeProject( const QString &uri ) = 0; + + /** + * Rename an existing project at the given URI to a different URI. Returns true if renaming + * was successful. + */ + virtual bool renameProject( const QString &uri, const QString &uriNew ) { Q_UNUSED( uri ); Q_UNUSED( uriNew ); return false; } + + /** + * Returns human-readable name of the storage. Used as the menu item text in QGIS. Empty name + * indicates that the storage does not implement GUI support (showLoadGui() and showSaveGui()). + * The name may be translatable and ideally unique as well. + */ + virtual QString visibleName() { return QString(); } - // TODO: load/save GUI + /** + * Opens GUI to allow user to select a project to be loaded (GUI specific to this storage type). + * Returns project URI if user has picked a project or empty string if the GUI was canceled. + */ + virtual QString showLoadGui() { return QString(); } + + /** + * Opens GUI to allow user to select where a project should be saved (GUI specific to this storage type). + * Returns project URI if user has picked a destination or empty string if the GUI was canceled. + */ + virtual QString showSaveGui() { return QString(); } }; #endif // QGSPROJECTSTORAGE_H diff --git a/tests/src/core/testqgsprojectstorage.cpp b/tests/src/core/testqgsprojectstorage.cpp index 8a2e40271008..e88d281edf62 100644 --- a/tests/src/core/testqgsprojectstorage.cpp +++ b/tests/src/core/testqgsprojectstorage.cpp @@ -109,6 +109,19 @@ class MemoryStorage : public QgsProjectStorage return true; } + virtual bool removeProject( const QString &uri ) override + { + QStringList lst = uri.split( ":" ); + Q_ASSERT( lst.count() == 2 ); + QString projectName = lst[1]; + + if ( !mProjects.contains( projectName ) ) + return false; + + mProjects.remove( projectName ); + return true; + } + private: QHash mProjects; }; @@ -146,12 +159,23 @@ void TestQgsProjectStorage::testMemoryStorage() QCOMPARE( prj2.mapLayers().count(), 1 ); QCOMPARE( prj2.title(), QString( "best project ever" ) ); + // test access of non-existant project + QgsProject prj3; prj3.setFileName( "memory:nooooooooo!" ); bool readInvalidOk = prj3.read(); - QVERIFY( !readInvalidOk ); + // test removal + + bool removeInvalidOk = memStorage->removeProject( "memory:projectXYZ" ); + QVERIFY( !removeInvalidOk ); + QCOMPARE( memStorage->listProjects( QString() ).count(), 1 ); + + bool removeOk = memStorage->removeProject( "memory:project1" ); + QVERIFY( removeOk ); + QCOMPARE( memStorage->listProjects( QString() ).count(), 0 ); + QgsApplication::projectStorageRegistry()->unregisterProjectStorage( memStorage ); }