43 : mName( other.mName )
44 , mVariables( other.mVariables )
45 , mHasFeature( other.mHasFeature )
46 , mFeature( other.mFeature )
48 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
49 for ( ; it != other.mFunctions.constEnd(); ++it )
51 mFunctions.insert( it.key(), it.value()->clone() );
58 mVariables = other.mVariables;
59 mHasFeature = other.mHasFeature;
60 mFeature = other.mFeature;
62 qDeleteAll( mFunctions );
64 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
65 for ( ; it != other.mFunctions.constEnd(); ++it )
67 mFunctions.insert( it.key(), it.value()->clone() );
75 qDeleteAll( mFunctions );
80 if ( mVariables.contains( name ) )
83 existing.
value = value;
95 mVariables.insert( variable.
name, variable );
100 return mVariables.remove( name ) > 0;
105 return mVariables.contains( name );
110 return hasVariable( name ) ? mVariables.value( name ).value : QVariant();
115 QStringList names = mVariables.keys();
119 bool QgsExpressionContextScope::variableNameSort(
const QString &a,
const QString &b )
121 return QString::localeAwareCompare( a, b ) < 0;
125 class QgsExpressionContextVariableCompare
132 bool operator()(
const QString &a,
const QString &b )
const 134 bool aReadOnly = mScope.isReadOnly( a );
135 bool bReadOnly = mScope.isReadOnly( b );
136 if ( aReadOnly != bReadOnly )
138 return QString::localeAwareCompare( a, b ) < 0;
148 QStringList allVariables = mVariables.keys();
149 QStringList filtered;
150 const auto constAllVariables = allVariables;
151 for (
const QString &
variable : constAllVariables )
158 QgsExpressionContextVariableCompare cmp( *
this );
159 std::sort( filtered.begin(), filtered.end(), cmp );
166 return hasVariable( name ) ? mVariables.value( name ).readOnly :
false;
171 return hasVariable( name ) ? mVariables.value( name ).isStatic :
false;
176 return hasVariable( name ) ? mVariables.value( name ).description : QString();
181 return mFunctions.contains( name );
186 return mFunctions.contains( name ) ? mFunctions.value( name ) :
nullptr;
191 return mFunctions.keys();
196 mFunctions.insert( name,
function );
207 const QDomNodeList variablesNodeList = element.childNodes();
208 for (
int i = 0; i < variablesNodeList.size(); ++i )
210 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
211 const QString key = variableElement.attribute( QStringLiteral(
"name" ) );
219 for (
auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
221 QDomElement varElem = document.createElement( QStringLiteral(
"Variable" ) );
222 varElem.setAttribute( QStringLiteral(
"name" ), it.key() );
224 varElem.appendChild( valueElem );
225 element.appendChild( varElem );
246 mHighlightedVariables = other.mHighlightedVariables;
247 mHighlightedFunctions = other.mHighlightedFunctions;
248 mCachedValues = other.mCachedValues;
253 if (
this != &other )
255 qDeleteAll( mStack );
257 mStack = other.mStack;
258 other.mStack.clear();
260 mHighlightedVariables = other.mHighlightedVariables;
261 mHighlightedFunctions = other.mHighlightedFunctions;
262 mCachedValues = other.mCachedValues;
269 qDeleteAll( mStack );
275 mHighlightedVariables = other.mHighlightedVariables;
276 mHighlightedFunctions = other.mHighlightedFunctions;
277 mCachedValues = other.mCachedValues;
283 qDeleteAll( mStack );
289 const auto constMStack = mStack;
301 return scope ? scope->
variable( name ) : QVariant();
308 const auto constNames = names;
309 for (
const QString &name : constNames )
318 return mHighlightedVariables.contains( name );
323 return mHighlightedVariables;
333 return mHighlightedFunctions.contains( name );
338 mHighlightedFunctions = names;
344 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
345 while ( it != mStack.constBegin() )
348 if ( ( *it )->hasVariable( name ) )
357 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
358 while ( it != mStack.constBegin() )
361 if ( ( *it )->hasVariable( name ) )
369 if ( index < 0 || index >= mStack.count() )
372 return mStack.at( index );
377 if ( mStack.count() < 1 )
380 return mStack.last();
388 return mStack.indexOf( scope );
394 const auto constMStack = mStack;
408 const auto constMStack = mStack;
413 return names.toSet().toList();
419 QStringList filtered;
420 const auto constAllVariables = allVariables;
421 for (
const QString &
variable : constAllVariables )
435 const auto constMStack = mStack;
452 const auto constMStack = mStack;
464 const auto constMStack = mStack;
469 result = result.toSet().toList();
477 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
478 while ( it != mStack.constBegin() )
481 if ( ( *it )->hasFunction( name ) )
482 return ( *it )->function( name );
489 return mStack.count();
494 mStack.append( scope );
499 mStack.append( scopes );
504 if ( !mStack.isEmpty() )
505 return mStack.takeLast();
512 QList<QgsExpressionContextScope *> stack = mStack;
519 mStack.append( scope );
525 if ( mStack.isEmpty() )
528 mStack.last()->setFeature( feature );
533 const auto constMStack = mStack;
545 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
546 while ( it != mStack.constBegin() )
549 if ( ( *it )->hasFeature() )
550 return ( *it )->feature();
557 if ( mStack.isEmpty() )
560 mStack.last()->setFields( fields );
570 if ( mStack.isEmpty() )
579 mCachedValues.insert( key, value );
584 return mCachedValues.contains( key );
589 return mCachedValues.value( key, QVariant() );
594 mCachedValues.clear();
bool hasVariable(const QString &name) const
Tests whether a variable with the specified name exists in the scope.
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
QVariant cachedValue(const QString &key) const
Returns the matching cached value, if set.
The class is used as a container of context for various read/write operations on other objects...
QString description(const QString &name) const
Returns the translated description for the variable with the specified name (if set).
static const QString EXPR_CLUSTER_COLOR
Inbuilt variable name for cluster color variable.
Single variable definition for use within a QgsExpressionContextScope.
void setCachedValue(const QString &key, const QVariant &value) const
Sets a value to cache within the expression context.
bool isHighlightedFunction(const QString &name) const
Returns true if the specified function name is intended to be highlighted to the user.
QgsExpressionContextScope * scope(int index)
Returns the scope at the specified index within the context.
bool isReadOnly(const QString &name) const
Returns whether a variable is read only, and should not be modifiable by users.
static const QString EXPR_GEOMETRY_POINT_COUNT
Inbuilt variable name for point count variable.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads scope variables from an XML element.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
QgsExpressionContext & operator=(const QgsExpressionContext &other)
bool hasFunction(const QString &name) const
Checks whether a specified function is contained in the context.
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
QgsExpressionContextScope(const QString &name=QString())
Constructor for QgsExpressionContextScope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsExpressionContextScope * activeScopeForVariable(const QString &name)
Returns the currently active scope from the context for a specified variable name.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
bool hasCachedValue(const QString &key) const
Returns true if the expression context contains a cached value with a matching key.
bool isReadOnly(const QString &name) const
Tests whether the specified variable is read only and should not be editable by users.
Container of fields for a vector layer.
bool hasVariable(const QString &name) const
Check whether a variable is specified by any scope within the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
QgsFields fields() const
Convenience function for retrieving the fields for the context, if set.
static QString variableHelpText(const QString &variableName)
Returns the help text for a specified variable.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsExpressionContextScope & operator=(const QgsExpressionContextScope &other)
QList< QgsExpressionContextScope * > takeScopes()
Returns all scopes from this context and remove them, leaving this context without any context...
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
QgsExpressionContext & operator<<(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
QString description(const QString &name) const
Returns a translated description string for the variable with specified name.
bool hasFeature() const
Returns true if the context has a feature associated with it.
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
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()
bool removeVariable(const QString &name)
Removes a variable from the context scope, if found.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
bool hasFunction(const QString &name) const
Tests whether a function with the specified name exists in the scope.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
int scopeCount() const
Returns the number of scopes contained in the context.
bool isHighlightedVariable(const QString &name) const
Returns true if the specified variable name is intended to be highlighted to the user.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
QVariant variable(const QString &name) const
Retrieves a variable's value from the scope.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
QgsExpressionFunction * function(const QString &name) const
Retrieves a function from the scope.
static const QString EXPR_FIELDS
Inbuilt variable name for fields storage.
QStringList variableNames() const
Returns a list of variables names set by all scopes in the context.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QStringList functionNames() const
Retrieves a list of names of functions contained in the scope.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes scope variables to an XML element.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QStringList highlightedVariables() const
Returns the current list of variables highlighted within the context.
A abstract base class for defining QgsExpression functions.
QStringList functionNames() const
Retrieves a list of function names contained in the context.
QgsExpressionContext()=default
Constructor for QgsExpressionContext.
void clearCachedValues() const
Clears all cached values from the context.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
bool isStatic(const QString &name) const
Tests whether the variable with the specified name is static and can be cached.
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
bool hasFeature() const
Returns true if the scope has a feature associated with it.
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
static const QString EXPR_GEOMETRY_POINT_NUM
Inbuilt variable name for point number variable.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
void appendScopes(const QList< QgsExpressionContextScope *> &scopes)
Appends a list of scopes to the end of the context.
QString name() const
Returns the friendly display name of the context scope.
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
static const QString EXPR_GEOMETRY_PART_NUM
Inbuilt variable name for geometry part number variable.
bool isStatic
A static variable can be cached for the lifetime of a context.
int indexOfScope(QgsExpressionContextScope *scope) const
Returns the index of the specified scope if it exists within the context.
static const QString EXPR_GEOMETRY_PART_COUNT
Inbuilt variable name for geometry part count variable.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
QStringList filteredVariableNames() const
Returns a filtered and sorted list of variable names contained within the scope.
QList< QgsExpressionContextScope *> scopes()
Returns a list of scopes contained within the stack.
QgsExpressionFunction * function(const QString &name) const
Fetches a matching function from the context.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
Expression function for use within a QgsExpressionContextScope.
QString name
Variable name.
QVariant value
Variable value.