Diffstat (limited to 'core/applets/batteryapplet/battery.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | core/applets/batteryapplet/battery.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/core/applets/batteryapplet/battery.cpp b/core/applets/batteryapplet/battery.cpp new file mode 100644 index 0000000..3d254fc --- a/dev/null +++ b/core/applets/batteryapplet/battery.cpp | |||
@@ -0,0 +1,152 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "battery.h" | ||
21 | #include "batterystatus.h" | ||
22 | |||
23 | #include <qpe/power.h> | ||
24 | |||
25 | #include <qpainter.h> | ||
26 | #include <qtimer.h> | ||
27 | |||
28 | |||
29 | BatteryMeter::BatteryMeter( QWidget *parent = 0 ) | ||
30 | : QWidget( parent ), charging(false) | ||
31 | { | ||
32 | ps = new PowerStatus; | ||
33 | startTimer( 10000 ); | ||
34 | setFixedHeight(12); | ||
35 | chargeTimer = new QTimer( this ); | ||
36 | connect( chargeTimer, SIGNAL(timeout()), this, SLOT(chargeTimeout()) ); | ||
37 | timerEvent(0); | ||
38 | } | ||
39 | |||
40 | BatteryMeter::~BatteryMeter() | ||
41 | { | ||
42 | delete ps; | ||
43 | } | ||
44 | |||
45 | QSize BatteryMeter::sizeHint() const | ||
46 | { | ||
47 | return QSize(10,12); | ||
48 | } | ||
49 | |||
50 | void BatteryMeter::mouseReleaseEvent( QMouseEvent *) | ||
51 | { | ||
52 | if ( batteryView && batteryView->isVisible() ) { | ||
53 | delete (QWidget *) batteryView; | ||
54 | } else { | ||
55 | if ( !batteryView ) | ||
56 | batteryView = new BatteryStatus( ps ); | ||
57 | batteryView->showMaximized(); | ||
58 | batteryView->raise(); | ||
59 | batteryView->show(); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | void BatteryMeter::timerEvent( QTimerEvent * ) | ||
64 | { | ||
65 | PowerStatus prev = *ps; | ||
66 | |||
67 | *ps = PowerStatusManager::readStatus(); | ||
68 | |||
69 | if ( prev != *ps ) { | ||
70 | percent = ps->batteryPercentRemaining(); | ||
71 | if ( !charging && ps->batteryStatus() == PowerStatus::Charging && percent < 0 ) { | ||
72 | percent = 0; | ||
73 | charging = true; | ||
74 | chargeTimer->start( 500 ); | ||
75 | } else if ( charging && ps->batteryStatus() != PowerStatus::Charging ) { | ||
76 | charging = false; | ||
77 | chargeTimer->stop(); | ||
78 | if ( batteryView ) | ||
79 | batteryView->updatePercent( percent ); | ||
80 | } | ||
81 | repaint(FALSE); | ||
82 | if ( batteryView ) | ||
83 | batteryView->repaint(); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | void BatteryMeter::chargeTimeout() | ||
88 | { | ||
89 | percent += 20; | ||
90 | if ( percent > 100 ) | ||
91 | percent = 0; | ||
92 | |||
93 | repaint(FALSE); | ||
94 | if ( batteryView ) | ||
95 | batteryView->updatePercent( percent ); | ||
96 | } | ||
97 | |||
98 | void BatteryMeter::paintEvent( QPaintEvent* ) | ||
99 | { | ||
100 | QPainter p(this); | ||
101 | |||
102 | QColor c; | ||
103 | QColor darkc; | ||
104 | QColor lightc; | ||
105 | if ( ps->acStatus() == PowerStatus::Offline ) { | ||
106 | c = blue.light(120); | ||
107 | darkc = c.dark(120); | ||
108 | lightc = c.light(140); | ||
109 | } else if ( ps->acStatus() == PowerStatus::Online ) { | ||
110 | c = green.dark(130); | ||
111 | darkc = c.dark(120); | ||
112 | lightc = c.light(180); | ||
113 | } else { | ||
114 | c = red; | ||
115 | darkc = c.dark(120); | ||
116 | lightc = c.light(160); | ||
117 | } | ||
118 | |||
119 | int w = 6; | ||
120 | int h = height()-3; | ||
121 | int pix = (percent * h) / 100; | ||
122 | int y2 = height() - 2; | ||
123 | int y = y2 - pix; | ||
124 | int x1 = (width() - w) / 2; | ||
125 | |||
126 | p.setPen(QColor(80,80,80)); | ||
127 | p.drawLine(x1+w/4,0,x1+w/4+w/2,0); | ||
128 | p.drawRect(x1,1,w,height()-1); | ||
129 | p.setBrush(c); | ||
130 | |||
131 | int extra = ((percent * h) % 100)/(100/4); | ||
132 | |||
133 | #define Y(i) ((i<=extra)?y-1:y) | ||
134 | #define DRAWUPPER(i) if ( Y(i) >= 2 ) p.drawLine(i+x1,2,i+x1,Y(i)); | ||
135 | p.setPen( gray ); | ||
136 | DRAWUPPER(1); | ||
137 | DRAWUPPER(3); | ||
138 | p.setPen( gray.light(130) ); | ||
139 | DRAWUPPER(2); | ||
140 | p.setPen( gray.dark(120) ); | ||
141 | DRAWUPPER(4); | ||
142 | |||
143 | #define DRAW(i) { if ( Y(i) < y2 ) p.drawLine(i+x1,Y(i)+1,i+x1,y2); } | ||
144 | p.setPen( c ); | ||
145 | DRAW(1); | ||
146 | DRAW(3); | ||
147 | p.setPen( lightc ); | ||
148 | DRAW(2); | ||
149 | p.setPen(darkc); | ||
150 | DRAW(4); | ||
151 | } | ||
152 | |||