Skip to content
Permalink
Browse files
Allow setting a QgsFeedback object in QgsExpressionContext
This can be checked by expression functions which are costly to
evaluate (e.g. those which fetch features from a layer) and which
would benefit from early exits when the results of the expression
evaluation are no longer needed (e.g. due to canceling a layer
rendering, etc)
  • Loading branch information
nyalldawson committed Jun 18, 2021
1 parent e416d77 commit 8869c9cbe4d3b2255cd937fb396636229f0c5ab4
Showing with 59 additions and 1 deletion.
  1. +23 −0 python/core/auto_generated/qgsexpressioncontext.sip.in
  2. +10 −0 src/core/qgsexpressioncontext.cpp
  3. +25 −0 src/core/qgsexpressioncontext.h
  4. +1 −1 src/core/qgsfeaturerequest.cpp
@@ -783,6 +783,29 @@ Clears all cached values from the context.
.. seealso:: :py:func:`cachedValue`

.. versionadded:: 2.16
%End

void setFeedback( QgsFeedback *feedback );
%Docstring
Attach a ``feedback`` object that can be queried regularly by the expression engine to check
if expression evaluation should be canceled.

Ownership of ``feedback`` is NOT transferred, and the caller must take care that it exists
for the lifetime of the expression context.

.. seealso:: :py:func:`feedback`

.. versionadded:: 3.20
%End

QgsFeedback *feedback() const;
%Docstring
Returns the feedback object that can be queried regularly by the expression to check
if evaluation should be canceled, if set.

.. seealso:: :py:func:`setFeedback`

.. versionadded:: 3.20
%End

static const QString EXPR_FIELDS;
@@ -592,3 +592,13 @@ void QgsExpressionContext::clearCachedValues() const
{
mCachedValues.clear();
}

void QgsExpressionContext::setFeedback( QgsFeedback *feedback )
{
mFeedback = feedback;
}

QgsFeedback *QgsExpressionContext::feedback() const
{
return mFeedback;
}
@@ -715,6 +715,29 @@ class CORE_EXPORT QgsExpressionContext
*/
void clearCachedValues() const;

/**
* Attach a \a feedback object that can be queried regularly by the expression engine to check
* if expression evaluation should be canceled.
*
* Ownership of \a feedback is NOT transferred, and the caller must take care that it exists
* for the lifetime of the expression context.
*
* \see feedback()
*
* \since QGIS 3.20
*/
void setFeedback( QgsFeedback *feedback );

/**
* Returns the feedback object that can be queried regularly by the expression to check
* if evaluation should be canceled, if set.
*
* \see setFeedback()
*
* \since QGIS 3.20
*/
QgsFeedback *feedback() const;

//! Inbuilt variable name for fields storage
static const QString EXPR_FIELDS;
//! Inbuilt variable name for value original value variable
@@ -748,6 +771,8 @@ class CORE_EXPORT QgsExpressionContext
QStringList mHighlightedVariables;
QStringList mHighlightedFunctions;

QgsFeedback *mFeedback = nullptr;

// Cache is mutable because we want to be able to add cached values to const contexts
mutable QMap< QString, QVariant > mCachedValues;

@@ -340,7 +340,7 @@ void QgsFeatureRequest::setFeedback( QgsFeedback *feedback )
mFeedback = feedback;
}

QgsFeedback *QgsFeatureRequest::feedback()
QgsFeedback *QgsFeatureRequest::feedback() const
{
return mFeedback;
}

0 comments on commit 8869c9c

Please sign in to comment.