QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
33 void QgsObjectCustomProperties::setValue( const QString& key, const QVariant& value )
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 
43 void QgsObjectCustomProperties::remove( const QString& key )
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;
59  QMap<QString, QVariant>::const_iterator pIt = mMap.constBegin();
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 
114 void QgsObjectCustomProperties::writeXml( QDomNode& parentNode, QDomDocument& doc ) const
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 
125  for ( QMap<QString, QVariant>::const_iterator it = mMap.constBegin(); it != mMap.constEnd(); ++it )
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  foreach ( 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 }