QGIS API Documentation 3.43.0-Master (32433f7016e)
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
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 )
60 : QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull, isContextual )
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 )
78 : QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull, isContextual )
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
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 )
138 , description( description )
139 {}
140
142 QString name;
143
145 QVariant value;
146
149
152
154 QString description;
155 };
156
161 QgsExpressionContextScope( const QString &name = QString() );
162
164
165 QgsExpressionContextScope &operator=( const QgsExpressionContextScope &other );
166
168
172 QString name() const { return mName; }
173
181 void setVariable( const QString &name, const QVariant &value, bool isStatic = false );
182
190 void addVariable( const QgsExpressionContextScope::StaticVariable &variable );
191
198 bool removeVariable( const QString &name );
199
207 bool hasVariable( const QString &name ) const;
208
216 QVariant variable( const QString &name ) const;
217
223 QStringList variableNames() const;
224
231 QStringList filteredVariableNames() const;
232
239 bool isReadOnly( const QString &name ) const;
240
246 bool isStatic( const QString &name ) const;
247
253 QString description( const QString &name ) const;
254
258 int variableCount() const { return mVariables.count(); }
259
267 bool hasFunction( const QString &name ) const;
268
277 QgsExpressionFunction *function( const QString &name ) const;
278
284 QStringList functionNames() const;
285
292 void addFunction( const QString &name, QgsScopedExpressionFunction *function SIP_TRANSFER );
293
298 bool hasFeature() const { return mHasFeature; }
299
305 QgsFeature feature() const { return mFeature; }
306
314 void setFeature( const QgsFeature &feature ) { mHasFeature = true; mFeature = feature; }
315
321 void removeFeature() { mHasFeature = false; mFeature = QgsFeature(); }
322
328 bool hasGeometry() const { return mHasGeometry; }
329
336 QgsGeometry geometry() const { return mGeometry; }
337
346 void setGeometry( const QgsGeometry &geometry ) { mHasGeometry = true; mGeometry = geometry; }
347
354 void removeGeometry() { mHasGeometry = false; mGeometry = QgsGeometry(); }
355
361 void setFields( const QgsFields &fields );
362
368 void readXml( const QDomElement &element, const QgsReadWriteContext &context );
369
375 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
376
377
386 QStringList hiddenVariables() const;
387
398 void setHiddenVariables( const QStringList &hiddenVariables );
399
400
411 void addHiddenVariable( const QString &hiddenVariable );
412
422 void removeHiddenVariable( const QString &hiddenVariable );
423
434 void addLayerStore( QgsMapLayerStore *store );
435
442 QList< QgsMapLayerStore * > layerStores() const;
443
444 private:
445 QString mName;
446 QHash<QString, StaticVariable> mVariables;
447 QHash<QString, QgsScopedExpressionFunction * > mFunctions;
448 bool mHasFeature = false;
449 QgsFeature mFeature;
450 bool mHasGeometry = false;
451 QgsGeometry mGeometry;
452 QStringList mHiddenVariables;
453
454 QList< QPointer< QgsMapLayerStore > > mLayerStores;
455};
456
471class CORE_EXPORT QgsExpressionContext
472{
473 public:
474
476
481 explicit QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
482
484
485 QgsExpressionContext &operator=( const QgsExpressionContext &other ) SIP_SKIP;
486
487 QgsExpressionContext &operator=( QgsExpressionContext &&other ) noexcept SIP_SKIP;
488
490
498 bool hasVariable( const QString &name ) const;
499
509 QVariant variable( const QString &name ) const;
510
515 QVariantMap variablesToMap() const;
516
524 bool isHighlightedVariable( const QString &name ) const;
525
532 QStringList highlightedVariables() const;
533
541 void setHighlightedVariables( const QStringList &variableNames );
542
551 bool isHighlightedFunction( const QString &name ) const;
552
564 void setHighlightedFunctions( const QStringList &names );
565
573 QgsExpressionContextScope *activeScopeForVariable( const QString &name );
574
583 const QgsExpressionContextScope *activeScopeForVariable( const QString &name ) const SIP_SKIP;
584
591 QgsExpressionContextScope *scope( int index );
592
597 QgsExpressionContextScope *lastScope();
598
603 QList< QgsExpressionContextScope * > scopes() { return mStack; }
604
610 int indexOfScope( QgsExpressionContextScope *scope ) const;
611
617 int indexOfScope( const QString &scopeName ) const;
618
627 QStringList variableNames() const;
628
635 QStringList filteredVariableNames() const;
636
643 bool isReadOnly( const QString &name ) const;
644
652 QString description( const QString &name ) const;
653
660 bool hasFunction( const QString &name ) const;
661
667 QStringList functionNames() const;
668
677 QgsExpressionFunction *function( const QString &name ) const;
678
682 int scopeCount() const;
683
690 void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
691
698 void appendScopes( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
699
703 QgsExpressionContextScope *popScope();
704
712 QList<QgsExpressionContextScope *> takeScopes() SIP_SKIP;
713
720
728 void setFeature( const QgsFeature &feature );
729
734 bool hasFeature() const;
735
740 QgsFeature feature() const;
741
750 void setGeometry( const QgsGeometry &geometry );
751
757 bool hasGeometry() const;
758
764 QgsGeometry geometry() const;
765
773 void setFields( const QgsFields &fields );
774
779 QgsFields fields() const;
780
786 void setOriginalValueVariable( const QVariant &value );
787
798 void setCachedValue( const QString &key, const QVariant &value ) const;
799
807 bool hasCachedValue( const QString &key ) const;
808
818 QVariant cachedValue( const QString &key ) const;
819
826 void clearCachedValues() const;
827
833 QList< QgsMapLayerStore * > layerStores() const;
834
846 void setLoadedLayerStore( QgsMapLayerStore *store );
847
855 QgsMapLayerStore *loadedLayerStore() const;
856
868 void setFeedback( QgsFeedback *feedback );
869
878 QgsFeedback *feedback() const;
879
890 QString uniqueHash( bool &ok SIP_OUT, const QSet<QString> &variables = QSet<QString>() ) const;
891
893 static const QString EXPR_FIELDS;
895 static const QString EXPR_ORIGINAL_VALUE;
897 static const QString EXPR_SYMBOL_COLOR;
899 static const QString EXPR_SYMBOL_ANGLE;
901 static const QString EXPR_GEOMETRY_PART_COUNT;
903 static const QString EXPR_GEOMETRY_PART_NUM;
904
909 static const QString EXPR_GEOMETRY_RING_NUM;
911 static const QString EXPR_GEOMETRY_POINT_COUNT;
913 static const QString EXPR_GEOMETRY_POINT_NUM;
915 static const QString EXPR_CLUSTER_SIZE;
917 static const QString EXPR_CLUSTER_COLOR;
918
919 private:
920
921 QList< QgsExpressionContextScope * > mStack;
922 QStringList mHighlightedVariables;
923 QStringList mHighlightedFunctions;
924
925 QgsFeedback *mFeedback = nullptr;
926
927 std::unique_ptr< LoadLayerFunction > mLoadLayerFunction;
928 QPointer< QgsMapLayerStore > mDestinationStore;
929
930 // Cache is mutable because we want to be able to add cached values to const contexts
931 mutable QMap< QString, QVariant > mCachedValues;
932
933};
934
935#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.
An 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.
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.
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_OUT
Definition qgis_sip.h:58
#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.