QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
31void QgsObjectCustomProperties::setValue( const QString &key, const QVariant &value )
32{
33 mMap[key] = value;
34}
35
36QVariant QgsObjectCustomProperties::value( const QString &key, const QVariant &defaultValue ) const
37{
38 return mMap.value( key, defaultValue );
39}
40
41void QgsObjectCustomProperties::remove( const QString &key )
42{
43 mMap.remove( key );
44}
45
46bool QgsObjectCustomProperties::contains( const QString &key ) const
47{
48 return mMap.contains( key );
49}
50
51void 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 mMap.insert( newProps.toMap() );
85 }
86 else
87 {
88 // backward compatibility code for QGIS < 3.20
89 const QDomNodeList nodes = propsNode.childNodes();
90
91 for ( int i = 0; i < nodes.size(); i++ )
92 {
93 const QDomNode propNode = nodes.at( i );
94 if ( propNode.isNull() || propNode.nodeName() != QLatin1String( "property" ) )
95 continue;
96 const QDomElement propElement = propNode.toElement();
97
98 const QString key = propElement.attribute( QStringLiteral( "key" ) );
99 if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
100 {
101 if ( propElement.hasAttribute( QStringLiteral( "value" ) ) )
102 {
103 const QString value = propElement.attribute( QStringLiteral( "value" ) );
104 mMap[key] = QVariant( value );
105 }
106 else
107 {
108 QStringList list;
109
110 for ( QDomElement itemElement = propElement.firstChildElement( QStringLiteral( "value" ) );
111 !itemElement.isNull();
112 itemElement = itemElement.nextSiblingElement( QStringLiteral( "value" ) ) )
113 {
114 list << itemElement.text();
115 }
116
117 mMap[key] = QVariant( list );
118 }
119 }
120 }
121 }
122}
123
124void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &doc ) const
125{
126 //remove already existing <customproperties> tags
127 const QDomNodeList propertyList = parentNode.toElement().elementsByTagName( QStringLiteral( "customproperties" ) );
128 for ( int i = 0; i < propertyList.size(); ++i )
129 {
130 parentNode.removeChild( propertyList.at( i ) );
131 }
132
133 QDomElement propsElement = doc.createElement( QStringLiteral( "customproperties" ) );
134 propsElement.appendChild( QgsXmlUtils::writeVariant( mMap, doc ) );
135 parentNode.appendChild( propsElement );
136}
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.