QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
244 virtual bool isActive( int key ) const = 0;
245
251 virtual bool hasActiveProperties() const = 0;
252
258 virtual bool hasDynamicProperties() const = 0;
259
266 virtual bool writeXml( QDomElement &collectionElem, const QgsPropertiesDefinition &definitions ) const;
267
274 virtual bool readXml( const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions );
275
282 virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
283
290 virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
291
293 template<class T> QDateTime valueAsDateTime( const T &key, const QgsExpressionContext &context, const QDateTime &defaultDateTime = QDateTime(), bool *ok = nullptr ) const SIP_SKIP
294 {
295 return valueAsDateTime( static_cast< int >( key ), context, defaultDateTime, ok );
296 }
297 template<class T> QString valueAsString( const T &key, const QgsExpressionContext &context, const QString &defaultString = QString(), bool *ok = nullptr ) const SIP_SKIP
298 {
299 return valueAsString( static_cast< int >( key ), context, defaultString, ok );
300 }
301 template<class T> QColor valueAsColor( const T &key, const QgsExpressionContext &context, const QColor &defaultColor = QColor(), bool *ok = nullptr ) const SIP_SKIP
302 {
303 return valueAsColor( static_cast< int >( key ), context, defaultColor, ok );
304 }
305 template<class T> double valueAsDouble( const T &key, const QgsExpressionContext &context, double defaultValue = 0.0, bool *ok = nullptr ) const SIP_SKIP
306 {
307 return valueAsDouble( static_cast< int >( key ), context, defaultValue, ok );
308 }
309 template<class T> int valueAsInt( const T &key, const QgsExpressionContext &context, int defaultValue = 0, bool *ok = nullptr ) const SIP_SKIP
310 {
311 return valueAsInt( static_cast< int >( key ), context, defaultValue, ok );
312 }
313 template<class T> bool valueAsBool( const T &key, const QgsExpressionContext &context, bool defaultValue = false, bool *ok = nullptr ) const SIP_SKIP
314 {
315 return valueAsBool( static_cast< int >( key ), context, defaultValue, ok );
316 }
318
319 private:
320 QString mName;
321};
322
340
342{
343 public:
348 QgsPropertyCollection( const QString &name = QString() );
349
351
353
354 bool operator==( const QgsPropertyCollection &other ) const;
355 bool operator!=( const QgsPropertyCollection &other ) const;
356
360 int count() const;
361
362 QSet<int> propertyKeys() const final;
363 void clear() final;
364 bool hasProperty( int key ) const final;
365
371 template< class T> bool hasProperty( T key ) const SIP_SKIP { return hasProperty( static_cast< int >( key ) ); }
372
373 QgsProperty property( int key ) const final SIP_SKIP;
374
379 template< class T> QgsProperty property( T key ) const SIP_SKIP { return property( static_cast< int >( key ) ); }
380
388 virtual QgsProperty &property( int key );
389
391 template< class T> QgsProperty &property( T key ) SIP_SKIP { return property( static_cast< int >( key ) ); }
393
394 QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const final;
395
400 template< class T> QVariant value( T key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const SIP_SKIP
401 {
402 return value( static_cast< int >( key ), context, defaultValue );
403 }
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 QHash<int, QgsProperty> mProperties;
466
467 mutable bool mDirty = false;
468 mutable bool mHasActiveProperties = false;
469 mutable bool mHasDynamicProperties = false;
470 mutable int mCount = 0;
471
473 void rescan() const;
474};
475
476
483
485{
486 public:
488
490
492
494
498 int count() const;
499
503 void clear() final;
504
511
517 QgsPropertyCollection *at( int index );
518
525 const QgsPropertyCollection *at( int index ) const SIP_SKIP;
526
532 QgsPropertyCollection *collection( const QString &name );
533
540 bool hasActiveProperties() const override;
541
547 bool hasDynamicProperties() const override;
548
555 bool isActive( int key ) const override;
556
564 QgsProperty property( int key ) const override;
565
575 QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
576
584 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
585 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
586
587 QSet<int> propertyKeys() const override;
588 bool hasProperty( int key ) const override;
589
590 QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
591
592 bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
593
594 private:
595 QList< QgsPropertyCollection * > mStack;
596};
597
598#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.
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.