add "Blink" option to ConditionalShowHide
[enigma2.git] / lib / python / Components / ConfigList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
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 getCurrentIndex(self):
49                 return self.l.getCurrentSelectionIndex()
50         
51         def setCurrentIndex(self, index):
52                 if self.instance is not None:
53                         self.instance.moveSelectionTo(index)
54         
55         def invalidateCurrent(self):
56                 self.l.invalidateEntry(self.l.getCurrentSelectionIndex())
57
58         def invalidate(self, entry):
59                 # when the entry to invalidate does not exist, just ignore the request.
60                 # this eases up conditional setup screens a lot.
61                 if entry in self.__list:
62                         self.l.invalidateEntry(self.__list.index(entry))
63
64         GUI_WIDGET = eListbox
65         
66         def selectionChanged(self):
67                 n = self.getCurrent()
68                 
69                 if self.help_window:
70                         self.session.deleteDialog(self.help_window)
71                 
72                 nh = n and n[1].helpWindow()
73                 if nh is not None and self.session is not None:
74                         self.help_window = self.session.instantiateDialog(*nh)
75                         self.help_window.show()
76
77                 self.current = n
78                 for x in self.onSelectionChanged:
79                         x()
80
81         def postWidgetCreate(self, instance):
82                 instance.setContent(self.l)
83                 instance.selectionChanged.get().append(self.selectionChanged)
84         
85         def preWidgetRemove(self, instance):
86                 instance.selectionChanged.get().remove(self.selectionChanged)
87         
88         def setList(self, l):
89                 self.timer.stop()
90                 self.__list = l
91                 self.l.setList(self.__list)
92
93                 if l is not None:
94                         for x in l:
95                                 assert isinstance(x[1], ConfigElement), "entry in ConfigList " + str(x[1]) + " must be a ConfigElement"
96
97         def getList(self):
98                 return self.__list
99
100         list = property(getList, setList)
101
102         def timeout(self):
103                 self.handleKey(KEY_TIMEOUT)
104
105         def isChanged(self):
106                 is_changed = False
107                 for x in self.list:
108                         is_changed |= x[1].isChanged()
109
110                 return is_changed
111
112 class ConfigListScreen:
113         def __init__(self, list, session = None, on_change = None):
114                 self["config_actions"] = NumberActionMap(["SetupActions", "TextInputActions"],
115                 {
116                         "ok": self.keyOK,
117                         "left": self.keyLeft,
118                         "right": self.keyRight,
119                         "delete": self.keyDelete,
120                         "1": self.keyNumberGlobal,
121                         "2": self.keyNumberGlobal,
122                         "3": self.keyNumberGlobal,
123                         "4": self.keyNumberGlobal,
124                         "5": self.keyNumberGlobal,
125                         "6": self.keyNumberGlobal,
126                         "7": self.keyNumberGlobal,
127                         "8": self.keyNumberGlobal,
128                         "9": self.keyNumberGlobal,
129                         "0": self.keyNumberGlobal
130                 }, -1) # to prevent left/right overriding the listbox
131                 
132                 self["config"] = ConfigList(list, session = session)
133                 if on_change is not None:
134                         self.__changed = on_change
135                 else:
136                         self.__changed = lambda: None
137
138         def keyOK(self):
139                 self["config"].handleKey(KEY_OK)
140
141         def keyLeft(self):
142                 self["config"].handleKey(KEY_LEFT)
143                 self.__changed()
144
145         def keyRight(self):
146                 self["config"].handleKey(KEY_RIGHT)
147                 self.__changed()
148
149         def keyDelete(self):
150                 self["config"].handleKey(KEY_DELETE)
151                 self.__changed()
152
153         def keyNumberGlobal(self, number):
154                 self["config"].handleKey(KEY_0 + number)
155                 self.__changed()
156
157         # keySave and keyCancel are just provided in case you need them.
158         # you have to call them by yourself.
159         def keySave(self):
160                 for x in self["config"].list:
161                         x[1].save()
162                 self.close()
163         
164         def cancelConfirm(self, result):
165                 if not result:
166                         return
167
168                 for x in self["config"].list:
169                         x[1].cancel()
170                 self.close()
171
172         def keyCancel(self):
173                 if self["config"].isChanged():
174                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
175                 else:
176                         self.close()