Skip to content

Commit

Permalink
Add temporary methods to save layer metadata in layer custom properties
Browse files Browse the repository at this point in the history
Allows temporary persistance of metadata inside a single project. Will
be removed when later work packages from the metadata project are
merged.
  • Loading branch information
nyalldawson committed May 3, 2017
1 parent 40fccf5 commit cc3d67a
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 25 deletions.
50 changes: 38 additions & 12 deletions python/core/metadata/qgslayermetadata.sip
Expand Up @@ -9,6 +9,7 @@




class QgsLayerMetadata
{
%Docstring
Expand Down Expand Up @@ -37,6 +38,8 @@ class QgsLayerMetadata
%End
public:

typedef QMap< QString, QStringList > KeywordMap;

struct Constraint
{

Expand All @@ -57,6 +60,9 @@ class QgsLayerMetadata
%End
};

typedef QList< QgsLayerMetadata::Constraint > ConstraintList;


struct Address
{

Expand Down Expand Up @@ -150,6 +156,9 @@ class QgsLayerMetadata
%End
};

typedef QList< QgsLayerMetadata::Contact > ContactList;


struct Link
{

Expand Down Expand Up @@ -195,6 +204,8 @@ class QgsLayerMetadata
%End
};

typedef QList< QgsLayerMetadata::Link > LinkList;

QgsLayerMetadata();
%Docstring
Constructor for QgsLayerMetadata.
Expand Down Expand Up @@ -304,14 +315,14 @@ class QgsLayerMetadata
\see fees()
%End

QList< QgsLayerMetadata::Constraint > constraints() const;
QgsLayerMetadata::ConstraintList constraints() const;
%Docstring
Returns a list of constraints associated with using the resource.
\see setConstraints()
:rtype: list of QgsLayerMetadata.Constraint
:rtype: QgsLayerMetadata.ConstraintList
%End

void setConstraints( const QList<QgsLayerMetadata::Constraint> &constraints );
void setConstraints( const QgsLayerMetadata::ConstraintList &constraints );
%Docstring
Sets the list of constraints associated with using the resource.
\see constraints()
Expand Down Expand Up @@ -374,7 +385,7 @@ class QgsLayerMetadata
\see setCrs()
%End

QMap<QString, QStringList> keywords() const;
KeywordMap keywords() const;
%Docstring
Returns the keywords map, which is a set of descriptive keywords associated with the resource.

Expand All @@ -385,10 +396,10 @@ class QgsLayerMetadata

\see setKeywords()
\see keywordVocabularies()
:rtype: QMap<str, list of str>
:rtype: KeywordMap
%End

void setKeywords( const QMap<QString, QStringList> &keywords );
void setKeywords( const KeywordMap &keywords );
%Docstring
Sets the keywords map, which is a set of descriptive keywords associated with the resource.

Expand Down Expand Up @@ -438,14 +449,14 @@ class QgsLayerMetadata
:rtype: list of str
%End

QList<QgsLayerMetadata::Contact> contacts() const;
QgsLayerMetadata::ContactList contacts() const;
%Docstring
Returns a list of contact persons or entities associated with the resource.
\see setContacts()
:rtype: list of QgsLayerMetadata.Contact
:rtype: QgsLayerMetadata.ContactList
%End

void setContacts( const QList<QgsLayerMetadata::Contact> &contacts );
void setContacts( const QgsLayerMetadata::ContactList &contacts );
%Docstring
Sets the list of contacts or entities associated with the resource. Any existing contacts
will be replaced.
Expand All @@ -460,14 +471,14 @@ class QgsLayerMetadata
\see setContacts()
%End

QList<QgsLayerMetadata::Link> links() const;
QgsLayerMetadata::LinkList links() const;
%Docstring
Returns a list of online resources associated with the resource.
\see setLinks()
:rtype: list of QgsLayerMetadata.Link
:rtype: QgsLayerMetadata.LinkList
%End

void setLinks( const QList<QgsLayerMetadata::Link> &links );
void setLinks( const QgsLayerMetadata::LinkList &links );
%Docstring
Sets the list of online resources associated with the resource. Any existing links
will be replaced.
Expand All @@ -482,8 +493,23 @@ class QgsLayerMetadata
\see setLinks()
%End

void saveToLayer( QgsMapLayer *layer ) const;
%Docstring
Saves the metadata to a layer's custom properties (see QgsMapLayer.setCustomProperty() ).
\see readFromLayer()
%End

void readFromLayer( const QgsMapLayer *layer );
%Docstring
Reads the metadata state from a layer's custom properties (see QgsMapLayer.customProperty() ).
\see saveToLayer()
%End

};




/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
38 changes: 38 additions & 0 deletions src/core/metadata/qgslayermetadata.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgslayermetadata.h"
#include "qgsmaplayer.h"

