QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
115{
116 public:
117
122 {
123
132 StaticVariable( const QString &name = QString(), const QVariant &value = QVariant(), bool readOnly = false, bool isStatic = false, const QString &description = QString() )
133 : name( name )
134 , value( value )
135 , readOnly( readOnly )
136 , isStatic( isStatic )
137 , description( description )
138 {}
139
141 QString name;
142
144 QVariant value;
145
148
151
153 QString description;
154 };
155
160 QgsExpressionContextScope( const QString &name = QString() );
161
163
164 QgsExpressionContextScope &operator=( const QgsExpressionContextScope &other );
165
167
171 QString name() const { return mName; }
172
180 void setVariable( const QString &name, const QVariant &value, bool isStatic = false );
181
189 void addVariable( const QgsExpressionContextScope::StaticVariable &variable );
190
197 bool removeVariable( const QString &name );
198
206 bool hasVariable( const QString &name ) const;
207
215 QVariant variable( const QString &name ) const;
216
222 QStringList variableNames() const;
223
230 QStringList filteredVariableNames() const;
231
238 bool isReadOnly( const QString &name ) const;
239
245 bool isStatic( const QString &name ) const;
246
252 QString description( const QString &name ) const;
253
257 int variableCount() const { return mVariables.count(); }
258
266 bool hasFunction( const QString &name ) const;
267
276 QgsExpressionFunction *function( const QString &name ) const;
277
283 QStringList functionNames() const;
284
291 void addFunction( const QString &name, QgsScopedExpressionFunction *function SIP_TRANSFER );
292
297 bool hasFeature() const { return mHasFeature; }
298
304 QgsFeature feature() const { return mFeature; }
305
313 void setFeature( const QgsFeature &feature ) { mHasFeature = true; mFeature = feature; }
314
320 void removeFeature() { mHasFeature = false; mFeature = QgsFeature(); }
321
327 bool hasGeometry() const { return mHasGeometry; }
328
335 QgsGeometry geometry() const { return mGeometry; }
336
345 void setGeometry( const QgsGeometry &geometry ) { mHasGeometry = true; mGeometry = geometry; }
346
353 void removeGeometry() { mHasGeometry = false; mGeometry = QgsGeometry(); }
354
360 void setFields( const QgsFields &fields );
361
367 void readXml( const QDomElement &element, const QgsReadWriteContext &context );
368
374 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
375
376
385 QStringList hiddenVariables() const;
386
397 void setHiddenVariables( const QStringList &hiddenVariables );
398
399
410 void addHiddenVariable( const QString &hiddenVariable );
411
421 void removeHiddenVariable( const QString &hiddenVariable );
422
433 void addLayerStore( QgsMapLayerStore *store );
434
441 QList< QgsMapLayerStore * > layerStores() const;
442
443 private:
444 QString mName;
445 QHash<QString, StaticVariable> mVariables;
446 QHash<QString, QgsScopedExpressionFunction * > mFunctions;
447 bool mHasFeature = false;
448 QgsFeature mFeature;
449 bool mHasGeometry = false;
450 QgsGeometry mGeometry;
451 QStringList mHiddenVariables;
452
453 QList< QPointer< QgsMapLayerStore > > mLayerStores;
454};
455
470class CORE_EXPORT QgsExpressionContext
471{
472 public:
473
475
480 explicit QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
481
483
484 QgsExpressionContext &operator=( const QgsExpressionContext &other ) SIP_SKIP;
485
486 QgsExpressionContext &operator=( QgsExpressionContext &&other ) noexcept SIP_SKIP;
487
489
497 bool hasVariable( const QString &name ) const;
498
508 QVariant variable( const QString &name ) const;
509
514 QVariantMap variablesToMap() const;
515
523 bool isHighlightedVariable( const QString &name ) const;
524
531 QStringList highlightedVariables() const;
532
540 void setHighlightedVariables( const QStringList &variableNames );
541
550 bool isHighlightedFunction( const QString &name ) const;
551
563 void setHighlightedFunctions( const QStringList &names );
564
572 QgsExpressionContextScope *activeScopeForVariable( const QString &name );
573
582 const QgsExpressionContextScope *activeScopeForVariable( const QString &name ) const SIP_SKIP;
583
590 QgsExpressionContextScope *scope( int index );
591
596 QgsExpressionContextScope *lastScope();
597
602 QList< QgsExpressionContextScope * > scopes() { return mStack; }
603
609 int indexOfScope( QgsExpressionContextScope *scope ) const;
610
616 int indexOfScope( const QString &scopeName ) const;
617
626 QStringList variableNames() const;
627
634 QStringList filteredVariableNames() const;
635
642 bool isReadOnly( const QString &name ) const;
643
651 QString description( const QString &name ) const;
652
659 bool hasFunction( const QString &name ) const;
660
666 QStringList functionNames() const;
667
676 QgsExpressionFunction *function( const QString &name ) const;
677
681 int scopeCount() const;
682
689 void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
690
697 void appendScopes( const QList<QgsExpressionContextScope *> &scopes SIP_TRANSFER );
698
702 QgsExpressionContextScope *popScope();
703
711 QList<QgsExpressionContextScope *> takeScopes() SIP_SKIP;
712
719
727 void setFeature( const QgsFeature &feature );
728
733 bool hasFeature() const;
734
739 QgsFeature feature() const;
740
749 void setGeometry( const QgsGeometry &geometry );
750
756 bool hasGeometry() const;
757
763 QgsGeometry geometry() const;
764
772 void setFields( const QgsFields &fields );
773
778 QgsFields fields() const;
779
785 void setOriginalValueVariable( const QVariant &value );
786
797 void setCachedValue( const QString &key, const QVariant &value ) const;
798
806 bool hasCachedValue( const QString &key ) const;
807
817 QVariant cachedValue( const QString &key ) const;
818
825 void clearCachedValues() const;
826
832 QList< QgsMapLayerStore * > layerStores() const;
833
845 void setLoadedLayerStore( QgsMapLayerStore *store );
846
854 QgsMapLayerStore *loadedLayerStore() const;
855
867 void setFeedback( QgsFeedback *feedback );
868
877 QgsFeedback *feedback() const;
878
889 QString uniqueHash( bool &ok SIP_OUT, const QSet<QString> &variables = QSet<QString>() ) const;
890
892 static const QString EXPR_FIELDS;
894 static const QString EXPR_ORIGINAL_VALUE;
896 static const QString EXPR_SYMBOL_COLOR;
898 static const QString EXPR_SYMBOL_ANGLE;
900 static const QString EXPR_GEOMETRY_PART_COUNT;
902 static const QString EXPR_GEOMETRY_PART_NUM;
903
908 static const QString EXPR_GEOMETRY_RING_NUM;
910 static const QString EXPR_GEOMETRY_POINT_COUNT;
912 static const QString EXPR_GEOMETRY_POINT_NUM;
914 static const QString EXPR_CLUSTER_SIZE;
916 static const QString EXPR_CLUSTER_COLOR;
917
918 private:
919
920 QList< QgsExpressionContextScope * > mStack;
921 QStringList mHighlightedVariables;
922 QStringList mHighlightedFunctions;
923
924 QgsFeedback *mFeedback = nullptr;
925
926 std::unique_ptr< LoadLayerFunction > mLoadLayerFunction;
927 QPointer< QgsMapLayerStore > mDestinationStore;
928
929 // Cache is mutable because we want to be able to add cached values to const contexts
930 mutable QMap< QString, QVariant > mCachedValues;
931
932};
933
934#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_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.