QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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( QStringLiteral( "_fields_" ) );
24const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( QStringLiteral( "value" ) );
25const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( QStringLiteral( "symbol_color" ) );
26const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( QStringLiteral( "symbol_angle" ) );
27const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( QStringLiteral( "geometry_part_count" ) );
28const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( QStringLiteral( "geometry_part_num" ) );
29const QString QgsExpressionContext::EXPR_GEOMETRY_RING_NUM( QStringLiteral( "geometry_ring_num" ) );
30const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( QStringLiteral( "geometry_point_count" ) );
31const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( QStringLiteral( "geometry_point_num" ) );
32const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( QStringLiteral( "cluster_size" ) );
33const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( QStringLiteral( "cluster_color" ) );
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#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
147 return mVariables.remove( name );
148#else
149 return mVariables.remove( name ) > 0;
150#endif
151}
152
154{
155 return mVariables.contains( name );
156}
157
158QVariant QgsExpressionContextScope::variable( const QString &name ) const
159{
160 return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
161}
162
164{
165 QStringList names = mVariables.keys();
166
167 if ( hasFeature() )
168 {
169 names.append( QStringLiteral( "feature" ) );
170 names.append( QStringLiteral( "id" ) );
171 names.append( QStringLiteral( "geometry" ) );
172 }
173
174 return names;
175}
176
178{
179 return mHiddenVariables;
180}
181
183{
184 mHiddenVariables = hiddenVariables;
185}
186
187void QgsExpressionContextScope::addHiddenVariable( const QString &hiddenVariable )
188{
189 if ( !mHiddenVariables.contains( hiddenVariable ) )
190 mHiddenVariables << hiddenVariable;
191}
192
193void QgsExpressionContextScope::removeHiddenVariable( const QString &hiddenVariable )
194{
195 if ( mHiddenVariables.contains( hiddenVariable ) )
196 mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
197}
198
200{
201 mLayerStores.append( store );
202}
203
204QList<QgsMapLayerStore *> QgsExpressionContextScope::layerStores() const
205{
206 QList<QgsMapLayerStore *> res;
207 res.reserve( mLayerStores.size() );
208 for ( QgsMapLayerStore *store : std::as_const( mLayerStores ) )
209 {
210 if ( store )
211 res << store;
212 }
213 return res;
214}
215
217class QgsExpressionContextVariableCompare
218{
219 public:
220 explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope &scope )
221 : mScope( scope )
222 { }
223
224 bool operator()( const QString &a, const QString &b ) const
225 {
226 bool aReadOnly = mScope.isReadOnly( a );
227 bool bReadOnly = mScope.isReadOnly( b );
228 if ( aReadOnly != bReadOnly )
229 return aReadOnly;
230 return QString::localeAwareCompare( a, b ) < 0;
231 }
232
233 private:
234 const QgsExpressionContextScope &mScope;
235};
237
239{
240 QStringList allVariables = mVariables.keys();
241 QStringList filtered;
242 const auto constAllVariables = allVariables;
243 for ( const QString &variable : constAllVariables )
244 {
245 if ( variable.startsWith( '_' ) )
246 continue;
247
248 filtered << variable;
249 }
250 QgsExpressionContextVariableCompare cmp( *this );
251 std::sort( filtered.begin(), filtered.end(), cmp );
252
253 return filtered;
254}
255
256bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
257{
258 return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
259}
260
261bool QgsExpressionContextScope::isStatic( const QString &name ) const
262{
263 return hasVariable( name ) ? mVariables.value( name ).isStatic : false;
264}
265
266QString QgsExpressionContextScope::description( const QString &name ) const
267{
268 return hasVariable( name ) ? mVariables.value( name ).description : QString();
269}
270
272{
273 return mFunctions.contains( name );
274}
275
277{
278 return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
279}
280
282{
283 return mFunctions.keys();
284}
285
287{
288 mFunctions.insert( name, function );
289}
290
291
293{
294 addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
295}
296
297void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
298{
299 const QDomNodeList variablesNodeList = element.childNodes();
300 for ( int i = 0; i < variablesNodeList.size(); ++i )
301 {
302 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
303 const QString key = variableElement.attribute( QStringLiteral( "name" ) );
304 if ( variableElement.tagName() == QLatin1String( "Variable" ) )
305 {
306 const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
307 setVariable( key, value );
308 }
309 else
310 addHiddenVariable( key );
311
312 }
313}
314
315bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
316{
317 for ( auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
318 {
319 QDomElement varElem = document.createElement( QStringLiteral( "Variable" ) );
320 varElem.setAttribute( QStringLiteral( "name" ), it.key() );
321 QDomElement valueElem = QgsXmlUtils::writeVariant( it.value().value, document );
322 varElem.appendChild( valueElem );
323 element.appendChild( varElem );
324 }
325
326 for ( QString hiddenVariable : mHiddenVariables )
327 {
328 QDomElement varElem = document.createElement( QStringLiteral( "HiddenVariable" ) );
329 varElem.setAttribute( QStringLiteral( "name" ), hiddenVariable );
330 element.appendChild( varElem );
331 }
332 return true;
333}
334
335
336//
337// QgsExpressionContext
338//
339
341{
342 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
343}
344
345QgsExpressionContext::QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes )
346 : mStack( scopes )
347{
348 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
349}
350
352 : mStack{}
353{
354 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
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 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
365 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
366}
367
369 : mStack( std::move( other.mStack ) )
370 , mHighlightedVariables( std::move( other.mHighlightedVariables ) )
371 , mHighlightedFunctions( std::move( other.mHighlightedFunctions ) )
372 , mFeedback( other.mFeedback )
373 , mLoadLayerFunction( std::move( other.mLoadLayerFunction ) )
374 , mDestinationStore( std::move( other.mDestinationStore ) )
375 , mCachedValues( std::move( other.mCachedValues ) )
376{
377}
378
380{
381 if ( this != &other )
382 {
383 qDeleteAll( mStack );
384 // move the stack over
385 mStack = std::move( other.mStack );
386
387 mHighlightedVariables = std::move( other.mHighlightedVariables );
388 mHighlightedFunctions = std::move( other.mHighlightedFunctions );
389 mLoadLayerFunction = std::move( other.mLoadLayerFunction );
390 mCachedValues = std::move( other.mCachedValues );
391 mFeedback = other.mFeedback;
392 mDestinationStore = std::move( other.mDestinationStore );
393 }
394 return *this;
395}
396
397// cppcheck-suppress operatorEqVarError
399{
400 if ( &other == this )
401 return *this;
402
403 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
404 qDeleteAll( mStack );
405 mStack.clear();
406 for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
407 {
408 mStack << new QgsExpressionContextScope( *scope );
409 }
410 mHighlightedVariables = other.mHighlightedVariables;
411 mHighlightedFunctions = other.mHighlightedFunctions;
412 mCachedValues = other.mCachedValues;
413 mFeedback = other.mFeedback;
414 mDestinationStore = other.mDestinationStore;
415 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
416 return *this;
417}
418
420{
421 qDeleteAll( mStack );
422 mStack.clear();
423}
424
425bool QgsExpressionContext::hasVariable( const QString &name ) const
426{
427 const auto constMStack = mStack;
428 for ( const QgsExpressionContextScope *scope : constMStack )
429 {
430 if ( scope->hasVariable( name ) )
431 return true;
432 }
433 return false;
434}
435
436QVariant QgsExpressionContext::variable( const QString &name ) const
437{
439 return scope ? scope->variable( name ) : QVariant();
440}
441
443{
444 QStringList names = variableNames();
445 QVariantMap m;
446 const auto constNames = names;
447 for ( const QString &name : constNames )
448 {
449 m.insert( name, variable( name ) );
450 }
451 return m;
452}
453
454bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
455{
456 return mHighlightedVariables.contains( name );
457}
458
460{
461 return mHighlightedVariables;
462}
463
465{
466 mHighlightedVariables = variableNames;
467}
468
469bool QgsExpressionContext::isHighlightedFunction( const QString &name ) const
470{
471 return mHighlightedFunctions.contains( name );
472}
473
474void QgsExpressionContext::setHighlightedFunctions( const QStringList &names )
475{
476 mHighlightedFunctions = names;
477}
478
480{
481 //iterate through stack backwards, so that higher priority variables take precedence
482 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
483 while ( it != mStack.constBegin() )
484 {
485 --it;
486 if ( ( *it )->hasVariable( name ) )
487 return ( *it );
488 }
489 return nullptr;
490}
491
493{
494 //iterate through stack backwards, so that higher priority variables take precedence
495 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
496 while ( it != mStack.constBegin() )
497 {
498 --it;
499 if ( ( *it )->hasVariable( name ) )
500 return ( *it );
501 }
502 return nullptr;
503}
504
506{
507 if ( index < 0 || index >= mStack.count() )
508 return nullptr;
509
510 return mStack.at( index );
511}
512
514{
515 if ( mStack.count() < 1 )
516 return nullptr;
517
518 return mStack.last();
519}
520
522{
523 if ( !scope )
524 return -1;
525
526 return mStack.indexOf( scope );
527}
528
529int QgsExpressionContext::indexOfScope( const QString &scopeName ) const
530{
531 int index = 0;
532 const auto constMStack = mStack;
533 for ( const QgsExpressionContextScope *scope : constMStack )
534 {
535 if ( scope->name() == scopeName )
536 return index;
537
538 index++;
539 }
540 return -1;
541}
542
544{
545 QSet< QString> names;
546 for ( const QgsExpressionContextScope *scope : mStack )
547 {
548 const QStringList variableNames = scope->variableNames();
549 for ( const QString &name : variableNames )
550 names.insert( name );
551 }
552 return QStringList( names.constBegin(), names.constEnd() );
553}
554
556{
557 QStringList allVariables = variableNames();
558 QStringList filtered;
559 const auto constAllVariables = allVariables;
560
561 QStringList hiddenVariables;
562
563 for ( const QgsExpressionContextScope *scope : mStack )
564 {
565 const QStringList scopeHiddenVariables = scope->hiddenVariables();
566 for ( const QString &name : scopeHiddenVariables )
567 hiddenVariables << name ;
568 }
569
570 for ( const QString &variable : constAllVariables )
571 {
572 if ( variable.startsWith( '_' ) ||
573 hiddenVariables.contains( variable ) )
574 continue;
575
576 filtered << variable;
577 }
578
579 filtered.sort();
580 return filtered;
581}
582
583bool QgsExpressionContext::isReadOnly( const QString &name ) const
584{
585 const auto constMStack = mStack;
586 for ( const QgsExpressionContextScope *scope : constMStack )
587 {
588 if ( scope->isReadOnly( name ) )
589 return true;
590 }
591 return false;
592}
593
594QString QgsExpressionContext::description( const QString &name ) const
595{
597 return ( scope && !scope->description( name ).isEmpty() ) ? scope->description( name ) : QgsExpression::variableHelpText( name );
598}
599
600bool QgsExpressionContext::hasFunction( const QString &name ) const
601{
602 if ( name.compare( QLatin1String( "load_layer" ) ) == 0 && mDestinationStore )
603 return true;
604
605 for ( const QgsExpressionContextScope *scope : mStack )
606 {
607 if ( scope->hasFunction( name ) )
608 return true;
609 }
610 return false;
611}
612
614{
615 QSet< QString > result;
616 for ( const QgsExpressionContextScope *scope : mStack )
617 {
618 const QStringList functionNames = scope->functionNames();
619 for ( const QString &name : functionNames )
620 result.insert( name );
621 }
622
623 if ( mDestinationStore )
624 result.insert( QStringLiteral( "load_layer" ) );
625
626 QStringList listResult( result.constBegin(), result.constEnd() );
627 listResult.sort();
628 return listResult;
629}
630
632{
633 if ( name.compare( QLatin1String( "load_layer" ) ) == 0 && mDestinationStore )
634 {
635 return mLoadLayerFunction.get();
636 }
637
638 //iterate through stack backwards, so that higher priority variables take precedence
639 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
640 while ( it != mStack.constBegin() )
641 {
642 --it;
643 if ( ( *it )->hasFunction( name ) )
644 return ( *it )->function( name );
645 }
646 return nullptr;
647}
648
650{
651 return mStack.count();
652}
653
658
659void QgsExpressionContext::appendScopes( const QList<QgsExpressionContextScope *> &scopes )
660{
661 mStack.append( scopes );
662}
663
665{
666 if ( !mStack.isEmpty() )
667 return mStack.takeLast();
668
669 return nullptr;
670}
671
672QList<QgsExpressionContextScope *> QgsExpressionContext::takeScopes()
673{
674 QList<QgsExpressionContextScope *> stack = mStack;
675 mStack.clear();
676 return stack;
677}
678
684
686{
687 if ( mStack.isEmpty() )
688 mStack.append( new QgsExpressionContextScope() );
689
690 mStack.last()->setFeature( feature );
691}
692
694{
695 for ( const QgsExpressionContextScope *scope : mStack )
696 {
697 if ( scope->hasFeature() )
698 return true;
699 }
700 return false;
701}
702
704{
705 //iterate through stack backwards, so that higher priority variables take precedence
706 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
707 while ( it != mStack.constBegin() )
708 {
709 --it;
710 if ( ( *it )->hasFeature() )
711 return ( *it )->feature();
712 }
713 return QgsFeature();
714}
715
717{
718 if ( mStack.isEmpty() )
719 mStack.append( new QgsExpressionContextScope() );
720
721 mStack.last()->setGeometry( geometry );
722}
723
725{
726 for ( const QgsExpressionContextScope *scope : mStack )
727 {
728 if ( scope->hasGeometry() )
729 return true;
730 }
731 return false;
732}
733
735{
736 //iterate through stack backwards, so that higher priority variables take precedence
737 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
738 while ( it != mStack.constBegin() )
739 {
740 --it;
741 if ( ( *it )->hasGeometry() )
742 return ( *it )->geometry();
743 }
744 return QgsGeometry();
745}
746
748{
749 if ( mStack.isEmpty() )
750 mStack.append( new QgsExpressionContextScope() );
751
752 mStack.last()->setFields( fields );
753}
754
756{
757 return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
758}
759
761{
762 if ( mStack.isEmpty() )
763 mStack.append( new QgsExpressionContextScope() );
764
766 value, true ) );
767}
768
769void QgsExpressionContext::setCachedValue( const QString &key, const QVariant &value ) const
770{
771 mCachedValues.insert( key, value );
772}
773
774bool QgsExpressionContext::hasCachedValue( const QString &key ) const
775{
776 return mCachedValues.contains( key );
777}
778
779QVariant QgsExpressionContext::cachedValue( const QString &key ) const
780{
781 return mCachedValues.value( key, QVariant() );
782}
783
785{
786 mCachedValues.clear();
787}
788
789QList<QgsMapLayerStore *> QgsExpressionContext::layerStores() const
790{
791 //iterate through stack backwards, so that higher priority layer stores take precedence
792 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
793 QList<QgsMapLayerStore *> res;
794 while ( it != mStack.constBegin() )
795 {
796 --it;
797 res.append( ( *it )->layerStores() );
798 }
799 // ensure that the destination store is also present in the list
800 if ( mDestinationStore && !res.contains( mDestinationStore ) )
801 res.append( mDestinationStore );
802 return res;
803}
804
806{
807 mDestinationStore = store;
808}
809
811{
812 return mDestinationStore;
813}
814
819
821{
822 return mFeedback;
823}
824
825QString QgsExpressionContext::uniqueHash( bool &ok, const QSet<QString> &variables ) const
826{
827 QString hash;
828 ok = true;
829 const QString delimiter( QStringLiteral( "||~~||" ) );
830
831 if ( hasFeature() )
832 {
833 hash.append( QString::number( feature().id() ) + delimiter + QString::number( qHash( feature() ) ) + delimiter );
834 }
835
836 QStringList sortedVars = qgis::setToList( variables );
837 if ( sortedVars.empty() )
838 sortedVars = variableNames();
839 std::sort( sortedVars.begin(), sortedVars.end() );
840
841 for ( const QString &variableName : std::as_const( sortedVars ) )
842 {
843 const QVariant value = variable( variableName );
844 hash.append( variableName + "=" );
845 if ( QgsVariantUtils::isNull( value ) )
846 {
847 hash.append( delimiter );
848 }
849 else if ( value.type() == QVariant::String )
850 {
851 hash.append( value.toString() + delimiter );
852 }
853 else
854 {
855 const QString variantString = value.toString();
856 if ( variantString.isEmpty() )
857 {
858 ok = false;
859 return QString();
860 }
861 hash.append( variantString + delimiter );
862 }
863 }
864
865 return hash;
866}
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:603
Single variable definition for use within a QgsExpressionContextScope.