QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
118  virtual QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const = 0;
119 
134  QString valueAsString( int key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
135 
150  QColor valueAsColor( int key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
151 
166  double valueAsDouble( int key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
167 
182  int valueAsInt( int key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
183 
198  bool valueAsBool( int key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
199 
205  virtual bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const = 0;
206 
211  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const = 0;
212 
218  virtual bool isActive( int key ) const = 0;
219 
225  virtual bool hasActiveProperties() const = 0;
226 
232  virtual bool hasDynamicProperties() const = 0;
233 
240  virtual bool writeXml( QDomElement &collectionElem, const QgsPropertiesDefinition &definitions ) const;
241 
248  virtual bool readXml( const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions );
249 
256  virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
257 
264  virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
265 
266  private:
267 
268  QString mName;
269 };
270 
291 {
292  public:
293 
298  QgsPropertyCollection( const QString &name = QString() );
299 
304 
305  QgsPropertyCollection &operator=( const QgsPropertyCollection &other );
306 
310  int count() const;
311 
312  QSet<int> propertyKeys() const override;
313  void clear() override;
314  bool hasProperty( int key ) const override;
315  QgsProperty property( int key ) const override SIP_SKIP;
316 
324  virtual QgsProperty &property( int key );
325 
326  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
327  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
328  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
329  bool isActive( int key ) const override;
330  bool hasActiveProperties() const override;
331  bool hasDynamicProperties() const override;
332 
333  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
334  bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) override;
335 
344  void setProperty( int key, const QgsProperty &property );
345 
353  void setProperty( int key, const QVariant &value );
354 
355  private:
356 
357  QHash<int, QgsProperty> mProperties;
358 
359  mutable bool mDirty = false;
360  mutable bool mHasActiveProperties = false;
361  mutable bool mHasDynamicProperties = false;
362  mutable int mCount = 0;
363 
365  void rescan() const;
366 };
367 
368 
378 {
379  public:
380 
384  QgsPropertyCollectionStack() = default;
385 
386  ~QgsPropertyCollectionStack() override;
387 
390 
391  QgsPropertyCollectionStack &operator=( const QgsPropertyCollectionStack &other );
392 
396  int count() const;
397 
401  void clear() override;
402 
408  void appendCollection( QgsPropertyCollection *collection SIP_TRANSFER );
409 
415  QgsPropertyCollection *at( int index );
416 
423  const QgsPropertyCollection *at( int index ) const SIP_SKIP;
424 
430  QgsPropertyCollection *collection( const QString &name );
431 
438  bool hasActiveProperties() const override;
439 
445  bool hasDynamicProperties() const override;
446 
453  bool isActive( int key ) const override;
454 
462  QgsProperty property( int key ) const override;
463 
473  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
474 
479  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
480  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
481 
482  QSet<int> propertyKeys() const override;
483  bool hasProperty( int key ) const override;
484 
485  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
486 
487  bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
488 
489  private:
490 
491  QList< QgsPropertyCollection * > mStack;
492 
493 };
494 
495 #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:126
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:189
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:58
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:172
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.