6f0670a1f6f059d8485d0c0016c8a79847ce7cd8
[vuplus_dvbapp] / 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                 self.__style = "default" # style might be an optional string which can be used to define different visualisations in the skin
21
22         def setList(self, list):
23                 self.__list = list
24                 self.changed((self.CHANGED_ALL,))
25
26         list = property(lambda self: self.__list, setList)
27
28         def entry_changed(self, index):
29                 if not self.disable_callbacks:
30                         self.downstream_elements.entry_changed(index)
31
32         def modifyEntry(self, index, data):
33                 self.__list[index] = data
34                 self.entry_changed(index)
35
36         def count(self):
37                 return len(self.__list)
38
39         def selectionChanged(self, index):
40                 if self.disable_callbacks:
41                         return
42
43                 # update all non-master targets
44                 for x in self.downstream_elements:
45                         if x is not self.master:
46                                 x.index = index
47
48                 for x in self.onSelectionChanged:
49                         x()
50
51         @cached
52         def getCurrent(self):
53                 return self.master is not None and self.master.current
54
55         current = property(getCurrent)
56
57         def setIndex(self, index):
58                 if self.master is not None:
59                         self.master.index = index
60                         self.selectionChanged(index)
61
62         @cached
63         def getIndex(self):
64                 if self.master is not None:
65                         return self.master.index
66                 else:
67                         return None
68
69         setCurrentIndex = setIndex
70
71         index = property(getIndex, setIndex)
72         
73         def selectNext(self):
74                 if self.getIndex() + 1 >= self.count():
75                         if self.enableWrapAround:
76                                 self.index = 0
77                 else:
78                         self.index += 1
79                 self.setIndex(self.index)
80
81         def selectPrevious(self):
82                 if self.getIndex() - 1 < 0:
83                         if self.enableWrapAround:
84                                 self.index = self.count() - 1
85                 else:
86                         self.index -= 1
87                 self.setIndex(self.index)
88
89         @cached
90         def getStyle(self):
91                 return self.__style
92
93         def setStyle(self, style):
94                 if self.__style != style:
95                         self.__style = style
96                         self.changed((self.CHANGED_SPECIFIC, "style"))
97
98         style = property(getStyle, setStyle)
99
100         def updateList(self, list):
101                 """Changes the list without changing the selection or emitting changed Events"""
102                 assert len(list) == len(self.__list)
103                 old_index = self.index
104                 self.disable_callbacks = True
105                 self.list = list
106                 self.index = old_index
107                 self.disable_callbacks = False