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