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