Skip to content

Commit

Permalink
[processing] allow multiple values in ParameterSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Oct 5, 2016
1 parent 8ce93b1 commit 36abbc4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
39 changes: 31 additions & 8 deletions python/plugins/processing/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,9 @@ class ParameterSelection(Parameter):


def __init__(self, name='', description='', options=[], default=None, isSource=False,
optional=False):
multiple=False, optional=False):
Parameter.__init__(self, name, description, default, optional)
self.multiple = multiple
isSource = parseBool(isSource)
self.options = options
if isSource:
Expand All @@ -1077,19 +1078,34 @@ def __init__(self, name='', description='', options=[], default=None, isSource=F
self.default = 0
self.value = self.default

def setValue(self, n):
if n is None:
def setValue(self, value):
if value is None:
if not self.optional:
return False
self.value = 0
return True

try:
n = int(n)
self.value = n
if isinstance(value, list):
if not self.multiple:
return False
values = []
for v in value:
try:
n = int(v)
values.append(n)
except:
return False
if not self.optional and len(values) == 0:
return False
self.value = values
return True
except:
return False
else:
try:
n = int(value)
self.value = n
return True
except:
return False

@classmethod
def fromScriptCode(self, line):
Expand All @@ -1101,6 +1117,13 @@ def fromScriptCode(self, line):
elif definition.lower().strip().startswith('selection'):
options = definition.strip()[len('selection '):].split(';')
return ParameterSelection(name, descName, options, optional=isOptional)
elif definition.lower().strip().startswith('multipleselectionfromfile'):
options = definition.strip()[len('multipleselectionfromfile '):].split(';')
return ParameterSelection(name, descName, options, isSource=True,
multiple=True, optional=isOptional)
elif definition.lower().strip().startswith('multipleselection'):
options = definition.strip()[len('multipleselection '):].split(';')
return ParameterSelection(name, descName, options, multiple=True, optional=isOptional)


class ParameterEvaluationException(Exception):
Expand Down
87 changes: 48 additions & 39 deletions python/plugins/processing/gui/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""
***************************************************************************
BooleanWidget.py
wrappers.py
---------------------
Date : May 2016
Copyright : (C) 2016 by Arnaud Morvan, Victor Olaya
Expand Down Expand Up @@ -43,7 +43,7 @@
ParameterTableField, ParameterExtent, ParameterFixedTable, ParameterCrs)
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.FileSelectionPanel import FileSelectionPanel
from processing.core.outputs import (OutputFile, OutputRaster, OutputVector, OutputNumber,
from processing.core.outputs import (OutputFile, OutputRaster, OutputVector, OutputNumber,
OutputString, OutputTable, OutputExtent)
from processing.tools import dataobjects
from processing.gui.MultipleInputPanel import MultipleInputPanel
Expand Down Expand Up @@ -122,21 +122,21 @@ def setComboValue(self, value):

def value(self):
pass

def anotherParameterWidgetHasChanged(self, wrapper):
pass

def postInitialize(self, wrappers):
pass

def refresh(self):
pass

class BasicWidgetWrapper(WidgetWrapper):

def createWidget(self):
return QLineEdit()

def setValue(self, value):
self.widget.setText(value)

Expand Down Expand Up @@ -200,7 +200,7 @@ def createWidget(self):
for r in raster:
widget.addItem("Crs of layer " + self.dialog.resolveValueDescription(r), r)
for v in vector:
widget.addItem("Crs of layer " + self.dialog.resolveValueDescription(v), v)
widget.addItem("Crs of layer " + self.dialog.resolveValueDescription(v), v)
if not self.param.default:
widget.setEditText(self.param.default)
return widget
Expand All @@ -226,7 +226,7 @@ def value(self):
class ExtentWidgetWrapper(WidgetWrapper):

USE_MIN_COVERING_EXTENT = "[Use min covering extent]"

def createWidget(self):
if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH):
return ExtentSelectionPanel(self.dialog, self.param)
Expand All @@ -247,7 +247,7 @@ def createWidget(self):
if not self.param.default:
widget.setEditText(self.param.default)
return widget

def setValue(self, value):
if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH):
self.widget.setExtentFromString(value)
Expand Down Expand Up @@ -364,7 +364,7 @@ def value(self):
return ParameterFixedTable.tableToString(table)
else:
return self.widget.table


class MultipleInputWidgetWrapper(WidgetWrapper):

Expand Down Expand Up @@ -404,7 +404,7 @@ def createWidget(self):
else:
options = [self.dialog.resolveValueDescription(opt) for opt in self._getOptions()]
return MultipleInputPanel(options)

def refresh(self):
if self.param.datatype != dataobjects.TYPE_FILE:
if self.param.datatype == dataobjects.TYPE_RASTER:
Expand Down Expand Up @@ -461,7 +461,7 @@ def createWidget(self):

def setValue(self, value):
self.widget.setValue(value)

def value(self):
return self.widget.getValue()

Expand Down Expand Up @@ -496,7 +496,7 @@ def refresh(self):
self.widget.cmbText.addItem(self.NOT_SELECTED, None)
for layer in layers:
self.widget.cmbText.addItem(getExtendedLayerName(layer), layer)

def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
pass # TODO
Expand All @@ -519,17 +519,26 @@ def validator(v):
class SelectionWidgetWrapper(WidgetWrapper):

def createWidget(self):
widget = QComboBox()
widget.addItems(self.param.options)
if self.param.default:
widget.setCurrentIndex(self.param.default)
return widget
if self.param.multiple:
return MultipleInputPanel(options=self.param.options)
else:
widget = QComboBox()
widget.addItems(self.param.options)
if self.param.default:
widget.setCurrentIndex(self.param.default)
return widget

def setValue(self, value):
self.widget.setCurrentIndex(int(value))
if self.param.multiple:
self.widget.setSelectedItems(value)
else:
self.widget.setCurrentIndex(int(value))

def value(self):
return self.widget.currentIndex()
if self.param.multiple:
return self.widget.selectedoptions
else:
return self.widget.currentIndex()


class VectorWidgetWrapper(WidgetWrapper):
Expand All @@ -551,7 +560,7 @@ def createWidget(self):
for layer in layers:
widget.addItem(self.dialog.resolveValueDescription(layer), layer)
return widget

def _populate(self, widget):
widget.clear()
layers = dataobjects.getVectorLayers(self.param.datatype)
Expand All @@ -562,10 +571,10 @@ def _populate(self, widget):
widget.addItem(getExtendedLayerName(layer), layer)
widget.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
widget.name = self.param.name

def refresh(self):
self._populate(self.widget)

def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
pass # TODO
Expand Down Expand Up @@ -719,8 +728,8 @@ def createWidget(self):
if self.dialogType == DIALOG_STANDARD:
return MultipleInputPanel(options=[])
else:
return QLineEdit()
else:
return QLineEdit()
else:
if self.dialogType == DIALOG_STANDARD:
widget = QComboBox()
return widget
Expand All @@ -746,13 +755,13 @@ def postInitialize(self, wrappers):
fields = self.getFields(layer, wrapper.param.datatype)
if self.param.multiple:
self.widget.updateForOptions(fields)
else:
else:
self.widget.clear()
if self.param.optional:
self.widget.addItem(self.tr(self.NOT_SET))
self.widget.addItems(fields)
break

def getFields(self, layer, datatype):
fieldTypes = []
if datatype == ParameterTableField.DATA_TYPE_STRING:
Expand All @@ -766,7 +775,7 @@ def getFields(self, layer, datatype):
if not fieldTypes or field.type() in fieldTypes:
fieldNames.add(unicode(field.name()))
return sorted(list(fieldNames), cmp=locale.strcoll)

def setValue(self, value):
if self.param.multiple:
if self.dialogType == DIALOG_STANDARD:
Expand All @@ -778,7 +787,7 @@ def setValue(self, value):
self.widget.setSelectedItems(selected)
else:
self.widget.setText(value)
else:
else:
if self.dialogType == DIALOG_STANDARD:
pass # TODO
elif self.dialogType == DIALOG_BATCH:
Expand All @@ -794,11 +803,11 @@ def value(self):
elif self.dialogType == DIALOG_BATCH:
return self.widget.text()
else:
text = self.widget.text()
text = self.widget.text()
if not bool(text) and not self.param.optional:
raise InvalidParameterValue()
return text
else:
else:
if self.dialogType == DIALOG_STANDARD:
if self.param.optional and self.widget.currentIndex() == 0:
return None
Expand All @@ -808,9 +817,9 @@ def value(self):
else:
def validator(v):
return bool(v) or self.param.optional
return self.comboValue(validator)
def anotherParameterWidgetHasChanged(self,wrapper):
return self.comboValue(validator)

def anotherParameterWidgetHasChanged(self, wrapper):
if wrapper.param.name == self.param.parent:
layer = wrapper.value()
if layer is not None:
Expand All @@ -822,15 +831,15 @@ def anotherParameterWidgetHasChanged(self,wrapper):
if self.param.optional:
self.widget.addItem(self.tr(self.NOT_SET))
self.widget.addItems(fields)


def GeometryPredicateWidgetWrapper(WidgetWrapper):

def createWidget(self):
return GeometryPredicateSelectionPanel()

def setValue(self, value):
self.widget.setValue(value)

def value(self):
return self.widget.value()
return self.widget.value()

0 comments on commit 36abbc4

Please sign in to comment.