Skip to content
Permalink
Browse files

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
  • Loading branch information
wonder
wonder committed Aug 7, 2009
1 parent 9513f3f commit 1aa0f672cd55f61646772792d2f52ea7c7b506f5
Showing with 89 additions and 2 deletions.
  1. +10 −0 python/core/qgsmaplayer.sip
  2. +60 −0 src/core/qgsmaplayer.cpp
  3. +19 −2 src/core/qgsmaplayer.h
@@ -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
@@ -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<QString, QVariant>::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);
}
@@ -19,11 +19,10 @@
#ifndef QGSMAPLAYER_H
#define QGSMAPLAYER_H

#include <vector>
#include <map>

#include <QObject>
#include <QUndoStack>
#include <QVariant>

#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<QString, QVariant> mCustomProperties;
};

#endif

0 comments on commit 1aa0f67

Please sign in to comment.
You can’t perform that action at this time.