the long awaited feature "blinking label" is now available
[enigma2.git] / lib / python / Components / ConditionalWidget.py
1 import skin
2 from GUIComponent import *
3
4 from enigma import *
5
6 class Widget(GUIComponent):
7         
8         SHOWN = 0
9         HIDDEN = 1
10         
11         def __init__(self):
12                 GUIComponent.__init__(self)
13                 self.instance = None
14                 self.state = self.SHOWN
15         
16         def GUIcreate(self, parent):
17                 self.instance = self.createWidget(parent)
18         
19         def GUIdelete(self):
20                 self.removeWidget(self.instance)
21                 self.instance = None
22         
23         def removeWidget(self, w):
24                 pass
25         
26         def showWidget(self):
27                 self.state = self.SHOWN
28                 self.instance.show()
29
30         def hideWidget(self):
31                 self.state = self.HIDDEN
32                 self.instance.hide()
33         
34 class ConditionalWidget(Widget):
35         def __init__(self, withTimer = True):
36                 Widget.__init__(self)
37                 
38                 self.setConnect(None)
39                 
40                 if (withTimer):
41                         self.conditionCheckTimer = eTimer()
42                         self.conditionCheckTimer.timeout.get().append(self.update)
43                         self.conditionCheckTimer.start(500)
44                 
45         def setConnect(self, conditionalFunction):
46                 self.conditionalFunction = conditionalFunction
47                 
48         def activateCondition(self, condition):
49                 if (condition):
50                         if (self.state == self.HIDDEN):
51                                 self.showWidget()
52                 else:
53                         if (self.state == self.SHOWN):
54                                 self.hideWidget()
55
56         def update(self):
57                 if (self.conditionalFunction != None):
58                         try:
59                                 self.conditionalFunction() # check, if the conditionalfunction is still valid
60                         except:
61                                 self.conditionalFunction = None
62                                 self.activateCondition(False)
63                         
64                         self.activateCondition(self.conditionalFunction())
65
66                         
67                         
68 import time
69
70 class BlinkingWidget(Widget):
71         def __init__(self):
72                 Widget.__init__(self)
73                 
74                 self.blinking = True
75                 
76                 self.setBlinkTime(500)
77
78                 self.timer = eTimer()
79                 self.timer.timeout.get().append(self.blink)
80         
81         def setBlinkTime(self, time):
82                 self.blinktime = time
83                 
84         def blink(self):
85                 if self.blinking == True:
86                         if (self.state == self.SHOWN):
87                                 self.hideWidget()
88                         elif (self.state == self.HIDDEN):
89                                 self.showWidget()
90                         
91         def startBlinking(self):
92                 self.blinking = True
93                 self.timer.start(self.blinktime)
94                 
95         def stopBlinking(self):
96                 self.blinking = False
97                 if (self.state == self.SHOWN):
98                         self.hideWidget()
99                 self.timer.stop()
100                 
101 class BlinkingWidgetConditional(BlinkingWidget, ConditionalWidget):
102         def __init__(self):
103                 BlinkingWidget.__init__(self)
104                 ConditionalWidget.__init__(self)
105                 
106         def activateCondition(self, condition):
107                 if (condition):
108                         if not self.blinking: # we are already blinking
109                                 self.startBlinking()
110                 else:
111                         if self.blinking: # we are blinking
112                                 self.stopBlinking()