QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 
16 #include "qgsexpressioncontext.h"
17 #include "qgsxmlutils.h"
18 #include "qgsexpression.h"
19 #include "qgsmaplayerstore.h"
21 
22 const QString QgsExpressionContext::EXPR_FIELDS( QStringLiteral( "_fields_" ) );
23 const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( QStringLiteral( "value" ) );
24 const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( QStringLiteral( "symbol_color" ) );
25 const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( QStringLiteral( "symbol_angle" ) );
26 const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( QStringLiteral( "geometry_part_count" ) );
27 const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( QStringLiteral( "geometry_part_num" ) );
28 const QString QgsExpressionContext::EXPR_GEOMETRY_RING_NUM( QStringLiteral( "geometry_ring_num" ) );
29 const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( QStringLiteral( "geometry_point_count" ) );
30 const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( QStringLiteral( "geometry_point_num" ) );
31 const QString QgsExpressionContext::EXPR_CLUSTER_SIZE( QStringLiteral( "cluster_size" ) );
32 const QString QgsExpressionContext::EXPR_CLUSTER_COLOR( QStringLiteral( "cluster_color" ) );
33 
34 //
35 // QgsExpressionContextScope
36 //
37 
39  : mName( name )
40 {
41 
42 }
43 
45  : mName( other.mName )
46  , mVariables( other.mVariables )
47  , mHasFeature( other.mHasFeature )
48  , mFeature( other.mFeature )
49  , mHasGeometry( other.mHasGeometry )
50  , mGeometry( other.mGeometry )
51  , mHiddenVariables( other.mHiddenVariables )
52  , mLayerStores( other.mLayerStores )
53 {
54  QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
55  for ( ; it != other.mFunctions.constEnd(); ++it )
56  {
57  mFunctions.insert( it.key(), it.value()->clone() );
58  }
59 }
60 
62 {
63  mName = other.mName;
64  mVariables = other.mVariables;
65  mHasFeature = other.mHasFeature;
66  mFeature = other.mFeature;
67  mHasGeometry = other.mHasGeometry;
68  mGeometry = other.mGeometry;
69  mHiddenVariables = other.mHiddenVariables;
70  mLayerStores = other.mLayerStores;
71 
72  qDeleteAll( mFunctions );
73  mFunctions.clear();
74  QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
75  for ( ; it != other.mFunctions.constEnd(); ++it )
76  {
77  mFunctions.insert( it.key(), it.value()->clone() );
78  }
79 
80  return *this;
81 }
82 
84 {
85  qDeleteAll( mFunctions );
86 }
87 
88 void QgsExpressionContextScope::setVariable( const QString &name, const QVariant &value, bool isStatic )
89 {
90  auto it = mVariables.find( name );
91  if ( it != mVariables.end() )
92  {
93  it->value = value;
94  it->isStatic = isStatic;
95  }
96  else
97  {
99  }
100 }
101 
103 {
104  mVariables.insert( variable.name, variable );
105 }
106 
107 bool QgsExpressionContextScope::removeVariable( const QString &name )
108 {
109  return mVariables.remove( name ) > 0;
110 }
111 
112 bool QgsExpressionContextScope::hasVariable( const QString &name ) const
113 {
114  return mVariables.contains( name );
115 }
116 
117 QVariant QgsExpressionContextScope::variable( const QString &name ) const
118 {
119  return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
120 }
121 
123 {
124  QStringList names = mVariables.keys();
125 
126  if ( hasFeature() )
127  {
128  names.append( QStringLiteral( "feature" ) );
129  names.append( QStringLiteral( "id" ) );
130  names.append( QStringLiteral( "geometry" ) );
131  }
132 
133  return names;
134 }
135 
137 {
138  return mHiddenVariables;
139 }
140 
141 void QgsExpressionContextScope::setHiddenVariables( const QStringList &hiddenVariables )
142 {
143  mHiddenVariables = hiddenVariables;
144 }
145 
146 void QgsExpressionContextScope::addHiddenVariable( const QString &hiddenVariable )
147 {
148  if ( !mHiddenVariables.contains( hiddenVariable ) )
149  mHiddenVariables << hiddenVariable;
150 }
151 
152 void QgsExpressionContextScope::removeHiddenVariable( const QString &hiddenVariable )
153 {
154  if ( mHiddenVariables.contains( hiddenVariable ) )
155  mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
156 }
157 
159 {
160  mLayerStores.append( store );
161 }
162 
163 QList<QgsMapLayerStore *> QgsExpressionContextScope::layerStores() const
164 {
165  QList<QgsMapLayerStore *> res;
166  res.reserve( mLayerStores.size() );
167  for ( QgsMapLayerStore *store : std::as_const( mLayerStores ) )
168  {
169  if ( store )
170  res << store;
171  }
172  return res;
173 }
174 
176 class QgsExpressionContextVariableCompare
177 {
178  public:
179  explicit QgsExpressionContextVariableCompare( const QgsExpressionContextScope &scope )
180  : mScope( scope )
181  { }
182 
183  bool operator()( const QString &a, const QString &b ) const
184  {
185  bool aReadOnly = mScope.isReadOnly( a );
186  bool bReadOnly = mScope.isReadOnly( b );
187  if ( aReadOnly != bReadOnly )
188  return aReadOnly;
189  return QString::localeAwareCompare( a, b ) < 0;
190  }
191 
192  private:
193  const QgsExpressionContextScope &mScope;
194 };
196 
198 {
199  QStringList allVariables = mVariables.keys();
200  QStringList filtered;
201  const auto constAllVariables = allVariables;
202  for ( const QString &variable : constAllVariables )
203  {
204  if ( variable.startsWith( '_' ) )
205  continue;
206 
207  filtered << variable;
208  }
209  QgsExpressionContextVariableCompare cmp( *this );
210  std::sort( filtered.begin(), filtered.end(), cmp );
211 
212  return filtered;
213 }
214 
215 bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
216 {
217  return hasVariable( name ) ? mVariables.value( name ).readOnly : false;
218 }
219 
220 bool QgsExpressionContextScope::isStatic( const QString &name ) const
221 {
222  return hasVariable( name ) ? mVariables.value( name ).isStatic : false;
223 }
224 
225 QString QgsExpressionContextScope::description( const QString &name ) const
226 {
227  return hasVariable( name ) ? mVariables.value( name ).description : QString();
228 }
229 
230 bool QgsExpressionContextScope::hasFunction( const QString &name ) const
231 {
232  return mFunctions.contains( name );
233 }
234 
236 {
237  return mFunctions.contains( name ) ? mFunctions.value( name ) : nullptr;
238 }
239 
241 {
242  return mFunctions.keys();
243 }
244 
246 {
247  mFunctions.insert( name, function );
248 }
249 
250 
252 {
253  addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
254 }
255 
256 void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
257 {
258  const QDomNodeList variablesNodeList = element.childNodes();
259  for ( int i = 0; i < variablesNodeList.size(); ++i )
260  {
261  const QDomElement variableElement = variablesNodeList.at( i ).toElement();
262  const QString key = variableElement.attribute( QStringLiteral( "name" ) );
263  if ( variableElement.tagName() == QLatin1String( "Variable" ) )
264  {
265  const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
266  setVariable( key, value );
267  }
268  else
269  addHiddenVariable( key );
270 
271  }
272 }
273 
274 bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
275 {
276  for ( auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
277  {
278  QDomElement varElem = document.createElement( QStringLiteral( "Variable" ) );
279  varElem.setAttribute( QStringLiteral( "name" ), it.key() );
280  QDomElement valueElem = QgsXmlUtils::writeVariant( it.value().value, document );
281  varElem.appendChild( valueElem );
282  element.appendChild( varElem );
283  }
284 
285  for ( QString hiddenVariable : mHiddenVariables )
286  {
287  QDomElement varElem = document.createElement( QStringLiteral( "HiddenVariable" ) );
288  varElem.setAttribute( QStringLiteral( "name" ), hiddenVariable );
289  element.appendChild( varElem );
290  }
291  return true;
292 }
293 
294 
295 //
296 // QgsExpressionContext
297 //
298 
300 {
301  mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
302 }
303 
304 QgsExpressionContext::QgsExpressionContext( const QList<QgsExpressionContextScope *> &scopes )
305  : mStack( scopes )
306 {
307  mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
308 }
309 
311 {
312  for ( const QgsExpressionContextScope *scope : std::as_const( other.mStack ) )
313  {
314  mStack << new QgsExpressionContextScope( *scope );
315  }
316  mHighlightedVariables = other.mHighlightedVariables;
317  mHighlightedFunctions = other.mHighlightedFunctions;
318  mCachedValues = other.mCachedValues;
319  mFeedback = other.mFeedback;
320  mDestinationStore = other.mDestinationStore;
321  mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
322 }
323 
324 // cppcheck-suppress operatorEqVarError
326 {
327  if ( this != &other )
328  {
329  qDeleteAll( mStack );
330  // move the stack over
331  mStack = other.mStack;
332  other.mStack.clear();
333 
334  mHighlightedVariables = other.mHighlightedVariables;
335  mHighlightedFunctions = other.mHighlightedFunctions;
336  mCachedValues = other.mCachedValues;
337  mFeedback = other.mFeedback;
338  mDestinationStore = other.mDestinationStore;
339  }
340  return *this;
341 }
342 
343 // cppcheck-suppress operatorEqVarError
345 {
346  if ( &other == this )
347  return *this;
348 
349  qDeleteAll( mStack );
350  mStack.clear();
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  return *this;
361 }
362 
364 {
365  qDeleteAll( mStack );
366  mStack.clear();
367 }
368 
369 bool QgsExpressionContext::hasVariable( const QString &name ) const
370 {
371  const auto constMStack = mStack;
372  for ( const QgsExpressionContextScope *scope : constMStack )
373  {
374  if ( scope->hasVariable( name ) )
375  return true;
376  }
377  return false;
378 }
379 
380 QVariant QgsExpressionContext::variable( const QString &name ) const
381 {
383  return scope ? scope->variable( name ) : QVariant();
384 }
385 
387 {
388  QStringList names = variableNames();
389  QVariantMap m;
390  const auto constNames = names;
391  for ( const QString &name : constNames )
392  {
393  m.insert( name, variable( name ) );
394  }
395  return m;
396 }
397 
398 bool QgsExpressionContext::isHighlightedVariable( const QString &name ) const
399 {
400  return mHighlightedVariables.contains( name );
401 }
402 
404 {
405  return mHighlightedVariables;
406 }
407 
408 void QgsExpressionContext::setHighlightedVariables( const QStringList &variableNames )
409 {
410  mHighlightedVariables = variableNames;
411 }
412 
413 bool QgsExpressionContext::isHighlightedFunction( const QString &name ) const
414 {
415  return mHighlightedFunctions.contains( name );
416 }
417 
418 void QgsExpressionContext::setHighlightedFunctions( const QStringList &names )
419 {
420  mHighlightedFunctions = names;
421 }
422 
424 {
425  //iterate through stack backwards, so that higher priority variables take precedence
426  QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
427  while ( it != mStack.constBegin() )
428  {
429  --it;
430  if ( ( *it )->hasVariable( name ) )
431  return ( *it );
432  }
433  return nullptr;
434 }
435 
437 {
438  //iterate through stack backwards, so that higher priority variables take precedence
439  QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
440  while ( it != mStack.constBegin() )
441  {
442  --it;
443  if ( ( *it )->hasVariable( name ) )
444  return ( *it );
445  }
446  return nullptr;
447 }
448 
450 {
451  if ( index < 0 || index >= mStack.count() )
452  return nullptr;
453 
454  return mStack.at( index );
455 }
456 
458 {
459  if ( mStack.count() < 1 )
460  return nullptr;
461 
462  return mStack.last();
463 }
464 
466 {
467  if ( !scope )
468  return -1;
469 
470  return mStack.indexOf( scope );
471 }
472 
473 int QgsExpressionContext::indexOfScope( const QString &scopeName ) const
474 {
475  int index = 0;
476  const auto constMStack = mStack;
477  for ( const QgsExpressionContextScope *scope : constMStack )
478  {
479  if ( scope->name() == scopeName )
480  return index;
481 
482  index++;
483  }
484  return -1;
485 }
486 
488 {
489  QSet< QString> names;
490  for ( const QgsExpressionContextScope *scope : mStack )
491  {
492  const QStringList variableNames = scope->variableNames();
493  for ( const QString &name : variableNames )
494  names.insert( name );
495  }
496  return QStringList( names.constBegin(), names.constEnd() );
497 }
498 
500 {
501  QStringList allVariables = variableNames();
502  QStringList filtered;
503  const auto constAllVariables = allVariables;
504 
505  QStringList hiddenVariables;
506 
507  for ( const QgsExpressionContextScope *scope : mStack )
508  {
509  const QStringList scopeHiddenVariables = scope->hiddenVariables();
510  for ( const QString &name : scopeHiddenVariables )
511  hiddenVariables << name ;
512  }
513 
514  for ( const QString &variable : constAllVariables )
515  {
516  if ( variable.startsWith( '_' ) ||
517  hiddenVariables.contains( variable ) )
518  continue;
519 
520  filtered << variable;
521  }
522 
523  filtered.sort();
524  return filtered;
525 }
526 
527 bool QgsExpressionContext::isReadOnly( const QString &name ) const
528 {
529  const auto constMStack = mStack;
530  for ( const QgsExpressionContextScope *scope : constMStack )
531  {
532  if ( scope->isReadOnly( name ) )
533  return true;
534  }
535  return false;
536 }
537 
538 QString QgsExpressionContext::description( const QString &name ) const
539 {
541  return ( scope && !scope->description( name ).isEmpty() ) ? scope->description( name ) : QgsExpression::variableHelpText( name );
542 }
543 
544 bool QgsExpressionContext::hasFunction( const QString &name ) const
545 {
546  if ( name.compare( QLatin1String( "load_layer" ) ) == 0 && mDestinationStore )
547  return true;
548 
549  for ( const QgsExpressionContextScope *scope : mStack )
550  {
551  if ( scope->hasFunction( name ) )
552  return true;
553  }
554  return false;
555 }
556 
558 {
559  QSet< QString > result;
560  for ( const QgsExpressionContextScope *scope : mStack )
561  {
562  const QStringList functionNames = scope->functionNames();
563  for ( const QString &name : functionNames )
564  result.insert( name );
565  }
566 
567  if ( mDestinationStore )
568  result.insert( QStringLiteral( "load_layer" ) );
569 
570  QStringList listResult( result.constBegin(), result.constEnd() );
571  listResult.sort();
572  return listResult;
573 }
574 
576 {
577  if ( name.compare( QLatin1String( "load_layer" ) ) == 0 && mDestinationStore )
578  {
579  return mLoadLayerFunction.get();
580  }
581 
582  //iterate through stack backwards, so that higher priority variables take precedence
583  QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
584  while ( it != mStack.constBegin() )
585  {
586  --it;
587  if ( ( *it )->hasFunction( name ) )
588  return ( *it )->function( name );
589  }
590  return nullptr;
591 }
592 
594 {
595  return mStack.count();
596 }
597 
599 {
600  mStack.append( scope );
601 }
602 
603 void QgsExpressionContext::appendScopes( const QList<QgsExpressionContextScope *> &scopes )
604 {
605  mStack.append( scopes );
606 }
607 
609 {
610  if ( !mStack.isEmpty() )
611  return mStack.takeLast();
612 
613  return nullptr;
614 }
615 
616 QList<QgsExpressionContextScope *> QgsExpressionContext::takeScopes()
617 {
618  QList<QgsExpressionContextScope *> stack = mStack;
619  mStack.clear();
620  return stack;
621 }
622 
624 {
625  mStack.append( scope );
626  return *this;
627 }
628 
630 {
631  if ( mStack.isEmpty() )
632  mStack.append( new QgsExpressionContextScope() );
633 
634  mStack.last()->setFeature( feature );
635 }
636 
638 {
639  for ( const QgsExpressionContextScope *scope : mStack )
640  {
641  if ( scope->hasFeature() )
642  return true;
643  }
644  return false;
645 }
646 
648 {
649  //iterate through stack backwards, so that higher priority variables take precedence
650  QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
651  while ( it != mStack.constBegin() )
652  {
653  --it;
654  if ( ( *it )->hasFeature() )
655  return ( *it )->feature();
656  }
657  return QgsFeature();
658 }
659 
661 {
662  if ( mStack.isEmpty() )
663  mStack.append( new QgsExpressionContextScope() );
664 
665  mStack.last()->setGeometry( geometry );
666 }
667 
669 {
670  for ( const QgsExpressionContextScope *scope : mStack )
671  {
672  if ( scope->hasGeometry() )
673  return true;
674  }
675  return false;
676 }
677 
679 {
680  //iterate through stack backwards, so that higher priority variables take precedence
681  QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
682  while ( it != mStack.constBegin() )
683  {
684  --it;
685  if ( ( *it )->hasGeometry() )
686  return ( *it )->geometry();
687  }
688  return QgsGeometry();
689 }
690 
692 {
693  if ( mStack.isEmpty() )
694  mStack.append( new QgsExpressionContextScope() );
695 
696  mStack.last()->setFields( fields );
697 }
698 
700 {
701  return qvariant_cast<QgsFields>( variable( QgsExpressionContext::EXPR_FIELDS ) );
702 }
703 
705 {
706  if ( mStack.isEmpty() )
707  mStack.append( new QgsExpressionContextScope() );
708 
710  value, true ) );
711 }
712 
713 void QgsExpressionContext::setCachedValue( const QString &key, const QVariant &value ) const
714 {
715  mCachedValues.insert( key, value );
716 }
717 
718 bool QgsExpressionContext::hasCachedValue( const QString &key ) const
719 {
720  return mCachedValues.contains( key );
721 }
722 
723 QVariant QgsExpressionContext::cachedValue( const QString &key ) const
724 {
725  return mCachedValues.value( key, QVariant() );
726 }
727 
729 {
730  mCachedValues.clear();
731 }
732 
733 QList<QgsMapLayerStore *> QgsExpressionContext::layerStores() const
734 {
735  //iterate through stack backwards, so that higher priority layer stores take precedence
736  QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
737  QList<QgsMapLayerStore *> res;
738  while ( it != mStack.constBegin() )
739  {
740  --it;
741  res.append( ( *it )->layerStores() );
742  }
743  // ensure that the destination store is also present in the list
744  if ( mDestinationStore && !res.contains( mDestinationStore ) )
745  res.append( mDestinationStore );
746  return res;
747 }
748 
750 {
751  mDestinationStore = store;
752 }
753 
755 {
756  return mDestinationStore;
757 }
758 
760 {
761  mFeedback = feedback;
762 }
763 
765 {
766  return mFeedback;
767 }
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addLayerStore(QgsMapLayerStore *store)
Adds a layer store to the scope.
bool hasVariable(const QString &name) const
Tests whether a variable with the specified name exists in the scope.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
QString description(const QString &name) const
Returns the translated description for the variable with the specified name (if set).
void setHiddenVariables(const QStringList &hiddenVariables)
Sets the list of variables intended to be hidden in the expression builder dialog and widget.
void addHiddenVariable(const QString &hiddenVariable)
Adds the passed variable to a list of hidden variables that won't be visible in the expression builde...
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads scope variables from an XML element.
QStringList hiddenVariables() const
Returns the list of variables hidden within the scope.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes scope variables to an XML element.
QgsExpressionFunction * function(const QString &name) const
Retrieves a function from the scope.
QVariant variable(const QString &name) const
Retrieves a variable's value from the scope.
QList< QgsMapLayerStore * > layerStores() const
Returns the list of layer stores associated with the scope.
void removeHiddenVariable(const QString &hiddenVariable)
Removes the passed variable from a list of hidden variables.
bool removeVariable(const QString &name)
Removes a variable from the context scope, if found.
bool isReadOnly(const QString &name) const
Tests whether the specified variable is read only and should not be editable by users.
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
bool hasFeature() const
Returns true if the scope has a feature associated with it.
bool hasFunction(const QString &name) const
Tests whether a function with the specified name exists in the scope.
QString name() const
Returns the friendly display name of the context scope.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
bool isStatic(const QString &name) const
Tests whether the variable with the specified name is static and can be cached.
bool hasGeometry() const
Returns true if the scope has a geometry associated with it.
QStringList filteredVariableNames() const
Returns a filtered and sorted list of variable names contained within the scope.
QStringList functionNames() const
Retrieves a list of names of functions contained in the scope.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
QgsExpressionContextScope & operator=(const QgsExpressionContextScope &other)
QgsExpressionContextScope(const QString &name=QString())
Constructor for QgsExpressionContextScope.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static const QString EXPR_GEOMETRY_PART_COUNT
Inbuilt variable name for geometry part count variable.
bool hasFunction(const QString &name) const
Checks whether a specified function is contained in the context.
QString description(const QString &name) const
Returns a translated description string for the variable with specified name.
static const QString EXPR_GEOMETRY_POINT_COUNT
Inbuilt variable name for point count variable.
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
QStringList highlightedVariables() const
Returns the current list of variables highlighted within the context.
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
QStringList functionNames() const
Retrieves a list of function names contained in the context.
static const QString EXPR_GEOMETRY_POINT_NUM
Inbuilt variable name for point number variable.
int indexOfScope(QgsExpressionContextScope *scope) const
Returns the index of the specified scope if it exists within the context.
void setCachedValue(const QString &key, const QVariant &value) const
Sets a value to cache within the expression context.
void clearCachedValues() const
Clears all cached values from the context.
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
QgsGeometry geometry() const
Convenience function for retrieving the geometry for the context, if set.
bool isHighlightedFunction(const QString &name) const
Returns true if the specified function name is intended to be highlighted to the user.
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
QgsExpressionContext & operator=(const QgsExpressionContext &other)
static const QString EXPR_FIELDS
Inbuilt variable name for fields storage.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
QList< QgsMapLayerStore * > layerStores() const
Returns the list of layer stores associated with the context.
void setGeometry(const QgsGeometry &geometry)
Convenience function for setting a geometry for the context.
static const QString EXPR_GEOMETRY_RING_NUM
Inbuilt variable name for geometry ring number variable.
bool isHighlightedVariable(const QString &name) const
Returns true if the specified variable name is intended to be highlighted to the user.
QgsExpressionContextScope * activeScopeForVariable(const QString &name)
Returns the currently active scope from the context for a specified variable name.
static const QString EXPR_GEOMETRY_PART_NUM
Inbuilt variable name for geometry part number variable.
QgsMapLayerStore * loadedLayerStore() const
Returns the destination layer store for any layers loaded during expression evaluation.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
QList< QgsExpressionContextScope * > scopes()
Returns a list of scopes contained within the stack.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
bool hasVariable(const QString &name) const
Check whether a variable is specified by any scope within the context.
QList< QgsExpressionContextScope * > takeScopes()
Returns all scopes from this context and remove them, leaving this context without any context.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly by the expression to check if evaluation sh...
void setFeedback(QgsFeedback *feedback)
Attach a feedback object that can be queried regularly by the expression engine to check if expressio...
int scopeCount() const
Returns the number of scopes contained in the context.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
bool isReadOnly(const QString &name) const
Returns whether a variable is read only, and should not be modifiable by users.
bool hasGeometry() const
Returns true if the context has a geometry associated with it.
QgsExpressionFunction * function(const QString &name) const
Fetches a matching function from the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
bool hasCachedValue(const QString &key) const
Returns true if the expression context contains a cached value with a matching key.
void setLoadedLayerStore(QgsMapLayerStore *store)
Sets the destination layer store for any layers loaded during expression evaluation.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
QgsExpressionContext & operator<<(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
static const QString EXPR_CLUSTER_COLOR
Inbuilt variable name for cluster color variable.
QStringList variableNames() const
Returns a list of variables names set by all scopes in the context.
QgsExpressionContext()
Constructor for QgsExpressionContext.
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
QgsExpressionContextScope * scope(int index)
Returns the scope at the specified index within the context.
QVariant cachedValue(const QString &key) const
Returns the matching cached value, if set.
bool hasFeature() const
Returns true if the context has a feature associated with it.
QgsFields fields() const
Convenience function for retrieving the fields for the context, if set.
A abstract base class for defining QgsExpression functions.
static QString variableHelpText(const QString &variableName)
Returns the help text for a specified variable.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
The class is used as a container of context for various read/write operations on other objects.
Expression function for use within a QgsExpressionContextScope.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
Single variable definition for use within a QgsExpressionContextScope.