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 )
54 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
55 for ( ; it != other.mFunctions.constEnd(); ++it )
57 mFunctions.insert( it.key(), it.value()->clone() );
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;
72 qDeleteAll( mFunctions );
74 QHash<QString, QgsScopedExpressionFunction * >::const_iterator it = other.mFunctions.constBegin();
75 for ( ; it != other.mFunctions.constEnd(); ++it )
77 mFunctions.insert( it.key(), it.value()->clone() );
85 qDeleteAll( mFunctions );
90 auto it = mVariables.find(
name );
91 if ( it != mVariables.end() )
109#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
110 return mVariables.remove(
name );
112 return mVariables.remove(
name ) > 0;
118 return mVariables.contains(
name );
128 QStringList names = mVariables.keys();
132 names.append( QStringLiteral(
"feature" ) );
133 names.append( QStringLiteral(
"id" ) );
134 names.append( QStringLiteral(
"geometry" ) );
142 return mHiddenVariables;
152 if ( !mHiddenVariables.contains( hiddenVariable ) )
153 mHiddenVariables << hiddenVariable;
158 if ( mHiddenVariables.contains( hiddenVariable ) )
159 mHiddenVariables.removeAt( mHiddenVariables.indexOf( hiddenVariable ) );
164 mLayerStores.append( store );
169 QList<QgsMapLayerStore *> res;
170 res.reserve( mLayerStores.size() );
180class QgsExpressionContextVariableCompare
187 bool operator()(
const QString &a,
const QString &b )
const
189 bool aReadOnly = mScope.isReadOnly( a );
190 bool bReadOnly = mScope.isReadOnly( b );
191 if ( aReadOnly != bReadOnly )
193 return QString::localeAwareCompare( a, b ) < 0;
203 QStringList allVariables = mVariables.keys();
204 QStringList filtered;
205 const auto constAllVariables = allVariables;
206 for (
const QString &
variable : constAllVariables )
213 QgsExpressionContextVariableCompare cmp( *
this );
214 std::sort( filtered.begin(), filtered.end(), cmp );
236 return mFunctions.contains(
name );
241 return mFunctions.contains(
name ) ? mFunctions.value(
name ) :
nullptr;
246 return mFunctions.keys();
262 const QDomNodeList variablesNodeList = element.childNodes();
263 for (
int i = 0; i < variablesNodeList.size(); ++i )
265 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
266 const QString key = variableElement.attribute( QStringLiteral(
"name" ) );
267 if ( variableElement.tagName() == QLatin1String(
"Variable" ) )
280 for (
auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
282 QDomElement varElem = document.createElement( QStringLiteral(
"Variable" ) );
283 varElem.setAttribute( QStringLiteral(
"name" ), it.key() );
285 varElem.appendChild( valueElem );
286 element.appendChild( varElem );
289 for ( QString hiddenVariable : mHiddenVariables )
291 QDomElement varElem = document.createElement( QStringLiteral(
"HiddenVariable" ) );
292 varElem.setAttribute( QStringLiteral(
"name" ), hiddenVariable );
293 element.appendChild( varElem );
305 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
311 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
320 mHighlightedVariables = other.mHighlightedVariables;
321 mHighlightedFunctions = other.mHighlightedFunctions;
322 mCachedValues = other.mCachedValues;
323 mFeedback = other.mFeedback;
324 mDestinationStore = other.mDestinationStore;
325 mLoadLayerFunction = std::make_unique< LoadLayerFunction >();
331 if (
this != &other )
333 qDeleteAll( mStack );
335 mStack = other.mStack;
336 other.mStack.clear();
338 mHighlightedVariables = other.mHighlightedVariables;
339 mHighlightedFunctions = other.mHighlightedFunctions;
340 mCachedValues = other.mCachedValues;
341 mFeedback = other.mFeedback;
342 mDestinationStore = other.mDestinationStore;
350 if ( &other ==
this )
353 qDeleteAll( mStack );
359 mHighlightedVariables = other.mHighlightedVariables;
360 mHighlightedFunctions = other.mHighlightedFunctions;
361 mCachedValues = other.mCachedValues;
362 mFeedback = other.mFeedback;
363 mDestinationStore = other.mDestinationStore;
369 qDeleteAll( mStack );
375 const auto constMStack = mStack;
394 const auto constNames = names;
395 for (
const QString &name : constNames )
404 return mHighlightedVariables.contains( name );
409 return mHighlightedVariables;
419 return mHighlightedFunctions.contains( name );
424 mHighlightedFunctions = names;
430 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
431 while ( it != mStack.constBegin() )
434 if ( ( *it )->hasVariable( name ) )
443 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
444 while ( it != mStack.constBegin() )
447 if ( ( *it )->hasVariable( name ) )
455 if ( index < 0 || index >= mStack.count() )
458 return mStack.at( index );
463 if ( mStack.count() < 1 )
466 return mStack.last();
474 return mStack.indexOf(
scope );
480 const auto constMStack = mStack;
493 QSet< QString> names;
498 names.insert( name );
500 return QStringList( names.constBegin(), names.constEnd() );
506 QStringList filtered;
507 const auto constAllVariables = allVariables;
509 QStringList hiddenVariables;
514 for (
const QString &name : scopeHiddenVariables )
515 hiddenVariables << name ;
518 for (
const QString &
variable : constAllVariables )
521 hiddenVariables.contains(
variable ) )
533 const auto constMStack = mStack;
550 if ( name.compare( QLatin1String(
"load_layer" ) ) == 0 && mDestinationStore )
563 QSet< QString > result;
568 result.insert( name );
571 if ( mDestinationStore )
572 result.insert( QStringLiteral(
"load_layer" ) );
574 QStringList listResult( result.constBegin(), result.constEnd() );
581 if ( name.compare( QLatin1String(
"load_layer" ) ) == 0 && mDestinationStore )
583 return mLoadLayerFunction.get();
587 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
588 while ( it != mStack.constBegin() )
591 if ( ( *it )->hasFunction( name ) )
592 return ( *it )->function( name );
599 return mStack.count();
604 mStack.append(
scope );
614 if ( !mStack.isEmpty() )
615 return mStack.takeLast();
622 QList<QgsExpressionContextScope *> stack = mStack;
629 mStack.append(
scope );
635 if ( mStack.isEmpty() )
638 mStack.last()->setFeature(
feature );
654 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
655 while ( it != mStack.constBegin() )
658 if ( ( *it )->hasFeature() )
659 return ( *it )->feature();
666 if ( mStack.isEmpty() )
669 mStack.last()->setGeometry(
geometry );
685 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
686 while ( it != mStack.constBegin() )
689 if ( ( *it )->hasGeometry() )
690 return ( *it )->geometry();
697 if ( mStack.isEmpty() )
700 mStack.last()->setFields(
fields );
710 if ( mStack.isEmpty() )
719 mCachedValues.insert( key, value );
724 return mCachedValues.contains( key );
729 return mCachedValues.value( key, QVariant() );
734 mCachedValues.clear();
740 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
741 QList<QgsMapLayerStore *> res;
742 while ( it != mStack.constBegin() )
745 res.append( ( *it )->layerStores() );
748 if ( mDestinationStore && !res.contains( mDestinationStore ) )
749 res.append( mDestinationStore );
755 mDestinationStore = store;
760 return mDestinationStore;
777 const QString delimiter( QStringLiteral(
"||~~||" ) );
781 hash.append( QString::number(
feature().
id() ) + delimiter + QString::number(
qHash(
feature() ) ) + delimiter );
784 QStringList sortedVars = qgis::setToList( variables );
785 if ( sortedVars.empty() )
787 std::sort( sortedVars.begin(), sortedVars.end() );
789 for (
const QString &variableName : std::as_const( sortedVars ) )
791 const QVariant value =
variable( variableName );
792 hash.append( variableName +
"=" );
795 hash.append( delimiter );
797 else if ( value.type() == QVariant::String )
799 hash.append( value.toString() + delimiter );
803 const QString variantString = value.toString();
804 if ( variantString.isEmpty() )
809 hash.append( variantString + delimiter );
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.
~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.
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.
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.
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 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.
Single variable definition for use within a QgsExpressionContextScope.