Skip to content
Permalink
Browse files

Set widget context for widget wrappers

  • Loading branch information
nyalldawson committed Sep 21, 2018
1 parent 55e22b9 commit 7af13f62c05dbfa328dc6f6f141d9ff39d2d959a
@@ -57,6 +57,15 @@ last for the lifetime of the widget.

~QgsProcessingModelerParameterWidget();

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

.. seealso:: :py:func:`widgetContext`
%End

void populateSources( const QStringList &compatibleParameterTypes,
const QStringList &compatibleOutputTypes,
const QList< int > &compatibleDataTypes );
@@ -36,6 +36,8 @@
QgsApplication,
QgsSettings,
QgsProcessingParameterDefinition)
from qgis.gui import QgsProcessingParameterWidgetContext
from qgis.utils import iface

from processing.gui.wrappers import WidgetWrapperFactory, WidgetWrapper
from processing.gui.BatchOutputSelectionPanel import BatchOutputSelectionPanel
@@ -253,6 +255,9 @@ def setCellWrapper(self, row, column, wrapper, context):
# TODO QGIS 4.0 - remove
is_cpp_wrapper = not issubclass(wrapper.__class__, WidgetWrapper)
if is_cpp_wrapper:
widget_context = QgsProcessingParameterWidgetContext()
widget_context.setMapCanvas(iface.mapCanvas())
wrapper.setWidgetContext(widget_context)
widget = wrapper.createWrappedWidget(context)
else:
widget = wrapper.widget
@@ -42,7 +42,9 @@
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorDestination,
QgsProject)
from qgis.gui import QgsProcessingContextGenerator
from qgis.gui import (QgsProcessingContextGenerator,
QgsProcessingParameterWidgetContext)
from qgis.utils import iface

from qgis.PyQt import uic
from qgis.PyQt.QtCore import QCoreApplication, Qt
@@ -122,6 +124,10 @@ def initWidgets(self):
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.grpAdvanced.show()
break

widget_context = QgsProcessingParameterWidgetContext()
widget_context.setMapCanvas(iface.mapCanvas())

# Create widgets and put them in layouts
for param in self.alg.parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
@@ -142,6 +148,7 @@ def initWidgets(self):
# TODO QGIS 4.0 - remove
is_python_wrapper = issubclass(wrapper.__class__, WidgetWrapper)
if not is_python_wrapper:
wrapper.setWidgetContext(widget_context)
widget = wrapper.createWrappedWidget(self.processing_context)
wrapper.registerProcessingContextGenerator(self.context_generator)
else:
@@ -58,7 +58,9 @@
QgsScrollArea,
QgsFilterLineEdit,
QgsHelp,
QgsProcessingModelerParameterWidget)
QgsProcessingModelerParameterWidget,
QgsProcessingParameterWidgetContext)
from qgis.utils import iface

from processing.gui.wrappers import WidgetWrapperFactory
from processing.gui.wrappers import InvalidParameterValue
@@ -131,6 +133,9 @@ def setupUi(self):
self.algorithmItem.setConfiguration(self.configuration)
self.verticalLayout.addWidget(self.algorithmItem)

widget_context = QgsProcessingParameterWidgetContext()
widget_context.setMapCanvas(iface.mapCanvas())

for param in self._alg.parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.advancedButton = QPushButton()
@@ -150,6 +155,7 @@ def setupUi(self):
self.wrappers[param.name()] = wrapper

if issubclass(wrapper.__class__, QgsProcessingModelerParameterWidget):
wrapper.setWidgetContext(widget_context)
widget = wrapper
else:
widget = wrapper.widget
@@ -113,6 +113,12 @@ QgsProcessingModelerParameterWidget::QgsProcessingModelerParameterWidget( QgsPro

QgsProcessingModelerParameterWidget::~QgsProcessingModelerParameterWidget() = default;

void QgsProcessingModelerParameterWidget::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
{
if ( mStaticWidgetWrapper )
mStaticWidgetWrapper->setWidgetContext( context );
}

const QgsProcessingParameterDefinition *QgsProcessingModelerParameterWidget::parameterDefinition() const
{
return mParameterDefinition;
@@ -30,6 +30,7 @@ class QgsProcessingParameterDefinition;
class QgsAbstractProcessingParameterWidgetWrapper;
class QgsExpressionLineEdit;
class QgsProcessingModelAlgorithm;
class QgsProcessingParameterWidgetContext;

class QLabel;
class QToolButton;
@@ -82,6 +83,15 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget

~QgsProcessingModelerParameterWidget() override;

/**
* Sets the \a context in which the modeler parameter widget is shown, e.g., the
* parent model algorithm and other relevant information which allows the widget
* to fine-tune its behavior.
*
* \see widgetContext()
*/
void setWidgetContext( const QgsProcessingParameterWidgetContext &context );

/**
* Populates the widget with available sources for the parameter's value, e.g.
* adding the available child algorithm outputs and model input parameter
@@ -40,6 +40,8 @@
#include "qgsprojectionselectionwidget.h"
#include "qgsdoublespinbox.h"
#include "qgsspinbox.h"
#include "qgsmapcanvas.h"
#include "models/qgsprocessingmodelalgorithm.h"

class TestParamType : public QgsProcessingParameterDefinition
{
@@ -306,6 +308,21 @@ void TestProcessingGui::testWrapperGeneral()
w = falseDefault.createWrappedWidget( context );
QVERIFY( !falseDefault.widgetValue().toBool() );
delete w;

std::unique_ptr< QgsMapCanvas > mc = qgis::make_unique< QgsMapCanvas >();
QgsProcessingParameterWidgetContext widgetContext;
widgetContext.setMapCanvas( mc.get() );
QCOMPARE( widgetContext.mapCanvas(), mc.get() );
std::unique_ptr< QgsProcessingModelAlgorithm > model = qgis::make_unique< QgsProcessingModelAlgorithm >();
widgetContext.setModel( model.get() );
QCOMPARE( widgetContext.model(), model.get() );
widgetContext.setModelChildAlgorithmId( QStringLiteral( "xx" ) );
QCOMPARE( widgetContext.modelChildAlgorithmId(), QStringLiteral( "xx" ) );

wrapper.setWidgetContext( widgetContext );
QCOMPARE( wrapper.widgetContext().mapCanvas(), mc.get() );
QCOMPARE( wrapper.widgetContext().model(), model.get() );
QCOMPARE( wrapper.widgetContext().modelChildAlgorithmId(), QStringLiteral( "xx" ) );
}

class TestProcessingContextGenerator : public QgsProcessingContextGenerator

0 comments on commit 7af13f6

Please sign in to comment.
You can’t perform that action at this time.