QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
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;
100 return mVariables.remove(
name ) > 0;
105 return mVariables.contains(
name );
115 QStringList names = mVariables.keys();
120 class QgsExpressionContextVariableCompare
127 bool operator()(
const QString &a,
const QString &b )
const
129 bool aReadOnly = mScope.isReadOnly( a );
130 bool bReadOnly = mScope.isReadOnly( b );
131 if ( aReadOnly != bReadOnly )
133 return QString::localeAwareCompare( a, b ) < 0;
143 QStringList allVariables = mVariables.keys();
144 QStringList filtered;
145 const auto constAllVariables = allVariables;
146 for (
const QString &
variable : constAllVariables )
153 QgsExpressionContextVariableCompare cmp( *
this );
154 std::sort( filtered.begin(), filtered.end(), cmp );
176 return mFunctions.contains(
name );
181 return mFunctions.contains(
name ) ? mFunctions.value(
name ) :
nullptr;
186 return mFunctions.keys();
191 mFunctions.insert(
name,
function );
202 const QDomNodeList variablesNodeList = element.childNodes();
203 for (
int i = 0; i < variablesNodeList.size(); ++i )
205 const QDomElement variableElement = variablesNodeList.at( i ).toElement();
206 const QString key = variableElement.attribute( QStringLiteral(
"name" ) );
214 for (
auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
216 QDomElement varElem = document.createElement( QStringLiteral(
"Variable" ) );
217 varElem.setAttribute( QStringLiteral(
"name" ), it.key() );
219 varElem.appendChild( valueElem );
220 element.appendChild( varElem );
241 mHighlightedVariables = other.mHighlightedVariables;
242 mHighlightedFunctions = other.mHighlightedFunctions;
243 mCachedValues = other.mCachedValues;
248 if (
this != &other )
250 qDeleteAll( mStack );
252 mStack = other.mStack;
253 other.mStack.clear();
255 mHighlightedVariables = other.mHighlightedVariables;
256 mHighlightedFunctions = other.mHighlightedFunctions;
257 mCachedValues = other.mCachedValues;
264 if ( &other ==
this )
267 qDeleteAll( mStack );
273 mHighlightedVariables = other.mHighlightedVariables;
274 mHighlightedFunctions = other.mHighlightedFunctions;
275 mCachedValues = other.mCachedValues;
281 qDeleteAll( mStack );
287 const auto constMStack = mStack;
306 const auto constNames = names;
307 for (
const QString &name : constNames )
316 return mHighlightedVariables.contains( name );
321 return mHighlightedVariables;
331 return mHighlightedFunctions.contains( name );
336 mHighlightedFunctions = names;
342 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
343 while ( it != mStack.constBegin() )
346 if ( ( *it )->hasVariable( name ) )
355 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
356 while ( it != mStack.constBegin() )
359 if ( ( *it )->hasVariable( name ) )
367 if ( index < 0 || index >= mStack.count() )
370 return mStack.at( index );
375 if ( mStack.count() < 1 )
378 return mStack.last();
386 return mStack.indexOf(
scope );
392 const auto constMStack = mStack;
406 const auto constMStack = mStack;
411 return qgis::setToList( qgis::listToSet( names ) );
417 QStringList filtered;
418 const auto constAllVariables = allVariables;
419 for (
const QString &
variable : constAllVariables )
433 const auto constMStack = mStack;
450 const auto constMStack = mStack;
462 const auto constMStack = mStack;
467 result = qgis::setToList( qgis::listToSet( result ) );
475 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
476 while ( it != mStack.constBegin() )
479 if ( ( *it )->hasFunction( name ) )
480 return ( *it )->function( name );
487 return mStack.count();
492 mStack.append(
scope );
502 if ( !mStack.isEmpty() )
503 return mStack.takeLast();
510 QList<QgsExpressionContextScope *> stack = mStack;
517 mStack.append(
scope );
523 if ( mStack.isEmpty() )
526 mStack.last()->setFeature(
feature );
531 const auto constMStack = mStack;
543 QList< QgsExpressionContextScope * >::const_iterator it = mStack.constEnd();
544 while ( it != mStack.constBegin() )
547 if ( ( *it )->hasFeature() )
548 return ( *it )->feature();
555 if ( mStack.isEmpty() )
558 mStack.last()->setFields(
fields );
568 if ( mStack.isEmpty() )
577 mCachedValues.insert( key, value );
582 return mCachedValues.contains( key );
587 return mCachedValues.value( key, QVariant() );
592 mCachedValues.clear();
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void addFunction(const QString &name, QgsScopedExpressionFunction *function)
Adds a function to the scope.
QList< QgsExpressionContextScope * > scopes()
Returns a list of scopes contained within the stack.
QgsExpressionFunction * function(const QString &name) const
Retrieves a function from the scope.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
QgsExpressionContextScope * scope(int index)
Returns the scope at the specified index within the context.
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 * popScope()
Removes the last scope from the expression context and return it.
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
QgsExpressionContextScope * activeScopeForVariable(const QString &name)
Returns the currently active scope from the context for a specified variable name.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
bool isHighlightedFunction(const QString &name) const
Returns true if the specified function name is intended to be highlighted to the user.
QgsExpressionContext & operator=(const QgsExpressionContext &other)
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.
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.
bool hasCachedValue(const QString &key) const
Returns true if the expression context contains a cached value with a matching key.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
static const QString EXPR_GEOMETRY_POINT_NUM
Inbuilt variable name for point number variable.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
QVariant value
Variable value.
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 functionNames() const
Retrieves a list of names of functions contained in the scope.
QString description(const QString &name) const
Returns a translated description string for the variable with specified name.
static const QString EXPR_GEOMETRY_PART_NUM
Inbuilt variable name for geometry part number variable.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
bool hasFunction(const QString &name) const
Tests whether a function with the specified name exists in the scope.
bool isHighlightedVariable(const QString &name) const
Returns true if the specified variable name is intended to be highlighted to the user.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
bool hasVariable(const QString &name) const
Tests whether a variable with the specified name exists in the scope.
QList< QgsExpressionContextScope * > takeScopes()
Returns all scopes from this context and remove them, leaving this context without any context.
void setCachedValue(const QString &key, const QVariant &value) const
Sets a value to cache within the expression context.
QgsFields fields() const
Convenience function for retrieving the fields for the context, if set.
QStringList variableNames() const
Returns a list of variable names contained within the scope.
bool isReadOnly(const QString &name) const
Returns whether a variable is read only, and should not be modifiable by users.
bool isStatic
A static variable can be cached for the lifetime of a context.
static const QString EXPR_SYMBOL_ANGLE
Inbuilt variable name for symbol angle variable.
int scopeCount() const
Returns the number of scopes contained in the context.
QVariant cachedValue(const QString &key) const
Returns the matching cached value, if set.
void setHighlightedFunctions(const QStringList &names)
Sets the list of function names intended to be highlighted to the user.
static const QString EXPR_FIELDS
Inbuilt variable name for fields storage.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
~QgsExpressionContextScope()
QStringList variableNames() const
Returns a list of variables names set by all scopes in the context.
bool hasFeature() const
Returns true if the scope has a feature associated with it.
bool isStatic(const QString &name) const
Tests whether the variable with the specified name is static and can be cached.
QgsExpressionContextScope & operator=(const QgsExpressionContextScope &other)
static QString variableHelpText(const QString &variableName)
Returns the help text for a specified variable.
QgsExpressionFunction * function(const QString &name) const
Fetches a matching function from the context.
QStringList highlightedVariables() const
Returns the current list of variables highlighted within the context.
Single scope for storing variables and functions for use within a QgsExpressionContext....
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static const QString EXPR_CLUSTER_SIZE
Inbuilt variable name for cluster size variable.
static const QString EXPR_GEOMETRY_PART_COUNT
Inbuilt variable name for geometry part count variable.
QStringList filteredVariableNames() const
Returns a filtered and sorted list of variable names contained within the scope.
QgsExpressionContextScope(const QString &name=QString())
Constructor for QgsExpressionContextScope.
QStringList functionNames() const
Retrieves a list of function names contained in the context.
QVariantMap variablesToMap() const
Returns a map of variable name to value representing all the expression variables contained by the co...
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
QVariant variable(const QString &name) const
Retrieves a variable's value from the scope.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
bool isReadOnly(const QString &name) const
Tests whether the specified variable is read only and should not be editable by users.
bool hasFunction(const QString &name) const
Checks whether a specified function is contained in the context.
void clearCachedValues() const
Clears all cached values from the context.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
Single variable definition for use within a QgsExpressionContextScope.
int indexOfScope(QgsExpressionContextScope *scope) const
Returns the index of the specified scope if it exists within the context.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
Expression function for use within a QgsExpressionContextScope. This differs from a standard QgsExpre...
QString name() const
Returns the friendly display name of the context scope.
QgsExpressionContext()=default
Constructor for QgsExpressionContext.
QgsExpressionContext & operator<<(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.
bool hasFeature() const
Returns true if the context has a feature associated with it.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
bool removeVariable(const QString &name)
Removes a variable from the context scope, if found.