8b880fba2728bcda81e89f7df2910f63f38c782d
[vuplus_dvbapp] / lib / python / Components / ConfigList.py
1 from HTMLComponent import *
2 from GUIComponent import *
3 from config import KEY_LEFT, KEY_RIGHT, KEY_0, KEY_DELETE, KEY_OK, KEY_TIMEOUT, ConfigElement
4 from Components.ActionMap import NumberActionMap
5 from enigma import eListbox, eListboxPythonConfigContent, 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.help_window = None
18                 self.setHelpWindowSession(session)
19
20         def execBegin(self):
21                 self.timer.timeout.get().append(self.timeout)
22
23         def execEnd(self):
24                 self.timer.timeout.get().remove(self.timeout)
25
26         def setHelpWindowSession(self, session):
27                 assert self.help_window is None, "you can't move a help window to another session"
28                 self.session = session
29
30         def toggle(self):
31                 selection = self.getCurrent()
32                 selection[1].toggle()
33                 self.invalidateCurrent()
34
35         def handleKey(self, key):
36                 selection = self.getCurrent()
37                 if selection and selection[1].enabled:
38                         selection[1].handleKey(key)
39                         self.invalidateCurrent()
40                         if self.help_window:
41                                 self.help_window.update(selection[1])
42                         if key not in [KEY_TIMEOUT, KEY_LEFT, KEY_RIGHT, KEY_DELETE, KEY_OK]:
43                                 self.timer.start(1000, 1)
44
45         def getCurrent(self):
46                 return self.l.getCurrentSelection()
47         
48         def invalidateCurrent(self):
49                 self.l.invalidateEntry(self.l.getCurrentSelectionIndex())
50
51         def invalidate(self, entry):
52                 # when the entry to invalidate does not exist, just ignore the request.
53                 # this eases up conditional setup screens a lot.
54                 if entry in self.__list:
55                         self.l.invalidateEntry(self.__list.index(entry))
56
57         GUI_WIDGET = eListbox
58         
59         def selectionChanged(self):
60                 n = self.getCurrent()
61                 
62                 if self.help_window:
63                         self.session.deleteDialog(self.help_window)
64                 
65                 nh = n and n[1].helpWindow()
66                 if nh is not None and self.session is not None:
67                         self.help_window = self.session.instantiateDialog(*nh)
68                         self.help_window.show()
69
70                 self.current = n
71                 for x in self.onSelectionChanged:
72                         x()
73
74         def postWidgetCreate(self, instance):
75                 instance.setContent(self.l)
76                 instance.selectionChanged.get().append(self.selectionChanged)
77         
78         def preWidgetRemove(self, instance):
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", "TextInputActions"],
108                 {
109                         "ok": self.keyOK,
110                         "left": self.keyLeft,
111                         "right": self.keyRight,
112                         "delete": self.keyDelete,
113                         "1": self.keyNumberGlobal,
114                         "2": self.keyNumberGlobal,
115                         "3": self.keyNumberGlobal,
116                         "4": self.keyNumberGlobal,
117                         "5": self.keyNumberGlobal,
118                         "6": self.keyNumberGlobal,
119                         "7": self.keyNumberGlobal,
120                         "8": self.keyNumberGlobal,
121                         "9": self.keyNumberGlobal,
122                         "0": self.keyNumberGlobal
123                 }, -1) # to prevent left/right overriding the listbox
124                 
125                 self["config"] = ConfigList(list, session = session)
126                 if on_change is not None:
127                         self.__changed = on_change
128                 else:
129                         self.__changed = lambda: None
130
131         def keyOK(self):
132                 self["config"].handleKey(KEY_OK)
133
134         def keyLeft(self):
135                 self["config"].handleKey(KEY_LEFT)
136                 self.__changed()
137
138         def keyRight(self):
139                 self["config"].handleKey(KEY_RIGHT)
140                 self.__changed()
141
142         def keyDelete(self):
143                 self["config"].handleKey(KEY_DELETE)
144                 self.__changed()
145
146         def keyNumberGlobal(self, number):
147                 self["config"].handleKey(KEY_0 + number)
148                 self.__changed()
149
150         # keySave and keyCancel are just provided in case you need them.
151         # you have to call them by yourself.
152         def keySave(self):
153                 for x in self["config"].list:
154                         x[1].save()
155                 self.close()
156         
157         def cancelConfirm(self, result):
158                 if not result:
159                         return
160
161                 for x in self["config"].list:
162                         x[1].cancel()
163                 self.close()
164
165         def keyCancel(self):
166                 if self["config"].isChanged():
167                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
168                 else:
169                         self.close()