Skip to content

Commit

Permalink
Cleaner approach -- create expression contexts in modeler parameter
Browse files Browse the repository at this point in the history
definition dialogs using expression context and processing context
generators
  • Loading branch information
nyalldawson committed Jan 14, 2021
1 parent 5a9e8c6 commit 814b65b
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 76 deletions.
Expand Up @@ -12,7 +12,8 @@



class QgsProcessingAbstractParameterDefinitionWidget : QWidget

class QgsProcessingAbstractParameterDefinitionWidget : QWidget, QgsExpressionContextGenerator
{
%Docstring
Abstract base class for widgets which allow users to specify the properties of a
Expand Down Expand Up @@ -53,6 +54,42 @@ Common properties for parameters, including the ``name``, ``description``, and p
method. Subclass implementations must use these properties when crafting a parameter definition which
also respects the additional properties specific to the parameter type handled by the widget subclass.
%End

virtual void setWidgetContext( const QgsProcessingParameterWidgetContext &context );
%Docstring
Sets the ``context`` in which the Processing definition widget is shown, e.g., the
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
to fine-tune its behavior.

Subclasses should take care to call the base class method when reimplementing this method.

.. seealso:: :py:func:`widgetContext`

.. versionadded:: 3.18
%End

const QgsProcessingParameterWidgetContext &widgetContext() const;
%Docstring
Returns the context in which the Processing definition widget is shown, e.g., the
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
to fine-tune its behavior.

.. seealso:: :py:func:`setWidgetContext`

.. versionadded:: 3.18
%End

void registerProcessingContextGenerator( QgsProcessingContextGenerator *generator );
%Docstring
Registers a Processing context ``generator`` class that will be used to retrieve
a Processing context for the widget when required.

.. versionadded:: 3.18
%End

virtual QgsExpressionContext createExpressionContext() const;


};


Expand Down Expand Up @@ -96,6 +133,14 @@ associated with the parameter.
Returns a new instance of a parameter definition, using the current settings defined in the dialog.

The ``name`` parameter specifies the name for the newly created parameter.
%End

void registerProcessingContextGenerator( QgsProcessingContextGenerator *generator );
%Docstring
Registers a Processing context ``generator`` class that will be used to retrieve
a Processing context for the widget when required.

.. versionadded:: 3.18
%End

};
Expand Down Expand Up @@ -181,6 +226,14 @@ Returns the color for the comments for the parameter.
void switchToCommentTab();
%Docstring
Switches the dialog to the comments tab.
%End

void registerProcessingContextGenerator( QgsProcessingContextGenerator *generator );
%Docstring
Registers a Processing context ``generator`` class that will be used to retrieve
a Processing context for the widget when required.

.. versionadded:: 3.18
%End

public slots:
Expand Down
9 changes: 0 additions & 9 deletions python/gui/auto_generated/qgsexpressionlineedit.sip.in
Expand Up @@ -127,15 +127,6 @@ an expression context for the widget.
create an expression context when required.
%End

void setExpressionContext( QgsExpressionContext &context );
%Docstring
Set a default expression context. Will be used if no QgsEcpressionContextGenerator or layer is set.

:param context: The QgsExpressionContext to use.

.. versionadded:: 3.18
%End

signals:

void expressionChanged( const QString &expression );
Expand Down
19 changes: 17 additions & 2 deletions python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -40,12 +40,13 @@
QgsProcessing,
QgsProject,
QgsProcessingModelParameter,
QgsSettings
QgsSettings,
)
from qgis.gui import (QgsProcessingParameterDefinitionDialog,
QgsProcessingParameterWidgetContext,
QgsModelGraphicsScene,
QgsModelDesignerDialog)
QgsModelDesignerDialog,
QgsProcessingContextGenerator)
from processing.gui.HelpEditionDialog import HelpEditionDialog
from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.modeler.ModelerParameterDefinitionDialog import ModelerParameterDefinitionDialog
Expand Down Expand Up @@ -105,6 +106,19 @@ def __init__(self, model=None, parent=None):

