Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 8869c9c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
23 changes: 23 additions & 0 deletions python/core/auto_generated/qgsexpressioncontext.sip.in
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -592,3 +592,13 @@ void QgsExpressionContext::clearCachedValues() const
{
mCachedValues.clear();
}

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

QgsFeedback *QgsExpressionContext::feedback() const
{
return mFeedback;
}
25 changes: 25 additions & 0 deletions src/core/qgsexpressioncontext.h
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsfeaturerequest.cpp
Expand Up @@ -340,7 +340,7 @@ void QgsFeatureRequest::setFeedback( QgsFeedback *feedback )
mFeedback = feedback;
}

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

0 comments on commit 8869c9c

Please sign in to comment.