QString QgsLayerMetadata::identifier() const
{
Expand Down Expand Up @@ -181,3 +182,40 @@ void QgsLayerMetadata::setLanguage( const QString &language )
{
mLanguage = language;
}

void QgsLayerMetadata::saveToLayer( QgsMapLayer *layer ) const
{
layer->setCustomProperty( QStringLiteral( "metadata/identifier" ), mIdentifier );
layer->setCustomProperty( QStringLiteral( "metadata/parentIdentifier" ), mParentIdentifier );
layer->setCustomProperty( QStringLiteral( "metadata/language" ), mLanguage );
layer->setCustomProperty( QStringLiteral( "metadata/type" ), mType );
layer->setCustomProperty( QStringLiteral( "metadata/title" ), mTitle );
layer->setCustomProperty( QStringLiteral( "metadata/abstract" ), mAbstract );
layer->setCustomProperty( QStringLiteral( "metadata/fees" ), mFees );
layer->setCustomProperty( QStringLiteral( "metadata/rights" ), mRights );
layer->setCustomProperty( QStringLiteral( "metadata/encoding" ), mEncoding );
layer->setCustomProperty( QStringLiteral( "metadata/crs" ), mCrs.authid() );
layer->setCustomProperty( QStringLiteral( "metadata/constraints" ), QVariant::fromValue( mConstraints ) );
layer->setCustomProperty( QStringLiteral( "metadata/keywords" ), QVariant::fromValue( mKeywords ) );
layer->setCustomProperty( QStringLiteral( "metadata/contacts" ), QVariant::fromValue( mContacts ) );
layer->setCustomProperty( QStringLiteral( "metadata/links" ), QVariant::fromValue( mLinks ) );
}

void QgsLayerMetadata::readFromLayer( const QgsMapLayer *layer )
{
mIdentifier = layer->customProperty( QStringLiteral( "metadata/identifier" ) ).toString();
mParentIdentifier = layer->customProperty( QStringLiteral( "metadata/parentIdentifier" ) ).toString();
mLanguage = layer->customProperty( QStringLiteral( "metadata/language" ) ).toString();
mType = layer->customProperty( QStringLiteral( "metadata/type" ) ).toString();
mTitle = layer->customProperty( QStringLiteral( "metadata/title" ) ).toString();
mAbstract = layer->customProperty( QStringLiteral( "metadata/abstract" ) ).toString();
mFees = layer->customProperty( QStringLiteral( "metadata/fees" ) ).toString();
mRights = layer->customProperty( QStringLiteral( "metadata/rights" ) ).toStringList();
mEncoding = layer->customProperty( QStringLiteral( "metadata/encoding" ) ).toString();
QString crsAuthId = layer->customProperty( QStringLiteral( "metadata/crs" ) ).toString();
mCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( crsAuthId );
mConstraints = layer->customProperty( QStringLiteral( "metadata/constraints" ) ).value<ConstraintList>();
mKeywords = layer->customProperty( QStringLiteral( "metadata/keywords" ) ).value<KeywordMap>();
mContacts = layer->customProperty( QStringLiteral( "metadata/contacts" ) ).value<ContactList>();
mLinks = layer->customProperty( QStringLiteral( "metadata/links" ) ).value<LinkList>();
}
67 changes: 55 additions & 12 deletions src/core/metadata/qgslayermetadata.h
Expand Up @@ -22,6 +22,8 @@
#include "qgis_core.h"
#include "qgscoordinatereferencesystem.h"

class QgsMapLayer;

/**
* \ingroup core
* \class QgsLayerMetadata
Expand All @@ -48,6 +50,11 @@ class CORE_EXPORT QgsLayerMetadata
{
public:

/**
* Map of vocabulary string to keyword list.
*/
typedef QMap< QString, QStringList > KeywordMap;

/**
* Metadata constraint structure.
*/
Expand All @@ -74,6 +81,12 @@ class CORE_EXPORT QgsLayerMetadata
QString constraint;
};

/**
* A list of constraints.
*/
typedef QList< QgsLayerMetadata::Constraint > ConstraintList;


/**
* Metadata address structure.
*/
Expand Down Expand Up @@ -180,6 +193,12 @@ class CORE_EXPORT QgsLayerMetadata
QString role;
};

/**
* A list of contacts.
*/
typedef QList< QgsLayerMetadata::Contact > ContactList;


/**
* Metadata link structure.
*/
Expand Down Expand Up @@ -232,6 +251,11 @@ class CORE_EXPORT QgsLayerMetadata
QString size;
};

/**
* A list of links.
*/
typedef QList< QgsLayerMetadata::Link > LinkList;

/**
* Constructor for QgsLayerMetadata.
*/
Expand Down Expand Up @@ -338,13 +362,13 @@ class CORE_EXPORT QgsLayerMetadata
* Returns a list of constraints associated with using the resource.
* \see setConstraints()
*/
QList< QgsLayerMetadata::Constraint > constraints() const;
QgsLayerMetadata::ConstraintList constraints() const;

