Skip to content

Commit

Permalink
Added support for custom properties in map layer (that are also saved…
Browse files Browse the repository at this point in the history
… 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 committed Aug 7, 2009
1 parent 9513f3f commit 1aa0f67
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
10 changes: 10 additions & 0 deletions python/core/qgsmaplayer.sip
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -239,6 +239,8 @@ bool QgsMapLayer::readXML( QDomNode & layer_node )
setTransparency( myElement.text().toInt() );
}

readCustomProperties( layer_node );

return true;
} // void QgsMapLayer::readXML

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
21 changes: 19 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -19,11 +19,10 @@
#ifndef QGSMAPLAYER_H
#define QGSMAPLAYER_H

#include <vector>
#include <map>

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

#include "qgsrectangle.h"

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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.