2nd try ;)
[enigma2.git] / lib / python / Components / Sources / List.py
1 from Source import Source
2 from Components.Element import cached
3
4 class List(Source, object):
5         """The datasource of a listbox. Currently, the format depends on the used converter. So
6 if you put a simple string list in here, you need to use a StringList converter, if you are
7 using a "multi content list styled"-list, you need to use the StaticMultiList converter, and
8 setup the "fonts". 
9
10 This has been done so another converter could convert the list to a different format, for example
11 to generate HTML."""
12         def __init__(self, list = [ ], enableWrapAround = False, item_height = 25, fonts = [ ]):
13                 Source.__init__(self)
14                 self.__list = list
15                 self.onSelectionChanged = [ ]
16                 self.item_height = item_height
17                 self.fonts = fonts
18                 self.disable_callbacks = False
19                 self.enableWrapAround = enableWrapAround
20
21         def setList(self, list):
22                 self.__list = list
23                 self.changed((self.CHANGED_ALL,))
24
25         list = property(lambda self: self.__list, setList)
26
27         def entry_changed(self, index):
28                 if not self.disable_callbacks:
29                         self.downstream_elements.entry_changed(self, index)
30                         
31         def count(self):
32                 return len(self.__list)
33
34         def selectionChanged(self, index):
35                 if self.disable_callbacks:
36                         return
37
38                 for x in self.onSelectionChanged:
39                         x()
40
41         @cached
42         def getCurrent(self):
43                 return self.master is not None and self.master.current
44
45         current = property(getCurrent)
46
47         def setIndex(self, index):
48                 if self.master is not None:
49                         self.master.index = index
50                         self.selectionChanged(index)
51
52         @cached
53         def getIndex(self):
54                 if self.master is not None:
55                         return self.master.index
56                 else:
57                         return None
58
59         setCurrentIndex = setIndex
60
61         index = property(getIndex, setIndex)
62         
63         def selectNext(self):
64                 if self.getIndex() + 1 >= self.count():
65                         if self.enableWrapAround:
66                                 self.index = 0
67                 else:
68                         self.index += 1
69                 self.setIndex(self.index)
70
71         def selectPrevious(self):
72                 if self.getIndex() - 1 < 0:
73                         if self.enableWrapAround:
74                                 self.index = self.count() - 1
75                 else:
76                         self.index -= 1
77                 self.setIndex(self.index)
78
79         def updateList(self, list):
80                 """Changes the list without changing the selection or emitting changed Events"""
81                 assert len(list) == len(self.__list)
82                 print "get old index"
83                 old_index = self.index
84                 print "disable callback"
85                 self.disable_callbacks = True
86                 print "set list"
87                 self.list = list
88                 print "set index"
89                 self.index = old_index
90                 print "reenable callbacks"
91                 self.disable_callbacks = False
92                 print "done"