QGIS API Documentation 3.99.0-Master (a8882ad4560)
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
18#include "qgsexpression.h"
20#include "qgsmaplayerstore.h"
21#include "qgsxmlutils.h"
22
23const QString QgsExpressionContext::EXPR_FIELDS( u"_fields_"_s );
24const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( u"value"_s );
25const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( u"symbol_color"_s );
26const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( u"symbol_angle"_s );
27const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( u"geometry_part_count"_s );
28const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( u"geometry_part_num"_s );
29const QString QgsExpressionContext::EXPR_GEOMETRY_RING_NUM( u"geometry_ring_num"_s );
30const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( u"geometry_point_count"_s );
31const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( u"geometry_point_num"_s );
32const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( u"cluster_size"_s );
33const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( u"cluster_color"_s );
34
35//
36// QgsExpressionContextScope
37//
38
40 : mName( name )
41{
42
43}
44
46//****** IMPORTANT! editing this? make sure you update the move constructor too! *****
47 : mName( other.mName )
48 , mVariables( other.mVariables )
49 , mHasFeature( other.mHasFeature )
50 , mFeature( other.mFeature )
51 , mHasGeometry( other.mHasGeometry )
52 , mGeometry( other.mGeometry )
53 , mHiddenVariables( other.mHiddenVariables )
54 , mLayerStores( other.mLayerStores )
55 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
56{
57 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
58 for ( ; it != other.mFunctions.constEnd(); ++it )
59 {
60 mFunctions.insert( it.key(), it.value()->clone() );
61 }
62}
63
65 : mName( std::move( other.mName ) )
66 , mVariables( std::move( other.mVariables ) )
67 , mFunctions( std::move( other.mFunctions ) )
68 , mHasFeature( other.mHasFeature )
69 , mFeature( std::move( other.mFeature ) )
70 , mHasGeometry( other.mHasGeometry )
71 , mGeometry( std::move( other.mGeometry ) )
72 , mHiddenVariables( std::move( other.mHiddenVariables ) )
73 , mLayerStores( std::move( other.mLayerStores ) )
74{
75}
76
78{
79 if ( &other == this )
80 return *this;
81
82 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
83 mName = other.mName;
84 mVariables = other.mVariables;
85 mHasFeature = other.mHasFeature;
86 mFeature = other.mFeature;
87 mHasGeometry = other.mHasGeometry;
88 mGeometry = other.mGeometry;
89 mHiddenVariables = other.mHiddenVariables;
90 mLayerStores = other.mLayerStores;
91
92 qDeleteAll( mFunctions );
93 mFunctions.clear();
94 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
95 for ( ; it != other.mFunctions.constEnd(); ++it )
96 {
97 mFunctions.insert( it.key(), it.value()->clone() );
98 }
99 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
100 return *this;
101}
102
104{
105 if ( &other == this )
106 return *this;
107
108 mName = std::move( other.mName );
109 mVariables = std::move( other.mVariables );
110 mHasFeature = other.mHasFeature;
111 mFeature = std::move( other.mFeature );
112 mHasGeometry = other.mHasGeometry;
113 mGeometry = std::move( other.mGeometry );
114 mHiddenVariables = std::move( other.mHiddenVariables );
115 mLayerStores = std::move( other.mLayerStores );
116 mFunctions = std::move( other.mFunctions );
117 return *this;
118}
119
121{
122 qDeleteAll( mFunctions );
123}
124
125void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value, bool isStatic )
126{
127 auto it = mVariables.find( name );
128 if ( it != mVariables.end() )
129 {
130 it->value = value;
131 it->isStatic = isStatic;
132 }
133 else
134 {
136 }
137}
138
143
145{
146 return mVariables.remove( name );
147}
148
150{
151 return mVariables.contains( name );
152}
153
154QVariant QgsExpressionContextScope::variable( const QString &name ) const
155{
156 return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
157}
158
160{
161 QStringList names = mVariables.keys();
162
163 if ( hasFeature() )
164 {
165 names.append( u"feature"_s );
166 names.append( u"id"_s );
167 names.append( u"geometry"_s );
168 }
169
170 return names;
171}
172
174{
175 return mHiddenVariables;
176}
177
179{
180 mHiddenVariables = hiddenVariables;
181}
182
183void QgsExpressionContextScope::addHiddenVariable( const QString &hiddenVariable )
184{
185 if ( !mHiddenVariables.contains( hiddenVariable ) )
186 mHiddenVariables << hiddenVariable;
187}
188
189void QgsExpressionContextScope::removeHiddenVariable( const QString &hiddenVariable )
190{
191 if ( mHiddenVariables.contains( hiddenVariable ) )
192 mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
193}
194
196{
197 mLayerStores.append( store );
198}
199
200QList<QgsMapLayerStore *> QgsExpressionContextScope::layerStores() const
201{
202 QList<QgsMapLayerStore *> res;
203 res.reserve( mLayerStores.size() );
204 for ( QgsMapLayerStore *store : std::as_const( mLayerStores ) )
205 {
206 if ( store )
207 res << store;
208 }
209 return res;
210}
211
213class QgsExpressionContextVariableCompare
214{
215 public:
216 explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope &scope )
217 : mScope( scope )
218 { }
219
220 bool operator()( const QString &a, const QString &b ) const
221 {
222 bool aReadOnly = mScope.isReadOnly( a );
223 bool bReadOnly = mScope.isReadOnly( b );
224 if ( aReadOnly != bReadOnly )
225 return aReadOnly;
226 return QString::localeAwareCompare( a, b ) < 0;
227 }
228
229 private:
230 const QgsExpressionContextScope &mScope;
231};
233
235{
236 QStringList allVariables = mVariables.keys();
237 QStringList filtered;
238 const auto constAllVariables = allVariables;
239 for ( const QString &variable : constAllVariables )
240 {
241 if ( variable.startsWith( '_' ) )
242 continue;
243
244 filtered << variable;
245 }
246 QgsExpressionContextVariableCompare cmp( *this );
247 std::sort( filtered.begin(), filtered.end(), cmp );
248
249 return filtered;
250}
251
252bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
253{
254 return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
255}
256
257bool QgsExpressionContextScope::isStatic( const QString &name ) const
258{
259 return hasVariable( name ) ? mVariables.value( name ).isStatic : false;
260}
261
262QString QgsExpressionContextScope::description( const QString &name ) const
263{
264 return hasVariable( name ) ? mVariables.value( name ).description : QString();
265}
266
268{
269 return mFunctions.contains( name );
270}
271
273{
274 return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
275}
276
278{
279 return mFunctions.keys();
280}
281
283{
284 mFunctions.insert( name, function );
285}
286
287
289{
290 addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
291}
292
293void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
294{
295 const QDomNodeList variablesNodeList = element.childNodes();
296 for ( int i = 0; i < variablesNodeList.size(); ++i )
297 {
298 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
299 const QString key = variableElement.attribute( u"name"_s );
300 if ( variableElement.tagName() == "Variable"_L1 )
301 {
302 const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( u"Option"_s ) );
303 setVariable( key, value );
304 }
305 else
306 addHiddenVariable( key );
307
308 }
309}
310
311bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
312{
313 for ( auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
314 {
315 QDomElement varElem = document.createElement( u"Variable"_s );
316 varElem.setAttribute( u"name"_s, it.key() );
317 QDomElement valueElem = QgsXmlUtils::writeVariant( it.value().value, document );
318 varElem.appendChild( valueElem );
319 element.appendChild( varElem );
320 }
321
322 for ( QString hiddenVariable : mHiddenVariables )
323 {
324 QDomElement varElem = document.createElement( u"HiddenVariable"_s );
325 varElem.setAttribute( u"name"_s, hiddenVariable );
326 element.appendChild( varElem );
327 }
328 return true;
329}
330
331
332//
333// QgsExpressionContext
334//
335
337{
338 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
339}
340
341QgsExpressionContext::QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes )
342 : mStack( scopes )
343{
344 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
345}
346
348 : mStack{}
349{
350 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
351 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
352 {
353 mStack << new QgsExpressionContextScope( *scope );
354 }
355 mHighlightedVariables = other.mHighlightedVariables;
356 mHighlightedFunctions = other.mHighlightedFunctions;
357 mCachedValues = other.mCachedValues;
358 mFeedback = other.mFeedback;
359 mDestinationStore = other.mDestinationStore;
360 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
361 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
362}
363
365 : mStack( std::move( other.mStack ) )
366 , mHighlightedVariables( std::move( other.mHighlightedVariables ) )
367 , mHighlightedFunctions( std::move( other.mHighlightedFunctions ) )
368 , mFeedback( other.mFeedback )
369 , mLoadLayerFunction( std::move( other.mLoadLayerFunction ) )
370 , mDestinationStore( std::move( other.mDestinationStore ) )
371 , mCachedValues( std::move( other.mCachedValues ) )
372{
373}
374
376{
377 if ( this != &other )
378 {
379 qDeleteAll( mStack );
380 // move the stack over
381 mStack = std::move( other.mStack );
382
383 mHighlightedVariables = std::move( other.mHighlightedVariables );
384 mHighlightedFunctions = std::move( other.mHighlightedFunctions );
385 mLoadLayerFunction = std::move( other.mLoadLayerFunction );
386 mCachedValues = std::move( other.mCachedValues );
387 mFeedback = other.mFeedback;
388 mDestinationStore = std::move( other.mDestinationStore );
389 }
390 return *this;
391}
392
393// cppcheck-suppress operatorEqVarError
395{
396 if ( &other == this )
397 return *this;
398
399 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
400 qDeleteAll( mStack );
401 mStack.clear();
402 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
403 {
404 mStack << new QgsExpressionContextScope( *scope );
405 }
406 mHighlightedVariables = other.mHighlightedVariables;
407 mHighlightedFunctions = other.mHighlightedFunctions;
408 mCachedValues = other.mCachedValues;
409 mFeedback = other.mFeedback;
410 mDestinationStore = other.mDestinationStore;
411 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
412 return *this;
413}
414
416{
417 qDeleteAll( mStack );
418 mStack.clear();
419}
420
421bool QgsExpressionContext::hasVariable( const QString &name ) const
422{
423 const auto constMStack = mStack;
424 for ( const QgsExpressionContextScope *scope : constMStack )
425 {
426 if ( scope->hasVariable( name ) )
427 return true;
428 }
429 return false;
430}
431
432QVariant QgsExpressionContext::variable( const QString &name ) const
433{
435 return scope ? scope->variable( name ) : QVariant();
436}
437
439{
440 QStringList names = variableNames();
441 QVariantMap m;
442 const auto constNames = names;
443 for ( const QString &name : constNames )
444 {
445 m.insert( name, variable( name ) );
446 }
447 return m;
448}
449
450bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
451{
452 return mHighlightedVariables.contains( name );
453}
454
456{
457 return mHighlightedVariables;
458}
459
461{
462 mHighlightedVariables = variableNames;
463}
464
465bool QgsExpressionContext::isHighlightedFunction( const QString &name ) const
466{
467 return mHighlightedFunctions.contains( name );
468}
469
470void QgsExpressionContext::setHighlightedFunctions( const QStringList &names )
471{
472 mHighlightedFunctions = names;
473}
474
476{
477 //iterate through stack backwards, so that higher priority variables take precedence
478 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
479 while ( it != mStack.constBegin() )
480 {
481 --it;
482 if ( ( *it )->hasVariable( name ) )
483 return ( *it );
484 }
485 return nullptr;
486}
487
489{
490 //iterate through stack backwards, so that higher priority variables take precedence
491 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
492 while ( it != mStack.constBegin() )
493 {
494 --it;
495 if ( ( *it )->hasVariable( name ) )
496 return ( *it );
497 }
498 return nullptr;
499}
500
502{
503 if ( index < 0 || index >= mStack.count() )
504 return nullptr;
505
506 return mStack.at( index );
507}
508
510{
511 if ( mStack.count() < 1 )
512 return nullptr;
513
514 return mStack.last();
515}
516
518{
519 if ( !scope )
520 return -1;
521
522 return mStack.indexOf( scope );
523}
524
525int QgsExpressionContext::indexOfScope( const QString &scopeName ) const
526{
527 int index = 0;
528 const auto constMStack = mStack;
529 for ( const QgsExpressionContextScope *scope : constMStack )
530 {
531 if ( scope->name() == scopeName )
532 return index;
533
534 index++;
535 }
536 return -1;
537}
538
540{
541 QSet< QString> names;
542 for ( const QgsExpressionContextScope *scope : mStack )
543 {
544 const QStringList variableNames = scope->variableNames();
545 for ( const QString &name : variableNames )
546 names.insert( name );
547 }
548 return QStringList( names.constBegin(), names.constEnd() );
549}
550
552{
553 QStringList allVariables = variableNames();
554 QStringList filtered;
555 const auto constAllVariables = allVariables;
556
557 QStringList hiddenVariables;
558
559 for ( const QgsExpressionContextScope *scope : mStack )
560 {
561 const QStringList scopeHiddenVariables = scope->hiddenVariables();
562 for ( const QString &name : scopeHiddenVariables )
563 hiddenVariables << name ;
564 }
565
566 for ( const QString &variable : constAllVariables )
567 {
568 if ( variable.startsWith( '_' ) ||
569 hiddenVariables.contains( variable ) )
570 continue;
571
572 filtered << variable;
573 }
574
575 filtered.sort();
576 return filtered;
577}
578
579bool QgsExpressionContext::isReadOnly( const QString &name ) const
580{
581 const auto constMStack = mStack;
582 for ( const QgsExpressionContextScope *scope : constMStack )
583 {
584 if ( scope->isReadOnly( name ) )
585 return true;
586 }
587 return false;
588}
589
590QString QgsExpressionContext::description( const QString &name ) const
591{
593 return ( scope && !scope->description( name ).isEmpty() ) ? scope->description( name ) : QgsExpression::variableHelpText( name );
594}
595
596bool QgsExpressionContext::hasFunction( const QString &name ) const
597{
598 if ( name.compare( "load_layer"_L1 ) == 0 && mDestinationStore )
599 return true;
600
601 for ( const QgsExpressionContextScope *scope : mStack )
602 {
603 if ( scope->hasFunction( name ) )
604 return true;
605 }
606 return false;
607}
608
610{
611 QSet< QString > result;
612 for ( const QgsExpressionContextScope *scope : mStack )
613 {
614 const QStringList functionNames = scope->functionNames();
615 for ( const QString &name : functionNames )
616 result.insert( name );
617 }
618
619 if ( mDestinationStore )
620 result.insert( u"load_layer"_s );
621
622 QStringList listResult( result.constBegin(), result.constEnd() );
623 listResult.sort();
624 return listResult;
625}
626
628{
629 if ( name.compare( "load_layer"_L1 ) == 0 && mDestinationStore )
630 {
631 return mLoadLayerFunction.get();
632 }
633
634 //iterate through stack backwards, so that higher priority variables take precedence
635 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
636 while ( it != mStack.constBegin() )
637 {
638 --it;
639 if ( ( *it )->hasFunction( name ) )
640 return ( *it )->function( name );
641 }
642 return nullptr;
643}
644
646{
647 return mStack.count();
648}
649
654
655void QgsExpressionContext::appendScopes( const QList<QgsExpressionContextScope *> &scopes )
656{
657 mStack.append( scopes );
658}
659
661{
662 if ( !mStack.isEmpty() )
663 return mStack.takeLast();
664
665 return nullptr;
666}
667
668QList<QgsExpressionContextScope *> QgsExpressionContext::takeScopes()
669{
670 QList<QgsExpressionContextScope *> stack = mStack;
671 mStack.clear();
672 return stack;
673}
674
680
682{
683 if ( mStack.isEmpty() )
684 mStack.append( new QgsExpressionContextScope() );
685
686 mStack.last()->setFeature( feature );
687}
688
690{
691 for ( const QgsExpressionContextScope *scope : mStack )
692 {
693 if ( scope->hasFeature() )
694 return true;
695 }
696 return false;
697}
698
700{
701 //iterate through stack backwards, so that higher priority variables take precedence
702 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
703 while ( it != mStack.constBegin() )
704 {
705 --it;
706 if ( ( *it )->hasFeature() )
707 return ( *it )->feature();
708 }
709 return QgsFeature();
710}
711
713{
714 if ( mStack.isEmpty() )
715 mStack.append( new QgsExpressionContextScope() );
716
717 mStack.last()->setGeometry( geometry );
718}
719
721{
722 for ( const QgsExpressionContextScope *scope : mStack )
723 {
724 if ( scope->hasGeometry() )
725 return true;
726 }
727 return false;
728}
729
731{
732 //iterate through stack backwards, so that higher priority variables take precedence
733 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
734 while ( it != mStack.constBegin() )
735 {
736 --it;
737 if ( ( *it )->hasGeometry() )
738 return ( *it )->geometry();
739 }
740 return QgsGeometry();
741}
742
744{
745 if ( mStack.isEmpty() )
746 mStack.append( new QgsExpressionContextScope() );
747
748 mStack.last()->setFields( fields );
749}
750
752{
753 return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
754}
755
757{
758 if ( mStack.isEmpty() )
759 mStack.append( new QgsExpressionContextScope() );
760
762 value, true ) );
763}
764
765void QgsExpressionContext::setCachedValue( const QString &key, const QVariant &value ) const
766{
767 mCachedValues.insert( key, value );
768}
769
770bool QgsExpressionContext::hasCachedValue( const QString &key ) const
771{
772 return mCachedValues.contains( key );
773}
774
775QVariant QgsExpressionContext::cachedValue( const QString &key ) const
776{
777 return mCachedValues.value( key, QVariant() );
778}
779
781{
782 mCachedValues.clear();
783}
784
785QList<QgsMapLayerStore *> QgsExpressionContext::layerStores() const
786{
787 //iterate through stack backwards, so that higher priority layer stores take precedence
788 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
789 QList<QgsMapLayerStore *> res;
790 while ( it != mStack.constBegin() )
791 {
792 --it;
793 res.append( ( *it )->layerStores() );
794 }
795 // ensure that the destination store is also present in the list
796 if ( mDestinationStore && !res.contains( mDestinationStore ) )
797 res.append( mDestinationStore );
798 return res;
799}
800
802{
803 mDestinationStore = store;
804}
805
807{
808 return mDestinationStore;
809}
810
815
817{
818 return mFeedback;
819}
820
821QString QgsExpressionContext::uniqueHash( bool &ok, const QSet<QString> &variables ) const
822{
823 QString hash;
824 ok = true;
825 const QString delimiter( u"||~~||"_s );
826
827 if ( hasFeature() )
828 {
829 hash.append( QString::number( feature().id() ) + delimiter + QString::number( qHash( feature() ) ) + delimiter );
830 }
831
832 QStringList sortedVars = qgis::setToList( variables );
833 if ( sortedVars.empty() )
834 sortedVars = variableNames();
835 std::sort( sortedVars.begin(), sortedVars.end() );
836
837 for ( const QString &variableName : std::as_const( sortedVars ) )
838 {
839 const QVariant value = variable( variableName );
840 hash.append( variableName + "=" );
841 if ( QgsVariantUtils::isNull( value ) )
842 {
843 hash.append( delimiter );
844 }
845 else if ( value.type() == QVariant::String )
846 {
847 hash.append( value.toString() + delimiter );
848 }
849 else
850 {
851 const QString variantString = value.toString();
852 if ( variantString.isEmpty() )
853 {
854 ok = false;
855 return QString();
856 }
857 hash.append( variantString + delimiter );
858 }
859 }
860
861 return hash;
862}
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.
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.
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.
QString uniqueHash(bool &ok, const QSet< QString > &variables=QSet< QString >()) const
Returns a unique hash representing the current state of the context.
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.
An 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...
A container for the context for various read/write operations on objects.
Expression function for use within a QgsExpressionContextScope.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
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.
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:607
Single variable definition for use within a QgsExpressionContextScope.