QGIS API Documentation 3.29.0-Master (006c3c0232)
qgsexpressioncontext.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressioncontext.cpp
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
17#include "qgslogger.h"
18#include "qgsxmlutils.h"
19#include "qgsexpression.h"
20
21const QString QgsExpressionContext::EXPR_FIELDS( QStringLiteral( "_fields_" ) );
22const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( QStringLiteral( "value" ) );
23const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( QStringLiteral( "symbol_color" ) );
24const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( QStringLiteral( "symbol_angle" ) );
25const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( QStringLiteral( "geometry_part_count" ) );
26const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( QStringLiteral( "geometry_part_num" ) );
27const QString QgsExpressionContext::EXPR_GEOMETRY_RING_NUM( QStringLiteral( "geometry_ring_num" ) );
28const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( QStringLiteral( "geometry_point_count" ) );
29const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( QStringLiteral( "geometry_point_num" ) );
30const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( QStringLiteral( "cluster_size" ) );
31const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( QStringLiteral( "cluster_color" ) );
32
33//
34// QgsExpressionContextScope
35//
36
38 : mName( name )
39{
40
41}
42
44 : mName( other.mName )
45 , mVariables( other.mVariables )
46 , mHasFeature( other.mHasFeature )
47 , mFeature( other.mFeature )
48 , mHasGeometry( other.mHasGeometry )
49 , mGeometry( other.mGeometry )
50 , mHiddenVariables( other.mHiddenVariables )
51{
52 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
53 for ( ; it != other.mFunctions.constEnd(); ++it )
54 {
55 mFunctions.insert( it.key(), it.value()->clone() );
56 }
57}
58
60{
61 mName = other.mName;
62 mVariables = other.mVariables;
63 mHasFeature = other.mHasFeature;
64 mFeature = other.mFeature;
65 mHasGeometry = other.mHasGeometry;
66 mGeometry = other.mGeometry;
67 mHiddenVariables = other.mHiddenVariables;
68
69 qDeleteAll( mFunctions );
70 mFunctions.clear();
71 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
72 for ( ; it != other.mFunctions.constEnd(); ++it )
73 {
74 mFunctions.insert( it.key(), it.value()->clone() );
75 }
76
77 return *this;
78}
79
81{
82 qDeleteAll( mFunctions );
83}
84
85void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value, bool isStatic )
86{
87 auto it = mVariables.find( name );
88 if ( it != mVariables.end() )
89 {
90 it->value = value;
91 it->isStatic = isStatic;
92 }
93 else
94 {
96 }
97}
98
100{
101 mVariables.insert( variable.name, variable );
102}
103
105{
106 return mVariables.remove( name ) > 0;
107}
108
109bool QgsExpressionContextScope::hasVariable( const QString &name ) const
110{
111 return mVariables.contains( name );
112}
113
114QVariant QgsExpressionContextScope::variable( const QString &name ) const
115{
116 return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
117}
118
120{
121 QStringList names = mVariables.keys();
122
123 if ( hasFeature() )
124 {
125 names.append( QStringLiteral( "feature" ) );
126 names.append( QStringLiteral( "id" ) );
127 names.append( QStringLiteral( "geometry" ) );
128 }
129
130 return names;
131}
132
134{
135 return mHiddenVariables;
136}
137
138void QgsExpressionContextScope::setHiddenVariables( const QStringList &hiddenVariables )
139{
140 mHiddenVariables = hiddenVariables;
141}
142
143void QgsExpressionContextScope::addHiddenVariable( const QString &hiddenVariable )
144{
145 if ( !mHiddenVariables.contains( hiddenVariable ) )
146 mHiddenVariables << hiddenVariable;
147}
148
149void QgsExpressionContextScope::removeHiddenVariable( const QString &hiddenVariable )
150{
151 if ( mHiddenVariables.contains( hiddenVariable ) )
152 mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
153}
154
155
157class QgsExpressionContextVariableCompare
158{
159 public:
160 explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope &scope )
161 : mScope( scope )
162 { }
163
164 bool operator()( const QString &a, const QString &b ) const
165 {
166 bool aReadOnly = mScope.isReadOnly( a );
167 bool bReadOnly = mScope.isReadOnly( b );
168 if ( aReadOnly != bReadOnly )
169 return aReadOnly;
170 return QString::localeAwareCompare( a, b ) < 0;
171 }
172
173 private:
174 const QgsExpressionContextScope &mScope;
175};
177
179{
180 QStringList allVariables = mVariables.keys();
181 QStringList filtered;
182 const auto constAllVariables = allVariables;
183 for ( const QString &variable : constAllVariables )
184 {
185 if ( variable.startsWith( '_' ) )
186 continue;
187
188 filtered << variable;
189 }
190 QgsExpressionContextVariableCompare cmp( *this );
191 std::sort( filtered.begin(), filtered.end(), cmp );
192
193 return filtered;
194}
195
196bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
197{
198 return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
199}
200
201bool QgsExpressionContextScope::isStatic( const QString &name ) const
202{
203 return hasVariable( name ) ? mVariables.value( name ).isStatic : false;
204}
205
206QString QgsExpressionContextScope::description( const QString &name ) const
207{
208 return hasVariable( name ) ? mVariables.value( name ).description : QString();
209}
210
211bool QgsExpressionContextScope::hasFunction( const QString &name ) const
212{
213 return mFunctions.contains( name );
214}
215
217{
218 return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
219}
220
222{
223 return mFunctions.keys();
224}
225
227{
228 mFunctions.insert( name, function );
229}
230
231
233{
234 addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
235}
236
237void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
238{
239 const QDomNodeList variablesNodeList = element.childNodes();
240 for ( int i = 0; i < variablesNodeList.size(); ++i )
241 {
242 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
243 const QString key = variableElement.attribute( QStringLiteral( "name" ) );
244 if ( variableElement.tagName() == QLatin1String( "Variable" ) )
245 {
246 const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
247 setVariable( key, value );
248 }
249 else
250 addHiddenVariable( key );
251
252 }
253}
254
255bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
256{
257 for ( auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
258 {
259 QDomElement varElem = document.createElement( QStringLiteral( "Variable" ) );
260 varElem.setAttribute( QStringLiteral( "name" ), it.key() );
261 QDomElement valueElem = QgsXmlUtils::writeVariant( it.value().value, document );
262 varElem.appendChild( valueElem );
263 element.appendChild( varElem );
264 }
265
266 for ( QString hiddenVariable : mHiddenVariables )
267 {
268 QDomElement varElem = document.createElement( QStringLiteral( "HiddenVariable" ) );
269 varElem.setAttribute( QStringLiteral( "name" ), hiddenVariable );
270 element.appendChild( varElem );
271 }
272 return true;
273}
274
275
276//
277// QgsExpressionContext
278//
279
280QgsExpressionContext::QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes )
281 : mStack( scopes )
282{
283}
284
286{
287 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
288 {
289 mStack << new QgsExpressionContextScope( *scope );
290 }
291 mHighlightedVariables = other.mHighlightedVariables;
292 mHighlightedFunctions = other.mHighlightedFunctions;
293 mCachedValues = other.mCachedValues;
294}
295
297{
298 if ( this != &other )
299 {
300 qDeleteAll( mStack );
301 // move the stack over
302 mStack = other.mStack;
303 other.mStack.clear();
304
305 mHighlightedVariables = other.mHighlightedVariables;
306 mHighlightedFunctions = other.mHighlightedFunctions;
307 mCachedValues = other.mCachedValues;
308 }
309 return *this;
310}
311
313{
314 if ( &other == this )
315 return *this;
316
317 qDeleteAll( mStack );
318 mStack.clear();
319 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
320 {
321 mStack << new QgsExpressionContextScope( *scope );
322 }
323 mHighlightedVariables = other.mHighlightedVariables;
324 mHighlightedFunctions = other.mHighlightedFunctions;
325 mCachedValues = other.mCachedValues;
326 return *this;
327}
328
330{
331 qDeleteAll( mStack );
332 mStack.clear();
333}
334
335bool QgsExpressionContext::hasVariable( const QString &name ) const
336{
337 const auto constMStack = mStack;
338 for ( const QgsExpressionContextScope *scope : constMStack )
339 {
340 if ( scope->hasVariable( name ) )
341 return true;
342 }
343 return false;
344}
345
346QVariant QgsExpressionContext::variable( const QString &name ) const
347{
349 return scope ? scope->variable( name ) : QVariant();
350}
351
353{
354 QStringList names = variableNames();
355 QVariantMap m;
356 const auto constNames = names;
357 for ( const QString &name : constNames )
358 {
359 m.insert( name, variable( name ) );
360 }
361 return m;
362}
363
364bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
365{
366 return mHighlightedVariables.contains( name );
367}
368
370{
371 return mHighlightedVariables;
372}
373
374void QgsExpressionContext::setHighlightedVariables( const QStringList &variableNames )
375{
376 mHighlightedVariables = variableNames;
377}
378
379bool QgsExpressionContext::isHighlightedFunction( const QString &name ) const
380{
381 return mHighlightedFunctions.contains( name );
382}
383
384void QgsExpressionContext::setHighlightedFunctions( const QStringList &names )
385{
386 mHighlightedFunctions = names;
387}
388
390{
391 //iterate through stack backwards, so that higher priority variables take precedence
392 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
393 while ( it != mStack.constBegin() )
394 {
395 --it;
396 if ( ( *it )->hasVariable( name ) )
397 return ( *it );
398 }
399 return nullptr;
400}
401
403{
404 //iterate through stack backwards, so that higher priority variables take precedence
405 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
406 while ( it != mStack.constBegin() )
407 {
408 --it;
409 if ( ( *it )->hasVariable( name ) )
410 return ( *it );
411 }
412 return nullptr;
413}
414
416{
417 if ( index < 0 || index >= mStack.count() )
418 return nullptr;
419
420 return mStack.at( index );
421}
422
424{
425 if ( mStack.count() < 1 )
426 return nullptr;
427
428 return mStack.last();
429}
430
432{
433 if ( !scope )
434 return -1;
435
436 return mStack.indexOf( scope );
437}
438
439int QgsExpressionContext::indexOfScope( const QString &scopeName ) const
440{
441 int index = 0;
442 const auto constMStack = mStack;
443 for ( const QgsExpressionContextScope *scope : constMStack )
444 {
445 if ( scope->name() == scopeName )
446 return index;
447
448 index++;
449 }
450 return -1;
451}
452
454{
455 QSet< QString> names;
456 for ( const QgsExpressionContextScope *scope : mStack )
457 {
458 const QStringList variableNames = scope->variableNames();
459 for ( const QString &name : variableNames )
460 names.insert( name );
461 }
462 return QStringList( names.constBegin(), names.constEnd() );
463}
464
466{
467 QStringList allVariables = variableNames();
468 QStringList filtered;
469 const auto constAllVariables = allVariables;
470
471 QStringList hiddenVariables;
472
473 for ( const QgsExpressionContextScope *scope : mStack )
474 {
475 const QStringList scopeHiddenVariables = scope->hiddenVariables();
476 for ( const QString &name : scopeHiddenVariables )
477 hiddenVariables << name ;
478 }
479
480 for ( const QString &variable : constAllVariables )
481 {
482 if ( variable.startsWith( '_' ) ||
483 hiddenVariables.contains( variable ) )
484 continue;
485
486 filtered << variable;
487 }
488
489 filtered.sort();
490 return filtered;
491}
492
493bool QgsExpressionContext::isReadOnly( const QString &name ) const
494{
495 const auto constMStack = mStack;
496 for ( const QgsExpressionContextScope *scope : constMStack )
497 {
498 if ( scope->isReadOnly( name ) )
499 return true;
500 }
501 return false;
502}
503
504QString QgsExpressionContext::description( const QString &name ) const
505{
507 return ( scope && !scope->description( name ).isEmpty() ) ? scope->description( name ) : QgsExpression::variableHelpText( name );
508}
509
510bool QgsExpressionContext::hasFunction( const QString &name ) const
511{
512 const auto constMStack = mStack;
513 for ( const QgsExpressionContextScope *scope : constMStack )
514 {
515 if ( scope->hasFunction( name ) )
516 return true;
517 }
518 return false;
519}
520
522{
523 QSet< QString > result;
524 for ( const QgsExpressionContextScope *scope : mStack )
525 {
526 const QStringList functionNames = scope->functionNames();
527 for ( const QString &name : functionNames )
528 result.insert( name );
529 }
530 QStringList listResult( result.constBegin(), result.constEnd() );
531 listResult.sort();
532 return listResult;
533}
534
536{
537 //iterate through stack backwards, so that higher priority variables take precedence
538 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
539 while ( it != mStack.constBegin() )
540 {
541 --it;
542 if ( ( *it )->hasFunction( name ) )
543 return ( *it )->function( name );
544 }
545 return nullptr;
546}
547
549{
550 return mStack.count();
551}
552
554{
555 mStack.append( scope );
556}
557
558void QgsExpressionContext::appendScopes( const QList<QgsExpressionContextScope *> &scopes )
559{
560 mStack.append( scopes );
561}
562
564{
565 if ( !mStack.isEmpty() )
566 return mStack.takeLast();
567
568 return nullptr;
569}
570
571QList<QgsExpressionContextScope *> QgsExpressionContext::takeScopes()
572{
573 QList<QgsExpressionContextScope *> stack = mStack;
574 mStack.clear();
575 return stack;
576}
577
579{
580 mStack.append( scope );
581 return *this;
582}
583
585{
586 if ( mStack.isEmpty() )
587 mStack.append( new QgsExpressionContextScope() );
588
589 mStack.last()->setFeature( feature );
590}
591
593{
594 for ( const QgsExpressionContextScope *scope : mStack )
595 {
596 if ( scope->hasFeature() )
597 return true;
598 }
599 return false;
600}
601
603{
604 //iterate through stack backwards, so that higher priority variables take precedence
605 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
606 while ( it != mStack.constBegin() )
607 {
608 --it;
609 if ( ( *it )->hasFeature() )
610 return ( *it )->feature();
611 }
612 return QgsFeature();
613}
614
616{
617 if ( mStack.isEmpty() )
618 mStack.append( new QgsExpressionContextScope() );
619
620 mStack.last()->setGeometry( geometry );
621}
622
624{
625 for ( const QgsExpressionContextScope *scope : mStack )
626 {
627 if ( scope->hasGeometry() )
628 return true;
629 }
630 return false;
631}
632
634{
635 //iterate through stack backwards, so that higher priority variables take precedence
636 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
637 while ( it != mStack.constBegin() )
638 {
639 --it;
640 if ( ( *it )->hasGeometry() )
641 return ( *it )->geometry();
642 }
643 return QgsGeometry();
644}
645
647{
648 if ( mStack.isEmpty() )
649 mStack.append( new QgsExpressionContextScope() );
650
651 mStack.last()->setFields( fields );
652}
653
655{
656 return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
657}
658
660{
661 if ( mStack.isEmpty() )
662 mStack.append( new QgsExpressionContextScope() );
663
665 value, true ) );
666}
667
668void QgsExpressionContext::setCachedValue( const QString &key, const QVariant &value ) const
669{
670 mCachedValues.insert( key, value );
671}
672
673bool QgsExpressionContext::hasCachedValue( const QString &key ) const
674{
675 return mCachedValues.contains( key );
676}
677
678QVariant QgsExpressionContext::cachedValue( const QString &key ) const
679{
680 return mCachedValues.value( key, QVariant() );
681}
682
684{
685 mCachedValues.clear();
686}
687
689{
690 mFeedback = feedback;
691}
692
694{
695 return mFeedback;
696}
Single scope for storing variables and functions for use within a QgsExpressionContext.
bool hasVariable(const QString &name) const
Tests whether a variable with the specified name exists in the scope.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
QString description(const QString &name) const
Returns the translated description for the variable with the specified name (if set).
void setHiddenVariables(const QStringList &hiddenVariables)
Sets the list of variables intended to be hidden in the expression builder dialog and widget.
void addHiddenVariable(const QString &hiddenVariable)
Adds the passed variable to a list of hidden variables that won't be visible in the expression builde...
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads scope variables from an XML element.
QStringList hiddenVariables() const
Returns the list of variables hidden within the scope.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes scope variables to an XML element.
QgsExpressionFunction * function(const QString &name) const
Retrieves a function from the scope.
QVariant variable(const QString &name) const
Retrieves a variable's value from the scope.
void removeHiddenVariable(const QString &hiddenVariable)
Removes the passed variable from a list of hidden variables.
bool removeVariable(const QString &name)
Removes a variable from the context scope, if found.
bool isReadOnly(const QString &name) const
Tests whether the specified variable is read only and should not be editable by users.
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
bool hasFeature() const
Returns true if the scope has a feature associated with it.
bool hasFunction(const QString &name) const
Tests whether a function with the specified name exists in the scope.
QString name() const
Returns the friendly display name of the context scope.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
bool isStatic(const QString &name) const
Tests whether the variable with the specified name is static and can be cached.
bool hasGeometry() const
Returns true if the scope has a geometry associated with it.
QStringList filteredVariableNames() const
Returns a filtered and sorted list of variable names contained within the scope.
QStringList functionNames() const
Retrieves a list of names of functions contained in the scope.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
QgsExpressionContextScope & operator=(const QgsExpressionContextScope &other)
QgsExpressionContextScope(const QString &name=QString())
Constructor for QgsExpressionContextScope.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
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.
bool hasFunction(const QString &name) const
Checks whether a specified function is contained in the context.
QString description(const QString &name) const
Returns a translated description string for the variable with specified name.
static const QString EXPR_GEOMETRY_POINT_COUNT
Inbuilt variable name for point count variable.
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
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.
QStringList functionNames() const
Retrieves a list of function names contained in the context.
static const QString EXPR_GEOMETRY_POINT_NUM
Inbuilt variable name for point number variable.
int indexOfScope(QgsExpressionContextScope *scope) const
Returns the index of the specified scope if it exists within the context.
void setCachedValue(const QString &key, const QVariant &value) const
Sets a value to cache within the expression context.
void clearCachedValues() const
Clears all cached values from the context.
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
QgsGeometry geometry() const
Convenience function for retrieving the geometry for the context, if set.
bool isHighlightedFunction(const QString &name) const
Returns true if the specified function name is intended to be highlighted to the user.
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
QgsExpressionContext & operator=(const QgsExpressionContext &other)
QgsExpressionContext()=default
Constructor for QgsExpressionContext.
static const QString EXPR_FIELDS
Inbuilt variable name for fields storage.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
void setGeometry(const QgsGeometry &geometry)
Convenience function for setting a geometry for the context.
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.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
bool hasVariable(const QString &name) const
Check whether a variable is specified by any scope within the context.
QList< QgsExpressionContextScope * > takeScopes()
Returns all scopes from this context and remove them, leaving this context without any context.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly by the expression to check if evaluation sh...
void setFeedback(QgsFeedback *feedback)
Attach a feedback object that can be queried regularly by the expression engine to check if expressio...
int scopeCount() const
Returns the number of scopes contained in the context.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for 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.
bool isReadOnly(const QString &name) const
Returns whether a variable is read only, and should not be modifiable by users.
bool hasGeometry() const
Returns true if the context has a geometry associated with it.
QgsExpressionFunction * function(const QString &name) const
Fetches a matching function from the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
bool hasCachedValue(const QString &key) const
Returns true if the expression context contains a cached value with a matching key.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
QgsExpressionContext & operator<<(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
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.
QVariant cachedValue(const QString &key) const
Returns the matching cached value, if set.
bool hasFeature() const
Returns true if the context has a feature associated with it.
QgsFields fields() const
Convenience function for retrieving the fields for the context, if set.
A abstract base class for defining QgsExpression functions.
static QString variableHelpText(const QString &variableName)
Returns the help text for a specified variable.
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.
Definition: qgsgeometry.h:164
The class is used as a container of context for various read/write operations on other objects.
Expression function for use within a QgsExpressionContextScope.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
Single variable definition for use within a QgsExpressionContextScope.