ListParameter bugfix: allow unhashable types as parameter values.

This commit is contained in:
Luke Campagnola 2013-07-12 13:14:09 -04:00
parent 5a2b946205
commit 46901ae83a
2 changed files with 29 additions and 28 deletions

View File

@ -67,7 +67,7 @@ params = [
{'name': 'Float', 'type': 'float', 'value': 10.5, 'step': 0.1}, {'name': 'Float', 'type': 'float', 'value': 10.5, 'step': 0.1},
{'name': 'String', 'type': 'str', 'value': "hi"}, {'name': 'String', 'type': 'str', 'value': "hi"},
{'name': 'List', 'type': 'list', 'values': [1,2,3], 'value': 2}, {'name': 'List', 'type': 'list', 'values': [1,2,3], 'value': 2},
{'name': 'Named List', 'type': 'list', 'values': {"one": 1, "two": 2, "three": 3}, 'value': 2}, {'name': 'Named List', 'type': 'list', 'values': {"one": 1, "two": "twosies", "three": [3,3,3]}, 'value': 2},
{'name': 'Boolean', 'type': 'bool', 'value': True, 'tip': "This is a checkbox"}, {'name': 'Boolean', 'type': 'bool', 'value': True, 'tip': "This is a checkbox"},
{'name': 'Color', 'type': 'color', 'value': "FF0", 'tip': "This is a color button"}, {'name': 'Color', 'type': 'color', 'value': "FF0", 'tip': "This is a color button"},
{'name': 'Gradient', 'type': 'colormap'}, {'name': 'Gradient', 'type': 'colormap'},

View File

@ -476,32 +476,16 @@ class ListParameterItem(WidgetParameterItem):
return w return w
def value(self): def value(self):
#vals = self.param.opts['limits']
key = asUnicode(self.widget.currentText()) key = asUnicode(self.widget.currentText())
#if isinstance(vals, dict):
#return vals[key]
#else:
#return key
#print key, self.forward
return self.forward.get(key, None) return self.forward.get(key, None)
def setValue(self, val): def setValue(self, val):
#vals = self.param.opts['limits']
#if isinstance(vals, dict):
#key = None
#for k,v in vals.iteritems():
#if v == val:
#key = k
#if key is None:
#raise Exception("Value '%s' not allowed." % val)
#else:
#key = unicode(val)
self.targetValue = val self.targetValue = val
if val not in self.reverse: if val not in self.reverse[0]:
self.widget.setCurrentIndex(0) self.widget.setCurrentIndex(0)
else: else:
key = self.reverse[val] key = self.reverse[1][self.reverse[0].index(val)]
ind = self.widget.findText(key) ind = self.widget.findText(key)
self.widget.setCurrentIndex(ind) self.widget.setCurrentIndex(ind)
@ -531,8 +515,8 @@ class ListParameter(Parameter):
itemClass = ListParameterItem itemClass = ListParameterItem
def __init__(self, **opts): def __init__(self, **opts):
self.forward = OrderedDict() ## name: value self.forward = OrderedDict() ## {name: value, ...}
self.reverse = OrderedDict() ## value: name self.reverse = ([], []) ## ([value, ...], [name, ...])
## Parameter uses 'limits' option to define the set of allowed values ## Parameter uses 'limits' option to define the set of allowed values
if 'values' in opts: if 'values' in opts:
@ -547,23 +531,40 @@ class ListParameter(Parameter):
Parameter.setLimits(self, limits) Parameter.setLimits(self, limits)
#print self.name(), self.value(), limits #print self.name(), self.value(), limits
if self.value() not in self.reverse and len(self.reverse) > 0: if len(self.reverse) > 0 and self.value() not in self.reverse[0]:
self.setValue(list(self.reverse.keys())[0]) self.setValue(self.reverse[0][0])
#def addItem(self, name, value=None):
#if name in self.forward:
#raise Exception("Name '%s' is already in use for this parameter" % name)
#limits = self.opts['limits']
#if isinstance(limits, dict):
#limits = limits.copy()
#limits[name] = value
#self.setLimits(limits)
#else:
#if value is not None:
#raise Exception ## raise exception or convert to dict?
#limits = limits[:]
#limits.append(name)
## what if limits == None?
@staticmethod @staticmethod
def mapping(limits): def mapping(limits):
## Return forward and reverse mapping dictionaries given a limit specification ## Return forward and reverse mapping objects given a limit specification
forward = OrderedDict() ## name: value forward = OrderedDict() ## {name: value, ...}
reverse = OrderedDict() ## value: name reverse = ([], []) ## ([value, ...], [name, ...])
if isinstance(limits, dict): if isinstance(limits, dict):
for k, v in limits.items(): for k, v in limits.items():
forward[k] = v forward[k] = v
reverse[v] = k reverse[0].append(v)
reverse[1].append(k)
else: else:
for v in limits: for v in limits:
n = asUnicode(v) n = asUnicode(v)
forward[n] = v forward[n] = v
reverse[v] = n reverse[0].append(v)
reverse[1].append(n)
return forward, reverse return forward, reverse
registerParameterType('list', ListParameter, override=True) registerParameterType('list', ListParameter, override=True)