QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
Classes | Public Member Functions | List of all members
QgsExpressionContextScope Class Reference

Single scope for storing variables and functions for use within a QgsExpressionContext. More...

#include <qgsexpressioncontext.h>

Classes

struct  StaticVariable
 Single variable definition for use within a QgsExpressionContextScope. More...
 

Public Member Functions

 QgsExpressionContextScope (const QString &name=QString())
 Constructor for QgsExpressionContextScope. More...
 
 QgsExpressionContextScope (const QgsExpressionContextScope &other)
 Copy constructor. More...
 
 ~QgsExpressionContextScope ()
 
void addFunction (const QString &name, QgsScopedExpressionFunction *function)
 Adds a function to the scope. More...
 
void addVariable (const QgsExpressionContextScope::StaticVariable &variable)
 Adds a variable into the context scope. More...
 
QStringList filteredVariableNames () const
 Returns a fitlered and sorted list of variable names contained within the scope. More...
 
QgsExpression::Functionfunction (const QString &name) const
 Retrieves a function from the scope. More...
 
QStringList functionNames () const
 Retrieves a list of names of functions contained in the scope. More...
 
bool hasFunction (const QString &name) const
 Tests whether a function with the specified name exists in the scope. More...
 
bool hasVariable (const QString &name) const
 Tests whether a variable with the specified name exists in the scope. More...
 
bool isReadOnly (const QString &name) const
 Tests whether the specified variable is read only and should not be editable by users. More...
 
QString name () const
 Returns the friendly display name of the context scope. More...
 
QgsExpressionContextScopeoperator= (const QgsExpressionContextScope &other)
 
bool removeVariable (const QString &name)
 Removes a variable from the context scope, if found. More...
 
void setFeature (const QgsFeature &feature)
 Convenience function for setting a feature for the scope. More...
 
void setFields (const QgsFields &fields)
 Convenience function for setting a fields for the scope. More...
 
void setVariable (const QString &name, const QVariant &value)
 Convenience method for setting a variable in the context scope by name and value. More...
 
QVariant variable (const QString &name) const
 Retrieves a variable's value from the scope. More...
 
int variableCount () const
 Returns the count of variables contained within the scope. More...
 
QStringList variableNames () const
 Returns a list of variable names contained within the scope. More...
 

Detailed Description

Single scope for storing variables and functions for use within a QgsExpressionContext.

Examples include a project's scope, which could contain information about the current project such as the project file's location. QgsExpressionContextScope can encapsulate both variables (static values) and functions(which are calculated only when an expression is evaluated).

See QgsExpressionContextUtils for helper methods for working with QgsExpressionContextScope objects.

Note
added in QGIS 2.12

Definition at line 79 of file qgsexpressioncontext.h.

Constructor & Destructor Documentation

◆ QgsExpressionContextScope() [1/2]

QgsExpressionContextScope::QgsExpressionContextScope ( const QString name = QString())

Constructor for QgsExpressionContextScope.

Parameters
namefriendly display name for the context scope

Definition at line 47 of file qgsexpressioncontext.cpp.

◆ QgsExpressionContextScope() [2/2]

QgsExpressionContextScope::QgsExpressionContextScope ( const QgsExpressionContextScope other)

Copy constructor.

Definition at line 53 of file qgsexpressioncontext.cpp.

◆ ~QgsExpressionContextScope()

QgsExpressionContextScope::~QgsExpressionContextScope ( )

Definition at line 80 of file qgsexpressioncontext.cpp.

Member Function Documentation

◆ addFunction()

void QgsExpressionContextScope::addFunction ( const QString name,
QgsScopedExpressionFunction function 
)

Adds a function to the scope.

Parameters
namefunction name
functionfunction to insert. Ownership is transferred to the scope.
See also
addVariable()

Definition at line 189 of file qgsexpressioncontext.cpp.

◆ addVariable()

void QgsExpressionContextScope::addVariable ( const QgsExpressionContextScope::StaticVariable variable)

Adds a variable into the context scope.

If a variable with the same name is already set then its value is overwritten, otherwise a new variable is added to the scope.

Parameters
variabledefinition of variable to insert
See also
setVariable()
addFunction()

Definition at line 99 of file qgsexpressioncontext.cpp.

