LIST_TYPE_DICT = 2
def __init__(self, choices, type = None):
+ object.__init__(self)
self.choices = choices
if type is None:
if isinstance(choices, list):
self.type = type
def __list__(self):
- if self.type is choicesList.LIST_TYPE_LIST:
+ if self.type == choicesList.LIST_TYPE_LIST:
ret = [isinstance(x, tuple) and x[0] or x for x in self.choices]
else:
ret = self.choices.keys()
return ret or [""]
def __iter__(self):
- if self.type is choicesList.LIST_TYPE_LIST:
+ if self.type == choicesList.LIST_TYPE_LIST:
ret = [isinstance(x, tuple) and x[0] or x for x in self.choices]
else:
ret = self.choices
return len(self.choices) or 1
def __getitem__(self, index):
- if self.type is choicesList.LIST_TYPE_LIST:
+ if self.type == choicesList.LIST_TYPE_LIST:
ret = self.choices[index]
if isinstance(ret, tuple):
ret = ret[0]
return self.__list__().index(value)
def __setitem__(self, index, value):
- if self.type is choicesList.LIST_TYPE_LIST:
- if isinstance(self.choices[index], tuple):
- self.choices[index] = (value, self.choices[index][1])
+ if self.type == choicesList.LIST_TYPE_LIST:
+ orig = self.choices[index]
+ if isinstance(orig, tuple):
+ self.choices[index] = (value, orig[1])
else:
self.choices[index] = value
else:
class descriptionList(choicesList): # XXX: we might want a better name for this
def __list__(self):
- if self.type is choicesList.LIST_TYPE_LIST:
+ if self.type == choicesList.LIST_TYPE_LIST:
ret = [isinstance(x, tuple) and x[1] or x for x in self.choices]
else:
ret = self.choices.values()
return iter(self.__list__())
def __getitem__(self, index):
- if self.type is choicesList.LIST_TYPE_LIST:
+ if self.type == choicesList.LIST_TYPE_LIST:
for x in self.choices:
if isinstance(x, tuple):
- if x[0] is index:
+ if x[0] == index:
return str(x[1])
- elif x is index:
+ elif x == index:
return str(x)
return str(index) # Fallback!
else:
return str(self.choices.get(index, ""))
def __setitem__(self, index, value):
- if self.type is choicesList.LIST_TYPE_LIST:
+ if self.type == choicesList.LIST_TYPE_LIST:
i = self.index(index)
- if isinstance(self.choices[i], tuple):
- self.choices[i] = (self.choices[i][0], value)
+ orig = self.choices[i]
+ if isinstance(orig, tuple):
+ self.choices[i] = (orig[0], value)
else:
self.choices[i] = value
else:
class ConfigSelection(ConfigElement):
def __init__(self, choices, default = None):
ConfigElement.__init__(self)
-
- # this is an exakt copy of def setChoices.. but we save the call overhead
self.choices = choicesList(choices)
if default is None:
if default is None:
default = self.choices.default()
- self.default = self._value = self.last_value = default
- self.changed()
+ if self.value not in self.choices:
+ self.value = default
def setValue(self, value):
if value in self.choices:
def setCurrentText(self, text):
i = self.choices.index(self.value)
self.choices[i] = text
- descriptionList(self.choices.choices, self.choices.type)[text] = text
+ self.description[text] = text
self._value = text
value = property(getValue, setValue)
self.value = self.choices[0]
elif key == KEY_END:
self.value = self.choices[nchoices - 1]
-
+
def selectNext(self):
nchoices = len(self.choices)
i = self.choices.index(self.value)
self.value = self.choices[(i + 1) % nchoices]
def getText(self):
- descr = descriptionList(self.choices.choices, self.choices.type)[self.value]
+ descr = self.description[self.value]
if len(descr):
return _(descr)
return descr
def getMulti(self, selected):
- descr = descriptionList(self.choices.choices, self.choices.type)[self.value]
+ descr = self.description[self.value]
if len(descr):
return ("text", _(descr))
return ("text", descr)
def getHTML(self, id):
res = ""
for v in self.choices:
- descr = descriptionList(self.choices.choices, self.choices.type)[v]
+ descr = self.description[v]
if self.value == v:
checked = 'checked="checked" '
else:
# setValue does check if value is in choices. This is safe enough.
self.value = value
+ description = property(lambda self: descriptionList(self.choices.choices, self.choices.type))
+
# a binary decision.
#
# several customized versions exist for different
def genString(self, lst):
res = ""
- description = descriptionList(self.choices.choices, choicesList.LIST_TYPE_LIST)
for x in lst:
- res += description[x]+" "
+ res += self.description[x]+" "
return res
def getText(self):
if not selected or self.pos == -1:
return ("text", self.genString(self.value))
else:
- description = descriptionList(self.choices.choices, choicesList.LIST_TYPE_LIST)
tmp = self.value[:]
ch = self.choices[self.pos]
mem = ch in self.value
val1 = self.genString(tmp[:ind])
val2 = " "+self.genString(tmp[ind+1:])
if mem:
- chstr = " "+description[ch]+" "
+ chstr = " "+self.description[ch]+" "
else:
- chstr = "("+description[ch]+")"
+ chstr = "("+self.description[ch]+")"
return ("mtext", val1+chstr+val2, range(len(val1),len(val1)+len(chstr)))
def onDeselect(self, session):
def fromstring(self, val):
return eval(val)
+ description = property(lambda self: descriptionList(self.choices.choices, choicesList.LIST_TYPE_LIST))
+
class ConfigLocations(ConfigElement):
def __init__(self, default = [], visible_width = False):
ConfigElement.__init__(self)