QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QDomNode>
21 #include <QStringList>
22 
23 
25 {
26 }
27 
29 {
30  return mMap.keys();
31 }
32 
34 {
35  mMap[key] = value;
36 }
37 
38 QVariant QgsObjectCustomProperties::value( const QString& key, const QVariant& defaultValue ) const
39 {
40  return mMap.value( key, defaultValue );
41 }
42 
44 {
45  mMap.remove( key );
46 }
47 
48 
49 void QgsObjectCustomProperties::readXml( const QDomNode& parentNode, const QString& keyStartsWith )
50 {
51  QDomNode propsNode = parentNode.namedItem( "customproperties" );
52  if ( propsNode.isNull() ) // no properties stored...
53  return;
54 
55  if ( !keyStartsWith.isEmpty() )
56  {
57  //remove old keys
58  QStringList keysToRemove;
60  for ( ; pIt != mMap.constEnd(); ++pIt )
61  {
62  if ( pIt.key().startsWith( keyStartsWith ) )
63  {
64  keysToRemove.push_back( pIt.key() );
65  }
66  }
67 
68  QStringList::const_iterator sIt = keysToRemove.constBegin();
69  for ( ; sIt != keysToRemove.constEnd(); ++sIt )
70  {
71  mMap.remove( *sIt );
72  }
73  }
74  else
75  {
76  mMap.clear();
77  }
78 
79  QDomNodeList nodes = propsNode.childNodes();
80 
81  for ( int i = 0; i < nodes.size(); i++ )
82  {
83  QDomNode propNode = nodes.at( i );
84  if ( propNode.isNull() || propNode.nodeName() != "property" )
85  continue;
86  QDomElement propElement = propNode.toElement();
87 
88  QString key = propElement.attribute( "key" );
89  if ( key.isEmpty() || key.startsWith( keyStartsWith ) )
90  {
91  if ( propElement.hasAttribute( "value" ) )
92  {
93  QString value = propElement.attribute( "value" );
94  mMap[key] = QVariant( value );
95  }
96  else
97  {
98  QStringList list;
99 
100  for ( QDomElement itemElement = propElement.firstChildElement( "value" );
101  !itemElement.isNull();
102  itemElement = itemElement.nextSiblingElement( "value" ) )
103  {
104  list << itemElement.text();
105  }
106 
107  mMap[key] = QVariant( list );
108  }
109  }
110  }
111 
112 }
113 
115 {
116  //remove already existing <customproperties> tags
117  QDomNodeList propertyList = parentNode.toElement().elementsByTagName( "customproperties" );
118  for ( int i = 0; i < propertyList.size(); ++i )
119  {
120  parentNode.removeChild( propertyList.at( i ) );
121  }
122 
123  QDomElement propsElement = doc.createElement( "customproperties" );
124 
126  {
127  QDomElement propElement = doc.createElement( "property" );
128  propElement.setAttribute( "key", it.key() );
129  if ( it.value().canConvert<QString>() )
130  {
131  propElement.setAttribute( "value", it.value().toString() );
132  }
133  else if ( it.value().canConvert<QStringList>() )
134  {
135  Q_FOREACH ( const QString& value, it.value().toStringList() )
136  {
137  QDomElement itemElement = doc.createElement( "value" );
138  itemElement.appendChild( doc.createTextNode( value ) );
139  propElement.appendChild( itemElement );
140  }
141  }
142  propsElement.appendChild( propElement );
143  }
144 
145  parentNode.appendChild( propsElement );
146 }
QDomNodeList elementsByTagName(const QString &tagname) const
QDomNode appendChild(const QDomNode &newChild)
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
void push_back(const T &value)
QString attribute(const QString &name, const QString &defValue) const
const_iterator constBegin() const
T value() const
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Return 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 clear()
QDomNodeList childNodes() const
void remove(const QString &key)
Remove a key (entry) from the store.
QDomElement toElement() const
QList< Key > keys() const
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.
bool hasAttribute(const QString &name) const
void setAttribute(const QString &name, const QString &value)
QString nodeName() const
bool isEmpty() const
const_iterator constEnd() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QStringList keys() const
Return list of stored keys.
QDomText createTextNode(const QString &value)
QDomNode removeChild(const QDomNode &oldChild)
QDomNode namedItem(const QString &name) const
bool isNull() const
const Key key(const T &value) const
QDomElement firstChildElement(const QString &tagName) const
int size() const
const_iterator constEnd() const
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
QDomNode at(int index) const
const T value(const Key &key) const
int remove(const Key &key)