self.view().centerOn(0, 0)

self.processing_context = createContext()

class ContextGenerator(QgsProcessingContextGenerator):

def __init__(self, context):
super().__init__()
self.processing_context = context

def processingContext(self):
return self.processing_context

self.context_generator = ContextGenerator(self.processing_context)

def editHelp(self):
alg = self.model()
dlg = HelpEditionDialog(alg)
Expand Down Expand Up @@ -270,6 +284,7 @@ def addInput(self, paramType, pos=None):
context=context,
widgetContext=widget_context,
algorithm=self.model())
dlg.registerProcessingContextGenerator(self.context_generator)
if dlg.exec_():
new_param = dlg.createParameter()
self.autogenerate_parameter_name(new_param)
Expand Down
18 changes: 17 additions & 1 deletion python/plugins/processing/modeler/ModelerGraphicItem.py
Expand Up @@ -31,7 +31,8 @@
QgsProcessingParameterWidgetContext,
QgsModelParameterGraphicItem,
QgsModelChildAlgorithmGraphicItem,
QgsModelOutputGraphicItem
QgsModelOutputGraphicItem,
QgsProcessingContextGenerator
)
from processing.modeler.ModelerParameterDefinitionDialog import ModelerParameterDefinitionDialog
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
Expand All @@ -50,6 +51,19 @@ class ModelerInputGraphicItem(QgsModelParameterGraphicItem):
def __init__(self, element, model):
super().__init__(element, model, None)

self.processing_context = createContext()

class ContextGenerator(QgsProcessingContextGenerator):

def __init__(self, context):
super().__init__()
self.processing_context = context

def processingContext(self):
return self.processing_context

self.context_generator = ContextGenerator(self.processing_context)

