QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgsexpressioncontext.h"
21 #include "qgsproperty.h"
22 
23 #include <QString>
24 #include <QVariant>
25 #include <QColor>
26 #include <QDateTime>
27 
28 class QDomElement;
29 class QDomDocument;
30 
32 typedef QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition;
33 
42 {
43 
44 #ifdef SIP_RUN
46  if ( dynamic_cast<QgsPropertyCollection *>( sipCpp ) )
47  sipType = sipType_QgsPropertyCollection;
48  else if ( dynamic_cast<QgsPropertyCollectionStack *>( sipCpp ) )
49  sipType = sipType_QgsPropertyCollectionStack;
50  else
51  sipType = sipType_QgsAbstractPropertyCollection;
52  SIP_END
53 #endif
54 
55  public:
56 
61  QgsAbstractPropertyCollection( const QString &name = QString() );
62 
63  virtual ~QgsAbstractPropertyCollection() = default;
64 
69  QString name() const { return mName; }
70 
75  void setName( const QString &name ) { mName = name; }
76 
80  virtual QSet<int> propertyKeys() const = 0;
81 
85  virtual void clear() = 0;
86 
93  virtual bool hasProperty( int key ) const = 0;
94 
102  virtual QgsProperty property( int key ) const = 0;
103 
120  virtual QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const = 0;
121 
138  QDateTime valueAsDateTime( int key, const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
139 
155  QString valueAsString( int key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
156 
172  QColor valueAsColor( int key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
173 
189  double valueAsDouble( int key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
190 
206  int valueAsInt( int key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
207 
223  bool valueAsBool( int key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
224 
230  virtual bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const = 0;
231 
239  virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const = 0;
240 
246  virtual bool isActive( int key ) const = 0;
247 
253  virtual bool hasActiveProperties() const = 0;
254 
260  virtual bool hasDynamicProperties() const = 0;
261 
268  virtual bool writeXml( QDomElement &collectionElem, const QgsPropertiesDefinition &definitions ) const;
269 
276  virtual bool readXml( const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions );
277 
284  virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
285 
292  virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
293 
294  private:
295 
296  QString mName;
297 };
298 
319 {
320  public:
321 
326  QgsPropertyCollection( const QString &name = QString() );
327 
332 
333  QgsPropertyCollection &operator=( const QgsPropertyCollection &other );
334 
338  int count() const;
339 
340  QSet<int> propertyKeys() const override;
341  void clear() override;
342  bool hasProperty( int key ) const override;
343  QgsProperty property( int key ) const override SIP_SKIP;
344 
352  virtual QgsProperty &property( int key );
353 
354  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
355  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
356  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
357  bool isActive( int key ) const override;
358  bool hasActiveProperties() const override;
359  bool hasDynamicProperties() const override;
360 
361  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
362  bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) override;
363 
372  void setProperty( int key, const QgsProperty &property );
373 
381  void setProperty( int key, const QVariant &value );
382 
383  private:
384 
385  QHash<int, QgsProperty> mProperties;
386 
387  mutable bool mDirty = false;
388  mutable bool mHasActiveProperties = false;
389  mutable bool mHasDynamicProperties = false;
390  mutable int mCount = 0;
391 
393  void rescan() const;
394 };
395 
396 
406 {
407  public:
408 
412  QgsPropertyCollectionStack() = default;
413 
414  ~QgsPropertyCollectionStack() override;
415 
418 
419  QgsPropertyCollectionStack &operator=( const QgsPropertyCollectionStack &other );
420 
424  int count() const;
425 
429  void clear() override;
430 
436  void appendCollection( QgsPropertyCollection *collection SIP_TRANSFER );
437 
443  QgsPropertyCollection *at( int index );
444 
451  const QgsPropertyCollection *at( int index ) const SIP_SKIP;
452 
458  QgsPropertyCollection *collection( const QString &name );
459 
466  bool hasActiveProperties() const override;
467 
473  bool hasDynamicProperties() const override;
474 
481  bool isActive( int key ) const override;
482 
490  QgsProperty property( int key ) const override;
491 
501  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
502 
510  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
511  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
512 
513  QSet<int> propertyKeys() const override;
514  bool hasProperty( int key ) const override;
515 
516  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
517 
518  bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
519 
520  private:
521 
522  QList< QgsPropertyCollection * > mStack;
523 
524 };
525 
526 #endif // QGSPROPERTYCOLLECTION_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsAbstractPropertyCollection::clear
virtual void clear()=0
Removes all properties from the collection.
QgsAbstractPropertyCollection::isActive
virtual bool isActive(int key) const =0
Returns true if the collection contains an active property with the specified key.
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsAbstractPropertyCollection
Abstract base class for QgsPropertyCollection like objects.
Definition: qgspropertycollection.h:41
QgsAbstractPropertyCollection::hasActiveProperties
virtual bool hasActiveProperties() const =0
Returns true if the collection has any active properties, or false if all properties within the colle...
QgsAbstractPropertyCollection::loadVariant
virtual bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions)=0
Loads this property collection from a QVariantMap, wrapped in a QVariant.
QgsAbstractPropertyCollection::setName
void setName(const QString &name)
Sets the descriptive name for the property collection.
Definition: qgspropertycollection.h:75
QgsAbstractPropertyCollection::hasDynamicProperties
virtual bool hasDynamicProperties() const =0
Returns true if the collection has any active, non-static properties, or false if either all non-stat...
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsPropertiesDefinition
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
Definition: qgspropertycollection.h:29
QgsAbstractPropertyCollection::hasProperty
virtual bool hasProperty(int key) const =0
Returns true if the collection contains a property with the specified key.
qgsexpressioncontext.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsAbstractPropertyCollection::name
QString name() const
Returns the descriptive name of the property collection.
Definition: qgspropertycollection.h:69
QgsAbstractPropertyCollection::property
virtual QgsProperty property(int key) const =0
Returns a matching property from the collection, if one exists.
QgsAbstractPropertyCollection::propertyKeys
virtual QSet< int > propertyKeys() const =0
Returns a list of property keys contained within the collection.
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsAbstractPropertyCollection::prepare
virtual bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const =0
Prepares the collection against a specified expression context.
QgsPropertyCollection
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
Definition: qgspropertycollection.h:318
QgsAbstractPropertyCollection::toVariant
virtual QVariant toVariant(const QgsPropertiesDefinition &definitions) const =0
Saves this property collection to a QVariantMap, wrapped in a QVariant.
qgsproperty.h
QgsAbstractPropertyCollection::referencedFields
virtual QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), bool ignoreContext=false) const =0
Returns the set of any fields referenced by the active properties from the collection.
QgsAbstractPropertyCollection::value
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.
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
QgsPropertyCollectionStack
An ordered stack of QgsPropertyCollection containers, where collections added later to the stack will...
Definition: qgspropertycollection.h:405