21 #include <QStringList> 36 return mMap.value( key, defaultValue );
47 QDomNode propsNode = parentNode.namedItem( QStringLiteral(
"customproperties" ) );
48 if ( propsNode.isNull() )
51 if ( !keyStartsWith.isEmpty() )
54 QStringList keysToRemove;
55 QMap<QString, QVariant>::const_iterator pIt =
mMap.constBegin();
56 for ( ; pIt !=
mMap.constEnd(); ++pIt )
58 if ( pIt.key().startsWith( keyStartsWith ) )
60 keysToRemove.push_back( pIt.key() );
64 QStringList::const_iterator sIt = keysToRemove.constBegin();
65 for ( ; sIt != keysToRemove.constEnd(); ++sIt )
75 QDomNodeList nodes = propsNode.childNodes();
77 for (
int i = 0; i < nodes.size(); i++ )
79 QDomNode propNode = nodes.at( i );
80 if ( propNode.isNull() || propNode.nodeName() != QLatin1String(
"property" ) )
82 QDomElement propElement = propNode.toElement();
84 QString key = propElement.attribute( QStringLiteral(
"key" ) );
85 if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
87 if ( propElement.hasAttribute( QStringLiteral(
"value" ) ) )
89 QString
value = propElement.attribute( QStringLiteral(
"value" ) );
90 mMap[key] = QVariant( value );
96 for ( QDomElement itemElement = propElement.firstChildElement( QStringLiteral(
"value" ) );
97 !itemElement.isNull();
98 itemElement = itemElement.nextSiblingElement( QStringLiteral(
"value" ) ) )
100 list << itemElement.text();
103 mMap[key] = QVariant( list );
113 QDomNodeList propertyList = parentNode.toElement().elementsByTagName( QStringLiteral(
"customproperties" ) );
114 for (
int i = 0; i < propertyList.size(); ++i )
116 parentNode.removeChild( propertyList.at( i ) );
119 QDomElement propsElement = doc.createElement( QStringLiteral(
"customproperties" ) );
121 for ( QMap<QString, QVariant>::const_iterator it =
mMap.constBegin(); it !=
mMap.constEnd(); ++it )
123 QDomElement propElement = doc.createElement( QStringLiteral(
"property" ) );
124 propElement.setAttribute( QStringLiteral(
"key" ), it.key() );
125 if ( it.value().canConvert<QString>() )
127 propElement.setAttribute( QStringLiteral(
"value" ), it.value().toString() );
129 else if ( it.value().canConvert<QStringList>() )
131 Q_FOREACH (
const QString &
value, it.value().toStringList() )
133 QDomElement itemElement = doc.createElement( QStringLiteral(
"value" ) );
134 itemElement.appendChild( doc.createTextNode( value ) );
135 propElement.appendChild( itemElement );
138 propsElement.appendChild( propElement );
141 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.