QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
335  bool operator==( const QgsPropertyCollection &other ) const;
336  bool operator!=( const QgsPropertyCollection &other ) const;
337 
341  int count() const;
342 
343  QSet<int> propertyKeys() const override;
344  void clear() override;
345  bool hasProperty( int key ) const override;
346  QgsProperty property( int key ) const override SIP_SKIP;
347 
355  virtual QgsProperty &property( int key );
356 
357  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
358  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
359  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
360  bool isActive( int key ) const override;
361  bool hasActiveProperties() const override;
362  bool hasDynamicProperties() const override;
363 
364  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
365  bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) override;
366 
375  void setProperty( int key, const QgsProperty &property );
376 
384  void setProperty( int key, const QVariant &value );
385 
386  private:
387 
388  QHash<int, QgsProperty> mProperties;
389 
390  mutable bool mDirty = false;
391  mutable bool mHasActiveProperties = false;
392  mutable bool mHasDynamicProperties = false;
393  mutable int mCount = 0;
394 
396  void rescan() const;
397 };
398 
399 
409 {
410  public:
411 
416 
417  ~QgsPropertyCollectionStack() override;
418 
421 
422  QgsPropertyCollectionStack &operator=( const QgsPropertyCollectionStack &other );
423 
427  int count() const;
428 
432  void clear() override;
433 
439  void appendCollection( QgsPropertyCollection *collection SIP_TRANSFER );
440 
446  QgsPropertyCollection *at( int index );
447 
454  const QgsPropertyCollection *at( int index ) const SIP_SKIP;
455 
461  QgsPropertyCollection *collection( const QString &name );
462 
469  bool hasActiveProperties() const override;
470 
476  bool hasDynamicProperties() const override;
477 
484  bool isActive( int key ) const override;
485 
493  QgsProperty property( int key ) const override;
494 
504  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
505 
513  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
514  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
515 
516  QSet<int> propertyKeys() const override;
517  bool hasProperty( int key ) const override;
518 
519  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
520 
521  bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
522 
523  private:
524 
525  QList< QgsPropertyCollection * > mStack;
526 
527 };
528 
529 #endif // QGSPROPERTYCOLLECTION_H
Abstract base class for QgsPropertyCollection like objects.
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.
void setName(const QString &name)
Sets the descriptive name for the property collection.
virtual bool hasProperty(int key) const =0
Returns true if the collection contains a property with the specified key.
virtual bool isActive(int key) const =0
Returns true if the collection contains an active property with the specified key.
virtual QgsProperty property(int key) const =0
Returns a matching property from the collection, if one exists.
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.
virtual void clear()=0
Removes all properties from the collection.
virtual bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const =0
Prepares the collection against a specified expression context.
virtual bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions)=0
Loads this property collection from a QVariantMap, wrapped in a QVariant.
virtual QSet< int > propertyKeys() const =0
Returns a list of property keys contained within the collection.
virtual bool hasActiveProperties() const =0
Returns true if the collection has any active properties, or false if all properties within the colle...
QString name() const
Returns the descriptive name of the property collection.
virtual bool hasDynamicProperties() const =0
Returns true if the collection has any active, non-static properties, or false if either all non-stat...
virtual ~QgsAbstractPropertyCollection()=default
virtual QVariant toVariant(const QgsPropertiesDefinition &definitions) const =0
Saves this property collection to a QVariantMap, wrapped in a QVariant.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An ordered stack of QgsPropertyCollection containers, where collections added later to the stack will...
QgsPropertyCollectionStack()=default
Constructor for QgsPropertyCollectionStack.
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
A store for object properties.
Definition: qgsproperty.h:232
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_END
Definition: qgis_sip.h:194
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.