Skip to content
Permalink
Browse files

Partially port wrappers to QgsProcessingParameterDefinition

And create a new WidgetWrapperFactory for creating a suitable wrapper
corresponding to a parameter
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 77ab933 commit 9997ab6e1e7ed5c62152a668f79bc1393ca87582
@@ -25,6 +25,11 @@ class QgsProcessingOutputDefinition

%TypeHeaderCode
#include "qgsprocessingoutputs.h"
%End

%ConvertToSubClassCode
if ( sipCpp->type() == "outputVector" )
sipType = sipType_QgsProcessingOutputVectorLayer;
%End
public:

@@ -26,6 +26,45 @@ class QgsProcessingParameterDefinition

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End

%ConvertToSubClassCode
if ( sipCpp->type() == "boolean" )
sipType = sipType_QgsProcessingParameterBoolean;
else if ( sipCpp->type() == "crs" )
sipType = sipType_QgsProcessingParameterCrs;
else if ( sipCpp->type() == "layer" )
sipType = sipType_QgsProcessingParameterMapLayer;
else if ( sipCpp->type() == "extent" )
sipType = sipType_QgsProcessingParameterExtent;
else if ( sipCpp->type() == "point" )
sipType = sipType_QgsProcessingParameterPoint;
else if ( sipCpp->type() == "file" )
sipType = sipType_QgsProcessingParameterFile;
else if ( sipCpp->type() == "matrix" )
sipType = sipType_QgsProcessingParameterMatrix;
else if ( sipCpp->type() == "multilayer" )
sipType = sipType_QgsProcessingParameterMultipleLayers;
else if ( sipCpp->type() == "number" )
sipType = sipType_QgsProcessingParameterNumber;
else if ( sipCpp->type() == "range" )
sipType = sipType_QgsProcessingParameterRange;
else if ( sipCpp->type() == "raster" )
sipType = sipType_QgsProcessingParameterRasterLayer;
else if ( sipCpp->type() == "enum" )
sipType = sipType_QgsProcessingParameterEnum;
else if ( sipCpp->type() == "string" )
sipType = sipType_QgsProcessingParameterString;
else if ( sipCpp->type() == "expression" )
sipType = sipType_QgsProcessingParameterExpression;
else if ( sipCpp->type() == "table" )
sipType = sipType_QgsProcessingParameterTable;
else if ( sipCpp->type() == "field" )
sipType = sipType_QgsProcessingParameterTableField;
else if ( sipCpp->type() == "vector" )
sipType = sipType_QgsProcessingParameterVectorLayer;
else if ( sipCpp->type() == "vectorOut" )
sipType = sipType_QgsProcessingParameterOutputVectorLayer;
%End
public:

@@ -89,17 +89,6 @@ def getCustomModelerParametersDialog(self, modelAlg, algName=None):
"""
return None

def getParameterDescriptions(self):
"""Returns a dict with param names as keys and detailed
descriptions of each param as value. These descriptions are
used as tool tips in the parameters dialog.
If a description does not exist, the parameter's
human-readable name is used.
"""
descs = {}
return descs

def checkParameterValuesBeforeExecuting(self):
"""If there is any check to do before launching the execution
of the algorithm, it should be done here.
@@ -31,13 +31,18 @@

import os

from qgis.core import (QgsProcessingParameterDefinition,
QgsProcessingParameterExtent,
QgsProcessingParameterPoint,
QgsProcessingParameterVectorLayer)
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtWidgets import (QWidget, QHBoxLayout, QToolButton,
QLabel, QCheckBox)
from qgis.PyQt.QtGui import QIcon

from processing.gui.OutputSelectionPanel import OutputSelectionPanel
from processing.gui.wrappers import WidgetWrapperFactory
from processing.core.parameters import ParameterVector, ParameterExtent, ParameterPoint
from processing.core.outputs import OutputRaster
from processing.core.outputs import OutputTable
@@ -78,83 +83,80 @@ def layerRegistryChanged(self, layers):

def initWidgets(self):
# If there are advanced parameters — show corresponding groupbox
for param in self.alg.parameters:
if param.isAdvanced:
for param in self.alg.parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.grpAdvanced.show()
break
# Create widgets and put them in layouts
for param in self.alg.parameters:
if param.hidden:
for param in self.alg.parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