◆ filteredVariableNames()

QStringList QgsExpressionContextScope::filteredVariableNames ( ) const

Returns a fitlered and sorted list of variable names contained within the scope.

Hidden variable names will be excluded, and the list will be sorted so that read only variables are listed first.

See also
variableNames()

Definition at line 152 of file qgsexpressioncontext.cpp.

◆ function()

QgsExpression::Function * QgsExpressionContextScope::function ( const QString name) const

Retrieves a function from the scope.

Parameters
namefunction name
Returns
function, or null if matching function could not be found
See also
hasFunction()
functionNames()
variable()

Definition at line 179 of file qgsexpressioncontext.cpp.

◆ functionNames()

QStringList QgsExpressionContextScope::functionNames ( ) const

Retrieves a list of names of functions contained in the scope.

See also
function()
variableNames()

Definition at line 184 of file qgsexpressioncontext.cpp.

◆ hasFunction()

bool QgsExpressionContextScope::hasFunction ( const QString name) const

Tests whether a function with the specified name exists in the scope.

Parameters
namefunction name
Returns
true if matching function was found in the scope
See also
function()
hasFunction()

Definition at line 174 of file qgsexpressioncontext.cpp.

◆ hasVariable()

bool QgsExpressionContextScope::hasVariable ( const QString name) const

Tests whether a variable with the specified name exists in the scope.

Parameters
namevariable name
Returns
true if matching variable was found in the scope
See also
variable()
hasFunction()

Definition at line 109 of file qgsexpressioncontext.cpp.

◆ isReadOnly()

bool QgsExpressionContextScope::isReadOnly ( const QString name) const

Tests whether the specified variable is read only and should not be editable by users.

Parameters
namevariable name
Returns
true if variable is read only

Definition at line 169 of file qgsexpressioncontext.cpp.

◆ name()

QString QgsExpressionContextScope::name ( ) const
inline

Returns the friendly display name of the context scope.

Definition at line 123 of file qgsexpressioncontext.h.

◆ operator=()

QgsExpressionContextScope & QgsExpressionContextScope::operator= ( const QgsExpressionContextScope other)

Definition at line 64 of file qgsexpressioncontext.cpp.

◆ removeVariable()

bool QgsExpressionContextScope::removeVariable ( const QString name)

Removes a variable from the context scope, if found.

Parameters
namename of variable to remove
Returns
true if variable was removed from the scope, false if matching variable was not found within the scope

Definition at line 104 of file qgsexpressioncontext.cpp.

◆ setFeature()

void QgsExpressionContextScope::setFeature ( const QgsFeature feature)

Convenience function for setting a feature for the scope.

Any existing feature set by the scope will be overwritten.

Parameters
featurefeature for scope

Definition at line 194 of file qgsexpressioncontext.cpp.

◆ setFields()

void QgsExpressionContextScope::setFields ( const QgsFields fields)

Convenience function for setting a fields for the scope.

Any existing fields set by the scope will be overwritten.

Parameters
fieldsfields for scope

Definition at line 199 of file qgsexpressioncontext.cpp.

◆ setVariable()

void QgsExpressionContextScope::setVariable ( const QString name,
const QVariant value 
)

Convenience method for setting a variable in the context scope by name and value.

If a variable with the same name is already set then its value is overwritten, otherwise a new variable is added to the scope.

Parameters
namevariable name
valuevariable value
See also
addVariable()

Definition at line 85 of file qgsexpressioncontext.cpp.

◆ variable()

QVariant QgsExpressionContextScope::variable ( const QString name) const

Retrieves a variable's value from the scope.

Parameters
namevariable name
Returns
variable value, or invalid QVariant if matching variable could not be found
See also
hasVariable()
function()

Definition at line 114 of file qgsexpressioncontext.cpp.

◆ variableCount()

int QgsExpressionContextScope::variableCount ( ) const
inline

Returns the count of variables contained within the scope.

Definition at line 186 of file qgsexpressioncontext.h.

◆ variableNames()

QStringList QgsExpressionContextScope::variableNames ( ) const

Returns a list of variable names contained within the scope.

See also
functionNames()
filteredVariableNames()

Definition at line 119 of file qgsexpressioncontext.cpp.


The documentation for this class was generated from the following files: