Skip to content

Commit 65a4e97

Browse files
committed
[processing] Support options as (value, text) in ParameterSelection
1 parent e135e79 commit 65a4e97

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

python/plugins/processing/algs/grass7/Grass7Algorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def processCommand(self):
426426
command += ' ' + param.name
427427
elif isinstance(param, ParameterSelection):
428428
idx = int(param.value)
429-
command += ' ' + param.name + '=' + str(param.options[idx])
429+
command += ' ' + param.name + '=' + str(param.options[idx][1])
430430
elif isinstance(param, ParameterString):
431431
command += ' ' + param.name + '="' + str(param.value) + '"'
432432
elif isinstance(param, ParameterPoint):

python/plugins/processing/algs/otb/OTBAlgorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def processAlgorithm(self, progress):
276276
elif isinstance(param, ParameterSelection):
277277
commands.append(param.name)
278278
idx = int(param.value)
279-
commands.append(str(param.options[idx]))
279+
commands.append(str(param.options[idx][1]))
280280
elif isinstance(param, ParameterBoolean):
281281
if param.value:
282282
commands.append(param.name)

python/plugins/processing/core/parameters.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,41 +1061,58 @@ def __init__(self, name='', description='', options=[], default=None, isSource=F
10611061
elif isinstance(self.options, str):
10621062
self.options = self.options.split(";")
10631063

1064+
# compute options as (value, text)
1065+
options = []
1066+
for i, option in enumerate(self.options):
1067+
if option is None or isinstance(option, basestring):
1068+
options.append((i, option))
1069+
else:
1070+
options.append((option[0], option[1]))
1071+
self.options = options
1072+
self.values = [option[0] for option in options]
1073+
1074+
self.value = None
10641075
if default is not None:
1065-
try:
1066-
self.default = int(default)
1067-
except:
1068-
self.default = 0
1069-
self.value = self.default
1076+
self.setValue(self.default)
10701077

10711078
def setValue(self, value):
10721079
if value is None:
10731080
if not self.optional:
10741081
return False
1075-
self.value = 0
1082+
self.value = None
10761083
return True
10771084

10781085
if isinstance(value, list):
10791086
if not self.multiple:
10801087
return False
10811088
values = []
10821089
for v in value:
1090+
if v in self.values:
1091+
values.append(v)
1092+
continue
10831093
try:
1084-
n = int(v)
1085-
values.append(n)
1094+
v = int(v)
10861095
except:
1096+
pass
1097+
if not v in self.values:
10871098
return False
1099+
values.append(v)
10881100
if not self.optional and len(values) == 0:
10891101
return False
10901102
self.value = values
10911103
return True
10921104
else:
1093-
try:
1094-
n = int(value)
1095-
self.value = n
1105+
if value in self.values:
1106+
self.value = value
10961107
return True
1108+
try:
1109+
value = int(value)
10971110
except:
1111+
pass
1112+
if not value in self.values:
10981113
return False
1114+
self.value = value
1115+
return True
10991116

11001117
@classmethod
11011118
def fromScriptCode(self, line):

python/plugins/processing/gui/MultipleInputDialog.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ def __init__(self, options, selectedoptions=None):
6868

6969
def populateList(self):
7070
model = QStandardItemModel()
71-
for i, option in enumerate(self.options):
72-
item = QStandardItem(option)
73-
item.setCheckState(Qt.Checked if i in self.selectedoptions else Qt.Unchecked)
71+
for value, text in self.options:
72+
item = QStandardItem(text)
73+
item.setData(value, Qt.UserRole)
74+
item.setCheckState(Qt.Checked if value in self.selectedoptions else Qt.Unchecked)
7475
item.setCheckable(True)
7576
model.appendRow(item)
7677

@@ -82,7 +83,7 @@ def accept(self):
8283
for i in range(model.rowCount()):
8384
item = model.item(i)
8485
if item.checkState() == Qt.Checked:
85-
self.selectedoptions.append(i)
86+
self.selectedoptions.append(item.data(Qt.UserRole))
8687
QDialog.accept(self)
8788

8889
def reject(self):

python/plugins/processing/gui/wrappers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,22 +652,23 @@ def createWidget(self):
652652
return MultipleInputPanel(options=self.param.options)
653653
else:
654654
widget = QComboBox()
655-
widget.addItems(self.param.options)
655+
for option in self.param.options:
656+
widget.addItem(option[1], option[0])
656657
if self.param.default:
657-
widget.setCurrentIndex(self.param.default)
658+
widget.setCurrentIndex(widget.findData(self.param.default))
658659
return widget
659660

660661
def setValue(self, value):
661662
if self.param.multiple:
662663
self.widget.setSelectedItems(value)
663664
else:
664-
self.widget.setCurrentIndex(int(value))
665+
self.widget.setCurrentIndex(self.widget.findData(value))
665666

666667
def value(self):
667668
if self.param.multiple:
668669
return self.widget.selectedoptions
669670
else:
670-
return self.widget.currentIndex()
671+
return self.widget.currentData()
671672

672673

673674
class VectorWidgetWrapper(WidgetWrapper):

python/plugins/processing/tests/ParametersTest.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,15 @@ def testDefault(self):
272272
parameter = ParameterSelection('myName', 'myDesc', ['option1', 'option2', 'option3'], default=0.0)
273273
self.assertEqual(parameter.value, 0)
274274
parameter = ParameterSelection('myName', 'myDesc', ['option1', 'option2', 'option3'], default='a')
275-
self.assertEqual(parameter.value, 0)
275+
self.assertEqual(parameter.value, None)
276276

277277
def testOptional(self):
278278
optionalParameter = ParameterSelection('myName', 'myDesc', ['option1', 'option2', 'option3'], default=0, optional=True)
279279
self.assertEqual(optionalParameter.value, 0)
280280
optionalParameter.setValue(1)
281281
self.assertEqual(optionalParameter.value, 1)
282282
self.assertTrue(optionalParameter.setValue(None))
283-
self.assertEqual(optionalParameter.value, 0)
283+
self.assertEqual(optionalParameter.value, None)
284284

285285
requiredParameter = ParameterSelection('myName', 'myDesc', ['option1', 'option2', 'option3'], default=0, optional=False)
286286
self.assertEqual(requiredParameter.value, 0)
@@ -289,6 +289,22 @@ def testOptional(self):
289289
self.assertFalse(requiredParameter.setValue(None))
290290
self.assertEqual(requiredParameter.value, 1)
291291

292+
def testTupleOptions(self):
293+
options = (
294+
('o1', 'option1'),
295+
('o2', 'option2'),
296+
('o3', 'option3'))
297+
298+
optionalParameter = ParameterSelection('myName', 'myDesc', options, default='o1')
299+
self.assertEqual(optionalParameter.value, 'o1')
300+
optionalParameter.setValue('o2')
301+
self.assertEqual(optionalParameter.value, 'o2')
302+
303+
optionalParameter = ParameterSelection('myName', 'myDesc', options, default=['o1', 'o2'], multiple=True)
304+
self.assertEqual(optionalParameter.value, ['o1', 'o2'])
305+
optionalParameter.setValue(['o2'])
306+
self.assertEqual(optionalParameter.value, ['o2'])
307+
292308

293309
class ParameterFileTest(unittest.TestCase):
294310

0 commit comments

Comments
 (0)