QGIS API Documentation 3.39.0-Master (3aed037ce22)
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"
20#include <QVariant>
21#include <QHash>
22#include <QString>
23#include <QStringList>
24#include <QSet>
25#include <QPointer>
26
28#include "qgsfeature.h"
29
32class LoadLayerFunction;
33
43{
44 public:
45
50 QgsScopedExpressionFunction( const QString &fnname,
51 int params,
52 const QString &group,
53 const QString &helpText = QString(),
54 bool usesGeometry = false,
55 const QSet<QString> &referencedColumns = QSet<QString>(),
56 bool lazyEval = false,
57 bool handlesNull = false,
58 bool isContextual = true )
59 : QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull, isContextual )
60 , mUsesGeometry( usesGeometry )
61 , mReferencedColumns( referencedColumns )
62 {}
63
68 QgsScopedExpressionFunction( const QString &fnname,
70 const QString &group,
71 const QString &helpText = QString(),
72 bool usesGeometry = false,
73 const QSet<QString> &referencedColumns = QSet<QString>(),
74 bool lazyEval = false,
75 bool handlesNull = false,
76 bool isContextual = true )
77 : QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull, isContextual )
78 , mUsesGeometry( usesGeometry )
79 , mReferencedColumns( referencedColumns )
80 {}
81
82 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override = 0;
83
88
89 bool usesGeometry( const QgsExpressionNodeFunction *node ) const override;
90
91 QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const override;
92
93 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
94
95 private:
96 bool mUsesGeometry;
97 QSet<QString> mReferencedColumns;
98};
99
100
114{
115 public:
116
121 {
122
131 StaticVariable( const QString &name = QString(), const QVariant &value = QVariant(), bool readOnly = false, bool isStatic = false, const QString &description = QString() )
132 : name( name )
133 , value( value )
134 , readOnly( readOnly )
135 , isStatic( isStatic )
136 , description( description )
137 {}
138
140 QString name;
141
143 QVariant value;
144
147
150
152 QString description;
153 };
154
159 QgsExpressionContextScope( const QString &name = QString() );
160
162
163 QgsExpressionContextScope &operator=( const QgsExpressionContextScope &other );
164
166
170 QString name() const { return mName; }
171
179 void setVariable( const QString &name, const QVariant &value, bool isStatic = false );
180
188 void addVariable( const QgsExpressionContextScope::StaticVariable &variable );
189
196 bool removeVariable( const QString &name );
197
205 bool hasVariable( const QString &name ) const;
206
214 QVariant variable( const QString &name ) const;
215
221 QStringList variableNames() const;
222
229 QStringList filteredVariableNames() const;
230
237 bool isReadOnly( const QString &name ) const;
238
244 bool isStatic( const QString &name ) const;
245
251 QString description( const QString &name ) const;
252
256 int variableCount() const { return mVariables.count(); }
257
265 bool hasFunction( const QString &name ) const;
266
275 QgsExpressionFunction *function( const QString &name ) const;
276
282 QStringList functionNames() const;
283
290 void addFunction( const QString &name, QgsScopedExpressionFunction *function SIP_TRANSFER );
291
296 bool hasFeature() const { return mHasFeature; }
297
303 QgsFeature feature() const { return mFeature; }
304
312 void setFeature( const QgsFeature &feature ) { mHasFeature = true; mFeature = feature; }
313
319 void removeFeature() { mHasFeature = false; mFeature = QgsFeature(); }
320
326 bool hasGeometry() const { return mHasGeometry; }
327
334 QgsGeometry geometry() const { return mGeometry; }
335
344 void setGeometry( const QgsGeometry &geometry ) { mHasGeometry = true; mGeometry = geometry; }
345
352 void removeGeometry() { mHasGeometry = false; mGeometry = QgsGeometry(); }
353
359 void setFields( const QgsFields &fields );
360
366 void readXml( const QDomElement &element, const QgsReadWriteContext &context );
367
373 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
374
375
384 QStringList hiddenVariables() const;
385
396 void setHiddenVariables( const QStringList &hiddenVariables );
397
398
409 void addHiddenVariable( const QString &hiddenVariable );
410
420 void removeHiddenVariable( const QString &hiddenVariable );
421
432 void addLayerStore( QgsMapLayerStore *store );
433
440 QList< QgsMapLayerStore * > layerStores() const;
441
442 private:
443 QString mName;
444 QHash<QString, StaticVariable> mVariables;
445 QHash<QString, QgsScopedExpressionFunction * > mFunctions;
446 bool mHasFeature = false;
447 QgsFeature mFeature;
448 bool mHasGeometry = false;
449 QgsGeometry mGeometry;
450 QStringList mHiddenVariables;
451
452 QList< QPointer< QgsMapLayerStore > > mLayerStores;
453};
454
467class CORE_EXPORT QgsExpressionContext
468{
469 public:
470
472
477 explicit QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
478
480
481 QgsExpressionContext &operator=( const QgsExpressionContext &other ) SIP_SKIP;
482
483 QgsExpressionContext &operator=( QgsExpressionContext &&other ) noexcept SIP_SKIP;
484
486
494 bool hasVariable( const QString &name ) const;
495
505 QVariant variable( const QString &name ) const;
506
511 QVariantMap variablesToMap() const;
512
520 bool isHighlightedVariable( const QString &name ) const;
521
528 QStringList highlightedVariables() const;
529
537 void setHighlightedVariables( const QStringList &variableNames );
538
547 bool isHighlightedFunction( const QString &name ) const;
548
560 void setHighlightedFunctions( const QStringList &names );
561
569 QgsExpressionContextScope *activeScopeForVariable( const QString &name );
570
579 const QgsExpressionContextScope *activeScopeForVariable( const QString &name ) const SIP_SKIP;
580
587 QgsExpressionContextScope *scope( int index );
588
593 QgsExpressionContextScope *lastScope();
594
599 QList< QgsExpressionContextScope * > scopes() { return mStack; }
600
606 int indexOfScope( QgsExpressionContextScope *scope ) const;
607
613 int indexOfScope( const QString &scopeName ) const;
614
623 QStringList variableNames() const;
624
631 QStringList filteredVariableNames() const;
632
639 bool isReadOnly( const QString &name ) const;
640
648 QString description( const QString &name ) const;
649
656 bool hasFunction( const QString &name ) const;
657
663 QStringList functionNames() const;
664
673 QgsExpressionFunction *function( const QString &name ) const;
674
678 int scopeCount() const;
679
686 void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
687
694 void appendScopes( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
695
699 QgsExpressionContextScope *popScope();
700
708 QList<QgsExpressionContextScope *> takeScopes() SIP_SKIP;
709
716
724 void setFeature( const QgsFeature &feature );
725
730 bool hasFeature() const;
731
736 QgsFeature feature() const;
737
746 void setGeometry( const QgsGeometry &geometry );
747
753 bool hasGeometry() const;
754
760 QgsGeometry geometry() const;
761
769 void setFields( const QgsFields &fields );
770
775 QgsFields fields() const;
776
782 void setOriginalValueVariable( const QVariant &value );
783
794 void setCachedValue( const QString &key, const QVariant &value ) const;
795
803 bool hasCachedValue( const QString &key ) const;
804
814 QVariant cachedValue( const QString &key ) const;
815
822 void clearCachedValues() const;
823
829 QList< QgsMapLayerStore * > layerStores() const;
830
842 void setLoadedLayerStore( QgsMapLayerStore *store );
843
851 QgsMapLayerStore *loadedLayerStore() const;
852
864 void setFeedback( QgsFeedback *feedback );
865
874 QgsFeedback *feedback() const;
875
877 static const QString EXPR_FIELDS;
879 static const QString EXPR_ORIGINAL_VALUE;
881 static const QString EXPR_SYMBOL_COLOR;
883 static const QString EXPR_SYMBOL_ANGLE;
885 static const QString EXPR_GEOMETRY_PART_COUNT;
887 static const QString EXPR_GEOMETRY_PART_NUM;
888
893 static const QString EXPR_GEOMETRY_RING_NUM;
895 static const QString EXPR_GEOMETRY_POINT_COUNT;
897 static const QString EXPR_GEOMETRY_POINT_NUM;
899 static const QString EXPR_CLUSTER_SIZE;
901 static const QString EXPR_CLUSTER_COLOR;
902
903 private:
904
905 QList< QgsExpressionContextScope * > mStack;
906 QStringList mHighlightedVariables;
907 QStringList mHighlightedFunctions;
908
909 QgsFeedback *mFeedback = nullptr;
910
911 std::unique_ptr< LoadLayerFunction > mLoadLayerFunction;
912 QPointer< QgsMapLayerStore > mDestinationStore;
913
914 // Cache is mutable because we want to be able to add cached values to const contexts
915 mutable QMap< QString, QVariant > mCachedValues;
916
917};
918
919#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.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QList< QgsExpressionContextScope * > scopes()
Returns a list of scopes contained within the stack.
A abstract base class for defining QgsExpression functions.
QList< QgsExpressionFunction::Parameter > ParameterList
List of parameters, used for function definition.
virtual bool isStatic(const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context) const
Will be called during prepare to determine if the function is static.
virtual QSet< QString > referencedColumns(const QgsExpressionNodeFunction *node) const
Returns a set of field names which are required for this function.
virtual bool usesGeometry(const QgsExpressionNodeFunction *node) const
Does this function use a geometry object.
An expression node for expression functions.
Class for 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...
The class is used as a container of context for various read/write operations on other objects.
Expression function for use within a QgsExpressionContextScope.
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.
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:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_FACTORY
Definition qgis_sip.h:76
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.