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 Q_FOREACH (
const QString &
variable, allVariables )
152 if ( variable.startsWith(
'_' ) )
157 QgsExpressionContextVariableCompare cmp( *
this );
158 std::sort( filtered.begin(), filtered.end(), cmp );
165 return hasVariable( name ) ? mVariables.value( name ).readOnly :
false;
170 return hasVariable( name ) ? mVariables.value( name ).isStatic :
false;
175 return hasVariable( name ) ? mVariables.value( name ).description : QString();
180 return mFunctions.contains( name );
185 return mFunctions.contains( name ) ? mFunctions.value( name ) :
nullptr;
190 return mFunctions.keys();
195 mFunctions.insert( name,
function );
206 const QDomNodeList variablesNodeList = element.childNodes();
207 for (
int i = 0; i < variablesNodeList.size(); ++i )
209 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
210 const QString key = variableElement.attribute( QStringLiteral(
"name" ) );
218 for (
auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
220 QDomElement varElem = document.createElement( QStringLiteral(
"Variable" ) );
221 varElem.setAttribute( QStringLiteral(
"name" ), it.key() );
223 varElem.appendChild( valueElem );
224 element.appendChild( varElem );
245 mHighlightedVariables = other.mHighlightedVariables;
246 mHighlightedFunctions = other.mHighlightedFunctions;
247 mCachedValues = other.mCachedValues;
252 if (
this != &other )
254 qDeleteAll( mStack );
256 mStack = other.mStack;
257 other.mStack.clear();
259 mHighlightedVariables = other.mHighlightedVariables;
260 mHighlightedFunctions = other.mHighlightedFunctions;
261 mCachedValues = other.mCachedValues;
268 qDeleteAll( mStack );
274 mHighlightedVariables = other.mHighlightedVariables;
275 mHighlightedFunctions = other.mHighlightedFunctions;
276 mCachedValues = other.mCachedValues;
282 qDeleteAll( mStack );
299 return scope ? scope->
variable( name ) : QVariant();
306 Q_FOREACH (
const QString &name, names )
315 return mHighlightedVariables.contains( name );
325 return mHighlightedFunctions.contains( name );
330 mHighlightedFunctions = names;
336 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
337 while ( it != mStack.constBegin() )
340 if ( ( *it )->hasVariable( name ) )
349 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
350 while ( it != mStack.constBegin() )
353 if ( ( *it )->hasVariable( name ) )
361 if ( index < 0 || index >= mStack.count() )
364 return mStack.at( index );
369 if ( mStack.count() < 1 )
372 return mStack.last();
380 return mStack.indexOf( scope );
388 if ( scope->
name() == scopeName )
403 return names.toSet().toList();
409 QStringList filtered;
410 Q_FOREACH (
const QString &
variable, allVariables )
412 if ( variable.startsWith(
'_' ) )
455 result = result.toSet().toList();
463 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
464 while ( it != mStack.constBegin() )
467 if ( ( *it )->hasFunction( name ) )
468 return ( *it )->function( name );
475 return mStack.count();
480 mStack.append( scope );
485 mStack.append( scopes );
490 if ( !mStack.isEmpty() )
491 return mStack.takeLast();
498 QList<QgsExpressionContextScope *> stack = mStack;
505 mStack.append( scope );
511 if ( mStack.isEmpty() )
514 mStack.last()->setFeature( feature );
530 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
531 while ( it != mStack.constBegin() )
534 if ( ( *it )->hasFeature() )
535 return ( *it )->feature();
542 if ( mStack.isEmpty() )
545 mStack.last()->setFields( fields );
555 if ( mStack.isEmpty() )
564 mCachedValues.insert( key, value );
569 return mCachedValues.contains( key );
574 return mCachedValues.value( key, QVariant() );
579 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.
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.