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