QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 bool QgsObjectCustomProperties::contains( const QString &key ) const
46 {
47  return mMap.contains( key );
48 }
49 
50 void QgsObjectCustomProperties::readXml( const QDomNode &parentNode, const QString &keyStartsWith )
51 {
52  QDomNode propsNode = parentNode.namedItem( QStringLiteral( "customproperties" ) );
53  if ( propsNode.isNull() ) // no properties stored...
54  return;
55 
56  if ( !keyStartsWith.isEmpty() )
57  {
58  //remove old keys
59  QStringList keysToRemove;
60  QMap<QString, QVariant>::const_iterator pIt = mMap.constBegin();
61  for ( ; pIt != mMap.constEnd(); ++pIt )
62  {
63  if ( pIt.key().startsWith( keyStartsWith ) )
64  {
65  keysToRemove.push_back( pIt.key() );
66  }
67  }
68 
69  QStringList::const_iterator sIt = keysToRemove.constBegin();
70  for ( ; sIt != keysToRemove.constEnd(); ++sIt )
71  {
72  mMap.remove( *sIt );
73  }
74  }
75  else
76  {
77  mMap.clear();
78  }
79 
80  QDomNodeList nodes = propsNode.childNodes();
81 
82  for ( int i = 0; i < nodes.size(); i++ )
83  {
84  QDomNode propNode = nodes.at( i );
85  if ( propNode.isNull() || propNode.nodeName() != QLatin1String( "property" ) )
86  continue;
87  QDomElement propElement = propNode.toElement();
88 
89  QString key = propElement.attribute( QStringLiteral( "key" ) );
90  if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
91  {
92  if ( propElement.hasAttribute( QStringLiteral( "value" ) ) )
93  {
94  QString value = propElement.attribute( QStringLiteral( "value" ) );
95  mMap[key] = QVariant( value );
96  }
97  else
98  {
99  QStringList list;
100 
101  for ( QDomElement itemElement = propElement.firstChildElement( QStringLiteral( "value" ) );
102  !itemElement.isNull();
103  itemElement = itemElement.nextSiblingElement( QStringLiteral( "value" ) ) )
104  {
105  list << itemElement.text();
106  }
107 
108  mMap[key] = QVariant( list );
109  }
110  }
111  }
112 
113 }
114 
115 void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &doc ) const
116 {
117  //remove already existing <customproperties> tags
118  QDomNodeList propertyList = parentNode.toElement().elementsByTagName( QStringLiteral( "customproperties" ) );
119  for ( int i = 0; i < propertyList.size(); ++i )
120  {
121  parentNode.removeChild( propertyList.at( i ) );
122  }
123 
124  QDomElement propsElement = doc.createElement( QStringLiteral( "customproperties" ) );
125 
126  auto keys = mMap.keys();
127 
128  std::sort( keys.begin(), keys.end() );
129 
130  for ( const auto &key : qgis::as_const( keys ) )
131  {
132  QDomElement propElement = doc.createElement( QStringLiteral( "property" ) );
133  propElement.setAttribute( QStringLiteral( "key" ), key );
134  const QVariant value = mMap.value( key );
135  if ( value.canConvert<QString>() )
136  {
137  propElement.setAttribute( QStringLiteral( "value" ), value.toString() );
138  }
139  else if ( value.canConvert<QStringList>() )
140  {
141  const auto constToStringList = value.toStringList();
142  for ( const QString &valueStr : constToStringList )
143  {
144  QDomElement itemElement = doc.createElement( QStringLiteral( "value" ) );
145  itemElement.appendChild( doc.createTextNode( valueStr ) );
146  propElement.appendChild( itemElement );
147  }
148  }
149  propsElement.appendChild( propElement );
150  }
151 
152  parentNode.appendChild( propsElement );
153 }
qgis.h
QgsObjectCustomProperties::readXml
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from an XML node.
Definition: qgsobjectcustomproperties.cpp:50
qgsobjectcustomproperties.h
QgsObjectCustomProperties::setValue
void setValue(const QString &key, const QVariant &value)
Add an entry to the store with the specified key.
Definition: qgsobjectcustomproperties.cpp:30
QgsObjectCustomProperties::remove
void remove(const QString &key)
Removes a key (entry) from the store.
Definition: qgsobjectcustomproperties.cpp:40
QgsObjectCustomProperties::mMap
QMap< QString, QVariant > mMap
Definition: qgsobjectcustomproperties.h:92
QgsObjectCustomProperties::contains
bool contains(const QString &key) const
Returns true if the properties contains a key with the specified name.
Definition: qgsobjectcustomproperties.cpp:45
QgsObjectCustomProperties::keys
QStringList keys() const
Returns a list of all stored keys.
Definition: qgsobjectcustomproperties.cpp:25
QgsObjectCustomProperties::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for the given key.
Definition: qgsobjectcustomproperties.cpp:35
QgsObjectCustomProperties::writeXml
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Writes the store contents to an XML node.
Definition: qgsobjectcustomproperties.cpp:115