- redraw now in idle
[enigma2.git] / components.py
1 from enigma import *
2 import time
3
4 # some helper classes first:
5 class HTMLComponent:
6         def produceHTML(self):
7                 return ""
8                 
9 class HTMLSkin:
10         order = ()
11
12         def __init__(self, order):
13                 self.order = order
14
15         def produceHTML(self):
16                 res = "<html>\n"
17                 for name in self.order:
18                         res += self[name].produceHTML()
19                 res += "</html>\n";
20                 return res
21
22 class GUISkin:
23         data = { }
24         def createGUIScreen(self, parent):
25                 for (name, val) in self.items():
26                         self.data[name] = { }
27                         val.GUIcreate(self.data[name], parent, None)
28
29 class GUIComponent:
30         """ GUI component """
31
32         def __init__(self):
33                 self.notifier = [ ]
34         
35         def GUIcreate(self, priv, parent, skindata):
36                 i = self.GUIcreateInstance(self, parent, skindata)
37                 priv["instance"] = i
38                 self.notifier.append(i)
39                 if self.notifierAdded:
40                         self.notifierAdded(i)
41
42 class VariableText:
43         """VariableText can be used for components which have a variable text, based on any widget with setText call"""
44         
45         def __init__(self):
46                 self.message = ""
47         
48         def notifierAdded(self, notifier):
49                 notifier.setText(self.message)
50
51         def setText(self, text):
52                 if self.message != text:
53                         self.message = text
54                         for x in self.notifier:
55                                 x.setText(self.message)
56
57         def getText(self):
58                 return self.message
59
60 class VariableValue:
61         """VariableValue can be used for components which have a variable value (like eSlider), based on any widget with setValue call"""
62         
63         def __init__(self):
64                 self.value = 0
65         
66         def notifierAdded(self, notifier):
67                 notifier.setValue(self.value)
68
69         def setValue(self, value):
70                 if self.value != value:
71                         self.value = value
72                         for x in self.notifier:
73                                 x.setValue(self.value)
74
75         def getValue(self):
76                 return self.value
77
78 # now some "real" components:
79
80 class Clock(HTMLComponent, GUIComponent, VariableText):
81         def __init__(self):
82                 VariableText.__init__(self)
83                 GUIComponent.__init__(self)
84                 self.doClock()
85                 
86                 self.clockTimer = eTimer()
87                 self.clockTimer.timeout.get().append(self.doClock)
88                 self.clockTimer.start(1000)
89
90 # "funktionalitaet"     
91         def doClock(self):
92                 self.setText("clock: " + time.asctime())
93
94 # realisierung als GUI
95         def GUIcreateInstance(self, priv, parent, skindata):
96                 g = eLabel(parent)
97                 return g
98
99 # ...und als HTML:
100         def produceHTML(self):
101                 return self.getText()
102
103 class Button(HTMLComponent, GUIComponent, VariableText):
104         def __init__(self, text=""):
105                 GUIComponent.__init__(self)
106                 VariableText.__init__(self)
107                 self.setText(text)
108                 self.onClick = [ ]
109         
110         def clicked(self):
111                 for x in self.onClick:
112                         x()
113                 return 0
114
115         def GUIcreate(self, priv, parent, skindata):
116                 GUIComponent.GUIcreate(self, priv,parent, skindata)
117                 priv["instance"].selected.get().append(self.clicked)
118         
119         def click(self):
120                 for x in self.onClick:
121                         x()
122
123 # html: 
124         def produceHTML(self):
125                 return "<input type=\"submit\" text=\"" + self.getText() + "\">\n"
126
127 # GUI:
128         def GUIcreateInstance(self, priv, parent, skindata):
129                 g = eButton(parent)
130 #               g.clicked = [ self.click ]
131                 return g
132
133 class Header(HTMLComponent, GUIComponent, VariableText):
134
135         def __init__(self, message):
136                 GUIComponent.__init__(self)
137                 VariableText.__init__(self)
138                 self.setText(message)
139         
140         def produceHTML(self):
141                 return "<h2>" + self.getText() + "</h2>\n"
142
143         def GUIcreateInstance(self, priv, parent, skindata):
144                 g = eLabel(parent)
145                 g.setText(self.message)
146                 return g
147
148 class VolumeBar(HTMLComponent, GUIComponent, VariableValue):
149         
150         def __init__(self):
151                 GUIComponent.__init__(self)
152                 VariableValue.__init__(self)
153
154         def GUIcreateInstance(self, priv, parent, skindata):
155                 g = eSlider(parent)
156                 g.setRange(0, 100)
157                 return g
158