/**
* Sets the list of \a constraints associated with using the resource.
* \see constraints()
*/
void setConstraints( const QList<QgsLayerMetadata::Constraint> &constraints );
void setConstraints( const QgsLayerMetadata::ConstraintList &constraints );

/**
* Returns a list of attribution or copyright strings associated with the resource.
Expand Down Expand Up @@ -411,7 +435,7 @@ class CORE_EXPORT QgsLayerMetadata
* \see setKeywords()
* \see keywordVocabularies()
*/
QMap<QString, QStringList> keywords() const;
KeywordMap keywords() const;

/**
* Sets the \a keywords map, which is a set of descriptive keywords associated with the resource.
Expand All @@ -425,7 +449,7 @@ class CORE_EXPORT QgsLayerMetadata
* \see keywords()
* \see addKeywords()
*/
void setKeywords( const QMap<QString, QStringList> &keywords );
void setKeywords( const KeywordMap &keywords );

/**
* Adds a list of descriptive \a keywords for a specified \a vocabulary. Any existing
Expand Down Expand Up @@ -465,15 +489,15 @@ class CORE_EXPORT QgsLayerMetadata
* Returns a list of contact persons or entities associated with the resource.
* \see setContacts()
*/
QList<QgsLayerMetadata::Contact> contacts() const;
QgsLayerMetadata::ContactList contacts() const;

/**
* Sets the list of \a contacts or entities associated with the resource. Any existing contacts
* will be replaced.
* \see contacts()
* \see addContact()
*/
void setContacts( const QList<QgsLayerMetadata::Contact> &contacts );
void setContacts( const QgsLayerMetadata::ContactList &contacts );

/**
* Adds an individual \a contact to the existing contacts.
Expand All @@ -486,15 +510,15 @@ class CORE_EXPORT QgsLayerMetadata
* Returns a list of online resources associated with the resource.
* \see setLinks()
*/
QList<QgsLayerMetadata::Link> links() const;
QgsLayerMetadata::LinkList links() const;

/**
* Sets the list of online resources associated with the resource. Any existing links
* will be replaced.
* \see links()
* \see addLink()
*/
void setLinks( const QList<QgsLayerMetadata::Link> &links );
void setLinks( const QgsLayerMetadata::LinkList &links );

/**
* Adds an individual \a link to the existing links.
Expand All @@ -503,6 +527,18 @@ class CORE_EXPORT QgsLayerMetadata
*/
void addLink( const QgsLayerMetadata::Link &link );

/**
* Saves the metadata to a \a layer's custom properties (see QgsMapLayer::setCustomProperty() ).
* \see readFromLayer()
*/
void saveToLayer( QgsMapLayer *layer ) const;

/**
* Reads the metadata state from a \a layer's custom properties (see QgsMapLayer::customProperty() ).
* \see saveToLayer()
*/
void readFromLayer( const QgsMapLayer *layer );

private:

/*
Expand All @@ -520,7 +556,7 @@ class CORE_EXPORT QgsLayerMetadata
QString mTitle;
QString mAbstract;
QString mFees;
QList<QgsLayerMetadata::Constraint> mConstraints;
ConstraintList mConstraints;
QStringList mRights;

// IMPORTANT - look up before adding anything here!!
Expand All @@ -531,11 +567,11 @@ class CORE_EXPORT QgsLayerMetadata
/**
* Keywords map. Key is the vocabulary, value is a list of keywords for that vocabulary.
*/
QMap< QString, QStringList > mKeywords;
KeywordMap mKeywords;

QList< Contact > mContacts;
ContactList mContacts;

QList< Link > mLinks;
LinkList mLinks;

/*
* IMPORTANT!!!!!!
Expand All @@ -547,4 +583,11 @@ class CORE_EXPORT QgsLayerMetadata

};

Q_DECLARE_METATYPE( QgsLayerMetadata::KeywordMap )
Q_DECLARE_METATYPE( QgsLayerMetadata::ConstraintList )
Q_DECLARE_METATYPE( QgsLayerMetadata::ContactList )
Q_DECLARE_METATYPE( QgsLayerMetadata::LinkList )



#endif // QGSLAYERMETADATA_H
3 changes: 3 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -506,6 +506,8 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, const QgsPathRe

readCustomProperties( layerElement );

mMetadata.readFromLayer( this );

return true;
} // bool QgsMapLayer::readLayerXML

Expand Down Expand Up @@ -1624,6 +1626,7 @@ void QgsMapLayer::triggerRepaint( bool deferredUpdate )
void QgsMapLayer::setMetadata( const QgsLayerMetadata &metadata )
{
mMetadata = metadata;
mMetadata.saveToLayer( this );
emit metadataChanged();
}

Expand Down

0 comments on commit cc3d67a

Please sign in to comment.