QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 #include <QCoreApplication>
29 
30 #include "qgis_core.h"
31 
32 class QDomNode;
33 class QDomElement;
34 class QDomDocument;
35 
36 
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 
126 class CORE_EXPORT QgsProjectPropertyValue : public QgsProjectProperty
127 {
128  public:
129 
132 
136  QgsProjectPropertyValue( const QVariant &value )
137  : mValue( value )
138  {}
139 
140  bool isKey() const override { return false; }
141  bool isValue() const override { return true; }
142  QVariant value() const override { return mValue; }
143 
144  //value nodes can also be qualified as leaf nodes even though we only count key nodes.
145  bool isLeaf() const override { return true; }
146 
147  void dump( int tabs = 0 ) const override;
148  bool readXml( const QDomNode &keyNode ) override;
149  bool writeXml( const QString &nodeName,
150  QDomElement &element,
151  QDomDocument &document ) override;
152 
153  private:
154 
155  // We use QVariant as it's very handy to keep multiple types and provides type conversions
156  QVariant mValue;
157 
158 };
159 
160 
180 class CORE_EXPORT QgsProjectPropertyKey : public QgsProjectProperty
181 {
182  Q_DECLARE_TR_FUNCTIONS( QgsProjectPropertyKey )
183 
184  public:
185 
189  QgsProjectPropertyKey( const QString &name = QString() );
190  ~QgsProjectPropertyKey() override;
191 
196  QString name() const { return mName; }
197 
203  void setName( const QString &name );
204 
209  QVariant value() const override;
210 
214  QgsProjectPropertyKey *addKey( const QString &keyName )
215  {
216  if ( mProperties.contains( keyName ) )
217  delete mProperties.take( keyName );
218 
219  QgsProjectPropertyKey *p = new QgsProjectPropertyKey( keyName );
220  mProperties.insert( keyName, p );
221 
222  return p;
223  }
224 
228  void removeKey( const QString &keyName )
229  {
230  delete mProperties.take( keyName );
231  }
232 
239  QgsProjectPropertyValue *setValue( const QString &name, const QVariant &value )
240  {
241  if ( mProperties.contains( name ) )
242  delete mProperties.take( name );
243 
245  mProperties.insert( name, p );
246 
247  return p;
248  }
249 
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  bool isKey() const override { return true; }
276  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
Project property key node.
QgsProjectPropertyValue * setValue(const QVariant &value)
Set the value associated with this key.
QString name() const
The name of the property is used as identifier.
QgsProjectPropertyKey * addKey(const QString &keyName)
Adds the specified property key as a sub-key.
void removeKey(const QString &keyName)
Removes the specified key.
bool isEmpty() const
Returns true if this property contains no sub-keys.
bool isKey() const override
Returns true if the property is a QgsProjectPropertyKey.
virtual void clearKeys()
Deletes any sub-nodes from the property.
virtual void clear()
Resets the property to a default, empty state.
QgsProjectProperty * find(const QString &propertyName) const
Attempts to find a property with a matching sub-key name.
bool isValue() const override
Returns true if the property is a QgsProjectPropertyValue.
int count() const
Returns the number of sub-keys contained by this property.
QgsProjectPropertyValue * setValue(const QString &name, const QVariant &value)
Sets the value associated with this key.
Project property value node, contains a QgsProjectPropertyKey's value.
QgsProjectPropertyValue()=default
Constructor for QgsProjectPropertyValue.
QVariant value() const override
Returns the node's value.
bool isValue() const override
Returns true if the property is a QgsProjectPropertyValue.
QgsProjectPropertyValue(const QVariant &value)
Constructor for QgsProjectPropertyValue, initialized to a specified value.
bool isKey() const override
Returns true if the property is a QgsProjectPropertyKey.
bool isLeaf() const override
Returns true if property is a leaf node.
An Abstract Base Class for QGIS project property hierarchys.
virtual bool isLeaf() const =0
Returns true if property is a leaf node.
virtual void dump(int tabs=0) const =0
Dumps out the keys and values.
virtual bool readXml(const QDomNode &keyNode)=0
Restores the property hierarchy from a specified DOM node.
virtual bool isKey() const =0
Returns true if the property is a QgsProjectPropertyKey.
virtual bool writeXml(const QString &nodeName, QDomElement &element, QDomDocument &document)=0
Writes the property hierarchy to a specified DOM element.
virtual QVariant value() const =0
Returns the node's value.
virtual bool isValue() const =0
Returns true if the property is a QgsProjectPropertyValue.
virtual ~QgsProjectProperty()=default