QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsexpressioncontext.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressioncontext.h
3 ----------------------
4 Date : April 2015
5 Copyright : (C) 2015 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 QGSEXPRESSIONCONTEXT_H
16#define QGSEXPRESSIONCONTEXT_H
17
18#include "qgis_core.h"
19#include "qgis_sip.h"
21#include "qgsfeature.h"
22
23#include <QHash>
24#include <QPointer>
25#include <QSet>
26#include <QString>
27#include <QStringList>
28#include <QVariant>
29
32class LoadLayerFunction;
33
42
44{
45 public:
46
51 QgsScopedExpressionFunction( const QString &fnname,
52 int params,
53 const QString &group,
54 const QString &helpText = QString(),
55 bool usesGeometry = false,
56 const QSet<QString> &referencedColumns = QSet<QString>(),
57 bool lazyEval = false,
58 bool handlesNull = false,
59 bool isContextual = true )
61 , mUsesGeometry( usesGeometry )
62 , mReferencedColumns( referencedColumns )
63 {}
64
69 QgsScopedExpressionFunction( const QString &fnname,
71 const QString &group,
72 const QString &helpText = QString(),
73 bool usesGeometry = false,
74 const QSet<QString> &referencedColumns = QSet<QString>(),
75 bool lazyEval = false,
76 bool handlesNull = false,
77 bool isContextual = true )
79 , mUsesGeometry( usesGeometry )
80 , mReferencedColumns( referencedColumns )
81 {}
82
83 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override = 0;
84
89
90 bool usesGeometry( const QgsExpressionNodeFunction *node ) const override;
91
92 QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const override;
93
94 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
95
96 private:
97 bool mUsesGeometry;
98 QSet<QString> mReferencedColumns;
99};
100
101
114
116{
117 public:
118
123 {
124
133 StaticVariable( const QString &name = QString(), const QVariant &value = QVariant(), bool readOnly = false, bool isStatic = false, const QString &description = QString() )
134 : name( name )
135 , value( value )
136 , readOnly( readOnly )
137 , isStatic( isStatic )
139 {}
140
142 QString name;
143
145 QVariant value;
146
149
152
154 QString description;
155 };
156
161 QgsExpressionContextScope( const QString &name = QString() );
162
165
168
170
174 QString name() const { return mName; }
175
183 void setVariable( const QString &name, const QVariant &value, bool isStatic = false );
184
192 void addVariable( const QgsExpressionContextScope::StaticVariable &variable );
193
200 bool removeVariable( const QString &name );
201
209 bool hasVariable( const QString &name ) const;
210
218 QVariant variable( const QString &name ) const;
219
225 QStringList variableNames() const;
226
233 QStringList filteredVariableNames() const;
234
241 bool isReadOnly( const QString &name ) const;
242
248 bool isStatic( const QString &name ) const;
249
255 QString description( const QString &name ) const;
256
260 int variableCount() const { return mVariables.count(); }
261
269 bool hasFunction( const QString &name ) const;
270
279 QgsExpressionFunction *function( const QString &name ) const;
280
286 QStringList functionNames() const;
287
294 void addFunction( const QString &name, QgsScopedExpressionFunction *function SIP_TRANSFER );
295
300 bool hasFeature() const { return mHasFeature; }
301
307 QgsFeature feature() const { return mFeature; }
308
316 void setFeature( const QgsFeature &feature ) { mHasFeature = true; mFeature = feature; }
317
323 void removeFeature() { mHasFeature = false; mFeature = QgsFeature(); }
324
330 bool hasGeometry() const { return mHasGeometry; }
331
338 QgsGeometry geometry() const { return mGeometry; }
339
348 void setGeometry( const QgsGeometry &geometry ) { mHasGeometry = true; mGeometry = geometry; }
349
356 void removeGeometry() { mHasGeometry = false; mGeometry = QgsGeometry(); }
357
363 void setFields( const QgsFields &fields );
364
370 void readXml( const QDomElement &element, const QgsReadWriteContext &context );
371
377 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
378
379
388 QStringList hiddenVariables() const;
389
400 void setHiddenVariables( const QStringList &hiddenVariables );
401
402
413 void addHiddenVariable( const QString &hiddenVariable );
414
424 void removeHiddenVariable( const QString &hiddenVariable );
425
436 void addLayerStore( QgsMapLayerStore *store );
437
444 QList< QgsMapLayerStore * > layerStores() const;
445
446 private:
447 QString mName;
448 QHash<QString, StaticVariable> mVariables;
449 QHash<QString, QgsScopedExpressionFunction * > mFunctions;
450 bool mHasFeature = false;
451 QgsFeature mFeature;
452 bool mHasGeometry = false;
453 QgsGeometry mGeometry;
454 QStringList mHiddenVariables;
455
456 QList< QPointer< QgsMapLayerStore > > mLayerStores;
457};
458
473class CORE_EXPORT QgsExpressionContext
474{
475 public:
476
478
483 explicit QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
484
487
489
491
493
501 bool hasVariable( const QString &name ) const;
502
512 QVariant variable( const QString &name ) const;
513
518 QVariantMap variablesToMap() const;
519
527 bool isHighlightedVariable( const QString &name ) const;
528
535 QStringList highlightedVariables() const;
536
544 void setHighlightedVariables( const QStringList &variableNames );
545
554 bool isHighlightedFunction( const QString &name ) const;
555
567 void setHighlightedFunctions( const QStringList &names );
568
577
586 const QgsExpressionContextScope *activeScopeForVariable( const QString &name ) const SIP_SKIP;
587
594 QgsExpressionContextScope *scope( int index );
595
601
606 QList< QgsExpressionContextScope * > scopes() { return mStack; }
607
613 int indexOfScope( QgsExpressionContextScope *scope ) const;
614
620 int indexOfScope( const QString &scopeName ) const;
621
630 QStringList variableNames() const;
631
638 QStringList filteredVariableNames() const;
639
646 bool isReadOnly( const QString &name ) const;
647
655 QString description( const QString &name ) const;
656
663 bool hasFunction( const QString &name ) const;
664
670 QStringList functionNames() const;
671
680 QgsExpressionFunction *function( const QString &name ) const;
681
685 int scopeCount() const;
686
693 void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
694
701 void appendScopes( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
702
706 QgsExpressionContextScope *popScope();
707
715 QList<QgsExpressionContextScope *> takeScopes() SIP_SKIP;
716
723
731 void setFeature( const QgsFeature &feature );
732
737 bool hasFeature() const;
738
743 QgsFeature feature() const;
744
753 void setGeometry( const QgsGeometry &geometry );
754
760 bool hasGeometry() const;
761
767 QgsGeometry geometry() const;
768
776 void setFields( const QgsFields &fields );
777
782 QgsFields fields() const;
783
789 void setOriginalValueVariable( const QVariant &value );
790
801 void setCachedValue( const QString &key, const QVariant &value ) const;
802
810 bool hasCachedValue( const QString &key ) const;
811
821 QVariant cachedValue( const QString &key ) const;
822
829 void clearCachedValues() const;
830
836 QList< QgsMapLayerStore * > layerStores() const;
837
849 void setLoadedLayerStore( QgsMapLayerStore *store );
850
858 QgsMapLayerStore *loadedLayerStore() const;
859
871 void setFeedback( QgsFeedback *feedback );
872
881 QgsFeedback *feedback() const;
882
893 QString uniqueHash( bool &ok SIP_OUT, const QSet<QString> &variables = QSet<QString>() ) const;
894
896 static const QString EXPR_FIELDS;
898 static const QString EXPR_ORIGINAL_VALUE;
900 static const QString EXPR_SYMBOL_COLOR;
902 static const QString EXPR_SYMBOL_ANGLE;
904 static const QString EXPR_GEOMETRY_PART_COUNT;
906 static const QString EXPR_GEOMETRY_PART_NUM;
907
912 static const QString EXPR_GEOMETRY_RING_NUM;
914 static const QString EXPR_GEOMETRY_POINT_COUNT;
916 static const QString EXPR_GEOMETRY_POINT_NUM;
918 static const QString EXPR_CLUSTER_SIZE;
920 static const QString EXPR_CLUSTER_COLOR;
921
922 private:
923
924 QList< QgsExpressionContextScope * > mStack;
925 QStringList mHighlightedVariables;
926 QStringList mHighlightedFunctions;
927
928 QgsFeedback *mFeedback = nullptr;
929
930 std::unique_ptr< LoadLayerFunction > mLoadLayerFunction;
931 QPointer< QgsMapLayerStore > mDestinationStore;
932
933 // Cache is mutable because we want to be able to add cached values to const contexts
934 mutable QMap< QString, QVariant > mCachedValues;
935
936};
937
938#endif // QGSEXPRESSIONCONTEXT_H
Single scope for storing variables and functions for use within a QgsExpressionContext.
void removeGeometry()
Removes any geometry associated with the scope.
void removeFeature()
Removes any feature associated with the scope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
bool hasFeature() const
Returns true if the scope has a feature associated with it.
QgsGeometry geometry() const
Sets the geometry associated with the scope.
QString name() const
Returns the friendly display name of the context scope.
int variableCount() const
Returns the count of variables contained within the scope.
bool hasGeometry() const
Returns true if the scope has a geometry associated with it.
void setGeometry(const QgsGeometry &geometry)
Convenience function for setting a geometry for the scope.
QgsFeature feature() const
Sets the feature associated with the scope.
QgsExpressionContextScope & operator=(const QgsExpressionContextScope &other)
QgsExpressionContextScope(const QString &name=QString())
Constructor for QgsExpressionContextScope.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static const QString EXPR_GEOMETRY_PART_COUNT
Inbuilt variable name for geometry part count variable.
static const QString EXPR_GEOMETRY_POINT_COUNT
Inbuilt variable name for point count variable.
QStringList highlightedVariables() const
Returns the current list of variables highlighted within the context.
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
static const QString EXPR_GEOMETRY_POINT_NUM
Inbuilt variable name for point number variable.
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
bool isHighlightedFunction(const QString &name) const
Returns true if the specified function name is intended to be highlighted to the user.
QgsExpressionContext & operator=(const QgsExpressionContext &other)
static const QString EXPR_FIELDS
Inbuilt variable name for fields storage.
static const QString EXPR_GEOMETRY_RING_NUM
Inbuilt variable name for geometry ring number variable.
bool isHighlightedVariable(const QString &name) const
Returns true if the specified variable name is intended to be highlighted to the user.
QgsExpressionContextScope * activeScopeForVariable(const QString &name)
Returns the currently active scope from the context for a specified variable name.
static const QString EXPR_GEOMETRY_PART_NUM
Inbuilt variable name for geometry part number variable.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
QList< QgsExpressionContextScope * > scopes()
Returns a list of scopes contained within the stack.
bool hasVariable(const QString &name) const
Check whether a variable is specified by any scope within the context.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
static const QString EXPR_CLUSTER_COLOR
Inbuilt variable name for cluster color variable.
QStringList variableNames() const
Returns a list of variables names set by all scopes in the context.
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
QgsExpressionContextScope * scope(int index)
Returns the scope at the specified index within the context.
An abstract base class for defining QgsExpression functions.
QList< QgsExpressionFunction::Parameter > ParameterList
List of parameters, used for function definition.
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object.
int params() const
The number of parameters this function takes.
QgsExpressionFunction(const QString &fnname, int params, const QString &group, const QString &helpText=QString(), bool lazyEval=false, bool handlesNull=false, bool isContextual=false)
Constructor for function which uses unnamed parameters.
virtual bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const
Will be called during prepare to determine if the function is static.
bool lazyEval() const
true if this function should use lazy evaluation.
QString group() const
Returns the first group which the function belongs to.
virtual QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const
Returns a set of field names which are required for this function.
virtual bool handlesNull() const
Returns true if the function handles NULL values in arguments by itself, and the default NULL value h...
const QString helpText() const
The help text for the function.
virtual bool usesGeometry(const QgsExpressionNodeFunction *node) const
Does this function use a geometry object.
An expression node for expression functions.
Handles parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Container of fields for a vector layer.
Definition qgsfields.h:46
A geometry is the spatial representation of a feature.
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
A container for the context for various read/write operations on objects.
Expression function for use within a QgsExpressionContextScope.
QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const override
Returns a set of field names which are required for this function.
virtual QgsScopedExpressionFunction * clone() const =0
Returns a clone of the function.
QgsScopedExpressionFunction(const QString &fnname, int params, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QSet< QString > &referencedColumns=QSet< QString >(), bool lazyEval=false, bool handlesNull=false, bool isContextual=true)
Create a new QgsScopedExpressionFunction.
QgsScopedExpressionFunction(const QString &fnname, const QgsExpressionFunction::ParameterList &params, const QString &group, const QString &helpText=QString(), bool usesGeometry=false, const QSet< QString > &referencedColumns=QSet< QString >(), bool lazyEval=false, bool handlesNull=false, bool isContextual=true)
Create a new QgsScopedExpressionFunction using named parameters.
bool usesGeometry(const QgsExpressionNodeFunction *node) const override
Does this function use a geometry object.
QVariant func(const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node) override=0
Returns result of evaluating the function.
#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_FACTORY
Definition qgis_sip.h:84
Single variable definition for use within a QgsExpressionContextScope.
bool readOnly
True if variable should not be editable by users.
StaticVariable(const QString &name=QString(), const QVariant &value=QVariant(), bool readOnly=false, bool isStatic=false, const QString &description=QString())
Constructor for StaticVariable.
bool isStatic
A static variable can be cached for the lifetime of a context.
QString description
Translated description of variable, for use within expression builder widgets.