desc = param.description
if isinstance(param, ParameterExtent):
desc += self.tr(' (xmin, xmax, ymin, ymax)')
if isinstance(param, ParameterPoint):
desc += self.tr(' (x, y)')
if param.optional:
desc += self.tr(' [optional]')

wrapper = self.getWidgetWrapperFromParameter(param)
self.wrappers[param.name] = wrapper
widget = wrapper.widget

if widget is not None:
if isinstance(param, ParameterVector):
layout = QHBoxLayout()
layout.setSpacing(2)
layout.setMargin(0)
layout.addWidget(widget)
button = QToolButton()
icon = QIcon(os.path.join(pluginPath, 'images', 'iterate.png'))
button.setIcon(icon)
button.setToolTip(self.tr('Iterate over this layer'))
button.setCheckable(True)
layout.addWidget(button)
self.iterateButtons[param.name] = button
button.toggled.connect(self.buttonToggled)
widget = QWidget()
widget.setLayout(layout)

tooltips = self.alg.getParameterDescriptions()
widget.setToolTip(tooltips.get(param.name, param.description))

if type(widget) is QCheckBox:
# checkbox widget - so description is embedded in widget rather than a separate
# label
widget.setText(desc)
else:
label = QLabel(desc)
# label.setToolTip(tooltip)
self.labels[param.name] = label

if param.isAdvanced:
self.layoutAdvanced.addWidget(label)
if param.isDestination():
label = QLabel(param.description())
widget = OutputSelectionPanel(param, self.alg)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
if isinstance(param, (OutputRaster, QgsProcessingParameterOutputVectorLayer, OutputTable)):
check = QCheckBox()
check.setText(self.tr('Open output file after running algorithm'))
check.setChecked(True)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
self.checkBoxes[param.name()] = check
self.outputWidgets[param.name()] = widget
else:
desc = param.description()
if isinstance(param, QgsProcessingParameterExtent):
desc += self.tr(' (xmin, xmax, ymin, ymax)')
if isinstance(param, QgsProcessingParameterPoint):
desc += self.tr(' (x, y)')
if param.flags() & QgsProcessingParameterDefinition.FlagOptional:
desc += self.tr(' [optional]')

wrapper = WidgetWrapperFactory.create_wrapper(param, self.parent)
self.wrappers[param.name()] = wrapper
widget = wrapper.widget

if widget is not None:
if isinstance(param, QgsProcessingParameterVectorLayer):
layout = QHBoxLayout()
layout.setSpacing(2)
layout.setMargin(0)
layout.addWidget(widget)
button = QToolButton()
icon = QIcon(os.path.join(pluginPath, 'images', 'iterate.png'))
button.setIcon(icon)
button.setToolTip(self.tr('Iterate over this layer'))
button.setCheckable(True)
layout.addWidget(button)
self.iterateButtons[param.name()] = button
button.toggled.connect(self.buttonToggled)
widget = QWidget()
widget.setLayout(layout)

widget.setToolTip(param.description())

if type(widget) is QCheckBox:
# checkbox widget - so description is embedded in widget rather than a separate
# label
widget.setText(desc)
else:
label = QLabel(desc)
# label.setToolTip(tooltip)
self.labels[param.name()] = label

if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.layoutAdvanced.addWidget(label)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, label)

if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.layoutAdvanced.addWidget(widget)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, label)

if param.isAdvanced:
self.layoutAdvanced.addWidget(widget)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, widget)
self.layoutMain.count() - 2, widget)

for output in self.alg.outputs:
if output.hidden:
continue

label = QLabel(output.description)
widget = OutputSelectionPanel(output, self.alg)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
if isinstance(output, (OutputRaster, OutputVector, OutputTable)):
check = QCheckBox()
check.setText(self.tr('Open output file after running algorithm'))
check.setChecked(True)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
self.checkBoxes[output.name] = check
self.outputWidgets[output.name] = widget
for wrapper in list(self.wrappers.values()):
wrapper.postInitialize(list(self.wrappers.values()))

@@ -164,6 +166,3 @@ def buttonToggled(self, value):
for button in list(self.iterateButtons.values()):
if button is not sender:
button.setChecked(False)

def getWidgetWrapperFromParameter(self, param):
return param.wrapper(self.parent)

0 comments on commit 9997ab6

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