Skip to content

Commit

Permalink
[processing] Show complete expression context in expression builder
Browse files Browse the repository at this point in the history
for pre-calculated expressions

Correctly exposes ALL the variables and functions available for use
in pre-calculated expressions so that users actually know they can
use these in their models!
  • Loading branch information
nyalldawson committed Sep 21, 2018
1 parent 8e49e94 commit 8f9f975
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Expand Up @@ -12,7 +12,7 @@



class QgsProcessingModelerParameterWidget : QWidget
class QgsProcessingModelerParameterWidget : QWidget, QgsExpressionContextGenerator
{
%Docstring

Expand Down Expand Up @@ -117,6 +117,9 @@ Returns the current value of the parameter.
.. seealso:: :py:func:`setWidgetValue`
%End

virtual QgsExpressionContext createExpressionContext() const;


};


Expand Down
19 changes: 18 additions & 1 deletion src/gui/processing/qgsprocessingmodelerparameterwidget.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsprocessingguiregistry.h"
#include "models/qgsprocessingmodelalgorithm.h"
#include "qgsgui.h"
#include "qgsexpressioncontext.h"
#include <QHBoxLayout>
#include <QToolButton>
#include <QStackedWidget>
Expand Down Expand Up @@ -172,7 +173,23 @@ QgsProcessingModelChildParameterSource QgsProcessingModelerParameterWidget::valu

QgsExpressionContext QgsProcessingModelerParameterWidget::createExpressionContext() const
{
return QgsExpressionContext();
QgsExpressionContext c = mContext.expressionContext();
if ( mModel )
{
QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( mModel->childAlgorithm( mChildId ).algorithm(), QVariantMap(), mContext );
c << algorithmScope;
QgsExpressionContextScope *childScope = mModel->createExpressionContextScopeForChildAlgorithm( mChildId, mContext, QVariantMap(), QVariantMap() );
c << childScope;

QStringList highlightedVariables = childScope->variableNames();
QStringList highlightedFunctions = childScope->functionNames();
highlightedVariables += algorithmScope->variableNames();
highlightedFunctions += algorithmScope->functionNames();
c.setHighlightedVariables( highlightedVariables );
c.setHighlightedFunctions( highlightedFunctions );
}

return c;
}

void QgsProcessingModelerParameterWidget::sourceMenuAboutToShow()
Expand Down
2 changes: 2 additions & 0 deletions src/gui/processing/qgsprocessingmodelerparameterwidget.h
Expand Up @@ -143,6 +143,8 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget, public Qg
*/
virtual QgsProcessingModelChildParameterSource value() const;

QgsExpressionContext createExpressionContext() const override;

private slots:

void sourceMenuAboutToShow();
Expand Down

0 comments on commit 8f9f975

Please sign in to comment.