QGIS API Documentation  3.0.2-Girona (307d082)
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.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 
281 {
282  public:
283 
288  QgsPropertyCollection( const QString &name = QString() );
289 
294 
295  QgsPropertyCollection &operator=( const QgsPropertyCollection &other );
296 
300  int count() const;
301 
302  QSet<int> propertyKeys() const override;
303  void clear() override;
304  bool hasProperty( int key ) const override;
305  QgsProperty property( int key ) const override SIP_SKIP;
306 
314  virtual QgsProperty &property( int key );
315 
316  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
317  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
318  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
319  bool isActive( int key ) const override;
320  bool hasActiveProperties() const override;
321  bool hasDynamicProperties() const override;
322 
323  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
324  bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) override;
325 
334  void setProperty( int key, const QgsProperty &property );
335 
343  void setProperty( int key, const QVariant &value );
344 
345  private:
346 
347  QHash<int, QgsProperty> mProperties;
348 
349  mutable bool mDirty = false;
350  mutable bool mHasActiveProperties = false;
351  mutable bool mHasDynamicProperties = false;
352  mutable int mCount = 0;
353 
355  void rescan() const;
356 };
357 
358 
368 {
369  public:
370 
374  QgsPropertyCollectionStack() = default;
375 
376  ~QgsPropertyCollectionStack() override;
377 
380 
381  QgsPropertyCollectionStack &operator=( const QgsPropertyCollectionStack &other );
382 
386  int count() const;
387 
391  void clear() override;
392 
398  void appendCollection( QgsPropertyCollection *collection SIP_TRANSFER );
399 
405  QgsPropertyCollection *at( int index );
406 
413  const QgsPropertyCollection *at( int index ) const SIP_SKIP;
414 
420  QgsPropertyCollection *collection( const QString &name );
421 
428  bool hasActiveProperties() const override;
429 
435  bool hasDynamicProperties() const override;
436 
443  bool isActive( int key ) const override;
444 
452  QgsProperty property( int key ) const override;
453 
463  QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
464 
469  QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
470  bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
471 
472  QSet<int> propertyKeys() const override;
473  bool hasProperty( int key ) const override;
474 
475  QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
476 
477  bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
478 
479  private:
480 
481  QList< QgsPropertyCollection * > mStack;
482 
483 };
484 
485 #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:175
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.