QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
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 "qgis.h"
21#include "qgsxmlutils.h"
22
23#include <QDomNode>
24#include <QString>
25#include <QStringList>
26
27using namespace Qt::StringLiterals;
28
30{
31 return mMap.keys();
32}
33
34void QgsObjectCustomProperties::setValue( const QString &key, const QVariant &value )
35{
36 mMap[key] = value;
37}
38
39QVariant QgsObjectCustomProperties::value( const QString &key, const QVariant &defaultValue ) const
40{
41 return mMap.value( key, defaultValue );
42}
43
44void QgsObjectCustomProperties::remove( const QString &key )
45{
46 mMap.remove( key );
47}
48
49bool QgsObjectCustomProperties::contains( const QString &key ) const
50{
51 return mMap.contains( key );
52}
53
54void QgsObjectCustomProperties::readXml( const QDomNode &parentNode, const QString &keyStartsWith )
55{
56 const QDomNode propsNode = parentNode.namedItem( u"customproperties"_s );
57 if ( propsNode.isNull() ) // no properties stored...
58 return;
59
60 if ( !keyStartsWith.isEmpty() )
61 {
62 //remove old keys
63 QStringList keysToRemove;
64 QMap<QString, QVariant>::const_iterator pIt = mMap.constBegin();
65 for ( ; pIt != mMap.constEnd(); ++pIt )
66 {
67 if ( pIt.key().startsWith( keyStartsWith ) )
68 {
69 keysToRemove.push_back( pIt.key() );
70 }
71 }
72
73 QStringList::const_iterator sIt = keysToRemove.constBegin();
74 for ( ; sIt != keysToRemove.constEnd(); ++sIt )
75 {
76 mMap.remove( *sIt );
77 }
78 }
79 else
80 {
81 mMap.clear();
82 }
83
84 const QVariant newProps = QgsXmlUtils::readVariant( propsNode.firstChildElement() );
85 if ( newProps.userType() == QMetaType::Type::QVariantMap )
86 {
87 mMap.insert( newProps.toMap() );
88 }
89 else
90 {
91 // backward compatibility code for QGIS < 3.20
92 const QDomNodeList nodes = propsNode.childNodes();
93
94 for ( int i = 0; i < nodes.size(); i++ )
95 {
96 const QDomNode propNode = nodes.at( i );
97 if ( propNode.isNull() || propNode.nodeName() != "property"_L1 )
98 continue;
99 const QDomElement propElement = propNode.toElement();
100
101 const QString key = propElement.attribute( u"key"_s );
102 if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
103 {
104 if ( propElement.hasAttribute( u"value"_s ) )
105 {
106 const QString value = propElement.attribute( u"value"_s );
107 mMap[key] = QVariant( value );
108 }
109 else
110 {
111 QStringList list;
112
113 for ( QDomElement itemElement = propElement.firstChildElement( u"value"_s );
114 !itemElement.isNull();
115 itemElement = itemElement.nextSiblingElement( u"value"_s ) )
116 {
117 list << itemElement.text();
118 }
119
120 mMap[key] = QVariant( list );
121 }
122 }
123 }
124 }
125}
126
127void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &doc ) const
128{
129 //remove already existing <customproperties> tags
130 const QDomNodeList propertyList = parentNode.toElement().elementsByTagName( u"customproperties"_s );
131 for ( int i = 0; i < propertyList.size(); ++i )
132 {
133 parentNode.removeChild( propertyList.at( i ) );
134 }
135
136 QDomElement propsElement = doc.createElement( u"customproperties"_s );
137 propsElement.appendChild( QgsXmlUtils::writeVariant( mMap, doc ) );
138 parentNode.appendChild( propsElement );
139}
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.