5b959dc2baef4dfddebefa442aa8e498cbce1627
[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
7 class ConfigList(HTMLComponent, GUIComponent, object):
8         def __init__(self, list, session = None):
9                 GUIComponent.__init__(self)
10                 self.l = eListboxPythonConfigContent()
11                 self.l.setSeperation(100)
12                 self.timer = eTimer()
13                 self.timer.timeout.get().append(self.timeout)
14                 self.list = list
15                 self.onSelectionChanged = [ ]
16                 self.current = None
17                 self.help_window = None
18                 self.setHelpWindowSession(session)
19
20         def execEnd(self):
21                 self.timer = eTimer()
22                 self.timer.timeout.get().append(self.timeout)
23
24         def setHelpWindowSession(self, session):
25                 assert self.help_window is None, "you can't move a help window to another session"
26                 self.session = session
27
28         def toggle(self):
29                 selection = self.getCurrent()
30                 selection[1].toggle()
31                 self.invalidateCurrent()
32
33         def handleKey(self, key):
34                 selection = self.getCurrent()
35                 if selection and selection[1].enabled:
36                         selection[1].handleKey(key)
37                         self.invalidateCurrent()
38                         if self.help_window:
39                                 self.help_window.update(selection[1])
40                         if key not in [KEY_TIMEOUT, KEY_LEFT, KEY_RIGHT, KEY_DELETE, KEY_OK]:
41                                 self.timer.start(1000, 1)
42
43         def getCurrent(self):
44                 return self.l.getCurrentSelection()
45         
46         def invalidateCurrent(self):
47                 self.l.invalidateEntry(self.l.getCurrentSelectionIndex())
48                 
49         def invalidate(self, entry):
50                 self.l.invalidateEntry(self.__list.index(entry))
51
52         GUI_WIDGET = eListbox
53         
54         def selectionChanged(self):
55                 n = self.getCurrent()
56                 
57                 if self.help_window:
58                         self.session.deleteDialog(self.help_window)
59                 
60                 nh = n and n[1].helpWindow()
61                 if nh is not None and self.session is not None:
62                         self.help_window = self.session.instantiateDialog(*nh)
63                         self.help_window.show()
64
65                 self.current = n
66                 for x in self.onSelectionChanged:
67                         x()
68
69         def postWidgetCreate(self, instance):
70                 instance.setContent(self.l)
71                 instance.selectionChanged.get().append(self.selectionChanged)
72         
73         def preWidgetRemove(self, instance):
74                 instance.selectionChanged.get().remove(self.selectionChanged)
75         
76         def setList(self, l):
77                 self.timer.stop()
78                 self.__list = l
79                 self.l.setList(self.__list)
80
81                 if l is not None:
82                         for x in l:
83                                 assert isinstance(x[1], ConfigElement), "entry in ConfigList " + str(x[1]) + " must be a ConfigElement"
84
85         def getList(self):
86                 return self.__list
87
88         list = property(getList, setList)
89
90         def timeout(self):
91                 self.handleKey(KEY_TIMEOUT)
92
93 class ConfigListScreen:
94         def __init__(self, list, session = None):
95                 self["config_actions"] = NumberActionMap(["SetupActions", "TextInputActions"],
96                 {
97                         "ok": self.keyOK,
98                         "left": self.keyLeft,
99                         "right": self.keyRight,
100                         "delete": self.keyDelete,
101                         "1": self.keyNumberGlobal,
102                         "2": self.keyNumberGlobal,
103                         "3": self.keyNumberGlobal,
104                         "4": self.keyNumberGlobal,
105                         "5": self.keyNumberGlobal,
106                         "6": self.keyNumberGlobal,
107                         "7": self.keyNumberGlobal,
108                         "8": self.keyNumberGlobal,
109                         "9": self.keyNumberGlobal,
110                         "0": self.keyNumberGlobal
111                 }, -1) # to prevent left/right overriding the listbox
112                 
113                 self["config"] = ConfigList(list, session = session)
114
115         def keyOK(self):
116                 self["config"].handleKey(KEY_OK)
117
118         def keyLeft(self):
119                 self["config"].handleKey(KEY_LEFT)
120
121         def keyRight(self):
122                 self["config"].handleKey(KEY_RIGHT)
123
124         def keyDelete(self):
125                 self["config"].handleKey(KEY_DELETE)
126
127         def keyNumberGlobal(self, number):
128                 self["config"].handleKey(KEY_0 + number)