QGIS API Documentation 4.3.0-Master (c8cdfe19d7b)
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
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
27const QString QgsExpressionContext::EXPR_FIELDS( u"_fields_"_s );
28const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( u"value"_s );
29const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( u"symbol_color"_s );
30const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( u"symbol_angle"_s );
31const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( u"geometry_part_count"_s );
32const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( u"geometry_part_num"_s );
33const QString QgsExpressionContext::EXPR_GEOMETRY_RING_NUM( u"geometry_ring_num"_s );
34const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( u"geometry_point_count"_s );
35const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( u"geometry_point_num"_s );
36const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( u"cluster_size"_s );
37const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( u"cluster_color"_s );
38
39//
40// QgsExpressionContextScope
41//
42
46
48 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
49 : mName( other.mName )
50 , mVariables( other.mVariables )
51 , mHasFeature( other.mHasFeature )
52 , mFeature( other.mFeature )
53 , mHasGeometry( other.mHasGeometry )
54 , mGeometry( other.mGeometry )
55 , mHiddenVariables( other.mHiddenVariables )
56 , mLayerStores( other.mLayerStores )
57//****** IMPORTANT! editing this? make sure you update the move constructor too! *****
58{
59 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
60 for ( ; it != other.mFunctions.constEnd(); ++it )
61 {
62 mFunctions.insert( it.key(), it.value()->clone() );
63 }
64}
65
67 : mName( std::move( other.mName ) )
68 , mVariables( std::move( other.mVariables ) )
69 , mFunctions( std::move( other.mFunctions ) )
70 , mHasFeature( other.mHasFeature )
71 , mFeature( std::move( other.mFeature ) )
72 , mHasGeometry( other.mHasGeometry )
73 , mGeometry( std::move( other.mGeometry ) )
74 , mHiddenVariables( std::move( other.mHiddenVariables ) )
75 , mLayerStores( std::move( other.mLayerStores ) )
76{}
77
79{
80 if ( &other == this )
81 return *this;
82
83 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
84 mName = other.mName;
85 mVariables = other.mVariables;
86 mHasFeature = other.mHasFeature;
87 mFeature = other.mFeature;
88 mHasGeometry = other.mHasGeometry;
89 mGeometry = other.mGeometry;
90 mHiddenVariables = other.mHiddenVariables;
91 mLayerStores = other.mLayerStores;
92
93 qDeleteAll( mFunctions );
94 mFunctions.clear();
95 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
96 for ( ; it != other.mFunctions.constEnd(); ++it )
97 {
98 mFunctions.insert( it.key(), it.value()->clone() );
99 }
100 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
101 return *this;
102}
103
105{
106 if ( &other == this )
107 return *this;
108
109 mName = std::move( other.mName );
110 mVariables = std::move( other.mVariables );
111 mHasFeature = other.mHasFeature;
112 mFeature = std::move( other.mFeature );
113 mHasGeometry = other.mHasGeometry;
114 mGeometry = std::move( other.mGeometry );
115 mHiddenVariables = std::move( other.mHiddenVariables );
116 mLayerStores = std::move( other.mLayerStores );
117 mFunctions = std::move( other.mFunctions );
118 return *this;
119}
120
122{
123 qDeleteAll( mFunctions );
124}
125
126void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value, bool isStatic )
127{
128 auto it = mVariables.find( name );
129 if ( it != mVariables.end() )
130 {
131 it->value = value;
132 it->isStatic = isStatic;
133 }
134 else
135 {
137 }
138}
139
144
146{
147 return mVariables.remove( name );
148}
149
151{
152 return mVariables.contains( name );
153}
154
155QVariant QgsExpressionContextScope::variable( const QString &name ) const
156{
157 return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
158}
159
161{
162 QStringList names = mVariables.keys();
163
164 if ( hasFeature() )
165 {
166 names.append( u"feature"_s );
167 names.append( u"id"_s );
168 names.append( u"geometry"_s );
169 }
170
171 return names;
172}
173
175{
176 return mHiddenVariables;
177}
178
180{
181 mHiddenVariables = hiddenVariables;
182}
183
184void QgsExpressionContextScope::addHiddenVariable( const QString &hiddenVariable )
185{
186 if ( !mHiddenVariables.contains( hiddenVariable ) )
187 mHiddenVariables << hiddenVariable;
188}
189
190void QgsExpressionContextScope::removeHiddenVariable( const QString &hiddenVariable )
191{
192 if ( mHiddenVariables.contains( hiddenVariable ) )
193 mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
194}
195
197{
198 mLayerStores.append( store );
199}
200
201QList<QgsMapLayerStore *> QgsExpressionContextScope::layerStores() const
202{
203 QList<QgsMapLayerStore *> res;
204 res.reserve( mLayerStores.size() );
205 for ( QgsMapLayerStore *store : std::as_const( mLayerStores ) )
206 {
207 if ( store )
208 res << store;
209 }
210 return res;
211}
212
214class QgsExpressionContextVariableCompare
215{
216 public:
217 explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope &scope )
218 : mScope( scope )
219 {}
220
221 bool operator()( const QString &a, const QString &b ) const
222 {
223 bool aReadOnly = mScope.isReadOnly( a );
224 bool bReadOnly = mScope.isReadOnly( b );
225 if ( aReadOnly != bReadOnly )
226 return aReadOnly;
227 return QString::localeAwareCompare( a, b ) < 0;
228 }
229
230 private:
231 const QgsExpressionContextScope &mScope;
232};
234
236{
237 QStringList allVariables = mVariables.keys();
238 QStringList filtered;
239 const auto constAllVariables = allVariables;
240 for ( const QString &variable : constAllVariables )
241 {
242 if ( variable.startsWith( '_' ) )
243 continue;
244
245 filtered << variable;
246 }
247 QgsExpressionContextVariableCompare cmp( *this );
248 std::sort( filtered.begin(), filtered.end(), cmp );
249
250 return filtered;
251}
252
253bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
254{
255 return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
256}
257
258bool QgsExpressionContextScope::isStatic( const QString &name ) const
259{
260 return hasVariable( name ) ? mVariables.value( name ).isStatic : false;
261}
262
263QString QgsExpressionContextScope::description( const QString &name ) const
264{
265 return hasVariable( name ) ? mVariables.value( name ).description : QString();
266}
267
269{
270 return mFunctions.contains( name );
271}
272
274{
275 return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
276}
277
279{
280 return mFunctions.keys();
281}
282
284{
285 mFunctions.insert( name, function );
286}
287
288
290{
291 addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
292}
293
294void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
295{
296 const QDomNodeList variablesNodeList = element.childNodes();
297 for ( int i = 0; i < variablesNodeList.size(); ++i )
298 {
299 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
300 const QString key = variableElement.attribute( u"name"_s );
301 if ( variableElement.tagName() == "Variable"_L1 )
302 {
303 const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( u"Option"_s ) );
304 setVariable( key, value );
305 }
306 else
307 addHiddenVariable( key );
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
347QgsExpressionContext::QgsExpressionContext( std::vector<std::unique_ptr<QgsExpressionContextScope>> scopes )
348{
349 for ( std::unique_ptr<QgsExpressionContextScope> &scope : scopes )
350 mStack.append( scope.release() );
351
352 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
353}
354
355QgsExpressionContext::QgsExpressionContext( std::unique_ptr<QgsExpressionContextScope> scope )
356{
357 mStack.append( scope.release() );
358 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
359}
360
362 : mStack {}
363{
364 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
365 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
366 {
367 mStack << new QgsExpressionContextScope( *scope );
368 }
369 mHighlightedVariables = other.mHighlightedVariables;
370 mHighlightedFunctions = other.mHighlightedFunctions;
371 mCachedValues = other.mCachedValues;
372 mFeedback = other.mFeedback;
373 mDestinationStore = other.mDestinationStore;
374 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
375 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
376}
377
379 : mStack( std::move( other.mStack ) )
380 , mHighlightedVariables( std::move( other.mHighlightedVariables ) )
381 , mHighlightedFunctions( std::move( other.mHighlightedFunctions ) )
382 , mFeedback( other.mFeedback )
383 , mLoadLayerFunction( std::move( other.mLoadLayerFunction ) )
384 , mDestinationStore( std::move( other.mDestinationStore ) )
385 , mCachedValues( std::move( other.mCachedValues ) )
386{}
387
389{
390 if ( this != &other )
391 {
392 qDeleteAll( mStack );
393 // move the stack over
394 mStack = std::move( other.mStack );
395
396 mHighlightedVariables = std::move( other.mHighlightedVariables );
397 mHighlightedFunctions = std::move( other.mHighlightedFunctions );
398 mLoadLayerFunction = std::move( other.mLoadLayerFunction );
399 mCachedValues = std::move( other.mCachedValues );
400 mFeedback = other.mFeedback;
401 mDestinationStore = std::move( other.mDestinationStore );
402 }
403 return *this;
404}
405
406// cppcheck-suppress operatorEqVarError
408{
409 if ( &other == this )
410 return *this;
411
412 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
413 qDeleteAll( mStack );
414 mStack.clear();
415 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
416 {
417 mStack << new QgsExpressionContextScope( *scope );
418 }
419 mHighlightedVariables = other.mHighlightedVariables;
420 mHighlightedFunctions = other.mHighlightedFunctions;
421 mCachedValues = other.mCachedValues;
422 mFeedback = other.mFeedback;
423 mDestinationStore = other.mDestinationStore;
424 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
425 return *this;
426}
427
429{
430 qDeleteAll( mStack );
431 mStack.clear();
432}
433
434bool QgsExpressionContext::hasVariable( const QString &name ) const
435{
436 const auto constMStack = mStack;
437 for ( const QgsExpressionContextScope *scope : constMStack )
438 {
439 if ( scope->hasVariable( name ) )
440 return true;
441 }
442 return false;
443}
444
445QVariant QgsExpressionContext::variable( const QString &name ) const
446{
448 return scope ? scope->variable( name ) : QVariant();
449}
450
452{
453 QStringList names = variableNames();
454 QVariantMap m;
455 const auto constNames = names;
456 for ( const QString &name : constNames )
457 {
458 m.insert( name, variable( name ) );
459 }
460 return m;
461}
462
463bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
464{
465 return mHighlightedVariables.contains( name );
466}
467
469{
470 return mHighlightedVariables;
471}
472
474{
475 mHighlightedVariables = variableNames;
476}
477
478bool QgsExpressionContext::isHighlightedFunction( const QString &name ) const
479{
480 return mHighlightedFunctions.contains( name );
481}
482
483void QgsExpressionContext::setHighlightedFunctions( const QStringList &names )
484{
485 mHighlightedFunctions = names;
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 //iterate through stack backwards, so that higher priority variables take precedence
504 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
505 while ( it != mStack.constBegin() )
506 {
507 --it;
508 if ( ( *it )->hasVariable( name ) )
509 return ( *it );
510 }
511 return nullptr;
512}
513
515{
516 if ( index < 0 || index >= mStack.count() )
517 return nullptr;
518
519 return mStack.at( index );
520}
521
523{
524 if ( mStack.count() < 1 )
525 return nullptr;
526
527 return mStack.last();
528}
529
531{
532 if ( !scope )
533 return -1;
534
535 return mStack.indexOf( scope );
536}
537
538int QgsExpressionContext::indexOfScope( const QString &scopeName ) const
539{
540 int index = 0;
541 const auto constMStack = mStack;
542 for ( const QgsExpressionContextScope *scope : constMStack )
543 {
544 if ( scope->name() == scopeName )
545 return index;
546
547 index++;
548 }
549 return -1;
550}
551
553{
554 QSet< QString> names;
555 for ( const QgsExpressionContextScope *scope : mStack )
556 {
557 const QStringList variableNames = scope->variableNames();
558 for ( const QString &name : variableNames )
559 names.insert( name );
560 }
561 return QStringList( names.constBegin(), names.constEnd() );
562}
563
565{
566 QStringList allVariables = variableNames();
567 QStringList filtered;
568 const auto constAllVariables = allVariables;
569
570 QStringList hiddenVariables;
571
572 for ( const QgsExpressionContextScope *scope : mStack )
573 {
574 const QStringList scopeHiddenVariables = scope->hiddenVariables();
575 for ( const QString &name : scopeHiddenVariables )
576 hiddenVariables << name;
577 }
578
579 for ( const QString &variable : constAllVariables )
580 {
581 if ( variable.startsWith( '_' ) || hiddenVariables.contains( variable ) )
582 continue;
583
584 filtered << variable;
585 }
586
587 filtered.sort();
588 return filtered;
589}
590
591bool QgsExpressionContext::isReadOnly( const QString &name ) const
592{
593 const auto constMStack = mStack;
594 for ( const QgsExpressionContextScope *scope : constMStack )
595 {
596 if ( scope->isReadOnly( name ) )
597 return true;
598 }
599 return false;
600}
601
602QString QgsExpressionContext::description( const QString &name ) const
603{
605 return ( scope && !scope->description( name ).isEmpty() ) ? scope->description( name ) : QgsExpression::variableHelpText( name );
606}
607
608bool QgsExpressionContext::hasFunction( const QString &name ) const
609{
610 if ( name.compare( "load_layer"_L1 ) == 0 && mDestinationStore )
611 return true;
612
613 for ( const QgsExpressionContextScope *scope : mStack )
614 {
615 if ( scope->hasFunction( name ) )
616 return true;
617 }
618 return false;
619}
620
622{
623 QSet< QString > result;
624 for ( const QgsExpressionContextScope *scope : mStack )
625 {
626 const QStringList functionNames = scope->functionNames();
627 for ( const QString &name : functionNames )
628 result.insert( name );
629 }
630
631 if ( mDestinationStore )
632 result.insert( u"load_layer"_s );
633
634 QStringList listResult( result.constBegin(), result.constEnd() );
635 listResult.sort();
636 return listResult;
637}
638
640{
641 if ( name.compare( "load_layer"_L1 ) == 0 && mDestinationStore )
642 {
643 return mLoadLayerFunction.get();
644 }
645
646 //iterate through stack backwards, so that higher priority variables take precedence
647 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
648 while ( it != mStack.constBegin() )
649 {
650 --it;
651 if ( ( *it )->hasFunction( name ) )
652 return ( *it )->function( name );
653 }
654 return nullptr;
655}
656
658{
659 return mStack.count();
660}
661
666
667void QgsExpressionContext::appendScope( std::unique_ptr<QgsExpressionContextScope> scope )
668{
669 appendScope( scope.release() );
670}
671
672void QgsExpressionContext::appendScopes( const QList<QgsExpressionContextScope *> &scopes )
673{
674 mStack.append( scopes );
675}
676
677void QgsExpressionContext::appendScopes( std::vector<std::unique_ptr<QgsExpressionContextScope>> scopes )
678{
679 for ( std::unique_ptr<QgsExpressionContextScope> &scope : scopes )
680 mStack.append( scope.release() );
681}
682
684{
685 if ( !mStack.isEmpty() )
686 return mStack.takeLast();
687
688 return nullptr;
689}
690
691QList<QgsExpressionContextScope *> QgsExpressionContext::takeScopes()
692{
693 QList<QgsExpressionContextScope *> stack = mStack;
694 mStack.clear();
695 return stack;
696}
697
703
704QgsExpressionContext &QgsExpressionContext::operator<<( std::unique_ptr<QgsExpressionContextScope> scope )
705{
706 return operator<<( scope.release() );
707}
708
710{
711 if ( mStack.isEmpty() )
712 mStack.append( new QgsExpressionContextScope() );
713
714 mStack.last()->setFeature( feature );
715}
716
718{
719 for ( const QgsExpressionContextScope *scope : mStack )
720 {
721 if ( scope->hasFeature() )
722 return true;
723 }
724 return false;
725}
726
728{
729 //iterate through stack backwards, so that higher priority variables take precedence
730 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
731 while ( it != mStack.constBegin() )
732 {
733 --it;
734 if ( ( *it )->hasFeature() )
735 return ( *it )->feature();
736 }
737 return QgsFeature();
738}
739
741{
742 if ( mStack.isEmpty() )
743 mStack.append( new QgsExpressionContextScope() );
744
745 mStack.last()->setGeometry( geometry );
746}
747
749{
750 for ( const QgsExpressionContextScope *scope : mStack )
751 {
752 if ( scope->hasGeometry() )
753 return true;
754 }
755 return false;
756}
757
759{
760 //iterate through stack backwards, so that higher priority variables take precedence
761 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
762 while ( it != mStack.constBegin() )
763 {
764 --it;
765 if ( ( *it )->hasGeometry() )
766 return ( *it )->geometry();
767 }
768 return QgsGeometry();
769}
770
772{
773 if ( mStack.isEmpty() )
774 mStack.append( new QgsExpressionContextScope() );
775
776 mStack.last()->setFields( fields );
777}
778
780{
781 return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
782}
783
785{
786 if ( mStack.isEmpty() )
787 mStack.append( new QgsExpressionContextScope() );
788
790}
791
792void QgsExpressionContext::setCachedValue( const QString &key, const QVariant &value ) const
793{
794 mCachedValues.insert( key, value );
795}
796
797bool QgsExpressionContext::hasCachedValue( const QString &key ) const
798{
799 return mCachedValues.contains( key );
800}
801
802QVariant QgsExpressionContext::cachedValue( const QString &key ) const
803{
804 return mCachedValues.value( key, QVariant() );
805}
806
808{
809 mCachedValues.clear();
810}
811
812QList<QgsMapLayerStore *> QgsExpressionContext::layerStores() const
813{
814 //iterate through stack backwards, so that higher priority layer stores take precedence
815 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
816 QList<QgsMapLayerStore *> res;
817 while ( it != mStack.constBegin() )
818 {
819 --it;
820 res.append( ( *it )->layerStores() );
821 }
822 // ensure that the destination store is also present in the list
823 if ( mDestinationStore && !res.contains( mDestinationStore ) )
824 res.append( mDestinationStore );
825 return res;
826}
827
829{
830 mDestinationStore = store;
831}
832
834{
835 return mDestinationStore;
836}
837
842
844{
845 return mFeedback;
846}
847
848QString QgsExpressionContext::uniqueHash( bool &ok, const QSet<QString> &variables ) const
849{
850 QString hash;
851 ok = true;
852 const QString delimiter( u"||~~||"_s );
853
854 if ( hasFeature() )
855 {
856 hash.append( QString::number( feature().id() ) + delimiter + QString::number( qHash( feature() ) ) + delimiter );
857 }
858
859 QStringList sortedVars = qgis::setToList( variables );
860 if ( sortedVars.empty() )
861 sortedVars = variableNames();
862 std::sort( sortedVars.begin(), sortedVars.end() );
863
864 for ( const QString &variableName : std::as_const( sortedVars ) )
865 {
866 const QVariant value = variable( variableName );
867 hash.append( variableName + "=" );
868 if ( QgsVariantUtils::isNull( value ) )
869 {
870 hash.append( delimiter );
871 }
872 else if ( value.type() == QVariant::String )
873 {
874 hash.append( value.toString() + delimiter );
875 }
876 else
877 {
878 const QString variantString = value.toString();
879 if ( variantString.isEmpty() )
880 {
881 ok = false;
882 return QString();
883 }
884 hash.append( variantString + delimiter );
885 }
886 }
887
888 return hash;
889}
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:60
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:45
A geometry is the spatial representation of a feature.
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
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.
size_t qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:611
Single variable definition for use within a QgsExpressionContextScope.