QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsprojectproperty.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsproject.h
3 
4  Implements persistent project state.
5 
6  -------------------
7  begin : February 24, 2005
8  copyright : (C) 2005 by Mark Coletti
9  email : mcoletti at gmail.com
10 ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 
22 #ifndef QGSPROJECTPROPERTY_H
23 #define QGSPROJECTPROPERTY_H
24 
25 #include <QHash>
26 #include <QVariant>
27 #include <QStringList>
28 
29 #include "qgis_core.h"
30 
31 class QDomNode;
32 class QDomElement;
33 class QDomDocument;
34 
35 
49 class CORE_EXPORT QgsProjectProperty
50 {
51  public:
53  virtual ~QgsProjectProperty() = default;
54 
60  virtual void dump( int tabs = 0 ) const = 0;
61 
67  virtual bool isKey() const = 0;
68 
74  virtual bool isValue() const = 0;
75 
84  virtual bool isLeaf() const = 0;
85 
91  virtual bool readXml( const QDomNode &keyNode ) = 0;
92 
102  virtual bool writeXml( const QString &nodeName,
103  QDomElement &element,
104  QDomDocument &document ) = 0;
105 
116  virtual QVariant value() const = 0;
117 
118 };
119 
120 
127 class CORE_EXPORT QgsProjectPropertyValue : public QgsProjectProperty
128 {
129  public:
130 
132  QgsProjectPropertyValue() = default;
133 
137  QgsProjectPropertyValue( const QVariant &value )
138  : mValue( value )
139  {}
140 
141  bool isKey() const override { return false; }
142  bool isValue() const override { return true; }
143  QVariant value() const override { return mValue; }
144 
145  //value nodes can also be qualified as leaf nodes even though we only count key nodes.
146  bool isLeaf() const override { return true; }
147 
148  void dump( int tabs = 0 ) const override;
149  bool readXml( const QDomNode &keyNode ) override;
150  bool writeXml( const QString &nodeName,
151  QDomElement &element,
152  QDomDocument &document ) override;
153 
154  private:
155 
156  // We use QVariant as it's very handy to keep multiple types and provides type conversions
157  QVariant mValue;
158 
159 };
160 
161 
182 class CORE_EXPORT QgsProjectPropertyKey : public QgsProjectProperty
183 {
184  public:
185 
189  QgsProjectPropertyKey( const QString &name = QString() );
190  ~QgsProjectPropertyKey() override;
191 
196  QString name() const { return mName; }
197 
204  void setName( const QString &name );
205 
210  QVariant value() const override;
211 
215  QgsProjectPropertyKey *addKey( const QString &keyName )
216  {
217  if ( mProperties.contains( keyName ) )
218  delete mProperties.take( keyName );
219 
220  QgsProjectPropertyKey *p = new QgsProjectPropertyKey( keyName );
221  mProperties.insert( keyName, p );
222 
223  return p;
224  }
225 
229  void removeKey( const QString &keyName )
230  {
231  delete mProperties.take( keyName );
232  }
233 
240  QgsProjectPropertyValue *setValue( const QString &name, const QVariant &value )
241  {
242  if ( mProperties.contains( name ) )
243  delete mProperties.take( name );
244 
246  mProperties.insert( name, p );
247 
248  return p;
249  }
250 
257  QgsProjectPropertyValue *setValue( const QVariant &value )
258  {
259  return setValue( name(), value );
260  }
261 
262  void dump( int tabs = 0 ) const override;
263  bool readXml( const QDomNode &keyNode ) override;
264  bool writeXml( const QString &nodeName, QDomElement &element, QDomDocument &document ) override;
265 
269  int count() const { return mProperties.count(); }
270 
274  bool isEmpty() const { return mProperties.isEmpty(); }
275 
276  bool isKey() const override { return true; }
277  bool isValue() const override { return false; }
278  bool isLeaf() const override;
279 
284  void entryList( QStringList &entries ) const;
285 
290  void subkeyList( QStringList &entries ) const;
291 
295  virtual void clear()
296  {
297  mName.clear();
298  clearKeys();
299  }
300 
304  virtual void clearKeys()
305  {
306  qDeleteAll( mProperties );
307  mProperties.clear();
308  }
309 
313  QgsProjectProperty *find( const QString &propertyName ) const
314  {
315  return mProperties.value( propertyName );
316  }
317 
318  private:
319 
321  QString mName;
322 
324  QHash < QString, QgsProjectProperty * > mProperties;
325 
326 };
327 
328 #endif
bool isKey() const override
Returns true if the property is a QgsProjectPropertyKey.
QVariant value() const override
Returns the node&#39;s value.
virtual void clear()
Resets the property to a default, empty state.
virtual QVariant value() const =0
Returns the node&#39;s value.
QgsProjectPropertyKey * addKey(const QString &keyName)
Adds the specified property key as a sub-key.
virtual bool isLeaf() const =0
Returns true if property is a leaf node.
void removeKey(const QString &keyName)
Removes the specified key.
Project property value node, contains a QgsProjectPropertyKey&#39;s value.
QgsProjectProperty * find(const QString &propertyName) const
Attempts to find a property with a matching sub-key name.
QgsProjectPropertyValue * setValue(const QString &name, const QVariant &value)
Sets the value associated with this key.
bool isKey() const override
Returns true if the property is a QgsProjectPropertyKey.
QString name() const
The name of the property is used as identifier.
QgsProjectPropertyValue * setValue(const QVariant &value)
Set the value associated with this key.
virtual bool writeXml(const QString &nodeName, QDomElement &element, QDomDocument &document)=0
Writes the property hierarchy to a specified DOM element.
int count() const
Returns the number of sub-keys contained by this property.
virtual void clearKeys()
Deletes any sub-nodes from the property.
virtual bool readXml(const QDomNode &keyNode)=0
Restores the property hierarchy from a specified DOM node.
bool isLeaf() const override
Returns true if property is a leaf node.
bool isValue() const override
Returns true if the property is a QgsProjectPropertyValue.
bool isEmpty() const
Returns true if this property contains no sub-keys.
Project property key node.
bool isValue() const override
Returns true if the property is a QgsProjectPropertyValue.
QgsProjectPropertyValue(const QVariant &value)
Constructor for QgsProjectPropertyValue, initialized to a specified value.
An Abstract Base Class for QGIS project property hierarchys.
virtual void dump(int tabs=0) const =0
Dumps out the keys and values.