QGIS API Documentation  2.99.0-Master (314842d)
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  virtual bool isKey() const override { return false; }
142  virtual 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  virtual ~QgsProjectPropertyKey();
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 
256  QgsProjectPropertyValue *setValue( const QVariant &value )
257  {
258  return setValue( name(), value );
259  }
260 
261  void dump( int tabs = 0 ) const override;
262  bool readXml( const QDomNode &keyNode ) override;
263  bool writeXml( const QString &nodeName, QDomElement &element, QDomDocument &document ) override;
264 
268  int count() const { return mProperties.count(); }
269 
273  bool isEmpty() const { return mProperties.isEmpty(); }
274 
275  virtual bool isKey() const override { return true; }
276  virtual bool isValue() const override { return false; }
277  bool isLeaf() const override;
278 
283  void entryList( QStringList &entries ) const;
284 
289  void subkeyList( QStringList &entries ) const;
290 
294  virtual void clear()
295  {
296  mName.clear();
297  clearKeys();
298  }
299 
303  virtual void clearKeys()
304  {
305  qDeleteAll( mProperties );
306  mProperties.clear();
307  }
308 
312  QgsProjectProperty *find( const QString &propertyName ) const
313  {
314  return mProperties.value( propertyName );
315  }
316 
317  private:
318 
320  QString mName;
321 
323  QHash < QString, QgsProjectProperty * > mProperties;
324 
325 };
326 
327 #endif
QVariant value() const override
Returns the node&#39;s value.
virtual void clear()
Resets the property to a default, empty state.
virtual bool isValue() const override
Returns true if the property is a QgsProjectPropertyValue.
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.
virtual bool isKey() const override
Returns true if the property is a QgsProjectPropertyKey.
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.
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 isEmpty() const
Returns true if this property contains no sub-keys.
Project property key node.
virtual bool isKey() const override
Returns true if the property is a QgsProjectPropertyKey.
QgsProjectPropertyValue(const QVariant &value)
Constructor for QgsProjectPropertyValue, initialized to a specified value.
An Abstract Base Class for QGIS project property hierarchys.
virtual bool isValue() const override
Returns true if the property is a QgsProjectPropertyValue.
virtual void dump(int tabs=0) const =0
Dumps out the keys and values.