QGIS API Documentation  3.21.0-Master (909859188c)
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 
50 class CORE_EXPORT QgsProjectProperty
51 {
52  public:
54  virtual ~QgsProjectProperty() = default;
55 
61  virtual void dump( int tabs = 0 ) const = 0;
62 
68  virtual bool isKey() const = 0;
69 
75  virtual bool isValue() const = 0;
76 
85  virtual bool isLeaf() const = 0;
86 
92  virtual bool readXml( const QDomNode &keyNode ) = 0;
93 
103  virtual bool writeXml( const QString &nodeName,
104  QDomElement &element,
105  QDomDocument &document ) = 0;
106 
117  virtual QVariant value() const = 0;
118 
119 };
120 
121 
128 class CORE_EXPORT QgsProjectPropertyValue : public QgsProjectProperty
129 {
130  public:
131 
134 
138  QgsProjectPropertyValue( const QVariant &value )
139  : mValue( value )
140  {}
141 
142  bool isKey() const override { return false; }
143  bool isValue() const override { return true; }
144  QVariant value() const override { return mValue; }
145 
146  //value nodes can also be qualified as leaf nodes even though we only count key nodes.
147  bool isLeaf() const override { return true; }
148 
149  void dump( int tabs = 0 ) const override;
150  bool readXml( const QDomNode &keyNode ) override;
151  bool writeXml( const QString &nodeName,
152  QDomElement &element,
153  QDomDocument &document ) override;
154 
155  private:
156 
157  // We use QVariant as it's very handy to keep multiple types and provides type conversions
158  QVariant mValue;
159 
160 };
161 
162 
183 class CORE_EXPORT QgsProjectPropertyKey : public QgsProjectProperty
184 {
185  Q_DECLARE_TR_FUNCTIONS( QgsProjectPropertyKey )
186 
187  public:
188 
192  QgsProjectPropertyKey( const QString &name = QString() );
193  ~QgsProjectPropertyKey() override;
194 
199  QString name() const { return mName; }
200 
207  void setName( const QString &name );
208 
213  QVariant value() const override;
214 
218  QgsProjectPropertyKey *addKey( const QString &keyName )
219  {
220  if ( mProperties.contains( keyName ) )
221  delete mProperties.take( keyName );
222 
223  QgsProjectPropertyKey *p = new QgsProjectPropertyKey( keyName );
224  mProperties.insert( keyName, p );
225 
226  return p;
227  }
228 
232  void removeKey( const QString &keyName )
233  {
234  delete mProperties.take( keyName );
235  }
236 
243  QgsProjectPropertyValue *setValue( const QString &name, const QVariant &value )
244  {
245  if ( mProperties.contains( name ) )
246  delete mProperties.take( name );
247 
249  mProperties.insert( name, p );
250 
251  return p;
252  }
253 
260  QgsProjectPropertyValue *setValue( const QVariant &value )
261  {
262  return setValue( name(), value );
263  }
264 
265  void dump( int tabs = 0 ) const override;
266  bool readXml( const QDomNode &keyNode ) override;
267  bool writeXml( const QString &nodeName, QDomElement &element, QDomDocument &document ) override;
268 
272  int count() const { return mProperties.count(); }
273 
277  bool isEmpty() const { return mProperties.isEmpty(); }
278 
279  bool isKey() const override { return true; }
280  bool isValue() const override { return false; }
281  bool isLeaf() const override;
282 
287  void entryList( QStringList &entries ) const;
288 
293  void subkeyList( QStringList &entries ) const;
294 
298  virtual void clear()
299  {
300  mName.clear();
301  clearKeys();
302  }
303 
307  virtual void clearKeys()
308  {
309  qDeleteAll( mProperties );
310  mProperties.clear();
311  }
312 
316  QgsProjectProperty *find( const QString &propertyName ) const
317  {
318  return mProperties.value( propertyName );
319  }
320 
321  private:
322 
324  QString mName;
325 
327  QHash < QString, QgsProjectProperty * > mProperties;
328 
329 };
330 
331 #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