From 1aa0f672cd55f61646772792d2f52ea7c7b506f5 Mon Sep 17 00:00:00 2001 From: wonder Date: Fri, 7 Aug 2009 10:45:46 +0000 Subject: [PATCH] Added support for custom properties in map layer (that are also saved in project). Useful mainly for plugins. git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11289 c8812cc2-4d05-0410-92ff-de0c093fc19c --- python/core/qgsmaplayer.sip | 10 +++++++ src/core/qgsmaplayer.cpp | 60 +++++++++++++++++++++++++++++++++++++ src/core/qgsmaplayer.h | 21 +++++++++++-- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/python/core/qgsmaplayer.sip b/python/core/qgsmaplayer.sip index ffbbe2ac507c..15ccf5da487d 100644 --- a/python/core/qgsmaplayer.sip +++ b/python/core/qgsmaplayer.sip @@ -144,6 +144,16 @@ public: */ bool writeXML(QDomNode & layer_node, QDomDocument & document) const; + /** Set a custom property for layer. Properties are stored in a map and saved in project file. + * @note Added in v1.3 */ + void setCustomProperty( const QString& key, const QVariant& value ); + /** Read a custom property from layer. Properties are stored in a map and saved in project file. + * @note Added in v1.3 */ + QVariant customProperty( const QString& value, const QVariant& defaultValue = QVariant() ) const; + /** Remove a custom property from layer. Properties are stored in a map and saved in project file. + * @note Added in v1.3 */ + void removeCustomProperty( const QString& key ); + /** Read the symbology for the current layer from the Dom node supplied. * @param QDomNode node that will contain the symbology definition for this layer. * @param errorMessage reference to string that will be updated with any error messages diff --git a/src/core/qgsmaplayer.cpp b/src/core/qgsmaplayer.cpp index 2886d18c6fb9..f3899b5dfaff 100644 --- a/src/core/qgsmaplayer.cpp +++ b/src/core/qgsmaplayer.cpp @@ -239,6 +239,8 @@ bool QgsMapLayer::readXML( QDomNode & layer_node ) setTransparency( myElement.text().toInt() ); } + readCustomProperties( layer_node ); + return true; } // void QgsMapLayer::readXML @@ -323,6 +325,8 @@ bool QgsMapLayer::writeXML( QDomNode & layer_node, QDomDocument & document ) layer_node.appendChild( maplayer ); + writeCustomProperties( maplayer, document ); + return writeXml( maplayer, document ); } // bool QgsMapLayer::writeXML @@ -723,3 +727,59 @@ QUndoStack* QgsMapLayer::undoStack() { return &mUndoStack; } + + + +void QgsMapLayer::setCustomProperty( const QString& key, const QVariant& value ) +{ + mCustomProperties[key] = value; +} + +QVariant QgsMapLayer::customProperty( const QString& value, const QVariant& defaultValue ) const +{ + return mCustomProperties.value(value, defaultValue); +} + +void QgsMapLayer::removeCustomProperty( const QString& key ) +{ + mCustomProperties.remove(key); +} + +void QgsMapLayer::readCustomProperties( QDomNode & layerNode ) +{ + QDomNode propsNode = layerNode.namedItem("customproperties"); + if ( propsNode.isNull() ) // no properties stored... + return; + + mCustomProperties.clear(); + + QDomNodeList nodes = propsNode.childNodes(); + + for ( int i = 0; i < nodes.size(); i++ ) + { + QDomNode propNode = nodes.at( i ); + if (propNode.isNull() || propNode.nodeName() != "property") + continue; + QDomElement propElement = propNode.toElement(); + + QString key = propElement.attribute( "key" ); + QString value = propElement.attribute( "value" ); + mCustomProperties[key] = QVariant(value); + } + +} + +void QgsMapLayer::writeCustomProperties( QDomNode & layerNode, QDomDocument & doc ) +{ + QDomElement propsElement = doc.createElement( "customproperties" ); + + for ( QMap::const_iterator it = mCustomProperties.begin(); it != mCustomProperties.end(); ++it ) + { + QDomElement propElement = doc.createElement( "property" ); + propElement.setAttribute( "key", it.key() ); + propElement.setAttribute( "value", it.value().toString() ); + propsElement.appendChild(propElement); + } + + layerNode.appendChild(propsElement); +} diff --git a/src/core/qgsmaplayer.h b/src/core/qgsmaplayer.h index e581d1358148..d52a82c3cd4b 100644 --- a/src/core/qgsmaplayer.h +++ b/src/core/qgsmaplayer.h @@ -19,11 +19,10 @@ #ifndef QGSMAPLAYER_H #define QGSMAPLAYER_H -#include -#include #include #include +#include #include "qgsrectangle.h" @@ -155,6 +154,16 @@ class CORE_EXPORT QgsMapLayer : public QObject */ bool writeXML( QDomNode & layer_node, QDomDocument & document ); + /** Set a custom property for layer. Properties are stored in a map and saved in project file. + * @note Added in v1.3 */ + void setCustomProperty( const QString& key, const QVariant& value ); + /** Read a custom property from layer. Properties are stored in a map and saved in project file. + * @note Added in v1.3 */ + QVariant customProperty( const QString& value, const QVariant& defaultValue = QVariant() ) const; + /** Remove a custom property from layer. Properties are stored in a map and saved in project file. + * @note Added in v1.3 */ + void removeCustomProperty( const QString& key ); + /** Copies the symbology settings from another layer. Returns true in case of success */ virtual bool copySymbologySettings( const QgsMapLayer& other ) = 0; @@ -315,6 +324,13 @@ class CORE_EXPORT QgsMapLayer : public QObject */ virtual bool writeXml( QDomNode & layer_node, QDomDocument & document ); + + /** Read custom properties from project file. Added in v1.3 */ + void readCustomProperties( QDomNode & layerNode ); + + /** Write custom properties to project file. Added in v1.3 */ + void writeCustomProperties( QDomNode & layerNode, QDomDocument & doc ); + /** debugging member - invoked when a connect() is made to this object */ void connectNotify( const char * signal ); @@ -362,6 +378,7 @@ class CORE_EXPORT QgsMapLayer : public QObject QUndoStack mUndoStack; + QMap mCustomProperties; }; #endif