QGIS API Documentation 3.39.0-Master (bca3cdb6021)
Loading...
Searching...
No Matches
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 "qgsxmlutils.h"
18#include "qgsexpression.h"
19#include "qgsmaplayerstore.h"
21
22const QString QgsExpressionContext::EXPR_FIELDS( QStringLiteral( "_fields_" ) );
23const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( QStringLiteral( "value" ) );
24const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( QStringLiteral( "symbol_color" ) );
25const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( QStringLiteral( "symbol_angle" ) );
26const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( QStringLiteral( "geometry_part_count" ) );
27const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( QStringLiteral( "geometry_part_num" ) );
28const QString QgsExpressionContext::EXPR_GEOMETRY_RING_NUM( QStringLiteral( "geometry_ring_num" ) );
29const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( QStringLiteral( "geometry_point_count" ) );
30const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( QStringLiteral( "geometry_point_num" ) );
31const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( QStringLiteral( "cluster_size" ) );
32const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( QStringLiteral( "cluster_color" ) );
33
34//
35// QgsExpressionContextScope
36//
37
39 : mName( name )
40{
41
42}
43
45 : mName( other.mName )
46 , mVariables( other.mVariables )
47 , mHasFeature( other.mHasFeature )
48 , mFeature( other.mFeature )
49 , mHasGeometry( other.mHasGeometry )
50 , mGeometry( other.mGeometry )
51 , mHiddenVariables( other.mHiddenVariables )
52 , mLayerStores( other.mLayerStores )
53{
54 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
55 for ( ; it != other.mFunctions.constEnd(); ++it )
56 {
57 mFunctions.insert( it.key(), it.value()->clone() );
58 }
59}
60
62{
63 mName = other.mName;
64 mVariables = other.mVariables;
65 mHasFeature = other.mHasFeature;
66 mFeature = other.mFeature;
67 mHasGeometry = other.mHasGeometry;
68 mGeometry = other.mGeometry;
69 mHiddenVariables = other.mHiddenVariables;
70 mLayerStores = other.mLayerStores;
71
72 qDeleteAll( mFunctions );
73 mFunctions.clear();
74 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
75 for ( ; it != other.mFunctions.constEnd(); ++it )
76 {
77 mFunctions.insert( it.key(), it.value()->clone() );
78 }
79
80 return *this;
81}
82
84{
85 qDeleteAll( mFunctions );
86}
87
88void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value, bool isStatic )
89{
90 auto it = mVariables.find( name );
91 if ( it != mVariables.end() )
92 {
93 it->value = value;
94 it->isStatic = isStatic;
95 }
96 else
97 {
99 }
100}
101
103{
104 mVariables.insert( variable.name, variable );
105}
106
108{
109#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
110 return mVariables.remove( name );
111#else
112 return mVariables.remove( name ) > 0;
113#endif
114}
115
116bool QgsExpressionContextScope::hasVariable( const QString &name ) const
117{
118 return mVariables.contains( name );
119}
120
121QVariant QgsExpressionContextScope::variable( const QString &name ) const
122{
123 return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
124}
125
127{
128 QStringList names = mVariables.keys();
129
130 if ( hasFeature() )
131 {
132 names.append( QStringLiteral( "feature" ) );
133 names.append( QStringLiteral( "id" ) );
134 names.append( QStringLiteral( "geometry" ) );
135 }
136
137 return names;
138}
139
141{
142 return mHiddenVariables;
143}
144
145void QgsExpressionContextScope::setHiddenVariables( const QStringList &hiddenVariables )
146{
147 mHiddenVariables = hiddenVariables;
148}
149
150void QgsExpressionContextScope::addHiddenVariable( const QString &hiddenVariable )
151{
152 if ( !mHiddenVariables.contains( hiddenVariable ) )
153 mHiddenVariables << hiddenVariable;
154}
155
156void QgsExpressionContextScope::removeHiddenVariable( const QString &hiddenVariable )
157{
158 if ( mHiddenVariables.contains( hiddenVariable ) )
159 mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
160}
161
163{
164 mLayerStores.append( store );
165}
166
167QList<QgsMapLayerStore *> QgsExpressionContextScope::layerStores() const
168{
169 QList<QgsMapLayerStore *> res;
170 res.reserve( mLayerStores.size() );
171 for ( QgsMapLayerStore *store : std::as_const( mLayerStores ) )
172 {
173 if ( store )
174 res << store;
175 }
176 return res;
177}
178
180class QgsExpressionContextVariableCompare
181{
182 public:
183 explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope &scope )
184 : mScope( scope )
185 { }
186
187 bool operator()( const QString &a, const QString &b ) const
188 {
189 bool aReadOnly = mScope.isReadOnly( a );
190 bool bReadOnly = mScope.isReadOnly( b );
191 if ( aReadOnly != bReadOnly )
192 return aReadOnly;
193 return QString::localeAwareCompare( a, b ) < 0;
194 }
195
196 private:
197 const QgsExpressionContextScope &mScope;
198};
200
202{
203 QStringList allVariables = mVariables.keys();
204 QStringList filtered;
205 const auto constAllVariables = allVariables;
206 for ( const QString &variable : constAllVariables )
207 {
208 if ( variable.startsWith( '_' ) )
209 continue;
210
211 filtered << variable;
212 }
213 QgsExpressionContextVariableCompare cmp( *this );
214 std::sort( filtered.begin(), filtered.end(), cmp );
215
216 return filtered;
217}
218
219bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
220{
221 return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
222}
223
224bool QgsExpressionContextScope::isStatic( const QString &name ) const
225{
226 return hasVariable( name ) ? mVariables.value( name ).isStatic : false;
227}
228
229QString QgsExpressionContextScope::description( const QString &name ) const
230{
231 return hasVariable( name ) ? mVariables.value( name ).description : QString();
232}
233
234bool QgsExpressionContextScope::hasFunction( const QString &name ) const
235{
236 return mFunctions.contains( name );
237}
238
240{
241 return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
242}
243
245{
246 return mFunctions.keys();
247}
248
250{
251 mFunctions.insert( name, function );
252}
253
254
256{
257 addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
258}
259
260void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
261{
262 const QDomNodeList variablesNodeList = element.childNodes();
263 for ( int i = 0; i < variablesNodeList.size(); ++i )
264 {
265 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
266 const QString key = variableElement.attribute( QStringLiteral( "name" ) );
267 if ( variableElement.tagName() == QLatin1String( "Variable" ) )
268 {
269 const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
270 setVariable( key, value );
271 }
272 else
273 addHiddenVariable( key );
274
275 }
276}
277
278bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
279{
280 for ( auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
281 {
282 QDomElement varElem = document.createElement( QStringLiteral( "Variable" ) );
283 varElem.setAttribute( QStringLiteral( "name" ), it.key() );
284 QDomElement valueElem = QgsXmlUtils::writeVariant( it.value().value, document );
285 varElem.appendChild( valueElem );
286 element.appendChild( varElem );
287 }
288
289 for ( QString hiddenVariable : mHiddenVariables )
290 {
291 QDomElement varElem = document.createElement( QStringLiteral( "HiddenVariable" ) );
292 varElem.setAttribute( QStringLiteral( "name" ), hiddenVariable );
293 element.appendChild( varElem );
294 }
295 return true;
296}
297
298
299//
300// QgsExpressionContext
301//
302
304{
305 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
306}
307
308QgsExpressionContext::QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes )
309 : mStack( scopes )
310{
311 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
312}
313
315{
316 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
317 {
318 mStack << new QgsExpressionContextScope( *scope );
319 }
320 mHighlightedVariables = other.mHighlightedVariables;
321 mHighlightedFunctions = other.mHighlightedFunctions;
322 mCachedValues = other.mCachedValues;
323 mFeedback = other.mFeedback;
324 mDestinationStore = other.mDestinationStore;
325 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
326}
327
328// cppcheck-suppress operatorEqVarError
330{
331 if ( this != &other )
332 {
333 qDeleteAll( mStack );
334 // move the stack over
335 mStack = other.mStack;
336 other.mStack.clear();
337
338 mHighlightedVariables = other.mHighlightedVariables;
339 mHighlightedFunctions = other.mHighlightedFunctions;
340 mCachedValues = other.mCachedValues;
341 mFeedback = other.mFeedback;
342 mDestinationStore = other.mDestinationStore;
343 }
344 return *this;
345}
346
347// cppcheck-suppress operatorEqVarError
349{
350 if ( &other == this )
351 return *this;
352
353 qDeleteAll( mStack );
354 mStack.clear();
355 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
356 {
357 mStack << new QgsExpressionContextScope( *scope );
358 }
359 mHighlightedVariables = other.mHighlightedVariables;
360 mHighlightedFunctions = other.mHighlightedFunctions;
361 mCachedValues = other.mCachedValues;
362 mFeedback = other.mFeedback;
363 mDestinationStore = other.mDestinationStore;
364 return *this;
365}
366
368{
369 qDeleteAll( mStack );
370 mStack.clear();
371}
372
373bool QgsExpressionContext::hasVariable( const QString &name ) const
374{
375 const auto constMStack = mStack;
376 for ( const QgsExpressionContextScope *scope : constMStack )
377 {
378 if ( scope->hasVariable( name ) )
379 return true;
380 }
381 return false;
382}
383
384QVariant QgsExpressionContext::variable( const QString &name ) const
385{
387 return scope ? scope->variable( name ) : QVariant();
388}
389
391{
392 QStringList names = variableNames();
393 QVariantMap m;
394 const auto constNames = names;
395 for ( const QString &name : constNames )
396 {
397 m.insert( name, variable( name ) );
398 }
399 return m;
400}
401
402bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
403{
404 return mHighlightedVariables.contains( name );
405}
406
408{
409 return mHighlightedVariables;
410}
411
412void QgsExpressionContext::setHighlightedVariables( const QStringList &variableNames )
413{
414 mHighlightedVariables = variableNames;
415}
416
417bool QgsExpressionContext::isHighlightedFunction( const QString &name ) const
418{
419 return mHighlightedFunctions.contains( name );
420}
421
422void QgsExpressionContext::setHighlightedFunctions( const QStringList &names )
423{
424 mHighlightedFunctions = names;
425}
426
428{
429 //iterate through stack backwards, so that higher priority variables take precedence
430 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
431 while ( it != mStack.constBegin() )
432 {
433 --it;
434 if ( ( *it )->hasVariable( name ) )
435 return ( *it );
436 }
437 return nullptr;
438}
439
441{
442 //iterate through stack backwards, so that higher priority variables take precedence
443 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
444 while ( it != mStack.constBegin() )
445 {
446 --it;
447 if ( ( *it )->hasVariable( name ) )
448 return ( *it );
449 }
450 return nullptr;
451}
452
454{
455 if ( index < 0 || index >= mStack.count() )
456 return nullptr;
457
458 return mStack.at( index );
459}
460
462{
463 if ( mStack.count() < 1 )
464 return nullptr;
465
466 return mStack.last();
467}
468
470{
471 if ( !scope )
472 return -1;
473
474 return mStack.indexOf( scope );
475}
476
477int QgsExpressionContext::indexOfScope( const QString &scopeName ) const
478{
479 int index = 0;
480 const auto constMStack = mStack;
481 for ( const QgsExpressionContextScope *scope : constMStack )
482 {
483 if ( scope->name() == scopeName )
484 return index;
485
486 index++;
487 }
488 return -1;
489}
490
492{
493 QSet< QString> names;
494 for ( const QgsExpressionContextScope *scope : mStack )
495 {
496 const QStringList variableNames = scope->variableNames();
497 for ( const QString &name : variableNames )
498 names.insert( name );
499 }
500 return QStringList( names.constBegin(), names.constEnd() );
501}
502
504{
505 QStringList allVariables = variableNames();
506 QStringList filtered;
507 const auto constAllVariables = allVariables;
508
509 QStringList hiddenVariables;
510
511 for ( const QgsExpressionContextScope *scope : mStack )
512 {
513 const QStringList scopeHiddenVariables = scope->hiddenVariables();
514 for ( const QString &name : scopeHiddenVariables )
515 hiddenVariables << name ;
516 }
517
518 for ( const QString &variable : constAllVariables )
519 {
520 if ( variable.startsWith( '_' ) ||
521 hiddenVariables.contains( variable ) )
522 continue;
523
524 filtered << variable;
525 }
526
527 filtered.sort();
528 return filtered;
529}
530
531bool QgsExpressionContext::isReadOnly( const QString &name ) const
532{
533 const auto constMStack = mStack;
534 for ( const QgsExpressionContextScope *scope : constMStack )
535 {
536 if ( scope->isReadOnly( name ) )
537 return true;
538 }
539 return false;
540}
541
542QString QgsExpressionContext::description( const QString &name ) const
543{
545 return ( scope && !scope->description( name ).isEmpty() ) ? scope->description( name ) : QgsExpression::variableHelpText( name );
546}
547
548bool QgsExpressionContext::hasFunction( const QString &name ) const
549{
550 if ( name.compare( QLatin1String( "load_layer" ) ) == 0 && mDestinationStore )
551 return true;
552
553 for ( const QgsExpressionContextScope *scope : mStack )
554 {
555 if ( scope->hasFunction( name ) )
556 return true;
557 }
558 return false;
559}
560
562{
563 QSet< QString > result;
564 for ( const QgsExpressionContextScope *scope : mStack )
565 {
566 const QStringList functionNames = scope->functionNames();
567 for ( const QString &name : functionNames )
568 result.insert( name );
569 }
570
571 if ( mDestinationStore )
572 result.insert( QStringLiteral( "load_layer" ) );
573
574 QStringList listResult( result.constBegin(), result.constEnd() );
575 listResult.sort();
576 return listResult;
577}
578
580{
581 if ( name.compare( QLatin1String( "load_layer" ) ) == 0 && mDestinationStore )
582 {
583 return mLoadLayerFunction.get();
584 }
585
586 //iterate through stack backwards, so that higher priority variables take precedence
587 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
588 while ( it != mStack.constBegin() )
589 {
590 --it;
591 if ( ( *it )->hasFunction( name ) )
592 return ( *it )->function( name );
593 }
594 return nullptr;
595}
596
598{
599 return mStack.count();
600}
601
603{
604 mStack.append( scope );
605}
606
607void QgsExpressionContext::appendScopes( const QList<QgsExpressionContextScope *> &scopes )
608{
609 mStack.append( scopes );
610}
611
613{
614 if ( !mStack.isEmpty() )
615 return mStack.takeLast();
616
617 return nullptr;
618}
619
620QList<QgsExpressionContextScope *> QgsExpressionContext::takeScopes()
621{
622 QList<QgsExpressionContextScope *> stack = mStack;
623 mStack.clear();
624 return stack;
625}
626
628{
629 mStack.append( scope );
630 return *this;
631}
632
634{
635 if ( mStack.isEmpty() )
636 mStack.append( new QgsExpressionContextScope() );
637
638 mStack.last()->setFeature( feature );
639}
640
642{
643 for ( const QgsExpressionContextScope *scope : mStack )
644 {
645 if ( scope->hasFeature() )
646 return true;
647 }
648 return false;
649}
650
652{
653 //iterate through stack backwards, so that higher priority variables take precedence
654 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
655 while ( it != mStack.constBegin() )
656 {
657 --it;
658 if ( ( *it )->hasFeature() )
659 return ( *it )->feature();
660 }
661 return QgsFeature();
662}
663
665{
666 if ( mStack.isEmpty() )
667 mStack.append( new QgsExpressionContextScope() );
668
669 mStack.last()->setGeometry( geometry );
670}
671
673{
674 for ( const QgsExpressionContextScope *scope : mStack )
675 {
676 if ( scope->hasGeometry() )
677 return true;
678 }
679 return false;
680}
681
683{
684 //iterate through stack backwards, so that higher priority variables take precedence
685 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
686 while ( it != mStack.constBegin() )
687 {
688 --it;
689 if ( ( *it )->hasGeometry() )
690 return ( *it )->geometry();
691 }
692 return QgsGeometry();
693}
694
696{
697 if ( mStack.isEmpty() )
698 mStack.append( new QgsExpressionContextScope() );
699
700 mStack.last()->setFields( fields );
701}
702
704{
705 return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
706}
707
709{
710 if ( mStack.isEmpty() )
711 mStack.append( new QgsExpressionContextScope() );
712
714 value, true ) );
715}
716
717void QgsExpressionContext::setCachedValue( const QString &key, const QVariant &value ) const
718{
719 mCachedValues.insert( key, value );
720}
721
722bool QgsExpressionContext::hasCachedValue( const QString &key ) const
723{
724 return mCachedValues.contains( key );
725}
726
727QVariant QgsExpressionContext::cachedValue( const QString &key ) const
728{
729 return mCachedValues.value( key, QVariant() );
730}
731
733{
734 mCachedValues.clear();
735}
736
737QList<QgsMapLayerStore *> QgsExpressionContext::layerStores() const
738{
739 //iterate through stack backwards, so that higher priority layer stores take precedence
740 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
741 QList<QgsMapLayerStore *> res;
742 while ( it != mStack.constBegin() )
743 {
744 --it;
745 res.append( ( *it )->layerStores() );
746 }
747 // ensure that the destination store is also present in the list
748 if ( mDestinationStore && !res.contains( mDestinationStore ) )
749 res.append( mDestinationStore );
750 return res;
751}
752
754{
755 mDestinationStore = store;
756}
757
759{
760 return mDestinationStore;
761}
762
764{
765 mFeedback = feedback;
766}
767
769{
770 return mFeedback;
771}
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addLayerStore(QgsMapLayerStore *store)
Adds a layer store to the scope.
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.
QList< QgsMapLayerStore * > layerStores() const
Returns the list of layer stores associated with 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)
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.
QList< QgsMapLayerStore * > layerStores() const
Returns the list of layer stores associated with 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.
QgsMapLayerStore * loadedLayerStore() const
Returns the destination layer store for any layers loaded during expression evaluation.
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 setLoadedLayerStore(QgsMapLayerStore *store)
Sets the destination layer store for any layers loaded during expression evaluation.
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: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.
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.