QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsobjectcustomproperties.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsobjectcustomproperties.cpp
3  -------------------
4  begin : April 2014
5  copyright : (C) 2014 by Martin Dobias
6  email : wonder.sk at gmail dot com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 
20 #include <QDomNode>
21 #include <QStringList>
22 
23 
25 {
26 }
27 
29 {
30  return mMap.keys();
31 }
32 
33 void QgsObjectCustomProperties::setValue( const QString& key, const QVariant& value )
34 {
35  mMap[key] = value;
36 }
37 
38 QVariant QgsObjectCustomProperties::value( const QString& key, const QVariant& defaultValue ) const
39 {
40  return mMap.value( key, defaultValue );
41 }
42 
43 void QgsObjectCustomProperties::remove( const QString& key )
44 {
45  mMap.remove( key );
46 }
47 
48 
49 void QgsObjectCustomProperties::readXml( const QDomNode& parentNode, const QString& keyStartsWith )
50 {
51  QDomNode propsNode = parentNode.namedItem( "customproperties" );
52  if ( propsNode.isNull() ) // no properties stored...
53  return;
54 
55  if ( !keyStartsWith.isEmpty() )
56  {
57  //remove old keys
58  QStringList keysToRemove;
59  QMap<QString, QVariant>::const_iterator pIt = mMap.constBegin();
60  for ( ; pIt != mMap.constEnd(); ++pIt )
61  {
62  if ( pIt.key().startsWith( keyStartsWith ) )
63  {
64  keysToRemove.push_back( pIt.key() );
65  }
66  }
67 
68  QStringList::const_iterator sIt = keysToRemove.constBegin();
69  for ( ; sIt != keysToRemove.constEnd(); ++sIt )
70  {
71  mMap.remove( *sIt );
72  }
73  }
74  else
75  {
76  mMap.clear();
77  }
78 
79  QDomNodeList nodes = propsNode.childNodes();
80 
81  for ( int i = 0; i < nodes.size(); i++ )
82  {
83  QDomNode propNode = nodes.at( i );
84  if ( propNode.isNull() || propNode.nodeName() != "property" )
85  continue;
86  QDomElement propElement = propNode.toElement();
87 
88  QString key = propElement.attribute( "key" );
89  if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
90  {
91  QString value = propElement.attribute( "value" );
92  mMap[key] = QVariant( value );
93  }
94  }
95 
96 }
97 
98 void QgsObjectCustomProperties::writeXml( QDomNode& parentNode, QDomDocument& doc ) const
99 {
100  //remove already existing <customproperties> tags
101  QDomNodeList propertyList = parentNode.toElement().elementsByTagName( "customproperties" );
102  for ( int i = 0; i < propertyList.size(); ++i )
103  {
104  parentNode.removeChild( propertyList.at( i ) );
105  }
106 
107  QDomElement propsElement = doc.createElement( "customproperties" );
108 
109  for ( QMap<QString, QVariant>::const_iterator it = mMap.constBegin(); it != mMap.constEnd(); ++it )
110  {
111  QDomElement propElement = doc.createElement( "property" );
112  propElement.setAttribute( "key", it.key() );
113  propElement.setAttribute( "value", it.value().toString() );
114  propsElement.appendChild( propElement );
115  }
116 
117  parentNode.appendChild( propsElement );
118 }
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
void remove(const QString &key)
Remove a key (entry) from the store.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Return value for the given key. If the key is not stored, default value will be used.
QMap< QString, QVariant > mMap
void setValue(const QString &key, const QVariant &value)
Add an entry to the store. If the entry with the keys exists already, it will be overwritten.
QStringList keys() const
Return list of stored keys.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.