QGIS API Documentation 3.37.0-Master (684a802617f)
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
357
358 QgsPropertyCollection &operator=( const QgsPropertyCollection &other );
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 { return value( static_cast< int >( key ), context, defaultValue ); }
407
408 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const final;
409 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const final;
410 bool isActive( int key ) const final;
411
417 template< class T> bool isActive( T key ) const SIP_SKIP { return isActive( static_cast< int >( key ) ); }
418
419 bool hasActiveProperties() const final;
420 bool hasDynamicProperties() const final;
421
422 QVariant toVariant( const QgsPropertiesDefinition &definitions ) const final;
423 bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) final;
424
433 void setProperty( int key, const QgsProperty &property );
434
445 template< class T> void setProperty( T key, const QgsProperty &property ) SIP_SKIP { setProperty( static_cast< int >( key ), property ); }
446
454 void setProperty( int key, const QVariant &value );
455
465 template< class T> void setProperty( T key, const QVariant &value ) SIP_SKIP { setProperty( static_cast< int >( key ), value ); }
466
467 private:
468
469 QHash<int, QgsProperty> mProperties;
470
471 mutable bool mDirty = false;
472 mutable bool mHasActiveProperties = false;
473 mutable bool mHasDynamicProperties = false;
474 mutable int mCount = 0;
475
477 void rescan() const;
478};
479
480
489{
490 public:
491
496
498
501
503
507 int count() const;
508
512 void clear() FINAL;
513
519 void appendCollection( QgsPropertyCollection *collection SIP_TRANSFER );
520
526 QgsPropertyCollection *at( int index );
527
534 const QgsPropertyCollection *at( int index ) const SIP_SKIP;
535
541 QgsPropertyCollection *collection( const QString &name );
542
549 bool hasActiveProperties() const override;
550
556 bool hasDynamicProperties() const override;
557
564 bool isActive( int key ) const override;
565
573 QgsProperty property( int key ) const override;
574
584 QVariant value( int key, const QgsExpressionContext &context, const QVariant &defaultValue = QVariant() ) const override;
585
593 QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext(), bool ignoreContext = false ) const override;
594 bool prepare( const QgsExpressionContext &context = QgsExpressionContext() ) const override;
595
596 QSet<int> propertyKeys() const override;
597 bool hasProperty( int key ) const override;
598
599 QVariant toVariant( const QgsPropertiesDefinition &definitions ) const override;
600
601 bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions ) override;
602
603 private:
604
605 QList< QgsPropertyCollection * > mStack;
606
607};
608
609#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
Constructor for QgsPropertyCollectionStack.
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.