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