Skip to content

Commit

Permalink
Use boolean wrapper in modeler algorithm dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-morvan authored and volaya committed Oct 5, 2016
1 parent fbab369 commit bb0938a
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions python/plugins/processing/modeler/ModelerParametersDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@

from qgis.gui import QgsMessageBar

from processing.gui.wrappers import (
DIALOG_MODELER,
wrapper_from_param,
NotYetImplementedWidgetWrapper,
)

from processing.gui.CrsSelectionPanel import CrsSelectionPanel
from processing.gui.MultipleInputPanel import MultipleInputPanel
from processing.gui.FixedTablePanel import FixedTablePanel
Expand All @@ -49,7 +55,6 @@
from processing.core.parameters import (ParameterExtent,
ParameterRaster,
ParameterVector,
ParameterBoolean,
ParameterTable,
ParameterFixedTable,
ParameterMultipleInput,
Expand Down Expand Up @@ -107,6 +112,7 @@ def setupUi(self):
self.widgets = {}
self.checkBoxes = {}
self.showAdvanced = False
self.widget_wrappers = {}
self.valueItems = {}
self.dependentItems = {}
self.resize(650, 450)
Expand Down Expand Up @@ -160,7 +166,9 @@ def setupUi(self):
desc += self.tr('(x, y)')
label = QLabel(desc)
self.labels[param.name] = label
widget = self.getWidgetFromParameter(param)
wrapper = self.getWidgetWrapperFromParameter(param)
self.widget_wrappers[param.name] = wrapper
widget = wrapper.widget
self.valueItems[param.name] = widget
if param.name in list(tooltips.keys()):
tooltip = tooltips[param.name]
Expand Down Expand Up @@ -308,6 +316,20 @@ def resolveValueDescription(self, value):
alg = self.model.algs[value.alg]
return self.tr("'%s' from algorithm '%s'") % (alg.algorithm.getOutputFromName(value.output).description, alg.description)

def getWidgetWrapperFromParameter(self, param):
extra_values = []
values = self.getAvailableValuesOfType(param.__class__, None)
for value in values:
extra_values.append((self.resolveValueDescription(value), value))

wrapper = wrapper_from_param(param, DIALOG_MODELER, extra_values)
if wrapper is not None:
return wrapper

widget = self.getWidgetFromParameter(param)
wrapper = NotYetImplementedWidgetWrapper(param, widget)
return wrapper

def getWidgetFromParameter(self, param):
if isinstance(param, ParameterRaster):
item = QComboBox()
Expand All @@ -333,17 +355,6 @@ def getWidgetFromParameter(self, param):
item.addItem(self.resolveValueDescription(table), table)
for layer in layers:
item.addItem(self.resolveValueDescription(layer), layer)
elif isinstance(param, ParameterBoolean):
item = QComboBox()
item.addItem('Yes')
item.addItem('No')
bools = self.getAvailableValuesOfType(ParameterBoolean, None)
for b in bools:
item.addItem(self.resolveValueDescription(b), b)
if param.default:
item.setCurrentIndex(0)
else:
item.setCurrentIndex(1)
elif isinstance(param, ParameterSelection):
item = QComboBox()
item.addItems(param.options)
Expand Down Expand Up @@ -487,11 +498,6 @@ def setComboBoxValue(self, combo, value, param):
combo.setEditText(str(value))
elif isinstance(param, ParameterSelection):
combo.setCurrentIndex(int(value))
elif isinstance(param, ParameterBoolean):
if value:
combo.setCurrentIndex(0)
else:
combo.setCurrentIndex(1)

def setPreviousValues(self):
if self._algName is not None:
Expand All @@ -500,19 +506,23 @@ def setPreviousValues(self):
for param in alg.algorithm.parameters:
if param.hidden:
continue
widget = self.valueItems[param.name]
if param.name in alg.params:
value = alg.params[param.name]
else:
value = param.default

wrapper = self.widget_wrappers[param.name]
if wrapper.implemented:
wrapper.setValue(value)

widget = wrapper.widget
if isinstance(param, (
ParameterRaster,
ParameterVector,
ParameterTable,
ParameterTableField,
ParameterSelection,
ParameterNumber,
ParameterBoolean,
ParameterExtent,
ParameterFile,
ParameterPoint,
Expand Down Expand Up @@ -567,7 +577,7 @@ def createAlgorithm(self):
for param in params:
if param.hidden:
continue
if not self.setParamValue(alg, param, self.valueItems[param.name]):
if not self.setParamValue(alg, param, self.widget_wrappers[param.name]):
self.bar.pushMessage("Error", "Wrong or missing value for parameter '%s'" % param.description,
level=QgsMessageBar.WARNING)
return None
Expand Down Expand Up @@ -723,17 +733,15 @@ def setParamCrsValue(self, alg, param, widget):
alg.params[param.name] = value
return True

def setParamValue(self, alg, param, widget):
def setParamValue(self, alg, param, wrapper):
if wrapper.implemented:
alg.params[param.name] = wrapper.value()
return True

widget = wrapper.widget
if isinstance(param, (ParameterRaster, ParameterVector,
ParameterTable)):
return self.setParamValueLayerOrTable(alg, param, widget)
elif isinstance(param, ParameterBoolean):
if widget.currentIndex() < 2:
value = widget.currentIndex() == 0
else:
value = widget.itemData(widget.currentIndex())
alg.params[param.name] = value
return True
elif isinstance(param, ParameterString):
return self.setParamStringValue(alg, param, widget)
elif isinstance(param, ParameterNumber):
Expand Down

0 comments on commit bb0938a

Please sign in to comment.