def create_widget_context(self):
"""
Returns a new widget context for use in the model editor
Expand Down Expand Up @@ -91,6 +105,8 @@ def edit(self, edit_comment=False):
algorithm=self.model())
dlg.setComments(comment)
dlg.setCommentColor(comment_color)
dlg.registerProcessingContextGenerator(self.context_generator)

if edit_comment:
dlg.switchToCommentTab()

Expand Down
39 changes: 38 additions & 1 deletion src/gui/processing/qgsprocessingparameterdefinitionwidget.cpp
Expand Up @@ -34,14 +34,35 @@
#include <QTextEdit>

QgsProcessingAbstractParameterDefinitionWidget::QgsProcessingAbstractParameterDefinitionWidget( QgsProcessingContext &,
const QgsProcessingParameterWidgetContext &,
const QgsProcessingParameterWidgetContext &context,
const QgsProcessingParameterDefinition *,
const QgsProcessingAlgorithm *, QWidget *parent )
: QWidget( parent )
, mWidgetContext( context )
{

}

void QgsProcessingAbstractParameterDefinitionWidget::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
{
mWidgetContext = context;
}

const QgsProcessingParameterWidgetContext &QgsProcessingAbstractParameterDefinitionWidget::widgetContext() const
{
return mWidgetContext;
}

void QgsProcessingAbstractParameterDefinitionWidget::registerProcessingContextGenerator( QgsProcessingContextGenerator *generator )
{
mContextGenerator = generator;
}

QgsExpressionContext QgsProcessingAbstractParameterDefinitionWidget::createExpressionContext() const
{
return QgsProcessingGuiUtils::createExpressionContext( mContextGenerator, mWidgetContext, nullptr, nullptr );
}

//
// QgsProcessingParameterDefinitionWidget
//
Expand Down Expand Up @@ -120,6 +141,14 @@ QgsProcessingParameterDefinition *QgsProcessingParameterDefinitionWidget::create
return param.release();
}

void QgsProcessingParameterDefinitionWidget::registerProcessingContextGenerator( QgsProcessingContextGenerator *generator )
{
if ( mDefinitionWidget )
{
mDefinitionWidget->registerProcessingContextGenerator( generator );
}
}

//
// QgsProcessingParameterDefinitionDialog
//
Expand Down Expand Up @@ -210,6 +239,14 @@ void QgsProcessingParameterDefinitionDialog::switchToCommentTab()
mCommentEdit->selectAll();
}

void QgsProcessingParameterDefinitionDialog::registerProcessingContextGenerator( QgsProcessingContextGenerator *generator )
{
if ( mWidget )
{
mWidget->registerProcessingContextGenerator( generator );
}
}

void QgsProcessingParameterDefinitionDialog::accept()
{
if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
Expand Down
60 changes: 58 additions & 2 deletions src/gui/processing/qgsprocessingparameterdefinitionwidget.h
Expand Up @@ -25,8 +25,11 @@
#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgsprocessingparameters.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgsprocessingwidgetwrapper.h"

class QgsProcessingContextGenerator;

class QgsProcessingParameterWidgetContext;
class QLineEdit;
class QCheckBox;
class QTabWidget;
Expand All @@ -40,7 +43,7 @@ class QgsColorButton;
* \ingroup gui
* \since QGIS 3.10
*/
class GUI_EXPORT QgsProcessingAbstractParameterDefinitionWidget : public QWidget
class GUI_EXPORT QgsProcessingAbstractParameterDefinitionWidget : public QWidget, public QgsExpressionContextGenerator
{
Q_OBJECT

Expand Down Expand Up @@ -73,6 +76,43 @@ class GUI_EXPORT QgsProcessingAbstractParameterDefinitionWidget : public QWidget
* also respects the additional properties specific to the parameter type handled by the widget subclass.
*/
virtual QgsProcessingParameterDefinition *createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const = 0 SIP_FACTORY;

/**
* Sets the \a context in which the Processing definition widget is shown, e.g., the
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
* to fine-tune its behavior.
*
* Subclasses should take care to call the base class method when reimplementing this method.
*
* \see widgetContext()
* \since QGIS 3.18
*/
virtual void setWidgetContext( const QgsProcessingParameterWidgetContext &context );

/**
* Returns the context in which the Processing definition widget is shown, e.g., the
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
* to fine-tune its behavior.
*
* \see setWidgetContext()
* \since QGIS 3.18
*/
const QgsProcessingParameterWidgetContext &widgetContext() const;

/**
* Registers a Processing context \a generator class that will be used to retrieve
* a Processing context for the widget when required.
*
* \since QGIS 3.18
*/
void registerProcessingContextGenerator( QgsProcessingContextGenerator *generator );

QgsExpressionContext createExpressionContext() const override;

private:

QgsProcessingContextGenerator *mContextGenerator = nullptr;
QgsProcessingParameterWidgetContext mWidgetContext;
};


Expand Down Expand Up @@ -117,6 +157,14 @@ class GUI_EXPORT QgsProcessingParameterDefinitionWidget: public QWidget
*/
QgsProcessingParameterDefinition *createParameter( const QString &name = QString() ) const SIP_FACTORY;

/**
* Registers a Processing context \a generator class that will be used to retrieve
* a Processing context for the widget when required.
*
* \since QGIS 3.18
*/
void registerProcessingContextGenerator( QgsProcessingContextGenerator *generator );

private:

QString mType;
Expand Down Expand Up @@ -203,6 +251,14 @@ class GUI_EXPORT QgsProcessingParameterDefinitionDialog: public QDialog
*/
void switchToCommentTab();

/**
* Registers a Processing context \a generator class that will be used to retrieve
* a Processing context for the widget when required.
*
* \since QGIS 3.18
*/
void registerProcessingContextGenerator( QgsProcessingContextGenerator *generator );

public slots:
void accept() override;

Expand Down

0 comments on commit 814b65b

Please sign in to comment.