QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgspropertycollection.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspropertycollection.h
3  -----------------------
4  Date : January 2017
5  Copyright : (C) 2017 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSPROPERTYCOLLECTION_H
16 #define QGSPROPERTYCOLLECTION_H
17 
18 #include "qgis_core.h"
19 #include "qgis_sip.h"
20 #include <QString>
21 #include <QVariant>
22 #include <QColor>
23 #include "qgsexpressioncontext.h"
24 #include "qgsproperty.h"
25 
26 class QDomElement;
27 class QDomDocument;
28 
30 typedef QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition;
31 
40 {
41 
42 #ifdef SIP_RUN
44  if ( dynamic_cast<QgsPropertyCollection *>( sipCpp ) )
45  sipType = sipType_QgsPropertyCollection;
46  else if ( dynamic_cast<QgsPropertyCollectionStack *>( sipCpp ) )
47  sipType = sipType_QgsPropertyCollectionStack;
48  else
49  sipType = sipType_QgsAbstractPropertyCollection;
50  SIP_END
51 #endif
52 
53  public:
54 
59  QgsAbstractPropertyCollection( const QString &name = QString() );
60 
61  virtual ~QgsAbstractPropertyCollection() = default;
62 
67  QString name() const { return mName; }
68 
73  void setName( const QString &name ) { mName = name; }
74 
78  virtual QSet<int> propertyKeys() const = 0;
79 
83  virtual void clear() = 0;
84 
91  virtual bool hasProperty( int key ) const = 0;
92 
100  virtual QgsProperty property( int key ) const = 0;
101 
116  virtual QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const = 0;
117 
132  QString valueAsString( int key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
133 
148  QColor valueAsColor( int key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
149 
164  double valueAsDouble( int key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
165 
180  int valueAsInt( int key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
181 
196  bool valueAsBool( int key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
197 
203  virtual bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const = 0;
204 
209  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const = 0;
210 
216  virtual bool isActive( int key ) const = 0;
217 
223  virtual bool hasActiveProperties() const = 0;
224 
230  virtual bool hasDynamicProperties() const = 0;
231 
238  virtual bool writeXml( QDomElement &collectionElem, const QgsPropertiesDefinition &definitions ) const;
239 
246  virtual bool readXml( const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions );
247 
254  virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
255 
262  virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
263 
264  private:
265 
266  QString mName;
267 };
268 
289 {
290  public:
291 
296  QgsPropertyCollection( const QString &name = QString() );
297 
302 
303  QgsPropertyCollection &operator=( const QgsPropertyCollection &other );
304 
308  int count() const;
309 
310  QSet<int> propertyKeys() const override;
311  void clear() override;
312  bool hasProperty( int key ) const override;
313  QgsProperty property( int key ) const override SIP_SKIP;
314 
322  virtual QgsProperty &property( int key );
323 
324  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
325  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
326  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
327  bool isActive( int key ) const override;
328  bool hasActiveProperties() const override;
329  bool hasDynamicProperties() const override;
330 
331  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
332  bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) override;
333 
342  void setProperty( int key, const QgsProperty &property );
343 
351  void setProperty( int key, const QVariant &value );
352 
353  private:
354 
355  QHash<int, QgsProperty> mProperties;
356 
357  mutable bool mDirty = false;
358  mutable bool mHasActiveProperties = false;
359  mutable bool mHasDynamicProperties = false;
360  mutable int mCount = 0;
361 
363  void rescan() const;
364 };
365 
366 
376 {
377  public:
378 
382  QgsPropertyCollectionStack() = default;
383 
384  ~QgsPropertyCollectionStack() override;
385 
388 
389  QgsPropertyCollectionStack &operator=( const QgsPropertyCollectionStack &other );
390 
394  int count() const;
395 
399  void clear() override;
400 
406  void appendCollection( QgsPropertyCollection *collection SIP_TRANSFER );
407 
413  QgsPropertyCollection *at( int index );
414 
421  const QgsPropertyCollection *at( int index ) const SIP_SKIP;
422 
428  QgsPropertyCollection *collection( const QString &name );
429 
436  bool hasActiveProperties() const override;
437 
443  bool hasDynamicProperties() const override;
444 
451  bool isActive( int key ) const override;
452 
460  QgsProperty property( int key ) const override;
461 
471  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
472 
477  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
478  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
479 
480  QSet<int> propertyKeys() const override;
481  bool hasProperty( int key ) const override;
482 
483  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
484 
485  bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
486 
487  private:
488 
489  QList< QgsPropertyCollection * > mStack;
490 
491 };
492 
493 #endif // QGSPROPERTYCOLLECTION_H
virtual bool hasActiveProperties() const =0
Returns true if the collection has any active properties, or false if all properties within the colle...
virtual void clear()=0
Removes all properties from the collection.
virtual bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions)=0
Loads this property collection from a QVariantMap, wrapped in a QVariant.
virtual QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext()) const =0
Returns the set of any fields referenced by the active properties from the collection.
virtual bool isActive(int key) const =0
Returns true if the collection contains an active property with the specified key.
void setName(const QString &name)
Sets the descriptive name for the property collection.
virtual QSet< int > propertyKeys() const =0
Returns a list of property keys contained within the collection.
virtual bool hasProperty(int key) const =0
Returns true if the collection contains a property with the specified key.
virtual bool hasDynamicProperties() const =0
Returns true if the collection has any active, non-static properties, or false if either all non-stat...
An ordered stack of QgsPropertyCollection containers, where collections added later to the stack will...
virtual bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const =0
Prepares the collection against a specified expression context.
#define SIP_SKIP
Definition: qgis_sip.h:119
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for QgsPropertyCollection like objects.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:182
virtual QgsProperty property(int key) const =0
Returns a matching property from the collection, if one exists.
A store for object properties.
Definition: qgsproperty.h:229
QString name() const
Returns the descriptive name of the property collection.
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
#define SIP_OUT
Definition: qgis_sip.h:51
virtual QVariant value(int key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const =0
Returns the calculated value of the property with the specified key from within the collection...
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
virtual QVariant toVariant(const QgsPropertiesDefinition &definitions) const =0
Saves this property collection to a QVariantMap, wrapped in a QVariant.