Skip to content

Commit

Permalink
Add QgsProjectMetadata member to QgsProject
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 22, 2018
1 parent 09aca08 commit bba3f6c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
33 changes: 33 additions & 0 deletions python/core/qgsproject.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,28 @@ provider.
Returns the current auxiliary storage.

.. versionadded:: 3.0
%End

const QgsProjectMetadata &metadata() const;
%Docstring
Returns a reference to the project's metadata store.

.. versionadded:: 3.2

.. seealso:: :py:func:`setMetadata`

.. seealso:: :py:func:`metadataChanged`
%End

void setMetadata( const QgsProjectMetadata &metadata );
%Docstring
Sets the project's ``metadata`` store.

.. versionadded:: 3.2

.. seealso:: :py:func:`metadata`

.. seealso:: :py:func:`metadataChanged`
%End

signals:
Expand Down Expand Up @@ -1060,6 +1082,17 @@ Emitted when global configuration of the labeling engine changes.
.. versionadded:: 3.0
%End

void metadataChanged();
%Docstring
Emitted when the project's metadata is changed.

.. seealso:: :py:func:`setMetadata`

.. seealso:: :py:func:`metadata`

.. versionadded:: 3.2
%End


void layersWillBeRemoved( const QStringList &layerIds );
%Docstring
Expand Down
29 changes: 28 additions & 1 deletion src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ void QgsProject::clear()
mDirty = false;
mTrustLayerMetadata = false;
mCustomVariables.clear();
mMetadata = QgsProjectMetadata();
emit metadataChanged();

QgsCoordinateTransformContext context;
context.readSettings();
Expand Down Expand Up @@ -973,7 +975,15 @@ bool QgsProject::readProjectFile( const QString &filename )
}
emit transformContextChanged();

QDomNodeList nl = doc->elementsByTagName( QStringLiteral( "autotransaction" ) );
QDomNodeList nl = doc->elementsByTagName( QStringLiteral( "projectMetadata" ) );
if ( !nl.isEmpty() )
{
QDomElement metadataElement = nl.at( 0 ).toElement();
mMetadata.readMetadataXml( metadataElement );
}
emit metadataChanged();

nl = doc->elementsByTagName( QStringLiteral( "autotransaction" ) );
if ( nl.count() )
{
QDomElement transactionElement = nl.at( 0 ).toElement();
Expand Down Expand Up @@ -1515,6 +1525,10 @@ bool QgsProject::writeProjectFile( const QString &filename )

mTransformContext.writeXml( qgisNode, context );

QDomElement metadataElem = doc->createElement( QStringLiteral( "projectMetadata" ) );
mMetadata.writeMetadataXml( metadataElem, *doc );
qgisNode.appendChild( metadataElem );

QDomElement annotationsElem = mAnnotationManager->writeXml( *doc, context );
qgisNode.appendChild( annotationsElem );

Expand Down Expand Up @@ -2507,3 +2521,16 @@ QgsAuxiliaryStorage *QgsProject::auxiliaryStorage()
{
return mAuxiliaryStorage.get();
}

const QgsProjectMetadata &QgsProject::metadata() const
{
return mMetadata;
}

void QgsProject::setMetadata( const QgsProjectMetadata &metadata )
{
mMetadata = metadata;
emit metadataChanged();

setDirty( true );
}
28 changes: 28 additions & 0 deletions src/core/qgsproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "qgsmaplayerstore.h"
#include "qgsarchive.h"
#include "qgsreadwritecontext.h"
#include "qgsprojectmetadata.h"

class QFileInfo;
class QDomDocument;
Expand Down Expand Up @@ -92,6 +93,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
Q_PROPERTY( QgsProjectMetadata metadata READ metadata WRITE setMetadata NOTIFY metadataChanged )

public:
//! Returns the QgsProject singleton instance
Expand Down Expand Up @@ -871,6 +873,22 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QgsAuxiliaryStorage *auxiliaryStorage();

/**
* Returns a reference to the project's metadata store.
* \since QGIS 3.2
* \see setMetadata()
* \see metadataChanged()
*/
const QgsProjectMetadata &metadata() const;

/**
* Sets the project's \a metadata store.
* \since QGIS 3.2
* \see metadata()
* \see metadataChanged()
*/
void setMetadata( const QgsProjectMetadata &metadata );

signals:
//! emitted when project is being read
void readProject( const QDomDocument & );
Expand Down Expand Up @@ -1017,6 +1035,14 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void labelingEngineSettingsChanged();

/**
* Emitted when the project's metadata is changed.
* \see setMetadata()
* \see metadata()
* \since QGIS 3.2
*/
void metadataChanged();

//
// signals from QgsMapLayerRegistry
//
Expand Down Expand Up @@ -1269,6 +1295,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QgsCoordinateTransformContext mTransformContext;

QgsProjectMetadata mMetadata;

friend class QgsProjectDirtyBlocker;
};

Expand Down
15 changes: 15 additions & 0 deletions tests/src/python/test_qgsprojectmetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

from qgis.core import (QgsProjectMetadata,
QgsMetadataBase,
QgsProject,
QgsNativeProjectMetadataValidator)
from qgis.PyQt.QtCore import (QDate,
QTime,
QDateTime)
from qgis.testing import start_app, unittest
from qgis.PyQt.QtTest import QSignalSpy

start_app()

Expand Down Expand Up @@ -327,6 +329,19 @@ def testValidateNative(self): # spellok
self.assertFalse(res)
self.assertEqual(list[0].section, 'creation')

def testProject(self):
p = QgsProject()
m = self.createTestMetadata()

metadata_changed_spy = QSignalSpy(p.metadataChanged)
p.setMetadata(m)
self.assertEqual(len(metadata_changed_spy), 1)
self.checkExpectedMetadata(p.metadata())

p.clear()
self.assertEqual(len(metadata_changed_spy), 2)
self.assertEqual(p.metadata().author(), '')


if __name__ == '__main__':
unittest.main()

0 comments on commit bba3f6c

Please sign in to comment.