44 : mName( other.mName )
45 , mVariables( other.mVariables )
46 , mHasFeature( other.mHasFeature )
47 , mFeature( other.mFeature )
48 , mHasGeometry( other.mHasGeometry )
49 , mGeometry( other.mGeometry )
51 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
52 for ( ; it != other.mFunctions.constEnd(); ++it )
54 mFunctions.insert( it.key(), it.value()->clone() );
61 mVariables = other.mVariables;
62 mHasFeature = other.mHasFeature;
63 mFeature = other.mFeature;
64 mHasGeometry = other.mHasGeometry;
65 mGeometry = other.mGeometry;
67 qDeleteAll( mFunctions );
69 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
70 for ( ; it != other.mFunctions.constEnd(); ++it )
72 mFunctions.insert( it.key(), it.value()->clone() );
80 qDeleteAll( mFunctions );
85 auto it = mVariables.find(
name );
86 if ( it != mVariables.end() )
104 return mVariables.remove(
name ) > 0;
109 return mVariables.contains(
name );
119 QStringList names = mVariables.keys();
124 class QgsExpressionContextVariableCompare
131 bool operator()(
const QString &a,
const QString &b )
const
133 bool aReadOnly = mScope.isReadOnly( a );
134 bool bReadOnly = mScope.isReadOnly( b );
135 if ( aReadOnly != bReadOnly )
137 return QString::localeAwareCompare( a, b ) < 0;
147 QStringList allVariables = mVariables.keys();
148 QStringList filtered;
149 const auto constAllVariables = allVariables;
150 for (
const QString &
variable : constAllVariables )
157 QgsExpressionContextVariableCompare cmp( *
this );
158 std::sort( filtered.begin(), filtered.end(), cmp );
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 if ( &other ==
this )
271 qDeleteAll( mStack );
277 mHighlightedVariables = other.mHighlightedVariables;
278 mHighlightedFunctions = other.mHighlightedFunctions;
279 mCachedValues = other.mCachedValues;
285 qDeleteAll( mStack );
291 const auto constMStack = mStack;
310 const auto constNames = names;
311 for (
const QString &name : constNames )
320 return mHighlightedVariables.contains( name );
325 return mHighlightedVariables;
335 return mHighlightedFunctions.contains( name );
340 mHighlightedFunctions = names;
346 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
347 while ( it != mStack.constBegin() )
350 if ( ( *it )->hasVariable( name ) )
359 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
360 while ( it != mStack.constBegin() )
363 if ( ( *it )->hasVariable( name ) )
371 if ( index < 0 || index >= mStack.count() )
374 return mStack.at( index );
379 if ( mStack.count() < 1 )
382 return mStack.last();
390 return mStack.indexOf(
scope );
396 const auto constMStack = mStack;
410 const auto constMStack = mStack;
415 return qgis::setToList( qgis::listToSet( names ) );
421 QStringList filtered;
422 const auto constAllVariables = allVariables;
423 for (
const QString &
variable : constAllVariables )
437 const auto constMStack = mStack;
454 const auto constMStack = mStack;
466 const auto constMStack = mStack;
471 result = qgis::setToList( qgis::listToSet( result ) );
479 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
480 while ( it != mStack.constBegin() )
483 if ( ( *it )->hasFunction( name ) )
484 return ( *it )->function( name );
491 return mStack.count();
496 mStack.append(
scope );
506 if ( !mStack.isEmpty() )
507 return mStack.takeLast();
514 QList<QgsExpressionContextScope *> stack = mStack;
521 mStack.append(
scope );
527 if ( mStack.isEmpty() )
530 mStack.last()->setFeature(
feature );
546 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
547 while ( it != mStack.constBegin() )
550 if ( ( *it )->hasFeature() )
551 return ( *it )->feature();
558 if ( mStack.isEmpty() )
561 mStack.last()->setGeometry(
geometry );
577 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
578 while ( it != mStack.constBegin() )
581 if ( ( *it )->hasGeometry() )
582 return ( *it )->geometry();
589 if ( mStack.isEmpty() )
592 mStack.last()->setFields(
fields );
602 if ( mStack.isEmpty() )
611 mCachedValues.insert( key, value );
616 return mCachedValues.contains( key );
621 return mCachedValues.value( key, QVariant() );
626 mCachedValues.clear();
Single scope for storing variables and functions for use within a QgsExpressionContext.
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 readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads scope variables from an XML element.
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.
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.
~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)
QgsExpressionContext()=default
Constructor for QgsExpressionContext.
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.
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.
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 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.
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...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
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.