Skip to content
Permalink
Browse files

Extend project storage interface: remove/rename projects, GUI support

  • Loading branch information
wonder-sk committed Apr 7, 2018
1 parent a30646f commit 5963028cafc5aef640122ed4abea96dbd2af2363
Showing with 86 additions and 4 deletions.
  1. +30 −0 python/core/qgsprojectstorage.sip.in
  2. +31 −3 src/core/qgsprojectstorage.h
  3. +25 −1 tests/src/core/testqgsprojectstorage.cpp
@@ -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
};

/************************************************************************
@@ -18,8 +18,9 @@

#include "qgis_core.h"

#include <QString>

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
@@ -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<QString, QByteArray> 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 );
}

0 comments on commit 5963028

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