fix progressbar, slider
[enigma2.git] / lib / python / Components / TunerInfo.py
1 from GUIComponent import GUIComponent
2
3 from enigma import eLabel, eSlider, iFrontendStatusInformation
4
5 from math import log
6
7 class TunerInfo(GUIComponent):
8         SNR = 0
9         AGC = 1
10         BER = 2
11         LOCK = 3
12         
13         SNR_PERCENTAGE = 0
14         AGC_PERCENTAGE = 1
15         BER_VALUE = 2
16         SNR_BAR = 3
17         AGC_BAR = 4
18         BER_BAR = 5
19         LOCK_STATE = 6
20         SYNC_STATE = 7
21         def __init__(self, type, servicefkt = None, frontendfkt = None):
22                 GUIComponent.__init__(self)
23                 self.instance = None
24                 self.message = None
25                 self.value = None
26                 
27                 self.servicefkt = servicefkt
28                 self.frontendfkt = frontendfkt
29                 self.type = type
30                 self.update()
31         
32         def setText(self, text):
33                 self.message = text
34                 if self.instance:
35                         self.instance.setText(self.message)
36         
37         def setValue(self, value):
38                 self.value = value
39                 if self.instance:
40                         self.instance.setValue(self.value)              
41                         
42         def calc(self,val):
43                 if not val:
44                         return 0
45                 if val < 2500:
46                         return (long)(log(val)/log(2))
47                 return val*100/65535
48         
49         def update(self):
50                 if self.type == self.SNR_PERCENTAGE or self.type == self.SNR_BAR:
51                         value = self.getValue(self.SNR) * 100 / 65536
52                 elif self.type == self.AGC_PERCENTAGE or self.type == self.AGC_BAR:
53                         value = self.getValue(self.AGC) * 100 / 65536
54                 elif self.type == self.BER_VALUE or self.type == self.BER_BAR:
55                         value = self.getValue(self.BER)
56                 elif self.type == self.LOCK_STATE:
57                         value = self.getValue(self.LOCK)
58                 
59                 if self.type == self.SNR_PERCENTAGE or self.type == self.AGC_PERCENTAGE:
60                         self.setText("%d%%" % (value))
61                 elif self.type == self.BER_VALUE:
62                         self.setText("%d" % (value))
63                 elif self.type == self.SNR_BAR or self.type == self.AGC_BAR:
64                         self.setValue(value)
65                 elif self.type == self.BER_BAR:
66                         self.setValue(self.calc(value))
67                 elif self.type == self.LOCK_STATE:
68                         if value == 1:
69                                 self.setText(_("locked"))
70                         else:
71                                 self.setText(_("not locked"))
72                                 
73         def getValue(self, what):
74                 if self.servicefkt is not None:
75                         service = self.servicefkt()
76                         if service is not None:
77                                 feinfo = service.frontendStatusInfo()
78                                 if feinfo is not None:
79                                         if what == self.SNR:
80                                                 return feinfo.getFrontendInfo(iFrontendStatusInformation.signalPower)
81                                         elif what == self.AGC:
82                                                 return feinfo.getFrontendInfo(iFrontendStatusInformation.signalQuality)
83                                         elif what == self.BER:
84                                                 return feinfo.getFrontendInfo(iFrontendStatusInformation.bitErrorRate)
85                                         elif what == self.LOCK:
86                                                 return feinfo.getFrontendInfo(iFrontendStatusInformation.LockState)
87                 elif self.frontendfkt is not None:
88                         frontend = self.frontendfkt()
89                         if what == self.SNR:
90                                 return frontend.readFrontendData(iFrontendStatusInformation.signalPower)
91                         elif what == self.AGC:
92                                 return frontend.readFrontendData(iFrontendStatusInformation.signalQuality)
93                         elif what == self.BER:
94                                 return frontend.readFrontendData(iFrontendStatusInformation.bitErrorRate)
95                         elif what == self.LOCK:
96                                 return frontend.readFrontendData(iFrontendStatusInformation.LockState)
97                 
98                 return 0
99                                 
100         def createWidget(self, parent):
101                 if self.SNR_PERCENTAGE <= self.type <= self.BER_VALUE or self.type == self.LOCK_STATE:
102                         return eLabel(parent)
103                 elif self.SNR_BAR <= self.type <= self.BER_BAR:
104                         self.g = eSlider(parent)
105                         self.g.setRange(0, 100)
106                         return self.g
107                 
108         def postWidgetCreate(self, instance):
109                 if self.message is not None:
110                         instance.setText(self.message)
111                 elif self.value is not None:
112                         instance.setValue(self.value)