Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved map layer's custom properties to a separate class
- Loading branch information
Showing
5 changed files
with
202 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |