22 #include <QStringList> 37 return mMap.value( key, defaultValue );
48 QDomNode propsNode = parentNode.namedItem( QStringLiteral(
"customproperties" ) );
49 if ( propsNode.isNull() )
52 if ( !keyStartsWith.isEmpty() )
55 QStringList keysToRemove;
56 QMap<QString, QVariant>::const_iterator pIt =
mMap.constBegin();
57 for ( ; pIt !=
mMap.constEnd(); ++pIt )
59 if ( pIt.key().startsWith( keyStartsWith ) )
61 keysToRemove.push_back( pIt.key() );
65 QStringList::const_iterator sIt = keysToRemove.constBegin();
66 for ( ; sIt != keysToRemove.constEnd(); ++sIt )
76 QDomNodeList nodes = propsNode.childNodes();
78 for (
int i = 0; i < nodes.size(); i++ )
80 QDomNode propNode = nodes.at( i );
81 if ( propNode.isNull() || propNode.nodeName() != QLatin1String(
"property" ) )
83 QDomElement propElement = propNode.toElement();
85 QString key = propElement.attribute( QStringLiteral(
"key" ) );
86 if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
88 if ( propElement.hasAttribute( QStringLiteral(
"value" ) ) )
90 QString
value = propElement.attribute( QStringLiteral(
"value" ) );
91 mMap[key] = QVariant( value );
97 for ( QDomElement itemElement = propElement.firstChildElement( QStringLiteral(
"value" ) );
98 !itemElement.isNull();
99 itemElement = itemElement.nextSiblingElement( QStringLiteral(
"value" ) ) )
101 list << itemElement.text();
104 mMap[key] = QVariant( list );
114 QDomNodeList propertyList = parentNode.toElement().elementsByTagName( QStringLiteral(
"customproperties" ) );
115 for (
int i = 0; i < propertyList.size(); ++i )
117 parentNode.removeChild( propertyList.at( i ) );
120 QDomElement propsElement = doc.createElement( QStringLiteral(
"customproperties" ) );
124 std::sort(
keys.begin(),
keys.end() );
126 for (
const auto &key : qgis::as_const(
keys ) )
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>() )
133 propElement.setAttribute( QStringLiteral(
"value" ), value.toString() );
135 else if ( value.canConvert<QStringList>() )
137 const auto constToStringList = value.toStringList();
138 for (
const QString &value : constToStringList )
140 QDomElement itemElement = doc.createElement( QStringLiteral(
"value" ) );
141 itemElement.appendChild( doc.createTextNode( value ) );
142 propElement.appendChild( itemElement );
145 propsElement.appendChild( propElement );
148 parentNode.appendChild( propsElement );
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.