config rewrite. some extensions still need to be updated.
[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
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                         print "close old help window!"
56                         self.session.deleteDialog(self.help_window)
57                 
58                 nh = n and n[1].helpWindow()
59                 print "n, nh:", n, nh
60                 if nh is not None and self.session is not None:
61                         print "show new help window"
62                         self.help_window = self.session.instantiateDialog(*nh)
63                         self.help_window.show()
64
65                 print "config selection changed, from ", self.current, " to ", n
66                 self.current = n
67                 for x in self.onSelectionChanged:
68                         x()
69
70         def postWidgetCreate(self, instance):
71                 instance.setContent(self.l)
72                 instance.selectionChanged.get().append(self.selectionChanged)
73         
74         def preWidgetRemove(self, instance):
75                 instance.selectionChanged.get().remove(self.selectionChanged)
76         
77         def setList(self, list):
78                 self.__list = list
79                 self.l.setList(self.__list)
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)
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)