d1b295baedff92dceff541a19474f26e0f4aca25
[vuplus_dvbapp] / lib / python / Components / ConfigList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3 from config import KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_0, KEY_DELETE, KEY_BACKSPACE, KEY_OK, KEY_TOGGLEOW, KEY_ASCII, KEY_TIMEOUT, KEY_NUMBERS, ConfigElement
4 from Components.ActionMap import NumberActionMap
5 from enigma import eListbox, eListboxPythonConfigContent, eRCInput, eTimer
6 from Screens.MessageBox import MessageBox
7
8 class ConfigList(HTMLComponent, GUIComponent, object):
9         def __init__(self, list, session = None):
10                 GUIComponent.__init__(self)
11                 self.l = eListboxPythonConfigContent()
12                 self.l.setSeperation(100)
13                 self.timer = eTimer()
14                 self.list = list
15                 self.onSelectionChanged = [ ]
16                 self.current = None
17                 self.session = session
18
19         def execBegin(self):
20                 rcinput = eRCInput.getInstance()
21                 rcinput.setKeyboardMode(rcinput.kmAscii)
22                 self.timer.callback.append(self.timeout)
23
24         def execEnd(self):
25                 rcinput = eRCInput.getInstance()
26                 rcinput.setKeyboardMode(rcinput.kmNone)
27                 self.timer.timeout.get().remove(self.timeout)
28
29         def toggle(self):
30                 selection = self.getCurrent()
31                 selection[1].toggle()
32                 self.invalidateCurrent()
33
34         def handleKey(self, key):
35                 selection = self.getCurrent()
36                 if selection and selection[1].enabled:
37                         selection[1].handleKey(key)
38                         self.invalidateCurrent()
39                         if key in KEY_NUMBERS:
40                                 self.timer.start(1000, 1)
41
42         def getCurrent(self):
43                 return self.l.getCurrentSelection()
44         
45         def getCurrentIndex(self):
46                 return self.l.getCurrentSelectionIndex()
47         
48         def setCurrentIndex(self, index):
49                 if self.instance is not None:
50                         self.instance.moveSelectionTo(index)
51         
52         def invalidateCurrent(self):
53                 self.l.invalidateEntry(self.l.getCurrentSelectionIndex())
54
55         def invalidate(self, entry):
56                 # when the entry to invalidate does not exist, just ignore the request.
57                 # this eases up conditional setup screens a lot.
58                 if entry in self.__list:
59                         self.l.invalidateEntry(self.__list.index(entry))
60
61         GUI_WIDGET = eListbox
62         
63         def selectionChanged(self):
64                 if self.current:
65                         self.current[1].onDeselect(self.session)
66                 self.current = self.getCurrent()
67                 if self.current:
68                         self.current[1].onSelect(self.session)
69                 for x in self.onSelectionChanged:
70                         x()
71
72         def postWidgetCreate(self, instance):
73                 instance.selectionChanged.get().append(self.selectionChanged)
74                 instance.setContent(self.l)
75         
76         def preWidgetRemove(self, instance):
77                 if self.current:
78                         self.current[1].onDeselect(self.session)
79                 instance.selectionChanged.get().remove(self.selectionChanged)
80         
81         def setList(self, l):
82                 self.timer.stop()
83                 self.__list = l
84                 self.l.setList(self.__list)
85
86                 if l is not None:
87                         for x in l:
88                                 assert isinstance(x[1], ConfigElement), "entry in ConfigList " + str(x[1]) + " must be a ConfigElement"
89
90         def getList(self):
91                 return self.__list
92
93         list = property(getList, setList)
94
95         def timeout(self):
96                 self.handleKey(KEY_TIMEOUT)
97
98         def isChanged(self):
99                 is_changed = False
100                 for x in self.list:
101                         is_changed |= x[1].isChanged()
102
103                 return is_changed
104
105 class ConfigListScreen:
106         def __init__(self, list, session = None, on_change = None):
107                 self["config_actions"] = NumberActionMap(["SetupActions", "InputAsciiActions", "KeyboardInputActions"],
108                 {
109                         "gotAsciiCode": self.keyGotAscii,
110                         "ok": self.keyOK,
111                         "left": self.keyLeft,
112                         "right": self.keyRight,
113                         "home": self.keyHome,
114                         "end": self.keyEnd,
115                         "deleteForward": self.keyDelete,
116                         "deleteBackward": self.keyBackspace,
117                         "toggleOverwrite": self.keyToggleOW,
118                         "1": self.keyNumberGlobal,
119                         "2": self.keyNumberGlobal,
120                         "3": self.keyNumberGlobal,
121                         "4": self.keyNumberGlobal,
122                         "5": self.keyNumberGlobal,
123                         "6": self.keyNumberGlobal,
124                         "7": self.keyNumberGlobal,
125                         "8": self.keyNumberGlobal,
126                         "9": self.keyNumberGlobal,
127                         "0": self.keyNumberGlobal
128                 }, -1) # to prevent left/right overriding the listbox
129                 
130                 self["config"] = ConfigList(list, session = session)
131                 if on_change is not None:
132                         self.__changed = on_change
133                 else:
134                         self.__changed = lambda: None
135
136         def keyOK(self):
137                 self["config"].handleKey(KEY_OK)
138
139         def keyLeft(self):
140                 self["config"].handleKey(KEY_LEFT)
141                 self.__changed()
142
143         def keyRight(self):
144                 self["config"].handleKey(KEY_RIGHT)
145                 self.__changed()
146
147         def keyHome(self):
148                 self["config"].handleKey(KEY_HOME)
149                 self.__changed()
150
151         def keyEnd(self):
152                 self["config"].handleKey(KEY_END)
153                 self.__changed()
154
155         def keyDelete(self):
156                 self["config"].handleKey(KEY_DELETE)
157                 self.__changed()
158
159         def keyBackspace(self):
160                 self["config"].handleKey(KEY_BACKSPACE)
161                 self.__changed()
162
163         def keyToggleOW(self):
164                 self["config"].handleKey(KEY_TOGGLEOW)
165                 self.__changed()
166
167         def keyGotAscii(self):
168                 self["config"].handleKey(KEY_ASCII)
169                 self.__changed()
170
171         def keyNumberGlobal(self, number):
172                 self["config"].handleKey(KEY_0 + number)
173                 self.__changed()
174
175         # keySave and keyCancel are just provided in case you need them.
176         # you have to call them by yourself.
177         def keySave(self):
178                 for x in self["config"].list:
179                         x[1].save()
180                 self.close()
181         
182         def cancelConfirm(self, result):
183                 if not result:
184                         return
185
186                 for x in self["config"].list:
187                         x[1].cancel()
188                 self.close()
189
190         def keyCancel(self):
191                 if self["config"].isChanged():
192                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
193                 else:
194                         self.close()