Initial commit (Vesion 0.1)
[tablet-suite] / src / ui / pcsdeviceinfoviewer.py
1 # Authors: Amaury Medeiros and Paulo Ouriques
2 # Software License: GPL
3
4 from time import sleep
5 import threading
6
7 from PyQt4.QtCore import *
8 from PyQt4.QtGui import *
9
10 from ui.pcsuiutils import *
11 from pcsuiutils import *
12 from ui.pcscustombuttons import PcsCustomButton as customButton
13
14 class PcsDeviceInfoViewer(QHBoxLayout):
15     
16     ''' Class that displays how the device is connected with the PC'''
17     
18     def __init__(self, pcSuite):
19         super(PcsDeviceInfoViewer,self).__init__()
20         
21         self.label = QLabel("")
22         self.label.setText("<font style='color: gray'; size=2>Maemo Release</font>")
23         self.label.setFixedSize(90,20)
24         
25         self.pcSuite = pcSuite
26         self.l2 = QLabel()
27         pixmap = QPixmap(SSH_IMAGE)
28         self.l2.setPixmap(pixmap)  
29         
30         spacer = QSpacerItem(60, 60)
31         maemoLayout = QVBoxLayout()
32         maemoLayout.setMargin(0)
33         maemoLayout.addItem(spacer)
34         maemoLayout.addWidget(self.label)
35         maemoLayout.addWidget(self.l2)
36         maemoLayout.addItem(spacer)
37         
38         spacer2 = QSpacerItem(0, 32)
39         spacer3 = QSpacerItem(0, 37.6)
40         self.statusLayout = QGridLayout()
41         self.statusLayout.setColumnStretch(1, 1)
42         self.statusLayout.addItem(spacer2, 0, 0)
43         self._createStatusWidget(self.statusLayout)
44         self.statusLayout.addItem(spacer3, 4, 0)
45         
46         self.setMargin(0)
47         self.addLayout(maemoLayout)
48         spacer4 = QSpacerItem(10, 10)
49         self.addItem(spacer4)
50         self.addLayout(self.statusLayout)
51         
52         self.modelLabel = QLabel("Model", self.pcSuite)
53         self.modelLabel.setText("<font style='color: white'; size=2>Model</font>")
54         self.modelLabel.setGeometry(QRect(330, 70, WINDOW_WIDTH, WINDOW_HEIGHT))
55        
56         self.arrowLabel = QLabel(self.pcSuite)
57         self.arrowLabel.setPixmap(QPixmap(WHITE_ARROW))
58         self.arrowLabel.setGeometry(QRect(365, 70, WINDOW_WIDTH, WINDOW_HEIGHT))
59         
60         self.deviceNameLabel = QLabel("None", self.pcSuite)
61         self.deviceNameLabel.setText("<font style='color: white'; size=2>None</font>")
62         self.deviceNameLabel.setGeometry(QRect(380, 70, WINDOW_WIDTH, WINDOW_HEIGHT))
63         
64         
65         self.deviceNameLabel2 = QLabel(self.pcSuite)
66         self.deviceNameLabel2.setText("<font style='color: #333333'; size=2>None</font>")
67         self.deviceNameLabel2.setGeometry(QRect(10, 39, 50, 15))
68        
69         self.arrowLabel = QLabel(self.pcSuite)
70         self.arrowLabel.setPixmap(QPixmap(BLACK_ARROW))
71         self.arrowLabel.setGeometry(QRect(40, 39, 15, 15))
72         
73         self.actionLabel = QLabel(self.pcSuite)
74         self.actionLabel.setText("<font style='color: white'; size=2>Select option</font>")
75         self.actionLabel.setGeometry(QRect(55, 36, 100, 20))
76                  
77         self.chargingThread = None;
78            
79     def setDeviceInfo(self, deviceInfo):
80         self.deviceInfo = deviceInfo
81         self.label.setText("<font style='color: grey'; size=2>" + deviceInfo.system +"</font>" )
82         
83         
84         self.label.repaint()
85         self.deviceNameLabel.setText("<font style='color: white'; size=2>" 
86                                      + deviceInfo.name + "</font>")
87         self.deviceNameLabel2.setText("<font style='color: #333333'; size=2>" 
88                                      + deviceInfo.name + "</font>")
89         if(self.chargingThread != None):
90             if self.deviceInfo.charging:
91                 self._batteryBar.setToolTip("Charging...")
92             else:
93                 self.emit(SIGNAL("stopThread"))
94                 self._batteryBar.setToolTip("Battery: %d%%" % (deviceInfo.battery))
95                 self._batteryBar.setValue(deviceInfo.battery)
96         else:
97             if self.deviceInfo.charging:
98                 self._batteryBar.setToolTip("Charging...")
99                 self.chargingThread = ChargingThread(self)
100                 self.connect(self.chargingThread, SIGNAL("charging"), self._charging)
101                 self.chargingThread.start()
102             else:
103                 self._batteryBar.setValue(deviceInfo.battery)
104                 self._batteryBar.setToolTip("Battery: %d%%" % (deviceInfo.battery))
105        
106
107         memory = deviceInfo.storage
108         tip = ""
109         total_m1 = 0
110         total_m2 = 0
111         current_m1 = 0
112         current_m2 = 0
113         if memory[1] != -1:
114             value_m1 = float(memory[1][1]) * 100 / float(memory[1][0])
115             current_m1 = float(memory[1][1])/1024
116             total_m1 = float(memory[1][0])/1024
117             tip += "MMC1: %.1fMB / %.1fMB - %d%%\n" % (current_m1, total_m1, value_m1)
118         
119         if memory[2] != -1:
120             value_m2 = float(memory[2][1]) * 100 / float(memory[2][0])
121             current_m2 = float(memory[2][1])/1024
122             total_m2 = float(memory[2][0])/1024
123             tip += "MMC2: %.1fMB / %.1fMB - %d%%\n" % (current_m2, total_m2, value_m2)
124
125         if total_m1 == 0 and total_m2 == 0:
126             tip = "No external memory found."
127         else:
128             current = current_m1 + current_m2
129             total = total_m1 + total_m2
130             value = (current_m1 + current_m2) * 100 / total
131             tip += "Total: %.1fMB / %.1fMB - %d%%" % (current, total, value)
132             self._memoryBar.setValue(value)
133         self._memoryBar.setToolTip(tip)
134
135         current = float(memory[0][1])/1024 
136         total = float(memory[0][0])/1024
137         value = current * 100 / total
138         self._deviceBar.setValue(value)
139         self._deviceBar.setToolTip("Device Memory: %.1fMB / %.1fMB - %d%%" % (current, total, value))
140         
141     def _createStatusWidget(self, layout):
142         self._batteryBar = self._newProgressBar(layout, 1, BATTERY_IMAGE)
143         self._deviceBar = self._newProgressBar(layout, 2, DEVICE_MEMORY_IMAGE)
144         self._memoryBar = self._newProgressBar(layout, 3, MEMORY_IMAGE)
145                     
146     def _newProgressBar(self, layout, line, image):
147         bar = QProgressBar()
148         bar.setMaximum(100)
149         bar.setMinimum(0)
150         bar.setFixedSize(150,12.9)
151         bar.setTextVisible(False)
152         layout.addWidget(bar, line, 0 )
153
154         label = QLabel()
155         label.setFixedSize(30,15)
156         pixmap = QPixmap(image)
157         label.setPixmap(pixmap)
158         layout.addWidget(label, line, 1)
159         
160         return bar
161     
162     def _charging(self):
163         currentValue = self._batteryBar.value()
164         if(currentValue == 100):
165             self._batteryBar.setValue(0)
166         self._batteryBar.setValue(currentValue + 1)
167         self._batteryBar.repaint()
168         
169         
170 class ChargingThread(QThread):
171     def __init__(self, infoViewer):
172         QThread.__init__(self)
173         self.flag = True
174         self.connect(infoViewer, SIGNAL("stopThread"), self.stopThread)
175         self.connect(infoViewer, SIGNAL("destroyed()"), self.stopThread)
176         
177     def stopThread(self):
178         self.flag = False
179         
180     def run(self):
181         while(self.flag):
182             try:
183                 sleep(0.02)
184                 self.emit(SIGNAL("charging"))
185             except:
186                 self.stopThread()
187             
188         
189