QGIS API Documentation 4.1.0-Master (d6fb7a379fb)
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#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;
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
136 QDateTime valueAsDateTime( int key, const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok SIP_OUT = nullptr ) const;
137
153 QString valueAsString( int key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok SIP_OUT = nullptr ) const;
154
170 QColor valueAsColor( int key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok SIP_OUT = nullptr ) const;
171
187 double valueAsDouble( int key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok SIP_OUT = nullptr ) const;
188
204 int valueAsInt( int key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok SIP_OUT = nullptr ) const;
205
221 bool valueAsBool( int key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok SIP_OUT = nullptr ) const;
222
228 virtual bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const = 0;
229
237 virtual QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const = 0;
238
243 virtual QSet<QString> referencedVariables() const = 0;
244
250 virtual bool isActive( int key ) const = 0;
251
257 virtual bool hasActiveProperties() const = 0;
258
264 virtual bool hasDynamicProperties() const = 0;
265
272 virtual bool writeXml( QDomElement &collectionElem, const QgsPropertiesDefinition &definitions ) const;
273
280 virtual bool readXml( const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions );
281
288 virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
289
296 virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
297
299 template<class T> QDateTime valueAsDateTime( const T &key, const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok = nullptr ) const SIP_SKIP
300 {
301 return valueAsDateTime( static_cast< int >( key ), context, defaultDateTime, ok );
302 }
303 template<class T> QString valueAsString( const T &key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok = nullptr ) const SIP_SKIP
304 {
305 return valueAsString( static_cast< int >( key ), context, defaultString, ok );
306 }
307 template<class T> QColor valueAsColor( const T &key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok = nullptr ) const SIP_SKIP
308 {
309 return valueAsColor( static_cast< int >( key ), context, defaultColor, ok );
310 }
311 template<class T> double valueAsDouble( const T &key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok = nullptr ) const SIP_SKIP
312 {
313 return valueAsDouble( static_cast< int >( key ), context, defaultValue, ok );
314 }
315 template<class T> int valueAsInt( const T &key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok = nullptr ) const SIP_SKIP
316 {
317 return valueAsInt( static_cast< int >( key ), context, defaultValue, ok );
318 }
319 template<class T> bool valueAsBool( const T &key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok = nullptr ) const SIP_SKIP
320 {
321 return valueAsBool( static_cast< int >( key ), context, defaultValue, ok );
322 }
324
325 private:
326 QString mName;
327};
328
346
348{
349 public:
354 QgsPropertyCollection( const QString &name = QString() );
355
357
359
360 bool operator==( const QgsPropertyCollection &other ) const;
361 bool operator!=( const QgsPropertyCollection &other ) const;
362
366 int count() const;
367
368 QSet<int> propertyKeys() const final;
369 void clear() final;
370 bool hasProperty( int key ) const final;
371
377 template< class T> bool hasProperty( T key ) const SIP_SKIP { return hasProperty( static_cast< int >( key ) ); }
378
379 QgsProperty property( int key ) const final SIP_SKIP;
380
385 template< class T> QgsProperty property( T key ) const SIP_SKIP { return property( static_cast< int >( key ) ); }
386
394 virtual QgsProperty &property( int key );
395
397 template< class T> QgsProperty &property( T key ) SIP_SKIP { return property( static_cast< int >( key ) ); }
399
400 QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const final;
401
406 template< class T> QVariant value( T key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const SIP_SKIP
407 {
408 return value( static_cast< int >( key ), context, defaultValue );
409 }
410
411 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const final;
412 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const final;
413
418 QSet<QString> referencedVariables() const final;
419
420 bool isActive( int key ) const final;
421
427 template< class T> bool isActive( T key ) const SIP_SKIP { return isActive( static_cast< int >( key ) ); }
428
429 bool hasActiveProperties() const final;
430 bool hasDynamicProperties() const final;
431
432 QVariant toVariant( const QgsPropertiesDefinition &definitions ) const final;
433 bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) final;
434
443 void setProperty( int key, const QgsProperty &property );
444
455 template< class T> void setProperty( T key, const QgsProperty &property ) SIP_SKIP { setProperty( static_cast< int >( key ), property ); }
456
464 void setProperty( int key, const QVariant &value );
465
475 template< class T> void setProperty( T key, const QVariant &value ) SIP_SKIP { setProperty( static_cast< int >( key ), value ); }
476
477 private:
478 QHash<int, QgsProperty> mProperties;
479
480 mutable bool mDirty = false;
481 mutable bool mHasActiveProperties = false;
482 mutable bool mHasDynamicProperties = false;
483 mutable int mCount = 0;
484
486 void rescan() const;
487};
488
489
496
498{
499 public:
501
503
505
507
511 int count() const;
512
516 void clear() final;
517
524
530 QgsPropertyCollection *at( int index );
531
538 const QgsPropertyCollection *at( int index ) const SIP_SKIP;
539
545 QgsPropertyCollection *collection( const QString &name );
546
553 bool hasActiveProperties() const override;
554
560 bool hasDynamicProperties() const override;
561
568 bool isActive( int key ) const override;
569
577 QgsProperty property( int key ) const override;
578
588 QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
589
597 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
598
603 QSet<QString> referencedVariables() const override;
604
605 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
606
607 QSet<int> propertyKeys() const override;
608 bool hasProperty( int key ) const override;
609
610 QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
611
612 bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
613
614 private:
615 QList< QgsPropertyCollection * > mStack;
616};
617
618#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 QSet< QString > referencedVariables() const =0
Returns the set of any variables referenced by the active expression properties in 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.
QSet< QString > referencedVariables() const override
Returns the set of any variables referenced by the active expression properties in 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.
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:198
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_END
Definition qgis_sip.h:215
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.