QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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"
21#include "qgsproperty.h"
22
23#include <QColor>
24#include <QDateTime>
25#include <QString>
26#include <QVariant>
27
28class QDomElement;
29class QDomDocument;
30
32typedef QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition;
33
39
41{
42
43#ifdef SIP_RUN
45 if ( dynamic_cast<QgsPropertyCollection *>( sipCpp ) )
46 sipType = sipType_QgsPropertyCollection;
47 else if ( dynamic_cast<QgsPropertyCollectionStack *>( sipCpp ) )
48 sipType = sipType_QgsPropertyCollectionStack;
49 else
50 sipType = sipType_QgsAbstractPropertyCollection;
52#endif
53
54 public:
55
60 QgsAbstractPropertyCollection( const QString &name = QString() );
61
62 virtual ~QgsAbstractPropertyCollection() = default;
63
68 QString name() const { return mName; }
69
74 void setName( const QString &name ) { mName = name; }
75
79 virtual QSet<int> propertyKeys() const = 0;
80
84 virtual void clear() = 0;
85
92 virtual bool hasProperty( int key ) const = 0;
93
101 virtual QgsProperty property( int key ) const = 0;
102
119 virtual QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const = 0;
120
137 QDateTime valueAsDateTime( int key, const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
138
154 QString valueAsString( int key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
155
171 QColor valueAsColor( int key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
172
188 double valueAsDouble( int key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
189
205 int valueAsInt( int key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
206
222 bool valueAsBool( int key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
223
229 virtual bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const = 0;
230
238 virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const = 0;
239
245 virtual bool isActive( int key ) const = 0;
246
252 virtual bool hasActiveProperties() const = 0;
253
259 virtual bool hasDynamicProperties() const = 0;
260
267 virtual bool writeXml( QDomElement &collectionElem, const QgsPropertiesDefinition &definitions ) const;
268
275 virtual bool readXml( const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions );
276
283 virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
284
291 virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
292
294 template<class T> QDateTime valueAsDateTime( const T &key, const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok = nullptr ) const SIP_SKIP
295 {
296 return valueAsDateTime( static_cast< int >( key ), context, defaultDateTime, ok );
297 }
298 template<class T> QString valueAsString( const T &key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok = nullptr ) const SIP_SKIP
299 {
300 return valueAsString( static_cast< int >( key ), context, defaultString, ok );
301 }
302 template<class T> QColor valueAsColor( const T &key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok = nullptr ) const SIP_SKIP
303 {
304 return valueAsColor( static_cast< int >( key ), context, defaultColor, ok );
305 }
306 template<class T> double valueAsDouble( const T &key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok = nullptr ) const SIP_SKIP
307 {
308 return valueAsDouble( static_cast< int >( key ), context, defaultValue, ok );
309 }
310 template<class T> int valueAsInt( const T &key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok = nullptr ) const SIP_SKIP
311 {
312 return valueAsInt( static_cast< int >( key ), context, defaultValue, ok );
313 }
314 template<class T> bool valueAsBool( const T &key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok = nullptr ) const SIP_SKIP
315 {
316 return valueAsBool( static_cast< int >( key ), context, defaultValue, ok );
317 }
319
320 private:
321
322 QString mName;
323};
324
342
344{
345 public:
346
351 QgsPropertyCollection( const QString &name = QString() );
352
354
356
357 bool operator==( const QgsPropertyCollection &other ) const;
358 bool operator!=( const QgsPropertyCollection &other ) const;
359
363 int count() const;
364
365 QSet<int> propertyKeys() const final;
366 void clear() final;
367 bool hasProperty( int key ) const final;
368
374 template< class T> bool hasProperty( T key ) const SIP_SKIP { return hasProperty( static_cast< int >( key ) ); }
375
376 QgsProperty property( int key ) const final SIP_SKIP;
377
382 template< class T> QgsProperty property( T key ) const SIP_SKIP { return property( static_cast< int >( key ) ); }
383
391 virtual QgsProperty &property( int key );
392
394 template< class T> QgsProperty &property( T key ) SIP_SKIP { return property( static_cast< int >( key ) ); }
396
397 QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const final;
398
403 template< class T> QVariant value( T key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const SIP_SKIP { return value( static_cast< int >( key ), context, defaultValue ); }
404
405 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const final;
406 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const final;
407 bool isActive( int key ) const final;
408
414 template< class T> bool isActive( T key ) const SIP_SKIP { return isActive( static_cast< int >( key ) ); }
415
416 bool hasActiveProperties() const final;
417 bool hasDynamicProperties() const final;
418
419 QVariant toVariant( const QgsPropertiesDefinition &definitions ) const final;
420 bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) final;
421
430 void setProperty( int key, const QgsProperty &property );
431
442 template< class T> void setProperty( T key, const QgsProperty &property ) SIP_SKIP { setProperty( static_cast< int >( key ), property ); }
443
451 void setProperty( int key, const QVariant &value );
452
462 template< class T> void setProperty( T key, const QVariant &value ) SIP_SKIP { setProperty( static_cast< int >( key ), value ); }
463
464 private:
465
466 QHash<int, QgsProperty> mProperties;
467
468 mutable bool mDirty = false;
469 mutable bool mHasActiveProperties = false;
470 mutable bool mHasDynamicProperties = false;
471 mutable int mCount = 0;
472
474 void rescan() const;
475};
476
477
484
486{
487 public:
488
490
492
494
496
500 int count() const;
501
505 void clear() final;
506
513
519 QgsPropertyCollection *at( int index );
520
527 const QgsPropertyCollection *at( int index ) const SIP_SKIP;
528
534 QgsPropertyCollection *collection( const QString &name );
535
542 bool hasActiveProperties() const override;
543
549 bool hasDynamicProperties() const override;
550
557 bool isActive( int key ) const override;
558
566 QgsProperty property( int key ) const override;
567
577 QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
578
586 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
587 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
588
589 QSet<int> propertyKeys() const override;
590 bool hasProperty( int key ) const override;
591
592 QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
593
594 bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
595
596 private:
597
598 QList< QgsPropertyCollection * > mStack;
599
600};
601
602#endif // QGSPROPERTYCOLLECTION_H
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.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
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.
QDateTime valueAsDateTime(int key, const QgsExpressionContext &context, const QDateTime &defaultDateTime=QDateTime(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a datetime.
int valueAsInt(int key, const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an integer.
virtual QgsProperty property(int key) const =0
Returns a matching property from the collection, if one exists.
bool valueAsBool(int key, const QgsExpressionContext &context, bool defaultValue=false, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an boolean.
virtual QSet< int > propertyKeys() const =0
Returns a list of property keys contained within the collection.
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.
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a double.
virtual bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const =0
Prepares the collection against a specified expression context.
QgsAbstractPropertyCollection(const QString &name=QString())
Constructor for QgsAbstractPropertyCollection.
virtual bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions)=0
Loads this property collection from a QVariantMap, wrapped in a QVariant.
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 readXml(const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions)
Reads property collection state from an XML element.
QString valueAsString(int key, const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a string.
virtual bool hasDynamicProperties() const =0
Returns true if the collection has any active, non-static properties, or false if either all non-stat...
virtual bool writeXml(QDomElement &collectionElem, const QgsPropertiesDefinition &definitions) const
Writes the current state of the property collection into an XML element.
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 & operator=(const QgsPropertyCollectionStack &other)
int count() const
Returns the number of collections contained within the stack.
QVariant value(int key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const override
Returns the calculated value of the highest priority property with the specified key from within the ...
void clear() final
Removes all collections from the stack.
QgsPropertyCollectionStack()=default
bool hasProperty(int key) const override
Returns true if the collection contains a property with the specified key.
void appendCollection(QgsPropertyCollection *collection)
Appends a collection to the end of the stack, and transfers ownership of the collection to the stack.
QgsPropertyCollection * at(int index)
Returns the collection at the corresponding index from the stack.
bool prepare(const QgsExpressionContext &context=QgsExpressionContext()) const override
Prepares the collection against a specified expression context.
bool hasActiveProperties() const override
Returns true if the collection has any active properties, or false if all properties within the colle...
QSet< int > propertyKeys() const override
Returns a list of property keys contained within the collection.
QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), bool ignoreContext=false) const override
Returns the set of any fields referenced by the active properties from the stack.
bool loadVariant(const QVariant &collection, const QgsPropertiesDefinition &definitions) override
Loads this property collection from a QVariantMap, wrapped in a QVariant.
QgsProperty property(int key) const override
Returns the highest priority property with a matching key from within the stack.
bool isActive(int key) const override
Returns true if the stack contains an active property with the specified key.
QgsPropertyCollection * collection(const QString &name)
Returns the first collection with a matching name from the stack.
bool hasDynamicProperties() const override
Returns true if the collection has any active, non-static properties, or false if either all non-stat...
QVariant toVariant(const QgsPropertiesDefinition &definitions) const override
Saves this property collection to a QVariantMap, wrapped in a QVariant.
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
QgsPropertyCollection & operator=(const QgsPropertyCollection &other)
QVariant value(int key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const final
Returns the calculated value of the property with the specified key from within the collection.
QSet< int > propertyKeys() const final
Returns a list of property keys contained within the collection.
bool hasProperty(T key) const
Returns true if the collection contains a property with the specified key.
bool hasProperty(int key) const final
Returns true if the collection contains a property with the specified key.
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
void setProperty(T key, const QVariant &value)
Convenience method, creates a QgsStaticProperty and stores it within the collection.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
void clear() final
Removes all properties from the collection.
int count() const
Returns the number of properties contained within the collection.
QVariant value(T key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const
Returns the value of the property with the specified key.
bool isActive(T key) const
Returns true if the property with the specified key is active.
QgsProperty property(T key) const
Returns the property with the specified key.
void setProperty(T key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
QgsProperty property(int key) const final
Returns a matching property from the collection, if one exists.
QgsPropertyCollection(const QString &name=QString())
Constructor for QgsPropertyCollection.
A store for object properties.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_END
Definition qgis_sip.h:216
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.