Navigation Menu

Skip to content

Commit

Permalink
Extend project storage interface: remove/rename projects, GUI support
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Apr 7, 2018
1 parent a30646f commit 5963028
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
30 changes: 30 additions & 0 deletions python/core/qgsprojectstorage.sip.in
Expand Up @@ -10,6 +10,7 @@




class QgsProjectStorage
{
%Docstring
Expand Down Expand Up @@ -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
};

/************************************************************************
Expand Down
34 changes: 31 additions & 3 deletions src/core/qgsprojectstorage.h
Expand Up @@ -18,8 +18,9 @@

#include "qgis_core.h"

#include <QString>

class QIODevice;
class QString;
class QStringList;

class QgsReadWriteContext;
Expand Down Expand Up @@ -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
26 changes: 25 additions & 1 deletion tests/src/core/testqgsprojectstorage.cpp
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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 );
}

Expand Down

0 comments on commit 5963028

Please sign in to comment.