QGIS API Documentation  3.6.0-Noosa (5873452)
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  return mMap.keys();
27 }
28 
29 void QgsObjectCustomProperties::setValue( const QString &key, const QVariant &value )
30 {
31  mMap[key] = value;
32 }
33 
34 QVariant QgsObjectCustomProperties::value( const QString &key, const QVariant &defaultValue ) const
35 {
36  return mMap.value( key, defaultValue );
37 }
38 
39 void QgsObjectCustomProperties::remove( const QString &key )
40 {
41  mMap.remove( key );
42 }
43 
44 
45 void QgsObjectCustomProperties::readXml( const QDomNode &parentNode, const QString &keyStartsWith )
46 {
47  QDomNode propsNode = parentNode.namedItem( QStringLiteral( "customproperties" ) );
48  if ( propsNode.isNull() ) // no properties stored...
49  return;
50 
51  if ( !keyStartsWith.isEmpty() )
52  {
53  //remove old keys
54  QStringList keysToRemove;
55  QMap<QString, QVariant>::const_iterator pIt = mMap.constBegin();
56  for ( ; pIt != mMap.constEnd(); ++pIt )
57  {
58  if ( pIt.key().startsWith( keyStartsWith ) )
59  {
60  keysToRemove.push_back( pIt.key() );
61  }
62  }
63 
64  QStringList::const_iterator sIt = keysToRemove.constBegin();
65  for ( ; sIt != keysToRemove.constEnd(); ++sIt )
66  {
67  mMap.remove( *sIt );
68  }
69  }
70  else
71  {
72  mMap.clear();
73  }
74 
75  QDomNodeList nodes = propsNode.childNodes();
76 
77  for ( int i = 0; i < nodes.size(); i++ )
78  {
79  QDomNode propNode = nodes.at( i );
80  if ( propNode.isNull() || propNode.nodeName() != QLatin1String( "property" ) )
81  continue;
82  QDomElement propElement = propNode.toElement();
83 
84  QString key = propElement.attribute( QStringLiteral( "key" ) );
85  if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
86  {
87  if ( propElement.hasAttribute( QStringLiteral( "value" ) ) )
88  {
89  QString value = propElement.attribute( QStringLiteral( "value" ) );
90  mMap[key] = QVariant( value );
91  }
92  else
93  {
94  QStringList list;
95 
96  for ( QDomElement itemElement = propElement.firstChildElement( QStringLiteral( "value" ) );
97  !itemElement.isNull();
98  itemElement = itemElement.nextSiblingElement( QStringLiteral( "value" ) ) )
99  {
100  list << itemElement.text();
101  }
102 
103  mMap[key] = QVariant( list );
104  }
105  }
106  }
107 
108 }
109 
110 void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &doc ) const
111 {
112  //remove already existing <customproperties> tags
113  QDomNodeList propertyList = parentNode.toElement().elementsByTagName( QStringLiteral( "customproperties" ) );
114  for ( int i = 0; i < propertyList.size(); ++i )
115  {
116  parentNode.removeChild( propertyList.at( i ) );
117  }
118 
119  QDomElement propsElement = doc.createElement( QStringLiteral( "customproperties" ) );
120 
121  for ( QMap<QString, QVariant>::const_iterator it = mMap.constBegin(); it != mMap.constEnd(); ++it )
122  {
123  QDomElement propElement = doc.createElement( QStringLiteral( "property" ) );
124  propElement.setAttribute( QStringLiteral( "key" ), it.key() );
125  if ( it.value().canConvert<QString>() )
126  {
127  propElement.setAttribute( QStringLiteral( "value" ), it.value().toString() );
128  }
129  else if ( it.value().canConvert<QStringList>() )
130  {
131  Q_FOREACH ( const QString &value, it.value().toStringList() )
132  {
133  QDomElement itemElement = doc.createElement( QStringLiteral( "value" ) );
134  itemElement.appendChild( doc.createTextNode( value ) );
135  propElement.appendChild( itemElement );
136  }
137  }
138  propsElement.appendChild( propElement );
139  }
140 
141  parentNode.appendChild( propsElement );
142 }
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns value for the given key. If the key is not stored, default value will be used.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.
void remove(const QString &key)
Remove a key (entry) from the store.
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
Returns list of stored keys.