show green button and subservices-text only when subservices are available (showing...
[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(1000)
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                                 print "update: " + str(self) + " SHOW"
52                                 self.showWidget()
53                 else:
54                         if (self.state == self.SHOWN):
55                                 print "update: " + str(self) + " HIDE"
56                                 self.hideWidget()
57
58         def update(self):
59                 if (self.conditionalFunction != None):
60                         try:
61                                 self.conditionalFunction() # check, if the conditionalfunction is still valid
62                         except:
63                                 self.conditionalFunction = None
64                                 self.activateCondition(False)
65                         
66                         self.activateCondition(self.conditionalFunction())