QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgsxmlutils.h"
21 
22 #include <QDomNode>
23 #include <QStringList>
24 
25 
27 {
28  return mMap.keys();
29 }
30 
31 void QgsObjectCustomProperties::setValue( const QString &key, const QVariant &value )
32 {
33  mMap[key] = value;
34 }
35 
36 QVariant QgsObjectCustomProperties::value( const QString &key, const QVariant &defaultValue ) const
37 {
38  return mMap.value( key, defaultValue );
39 }
40 
41 void QgsObjectCustomProperties::remove( const QString &key )
42 {
43  mMap.remove( key );
44 }
45 
46 bool QgsObjectCustomProperties::contains( const QString &key ) const
47 {
48  return mMap.contains( key );
49 }
50 
51 void QgsObjectCustomProperties::readXml( const QDomNode &parentNode, const QString &keyStartsWith )
52 {
53  const QDomNode propsNode = parentNode.namedItem( QStringLiteral( "customproperties" ) );
54  if ( propsNode.isNull() ) // no properties stored...
55  return;
56 
57  if ( !keyStartsWith.isEmpty() )
58  {
59  //remove old keys
60  QStringList keysToRemove;
61  QMap<QString, QVariant>::const_iterator pIt = mMap.constBegin();
62  for ( ; pIt != mMap.constEnd(); ++pIt )
63  {
64  if ( pIt.key().startsWith( keyStartsWith ) )
65  {
66  keysToRemove.push_back( pIt.key() );
67  }
68  }
69 
70  QStringList::const_iterator sIt = keysToRemove.constBegin();
71  for ( ; sIt != keysToRemove.constEnd(); ++sIt )
72  {
73  mMap.remove( *sIt );
74  }
75  }
76  else
77  {
78  mMap.clear();
79  }
80 
81  const QVariant newProps = QgsXmlUtils::readVariant( propsNode.firstChildElement() );
82  if ( newProps.type() == QVariant::Map )
83  {
84 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
85  const QVariantMap propsMap = newProps.toMap();
86  for ( auto it = propsMap.constBegin(); it != propsMap.constEnd(); ++it )
87  mMap.insert( it.key(), it.value() );
88 #else
89  mMap.insert( newProps.toMap() );
90 #endif
91  }
92  else
93  {
94  // backward compatibility code for QGIS < 3.20
95  const QDomNodeList nodes = propsNode.childNodes();
96 
97  for ( int i = 0; i < nodes.size(); i++ )
98  {
99  const QDomNode propNode = nodes.at( i );
100  if ( propNode.isNull() || propNode.nodeName() != QLatin1String( "property" ) )
101  continue;
102  const QDomElement propElement = propNode.toElement();
103 
104  const QString key = propElement.attribute( QStringLiteral( "key" ) );
105  if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
106  {
107  if ( propElement.hasAttribute( QStringLiteral( "value" ) ) )
108  {
109  const QString value = propElement.attribute( QStringLiteral( "value" ) );
110  mMap[key] = QVariant( value );
111  }
112  else
113  {
114  QStringList list;
115 
116  for ( QDomElement itemElement = propElement.firstChildElement( QStringLiteral( "value" ) );
117  !itemElement.isNull();
118  itemElement = itemElement.nextSiblingElement( QStringLiteral( "value" ) ) )
119  {
120  list << itemElement.text();
121  }
122 
123  mMap[key] = QVariant( list );
124  }
125  }
126  }
127  }
128 }
129 
130 void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &doc ) const
131 {
132  //remove already existing <customproperties> tags
133  const QDomNodeList propertyList = parentNode.toElement().elementsByTagName( QStringLiteral( "customproperties" ) );
134  for ( int i = 0; i < propertyList.size(); ++i )
135  {
136  parentNode.removeChild( propertyList.at( i ) );
137  }
138 
139  QDomElement propsElement = doc.createElement( QStringLiteral( "customproperties" ) );
140  propsElement.appendChild( QgsXmlUtils::writeVariant( mMap, doc ) );
141  parentNode.appendChild( propsElement );
142 }
void setValue(const QString &key, const QVariant &value)
Add an entry to the store with the specified key.
QMap< QString, QVariant > mMap
QStringList keys() const
Returns a list of all stored keys.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Writes the store contents to an XML node.
void remove(const QString &key)
Removes a key (entry) from the store.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for the given key.
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from an XML node.
bool contains(const QString &key) const
Returns true if the properties contains a key with the specified name.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.