Skip to content
Permalink
Browse files

Add QgsProjectMetadata member to QgsProject

  • Loading branch information
nyalldawson committed Mar 19, 2018
1 parent 09aca08 commit bba3f6c6fefcba19f50957bbb0cc6aab813e74c0
Showing with 104 additions and 1 deletion.
  1. +33 −0 python/core/qgsproject.sip.in
  2. +28 −1 src/core/qgsproject.cpp
  3. +28 −0 src/core/qgsproject.h
  4. +15 −0 tests/src/python/test_qgsprojectmetadata.py
@@ -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:
@@ -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
@@ -511,6 +511,8 @@ void QgsProject::clear()
mDirty = false;
mTrustLayerMetadata = false;
mCustomVariables.clear();
mMetadata = QgsProjectMetadata();
emit metadataChanged();

QgsCoordinateTransformContext context;
context.readSettings();
@@ -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();
@@ -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 );

@@ -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 );
}
@@ -43,6 +43,7 @@
#include "qgsmaplayerstore.h"
#include "qgsarchive.h"
#include "qgsreadwritecontext.h"
#include "qgsprojectmetadata.h"

class QFileInfo;
class QDomDocument;
@@ -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
@@ -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 & );
@@ -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
//
@@ -1269,6 +1295,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QgsCoordinateTransformContext mTransformContext;

QgsProjectMetadata mMetadata;

friend class QgsProjectDirtyBlocker;
};

@@ -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()

@@ -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.
You can’t perform that action at this time.