Skip to content

Commit

Permalink
Moved map layer's custom properties to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Apr 17, 2014
1 parent f97e234 commit 77a1143
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 74 deletions.
6 changes: 4 additions & 2 deletions src/core/CMakeLists.txt
Expand Up @@ -62,6 +62,7 @@ SET(QGIS_CORE_SRCS
qgscontexthelp.cpp
qgscontexthelp_texts.cpp
qgscoordinatetransform.cpp
qgscredentials.cpp
qgscrscache.cpp
qgsdatadefined.cpp
qgsdatasourceuri.cpp
Expand Down Expand Up @@ -99,7 +100,7 @@ SET(QGIS_CORE_SRCS
qgsmimedatautils.cpp
qgsmessagelog.cpp
qgsnetworkreplyparser.cpp
qgscredentials.cpp
qgsobjectcustomproperties.cpp
qgsofflineediting.cpp
qgsogcutils.cpp
qgsowsconnection.cpp
Expand Down Expand Up @@ -405,6 +406,7 @@ SET(QGIS_CORE_HDRS
qgsclipper.h
qgscontexthelp.h
qgscoordinatetransform.h
qgscredentials.h
qgsdatadefined.h
qgsdatasourceuri.h
qgsdataitem.h
Expand Down Expand Up @@ -436,7 +438,7 @@ SET(QGIS_CORE_HDRS
qgsmessageoutput.h
qgsmimedatautils.h
qgsnetworkreplyparser.h
qgscredentials.h
qgsobjectcustomproperties.h
qgsofflineediting.h
qgsogcutils.h
qgsowsconnection.h
Expand Down
82 changes: 12 additions & 70 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -648,6 +648,17 @@ bool QgsMapLayer::writeXml( QDomNode & layer_node, QDomDocument & document )
} // void QgsMapLayer::writeXml


void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
{
mCustomProperties.readXml( layerNode, keyStartsWith );
}

void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
{
mCustomProperties.writeXml( layerNode, doc );
}




bool QgsMapLayer::isValid()
Expand Down Expand Up @@ -1308,7 +1319,7 @@ QUndoStack* QgsMapLayer::undoStack()

void QgsMapLayer::setCustomProperty( const QString& key, const QVariant& value )
{
mCustomProperties[key] = value;
mCustomProperties.setValue( key, value );
}

QVariant QgsMapLayer::customProperty( const QString& value, const QVariant& defaultValue ) const
Expand All @@ -1321,76 +1332,7 @@ void QgsMapLayer::removeCustomProperty( const QString& key )
mCustomProperties.remove( key );
}

void QgsMapLayer::readCustomProperties( const QDomNode& layerNode, const QString& keyStartsWith )
{
QDomNode propsNode = layerNode.namedItem( "customproperties" );
if ( propsNode.isNull() ) // no properties stored...
return;

if ( !keyStartsWith.isEmpty() )
{
//remove old keys
QStringList keysToRemove;
QMap<QString, QVariant>::const_iterator pIt = mCustomProperties.constBegin();
for ( ; pIt != mCustomProperties.constEnd(); ++pIt )
{
if ( pIt.key().startsWith( keyStartsWith ) )
{
keysToRemove.push_back( pIt.key() );
}
}

QStringList::const_iterator sIt = keysToRemove.constBegin();
for ( ; sIt != keysToRemove.constEnd(); ++sIt )
{
mCustomProperties.remove( *sIt );
}
}
else
{
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" );
if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
{
QString value = propElement.attribute( "value" );
mCustomProperties[key] = QVariant( value );
}
}

}

void QgsMapLayer::writeCustomProperties( QDomNode & layerNode, QDomDocument & doc ) const
{
//remove already existing <customproperties> tags
QDomNodeList propertyList = layerNode.toElement().elementsByTagName( "customproperties" );
for ( int i = 0; i < propertyList.size(); ++i )
{
layerNode.removeChild( propertyList.at( i ) );
}

QDomElement propsElement = doc.createElement( "customproperties" );

for ( QMap<QString, QVariant>::const_iterator it = mCustomProperties.constBegin(); it != mCustomProperties.constEnd(); ++it )
{
QDomElement propElement = doc.createElement( "property" );
propElement.setAttribute( "key", it.key() );
propElement.setAttribute( "value", it.value().toString() );
propsElement.appendChild( propElement );
}

layerNode.appendChild( propsElement );
}

bool QgsMapLayer::isEditable() const
{
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -30,6 +30,7 @@
#include "qgserror.h"
#include "qgsrectangle.h"
#include "qgsmaprenderer.h"
#include "qgsobjectcustomproperties.h"

class QgsRenderContext;
class QgsCoordinateReferenceSystem;
Expand Down Expand Up @@ -543,8 +544,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
/** Collection of undoable operations for this layer. **/
QUndoStack mUndoStack;

QMap<QString, QVariant> mCustomProperties;

//! Layer's persistent storage of additional properties (may be used by plugins)
QgsObjectCustomProperties mCustomProperties;
};

#endif
118 changes: 118 additions & 0 deletions src/core/qgsobjectcustomproperties.cpp
@@ -0,0 +1,118 @@
/***************************************************************************
qgsobjectcustomproperties.cpp
-------------------
begin : April 2014
copyright : (C) 2014 by Martin Dobias
email : wonder.sk at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsobjectcustomproperties.h"

#include <QDomNode>
#include <QStringList>


QgsObjectCustomProperties::QgsObjectCustomProperties()
{
}

QStringList QgsObjectCustomProperties::keys() const
{
return mMap.keys();
}

void QgsObjectCustomProperties::setValue( const QString& key, const QVariant& value )
{
mMap[key] = value;
}

QVariant QgsObjectCustomProperties::value( const QString& key, const QVariant& defaultValue ) const
{
return mMap.value( key, defaultValue );
}

void QgsObjectCustomProperties::remove( const QString& key )
{
mMap.remove( key );
}


void QgsObjectCustomProperties::readXml( const QDomNode& parentNode, const QString& keyStartsWith )
{
QDomNode propsNode = parentNode.namedItem( "customproperties" );
if ( propsNode.isNull() ) // no properties stored...
return;

if ( !keyStartsWith.isEmpty() )
{
//remove old keys
QStringList keysToRemove;
QMap<QString, QVariant>::const_iterator pIt = mMap.constBegin();
for ( ; pIt != mMap.constEnd(); ++pIt )
{
if ( pIt.key().startsWith( keyStartsWith ) )
{
keysToRemove.push_back( pIt.key() );
}
}

QStringList::const_iterator sIt = keysToRemove.constBegin();
for ( ; sIt != keysToRemove.constEnd(); ++sIt )
{
mMap.remove( *sIt );
}
}
else
{
mMap.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" );
if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
{
QString value = propElement.attribute( "value" );
mMap[key] = QVariant( value );
}
}

}

void QgsObjectCustomProperties::writeXml( QDomNode& parentNode, QDomDocument& doc ) const
{
//remove already existing <customproperties> tags
QDomNodeList propertyList = parentNode.toElement().elementsByTagName( "customproperties" );
for ( int i = 0; i < propertyList.size(); ++i )
{
parentNode.removeChild( propertyList.at( i ) );
}

QDomElement propsElement = doc.createElement( "customproperties" );

for ( QMap<QString, QVariant>::const_iterator it = mMap.constBegin(); it != mMap.constEnd(); ++it )
{
QDomElement propElement = doc.createElement( "property" );
propElement.setAttribute( "key", it.key() );
propElement.setAttribute( "value", it.value().toString() );
propsElement.appendChild( propElement );
}

parentNode.appendChild( propsElement );
}
65 changes: 65 additions & 0 deletions src/core/qgsobjectcustomproperties.h
@@ -0,0 +1,65 @@
/***************************************************************************
qgsobjectcustomproperties.h
-------------------
begin : April 2014
copyright : (C) 2014 by Martin Dobias
email : wonder.sk at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSOBJECTCUSTOMPROPERTIES_H
#define QGSOBJECTCUSTOMPROPERTIES_H

#include <QMap>
#include <QVariant>

class QDomDocument;
class QDomNode;

/**
* Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML
* in <customproperties> element.
*
* \note added in 2.4
*/
class CORE_EXPORT QgsObjectCustomProperties
{
public:
QgsObjectCustomProperties();

//! Return list of stored keys
QStringList keys() const;

//! Add an entry to the store. If the entry with the keys exists already, it will be overwritten
void setValue( const QString& key, const QVariant& value );

//! Return value for the given key. If the key is not stored, default value will be used
QVariant value( const QString& key, const QVariant& defaultValue = QVariant() ) const;

//! Remove a key (entry) from the store
void remove( const QString& key );


/** Read store contents from XML
@param layerNode note to read from
@param keyStartsWith reads only properties starting with the specified string (or all if the string is empty)*/
void readXml( const QDomNode& parentNode, const QString& keyStartsWith = QString() );

/** Write store contents to XML */
void writeXml( QDomNode& parentNode, QDomDocument& doc ) const;


protected:
QMap<QString, QVariant> mMap;

};

#endif // QGSOBJECTCUSTOMPROPERTIES_H

0 comments on commit 77a1143

Please sign in to comment.