QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
44{
45 public:
46
52 QgsScopedExpressionFunction( const QString &fnname,
53 int params,
54 const QString &group,
55 const QString &helpText = QString(),
56 bool usesGeometry = false,
57 const QSet<QString> &referencedColumns = QSet<QString>(),
58 bool lazyEval = false,
59 bool handlesNull = false,
60 bool isContextual = true )
61 : QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull, isContextual )
62 , mUsesGeometry( usesGeometry )
63 , mReferencedColumns( referencedColumns )
64 {}
65
71 QgsScopedExpressionFunction( const QString &fnname,
73 const QString &group,
74 const QString &helpText = QString(),
75 bool usesGeometry = false,
76 const QSet<QString> &referencedColumns = QSet<QString>(),
77 bool lazyEval = false,
78 bool handlesNull = false,
79 bool isContextual = true )
80 : QgsExpressionFunction( fnname, params, group, helpText, lazyEval, handlesNull, isContextual )
81 , mUsesGeometry( usesGeometry )
82 , mReferencedColumns( referencedColumns )
83 {}
84
85 QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node ) override = 0;
86
91
92 bool usesGeometry( const QgsExpressionNodeFunction *node ) const override;
93
94 QSet<QString> referencedColumns( const QgsExpressionNodeFunction *node ) const override;
95
96 bool isStatic( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
97
98 private:
99 bool mUsesGeometry;
100 QSet<QString> mReferencedColumns;
101};
102
103
118{
119 public:
120
125 {
126
135 StaticVariable( const QString &name = QString(), const QVariant &value = QVariant(), bool readOnly = false, bool isStatic = false, const QString &description = QString() )
136 : name( name )
137 , value( value )
138 , readOnly( readOnly )
139 , isStatic( isStatic )
140 , description( description )
141 {}
142
144 QString name;
145
147 QVariant value;
148
151
154
156 QString description;
157 };
158
163 QgsExpressionContextScope( const QString &name = QString() );
164
169
170 QgsExpressionContextScope &operator=( const QgsExpressionContextScope &other );
171
173
177 QString name() const { return mName; }
178
186 void setVariable( const QString &name, const QVariant &value, bool isStatic = false );
187
195 void addVariable( const QgsExpressionContextScope::StaticVariable &variable );
196
203 bool removeVariable( const QString &name );
204
212 bool hasVariable( const QString &name ) const;
213
221 QVariant variable( const QString &name ) const;
222
228 QStringList variableNames() const;
229
236 QStringList filteredVariableNames() const;
237
244 bool isReadOnly( const QString &name ) const;
245
252 bool isStatic( const QString &name ) const;
253
260 QString description( const QString &name ) const;
261
265 int variableCount() const { return mVariables.count(); }
266
274 bool hasFunction( const QString &name ) const;
275
284 QgsExpressionFunction *function( const QString &name ) const;
285
291 QStringList functionNames() const;
292
299 void addFunction( const QString &name, QgsScopedExpressionFunction *function SIP_TRANSFER );
300
306 bool hasFeature() const { return mHasFeature; }
307
314 QgsFeature feature() const { return mFeature; }
315
323 void setFeature( const QgsFeature &feature ) { mHasFeature = true; mFeature = feature; }
324
331 void removeFeature() { mHasFeature = false; mFeature = QgsFeature(); }
332
338 bool hasGeometry() const { return mHasGeometry; }
339
346 QgsGeometry geometry() const { return mGeometry; }
347
356 void setGeometry( const QgsGeometry &geometry ) { mHasGeometry = true; mGeometry = geometry; }
357
364 void removeGeometry() { mHasGeometry = false; mGeometry = QgsGeometry(); }
365
371 void setFields( const QgsFields &fields );
372
378 void readXml( const QDomElement &element, const QgsReadWriteContext &context );
379
385 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
386
387
396 QStringList hiddenVariables() const;
397
408 void setHiddenVariables( const QStringList &hiddenVariables );
409
410
421 void addHiddenVariable( const QString &hiddenVariable );
422
432 void removeHiddenVariable( const QString &hiddenVariable );
433
444 void addLayerStore( QgsMapLayerStore *store );
445
452 QList< QgsMapLayerStore * > layerStores() const;
453
454 private:
455 QString mName;
456 QHash<QString, StaticVariable> mVariables;
457 QHash<QString, QgsScopedExpressionFunction * > mFunctions;
458 bool mHasFeature = false;
459 QgsFeature mFeature;
460 bool mHasGeometry = false;
461 QgsGeometry mGeometry;
462 QStringList mHiddenVariables;
463
464 QList< QPointer< QgsMapLayerStore > > mLayerStores;
465};
466
480class CORE_EXPORT QgsExpressionContext
481{
482 public:
483
486
492 explicit QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
493
498
499 QgsExpressionContext &operator=( const QgsExpressionContext &other ) SIP_SKIP;
500
501 QgsExpressionContext &operator=( QgsExpressionContext &&other ) noexcept SIP_SKIP;
502
504
512 bool hasVariable( const QString &name ) const;
513
523 QVariant variable( const QString &name ) const;
524
530 QVariantMap variablesToMap() const;
531
539 bool isHighlightedVariable( const QString &name ) const;
540
547 QStringList highlightedVariables() const;
548
556 void setHighlightedVariables( const QStringList &variableNames );
557
566 bool isHighlightedFunction( const QString &name ) const;
567
579 void setHighlightedFunctions( const QStringList &names );
580
588 QgsExpressionContextScope *activeScopeForVariable( const QString &name );
589
598 const QgsExpressionContextScope *activeScopeForVariable( const QString &name ) const SIP_SKIP;
599
606 QgsExpressionContextScope *scope( int index );
607
612 QgsExpressionContextScope *lastScope();
613
618 QList< QgsExpressionContextScope * > scopes() { return mStack; }
619
625 int indexOfScope( QgsExpressionContextScope *scope ) const;
626
633 int indexOfScope( const QString &scopeName ) const;
634
643 QStringList variableNames() const;
644
651 QStringList filteredVariableNames() const;
652
659 bool isReadOnly( const QString &name ) const;
660
669 QString description( const QString &name ) const;
670
677 bool hasFunction( const QString &name ) const;
678
684 QStringList functionNames() const;
685
694 QgsExpressionFunction *function( const QString &name ) const;
695
699 int scopeCount() const;
700
707 void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
708
716 void appendScopes( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
717
721 QgsExpressionContextScope *popScope();
722
731 QList<QgsExpressionContextScope *> takeScopes() SIP_SKIP;
732
739
747 void setFeature( const QgsFeature &feature );
748
754 bool hasFeature() const;
755
760 QgsFeature feature() const;
761
770 void setGeometry( const QgsGeometry &geometry );
771
777 bool hasGeometry() const;
778
784 QgsGeometry geometry() const;
785
793 void setFields( const QgsFields &fields );
794
799 QgsFields fields() const;
800
807 void setOriginalValueVariable( const QVariant &value );
808
820 void setCachedValue( const QString &key, const QVariant &value ) const;
821
830 bool hasCachedValue( const QString &key ) const;
831
842 QVariant cachedValue( const QString &key ) const;
843
851 void clearCachedValues() const;
852
858 QList< QgsMapLayerStore * > layerStores() const;
859
871 void setLoadedLayerStore( QgsMapLayerStore *store );
872
880 QgsMapLayerStore *loadedLayerStore() const;
881
893 void setFeedback( QgsFeedback *feedback );
894
903 QgsFeedback *feedback() const;
904
906 static const QString EXPR_FIELDS;
908 static const QString EXPR_ORIGINAL_VALUE;
910 static const QString EXPR_SYMBOL_COLOR;
912 static const QString EXPR_SYMBOL_ANGLE;
914 static const QString EXPR_GEOMETRY_PART_COUNT;
916 static const QString EXPR_GEOMETRY_PART_NUM;
917
922 static const QString EXPR_GEOMETRY_RING_NUM;
924 static const QString EXPR_GEOMETRY_POINT_COUNT;
926 static const QString EXPR_GEOMETRY_POINT_NUM;
928 static const QString EXPR_CLUSTER_SIZE;
930 static const QString EXPR_CLUSTER_COLOR;
931
932 private:
933
934 QList< QgsExpressionContextScope * > mStack;
935 QStringList mHighlightedVariables;
936 QStringList mHighlightedFunctions;
937
938 QgsFeedback *mFeedback = nullptr;
939
940 std::unique_ptr< LoadLayerFunction > mLoadLayerFunction;
941 QPointer< QgsMapLayerStore > mDestinationStore;
942
943 // Cache is mutable because we want to be able to add cached values to const contexts
944 mutable QMap< QString, QVariant > mCachedValues;
945
946};
947
948#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:56
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:45
Container of fields for a vector layer.
Definition qgsfields.h:45
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.