63e5669e9e0e59af5ade944c976ecfe1b7c996a8
[enigma2.git] / components.py
1 from enigma import *
2 import time
3 import sys
4
5 # some helper classes first:
6 class HTMLComponent:
7         def produceHTML(self):
8                 return ""
9                 
10 class HTMLSkin:
11         order = ()
12
13         def __init__(self, order):
14                 self.order = order
15
16         def produceHTML(self):
17                 res = "<html>\n"
18                 for name in self.order:
19                         res += self[name].produceHTML()
20                 res += "</html>\n";
21                 return res
22
23 class GUISkin:
24         def __init__(self):
25                 pass
26         
27         def createGUIScreen(self, parent):
28                 for (name, val) in self.items():
29                         if isinstance(val, GUIComponent):
30                                 val.GUIcreate(parent, None)
31         
32         def deleteGUIScreen(self):
33                 for (name, val) in self.items():
34                         if isinstance(val, GUIComponent):
35                                 val.GUIdelete()
36                         try:
37                                 val.fix()
38                         except:
39                                 pass
40                         
41                         # note: you'll probably run into this assert. if this happens, don't panic!
42                         # yes, it's evil. I told you that programming in python is just fun, and 
43                         # suddently, you have to care about things you don't even know.
44                         #
45                         # but calm down, the solution is easy, at least on paper:
46                         #
47                         # Each Component, which is a GUIComponent, owns references to each
48                         # instantiated eWidget (namely in screen.data[name]["instance"], in case
49                         # you care.)
50                         # on deleteGUIscreen, all eWidget *must* (!) be deleted (otherwise,
51                         # well, problems appear. I don't want to go into details too much,
52                         # but this would be a memory leak anyway.)
53                         # The assert beyond checks for that. It asserts that the corresponding
54                         # eWidget is about to be removed (i.e., that the refcount becomes 0 after
55                         # running deleteGUIscreen).
56                         # (You might wonder why the refcount is checked for 2 and not for 1 or 0 -
57                         # one reference is still hold by the local variable 'w', another one is
58                         # hold be the function argument to sys.getrefcount itself. So only if it's
59                         # 2 at this point, the object will be destroyed after leaving deleteGUIscreen.)
60                         #
61                         # Now, how to fix this problem? You're holding a reference somewhere. (References
62                         # can only be hold from Python, as eWidget itself isn't related to the c++
63                         # way of having refcounted objects. So it must be in python.)
64                         #
65                         # It could be possible that you're calling deleteGUIscreen trough a call of
66                         # a PSignal. For example, you could try to call screen.doClose() in response
67                         # to a Button::click. This will fail. (It wouldn't work anyway, as you would
68                         # remove a dialog while running it. It never worked - enigma1 just set a 
69                         # per-mainloop variable on eWidget::close() to leave the exec()...)
70                         # That's why Session supports delayed closes. Just call Session.close() and
71                         # it will work.
72                         #
73                         # Another reason is that you just stored the data["instance"] somewhere. or
74                         # added it into a notifier list and didn't removed it.
75                         #
76                         # If you can't help yourself, just ask me. I'll be glad to help you out.
77                         # Sorry for not keeping this code foolproof. I really wanted to archive
78                         # that, but here I failed miserably. All I could do was to add this assert.
79 #                       assert sys.getrefcount(w) == 2, "too many refs hold to " + str(w)
80         
81         def close(self):
82                 self.deleteGUIScreen()
83
84 class GUIComponent:
85         """ GUI component """
86
87         def __init__(self):
88                 pass
89         
90 class VariableText:
91         """VariableText can be used for components which have a variable text, based on any widget with setText call"""
92         
93         def __init__(self):
94                 self.message = ""
95                 self.instance = None
96         
97         def setText(self, text):
98                 self.message = text
99                 if self.instance:
100                         self.instance.setText(self.message)
101
102         def getText(self):
103                 return self.message
104         
105         def GUIcreate(self, parent, skindata):
106                 self.instance = self.createWidget(parent, skindata)
107                 self.instance.setText(self.message)
108         
109         def GUIdelete(self):
110                 self.removeWidget(self.instance)
111                 del self.instance
112         
113         def removeWidget(self, instance):
114                 pass
115
116 class VariableValue:
117         """VariableValue can be used for components which have a variable value (like eSlider), based on any widget with setValue call"""
118         
119         def __init__(self):
120                 self.value = 0
121                 self.instance = None
122         
123         def setValue(self, value):
124                 self.value = value
125                 if self.instance:
126                         self.instance.setValue(self.value)
127
128         def getValue(self):
129                 return self.value
130                 
131         def GUIcreate(self, parent, skindata):
132                 self.instance = self.createWidget(parent, skindata)
133                 self.instance.setValue(self.value)
134         
135         def GUIdelete(self):
136                 self.removeWidget(self.instance)
137                 del self.instance
138         
139         def removeWidget(self, instance):
140                 pass
141
142 # now some "real" components:
143
144 class Clock(HTMLComponent, GUIComponent, VariableText):
145         def __init__(self):
146                 VariableText.__init__(self)
147                 GUIComponent.__init__(self)
148                 self.doClock()
149                 
150                 self.clockTimer = eTimer()
151                 self.clockTimer.timeout.get().append(self.doClock)
152                 self.clockTimer.start(1000)
153
154 # "funktionalitaet"     
155         def doClock(self):
156                 self.setText("clock: " + time.asctime())
157
158 # realisierung als GUI
159         def createWidget(self, parent, skindata):
160                 return eLabel(parent)
161
162         def removeWidget(self, w):
163                 del self.clockTimer
164
165 # ...und als HTML:
166         def produceHTML(self):
167                 return self.getText()
168                 
169 class Button(HTMLComponent, GUIComponent, VariableText):
170         def __init__(self, text="", onClick = [ ]):
171                 GUIComponent.__init__(self)
172                 VariableText.__init__(self)
173                 self.setText(text)
174                 self.onClick = onClick
175         
176         def push(self):
177                 for x in self.onClick:
178                         x()
179                 return 0
180         
181         def disable(self):
182 #               self.instance.hide()
183                 pass
184         
185         def enable(self):
186 #               self.instance.show()
187                 pass
188
189 # html:
190         def produceHTML(self):
191                 return "<input type=\"submit\" text=\"" + self.getText() + "\">\n"
192
193 # GUI:
194         def createWidget(self, parent, skindata):
195                 g = eButton(parent)
196                 g.selected.get().append(self.push)
197                 return g
198
199         def removeWidget(self, w):
200                 w.selected.get().remove(self.push)
201
202 class Label(HTMLComponent, GUIComponent, VariableText):
203         def __init__(self, text=""):
204                 GUIComponent.__init__(self)
205                 VariableText.__init__(self)
206                 self.setText(text)
207         
208 # html: 
209         def produceHTML(self):
210                 return self.getText()
211
212 # GUI:
213         def createWidget(self, parent, skindata):
214                 return eLabel(parent)
215         
216 class Header(HTMLComponent, GUIComponent, VariableText):
217
218         def __init__(self, message):
219                 GUIComponent.__init__(self)
220                 VariableText.__init__(self)
221                 self.setText(message)
222         
223         def produceHTML(self):
224                 return "<h2>" + self.getText() + "</h2>\n"
225
226         def createWidget(self, parent, skindata):
227                 g = eLabel(parent)
228                 return g
229
230 class VolumeBar(HTMLComponent, GUIComponent, VariableValue):
231         
232         def __init__(self):
233                 GUIComponent.__init__(self)
234                 VariableValue.__init__(self)
235
236         def createWidget(self, parent, skindata):
237                 g = eSlider(parent)
238                 g.setRange(0, 100)
239                 return g
240                 
241 # a general purpose progress bar
242 class ProgressBar(HTMLComponent, GUIComponent, VariableValue):
243         def __init__(self):
244                 GUIComponent.__init__(self)
245                 VariableValue.__init__(self)
246
247         def createWidget(self, parent, skindata):
248                 g = eSlider(parent)
249                 g.setRange(0, 100)
250                 return g
251         
252 class MenuList(HTMLComponent, GUIComponent):
253         def __init__(self, list):
254                 GUIComponent.__init__(self)
255                 self.l = eListboxPythonStringContent()
256                 self.l.setList(list)
257         
258         def getCurrent(self):
259                 return self.l.getCurrentSelection()
260         
261         def GUIcreate(self, parent, skindata):
262                 self.instance = eListbox(parent)
263                 self.instance.setContent(self.l)
264         
265         def GUIdelete(self):
266                 self.instance.setContent(None)
267
268 class ServiceList(HTMLComponent, GUIComponent):
269         def __init__(self):
270                 GUIComponent.__init__(self)
271                 self.l = eListboxServiceContent()
272         
273         def getCurrent(self):
274                 r = eServiceReference()
275                 self.l.getCurrent(r)
276                 return r
277
278         def GUIcreate(self, parent, skindata):
279                 self.instance = eListbox(parent)
280                 self.instance.setContent(self.l)
281         
282         def GUIdelete(self):
283                 del self.instance
284
285         def setRoot(self, root):
286                 self.l.setRoot(root)
287
288 class ServiceScan:
289         
290         Idle = 1
291         Running = 2
292         Done = 3
293         Error = 4
294                 
295         def scanStatusChanged(self):
296                 if self.state == self.Running:
297                         self.progressbar.setValue(self.scan.getProgress())
298                         if self.scan.isDone():
299                                 self.state = self.Done
300                         else:
301                                 self.text.setText("scan in progress - %d %% done!\n%d services found!" % (self.scan.getProgress(), self.scan.getNumServices()))
302                 
303                 if self.state == self.Done:
304                         self.text.setText("scan done!")
305                 
306                 if self.state == self.Error:
307                         self.text.setText("ERROR - failed to scan!")
308         
309         def __init__(self, progressbar, text):
310                 self.progressbar = progressbar
311                 self.text = text
312                 self.scan = eComponentScan()
313                 if self.scan.start():
314                         self.state = self.Error
315                 else:
316                         self.state = self.Running
317                 self.scan.statusChanged.get().append(self.scanStatusChanged)
318                 self.scanStatusChanged()
319
320         def isDone(self):
321                 return self.state == self.Done
322
323         def fix(self):
324                 self.scan.statusChanged.get().remove(self.scanStatusChanged)
325