QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 <QString>
24#include <QVariant>
25#include <QColor>
26#include <QDateTime>
27
28class QDomElement;
29class QDomDocument;
30
32typedef QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition;
33
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
344{
345 public:
346
351 QgsPropertyCollection( const QString &name = QString() );
352
354
355 QgsPropertyCollection &operator=( const QgsPropertyCollection &other );
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
486{
487 public:
488
490
492
494
496
500 int count() const;
501
505 void clear() FINAL;
506
512 void appendCollection( QgsPropertyCollection *collection SIP_TRANSFER );
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
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< 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.
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 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
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
bool hasProperty(T key) const
Returns true if the collection contains a property with the specified key.
void setProperty(T key, const QVariant &value)
Convenience method, creates a QgsStaticProperty and stores it 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.
A store for object properties.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_SKIP
Definition qgis_sip.h:126
#define FINAL
Definition qgis_sip.h:242
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_END
Definition qgis_sip.h:208
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.