summaryrefslogtreecommitdiff
authordrw <drw>2004-12-21 00:05:06 (UTC)
committer drw <drw>2004-12-21 00:05:06 (UTC)
commit8c316a01b28879b9f4fc6df736773245c8358ffc (patch) (unidiff)
tree3cfd20bfac60255a9ebe1460baeabe68427c8ac6
parentfaf7441b78dfc293f1dea1edebc202244fc3a1cf (diff)
downloadopie-8c316a01b28879b9f4fc6df736773245c8358ffc.zip
opie-8c316a01b28879b9f4fc6df736773245c8358ffc.tar.gz
opie-8c316a01b28879b9f4fc6df736773245c8358ffc.tar.bz2
Some cleaner string handling
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/applets/batteryapplet/batterystatus.cpp55
1 files changed, 36 insertions, 19 deletions
diff --git a/core/applets/batteryapplet/batterystatus.cpp b/core/applets/batteryapplet/batterystatus.cpp
index addb517..7f8de2c 100644
--- a/core/applets/batteryapplet/batterystatus.cpp
+++ b/core/applets/batteryapplet/batterystatus.cpp
@@ -1,262 +1,279 @@
1 1
2#include "batterystatus.h" 2#include "batterystatus.h"
3 3
4/* OPIE */ 4/* OPIE */
5#include <opie2/odevice.h> 5#include <opie2/odevice.h>
6#include <qpe/power.h> 6#include <qpe/power.h>
7 7
8/* QT */ 8/* QT */
9#include <qpushbutton.h> 9#include <qpushbutton.h>
10#include <qdrawutil.h> 10#include <qdrawutil.h>
11#include <qfile.h> 11#include <qfile.h>
12#include <qtextstream.h> 12#include <qtextstream.h>
13#include <qmessagebox.h> 13#include <qmessagebox.h>
14 14
15using namespace Opie::Core; 15using namespace Opie::Core;
16 16
17BatteryStatus::BatteryStatus( const PowerStatus *p, QWidget *parent, WFlags f ) 17BatteryStatus::BatteryStatus( const PowerStatus *p, QWidget *parent, WFlags f )
18: QFrame( parent, 0, f), ps(p), bat2(false) { 18: QFrame( parent, 0, f), ps(p), bat2(false) {
19 19
20 UpdateBatteryStatus(); 20 UpdateBatteryStatus();
21} 21}
22 22
23BatteryStatus::~BatteryStatus() {} 23BatteryStatus::~BatteryStatus() {}
24 24
25void BatteryStatus::UpdateBatteryStatus() { 25void BatteryStatus::UpdateBatteryStatus() {
26 26
27 jackPercent = 0; 27 jackPercent = 0;
28 28
29 if ( ODevice::inst ( )-> series ( ) == Model_iPAQ ) { 29 if ( ODevice::inst ( )-> series ( ) == Model_iPAQ ) {
30 getProcApmStatusIpaq(); 30 getProcApmStatusIpaq();
31 } 31 }
32 percent = ps->batteryPercentRemaining(); 32 percent = ps->batteryPercentRemaining();
33} 33}
34 34
35/* 35/*
36 * Make use of the advanced apm interface of the ipaq 36 * Make use of the advanced apm interface of the ipaq
37 */ 37 */
38bool BatteryStatus::getProcApmStatusIpaq() { 38bool BatteryStatus::getProcApmStatusIpaq() {
39 39
40 bat2 = false; 40 bat2 = false;
41 41
42 QFile procApmIpaq("/proc/hal/battery"); 42 QFile procApmIpaq("/proc/hal/battery");
43 43
44 if (procApmIpaq.open(IO_ReadOnly) ) { 44 if (procApmIpaq.open(IO_ReadOnly) ) {
45 QStringList list; 45 QStringList list;
46 // since it is /proc we _must_ use QTextStream 46 // since it is /proc we _must_ use QTextStream
47 QTextStream stream ( &procApmIpaq); 47 QTextStream stream ( &procApmIpaq);
48 QString streamIn; 48 QString streamIn;
49 streamIn = stream.read(); 49 streamIn = stream.read();
50 list = QStringList::split("\n", streamIn); 50 list = QStringList::split("\n", streamIn);
51 51
52 for(QStringList::Iterator line=list.begin(); line!=list.end(); line++) { 52 for(QStringList::Iterator line=list.begin(); line!=list.end(); line++) {
53 // not nice, need a rewrite later 53 // not nice, need a rewrite later
54 if( (*line).startsWith(" Percentage") ) { 54 if( (*line).startsWith(" Percentage") ) {
55 if (bat2 == true) { 55 if (bat2 == true) {
56 perc2 = (*line).mid(((*line).find('(')) +1,(*line).find(')')-(*line).find('(')-2); 56 perc2 = (*line).mid(((*line).find('(')) +1,(*line).find(')')-(*line).find('(')-2);
57 } else { 57 } else {
58 perc1 = (*line).mid(((*line).find('('))+1,(*line).find(')')-(*line).find('(')-2); 58 perc1 = (*line).mid(((*line).find('('))+1,(*line).find(')')-(*line).find('(')-2);
59 } 59 }
60 } else if( (*line).startsWith(" Life") ) { 60 } else if( (*line).startsWith(" Life") ) {
61 if (bat2 == true) { 61 if (bat2 == true) {
62 sec2 = (*line).mid(((*line).find(':')+2), 5 ); 62 sec2 = (*line).mid(((*line).find(':')+2), 5 );
63 } else { 63 } else {
64 sec1 = (*line).mid(((*line).find(':')+2), 5 ); 64 sec1 = (*line).mid(((*line).find(':')+2), 5 );
65 } 65 }
66 } else if( (*line).startsWith("Battery #1") ) { 66 } else if( (*line).startsWith("Battery #1") ) {
67 bat2 = true; 67 bat2 = true;
68 } else if( (*line).startsWith(" Status") ) { 68 } else if( (*line).startsWith(" Status") ) {
69 if (bat2 == true) { 69 if (bat2 == true) {
70 jackStatus = (*line).mid((*line).find('(')+1, (*line).find(')')-(*line).find('(')-1); 70 jackStatus = (*line).mid((*line).find('(')+1, (*line).find(')')-(*line).find('(')-1);
71 } else { 71 } else {
72 ipaqStatus = (*line).mid((*line).find('(')+1, (*line).find(')')-(*line).find('(')-1); 72 ipaqStatus = (*line).mid((*line).find('(')+1, (*line).find(')')-(*line).find('(')-1);
73 } 73 }
74 } else if( (*line).startsWith(" Chemistry") ) { 74 } else if( (*line).startsWith(" Chemistry") ) {
75 if (bat2 == true) { 75 if (bat2 == true) {
76 jackChem = (*line).mid((*line).find('('), (*line).find(')')-(*line).find('(')+1); 76 jackChem = (*line).mid((*line).find('('), (*line).find(')')-(*line).find('(')+1);
77 } else { 77 } else {
78 ipaqChem = (*line).mid((*line).find('('), (*line).find(')')-(*line).find('(')+1); 78 ipaqChem = (*line).mid((*line).find('('), (*line).find(')')-(*line).find('(')+1);
79 } 79 }
80 } 80 }
81 } 81 }
82 } else { 82 } else {
83 QMessageBox::warning(this, tr("Failure"),tr("could not open file")); 83 QMessageBox::warning(this, tr("Failure"),tr("could not open file"));
84 } 84 }
85 85
86 procApmIpaq.close(); 86 procApmIpaq.close();
87 jackPercent = perc2.toInt(); 87 jackPercent = perc2.toInt();
88 ipaqPercent = perc1.toInt(); 88 ipaqPercent = perc1.toInt();
89 89
90 if (perc2.isEmpty()) { 90 if (perc2.isEmpty()) {
91 perc2 = tr("no data"); 91 perc2 = tr("no data");
92 } else { 92 } else {
93 perc2 += " %"; 93 perc2 += " %";
94 } 94 }
95 95
96 if (sec2 == "0" || sec2 == "" || sec2.isEmpty()) { 96 if (sec2 == "0" || sec2 == "" || sec2.isEmpty()) {
97 sec2 = tr("no data"); 97 sec2 = tr("no data");
98 } else { 98 } else {
99 sec2 += " min"; 99 sec2 += " min";
100 } 100 }
101 101
102 jackStatus == (" ( " + jackStatus + " )"); 102 jackStatus.prepend( " ( " );
103 jackStatus.append( " )" );
103 return true; 104 return true;
104} 105}
105 106
106 107
107void BatteryStatus::updatePercent( int pc ) { 108void BatteryStatus::updatePercent( int pc ) {
108 percent = pc; 109 percent = pc;
109 repaint(FALSE); 110 repaint(FALSE);
110} 111}
111 112
112void BatteryStatus::drawSegment( QPainter *p, const QRect &r, const QColor &topgrad, const QColor &botgrad, const QColor &highlight, int hightlight_height ) { 113void BatteryStatus::drawSegment( QPainter *p, const QRect &r, const QColor &topgrad, const QColor &botgrad, const QColor &highlight, int hightlight_height ) {
113 int h1, h2, s1, s2, v1, v2, ng = r.height(), hy = ng*30/100, hh = hightlight_height; 114 int h1, h2, s1, s2, v1, v2, ng = r.height(), hy = ng*30/100, hh = hightlight_height;
114 topgrad.hsv( &h1, &s1, &v1 ); 115 topgrad.hsv( &h1, &s1, &v1 );
115 botgrad.hsv( &h2, &s2, &v2 ); 116 botgrad.hsv( &h2, &s2, &v2 );
116 for ( int j = 0; j < hy-2; j++ ) { 117 for ( int j = 0; j < hy-2; j++ ) {
117 p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1), s1 + ((s2-s1)*j)/(ng-1), 118 p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1), s1 + ((s2-s1)*j)/(ng-1),
118 v1 + ((v2-v1)*j)/(ng-1), QColor::Hsv ) ); 119 v1 + ((v2-v1)*j)/(ng-1), QColor::Hsv ) );
119 p->drawLine( r.x(), r.top()+hy-2-j, r.x()+r.width(), r.top()+hy-2-j ); 120 p->drawLine( r.x(), r.top()+hy-2-j, r.x()+r.width(), r.top()+hy-2-j );
120 } 121 }
121 for ( int j = 0; j < hh; j++ ) { 122 for ( int j = 0; j < hh; j++ ) {
122 p->setPen( highlight ); 123 p->setPen( highlight );
123 p->drawLine( r.x(), r.top()+hy-2+j, r.x()+r.width(), r.top()+hy-2+j ); 124 p->drawLine( r.x(), r.top()+hy-2+j, r.x()+r.width(), r.top()+hy-2+j );
124 } 125 }
125 for ( int j = 0; j < ng-hy-hh; j++ ) { 126 for ( int j = 0; j < ng-hy-hh; j++ ) {
126 p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1), s1 + ((s2-s1)*j)/(ng-1), 127 p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1), s1 + ((s2-s1)*j)/(ng-1),
127 v1 + ((v2-v1)*j)/(ng-1), QColor::Hsv ) ); 128 v1 + ((v2-v1)*j)/(ng-1), QColor::Hsv ) );
128 p->drawLine( r.x(), r.top()+hy+hh-2+j, r.x()+r.width(), r.top()+hy+hh-2+j ); 129 p->drawLine( r.x(), r.top()+hy+hh-2+j, r.x()+r.width(), r.top()+hy+hh-2+j );
129 } 130 }
130} 131}
131 132
132QString BatteryStatus::statusText() const { 133QString BatteryStatus::statusText() const {
133 QString text; 134 QString text;
134 135
135 if ( ps->batteryStatus() == PowerStatus::Charging ) { 136 if ( ps->batteryStatus() == PowerStatus::Charging ) {
136 if (bat2) { 137 if (bat2) {
137 text = tr("Charging both devices"); 138 text = tr("Charging both devices");
138 } else { 139 } else {
139 text = tr("Charging"); 140 text = tr("Charging");
140 } 141 }
141 } else if ( ps->batteryPercentAccurate() ) { 142 } else if ( ps->batteryPercentAccurate() ) {
142 text.sprintf( tr("Remaining Power") + ": %i%%", percent ); 143 text = tr( "Remaining Power: %1%" ).arg( percent );
144 //text.sprintf( tr("Remaining Power") + ": %i%%", percent );
143 } else { 145 } else {
144 text = tr("Battery status: "); 146 text = tr( "Battery status: " );
145 switch ( ps->batteryStatus() ) { 147 switch ( ps->batteryStatus() ) {
146 case PowerStatus::High: 148 case PowerStatus::High:
147 text += tr("Good"); 149 text.append( tr( "Good" ) );
148 break; 150 break;
149 case PowerStatus::Low: 151 case PowerStatus::Low:
150 text += tr("Low"); 152 text.append( tr( "Low" ) );
151 break; 153 break;
152 case PowerStatus::VeryLow: 154 case PowerStatus::VeryLow:
153 text += tr("Very Low"); 155 text.append( tr( "Very Low" ) );
154 break; 156 break;
155 case PowerStatus::Critical: 157 case PowerStatus::Critical:
156 text += tr("Critical"); 158 text.append( tr( "Critical" ) );
157 break; 159 break;
158 default: // NotPresent, etc. 160 default: // NotPresent, etc.
159 text += tr("Unknown"); 161 text.append( tr( "Unknown" ) );
160 } 162 }
161 } 163 }
162 164
163 if ( ps->acStatus() == PowerStatus::Backup ) 165 if ( ps->acStatus() == PowerStatus::Backup )
164 text += "\n" + tr("On backup power"); 166 {
167 text.append( '\n' );
168 text.append( tr( "On backup power" ) );
169 }
165 else if ( ps->acStatus() == PowerStatus::Online ) 170 else if ( ps->acStatus() == PowerStatus::Online )
166 text += "\n" + tr("Power on-line"); 171 {
172 text.append( '\n' );
173 text.append( tr( "Power on-line" ) );
174 }
167 else if ( ps->acStatus() == PowerStatus::Offline ) 175 else if ( ps->acStatus() == PowerStatus::Offline )
168 text += "\n" + tr("External power disconnected"); 176 {
177 text.append( '\n' );
178 text.append( tr( "External power disconnected" ) );
179 }
169 180
170 if ( ps->batteryTimeRemaining() >= 0 ) { 181 if ( ps->batteryTimeRemaining() >= 0 )
171 text += "\n" + QString().sprintf( tr("Remaining Time") + ": %im %02is", 182 {
172 ps->batteryTimeRemaining() / 60, ps->batteryTimeRemaining() % 60 ); 183 text.append( '\n' );
184 text.append( tr("Remaining Time: %1m %2s" ).arg( ps->batteryTimeRemaining() / 60 )
185 .arg( ps->batteryTimeRemaining() % 60, 2 ) );
186/* text += "\n" + QString().sprintf( tr("Remaining Time") + ": %im %02is",
187 ps->batteryTimeRemaining() / 60, ps->batteryTimeRemaining() % 60 );*/
173 } 188 }
174 return text; 189 return text;
175} 190}
176 191
177QString BatteryStatus::statusTextIpaq() const { 192QString BatteryStatus::statusTextIpaq() const {
178 QString text; 193 QString text = tr( "Remaing Power: %1 %2\nRemaining Time: %3" ).arg( perc2 )
179 text += tr("Remaining Power: ") + perc2 + " " + jackStatus; 194 .arg( jackStatus )
180 text += "\n" + tr("Remaining Time: ") + sec2; 195 .arg( sec2 );
196/* QString text = tr("Remaining Power: ") + perc2 + " " + jackStatus;
197 text += "\n" + tr("Remaining Time: ") + sec2;*/
181 return text; 198 return text;
182} 199}
183 200
184void BatteryStatus::paintEvent( QPaintEvent * ev ) { 201void BatteryStatus::paintEvent( QPaintEvent * ev ) {
185 202
186 QPainter p( this ); 203 QPainter p( this );
187 204
188 QString text = statusText(); 205 QString text = statusText();
189 p.drawText( 10, 50, width() - 20, 40 , AlignVCenter, text ); 206 p.drawText( 10, 50, width() - 20, 40 , AlignVCenter, text );
190 207
191 QColor c; 208 QColor c;
192 QColor darkc; 209 QColor darkc;
193 QColor lightc; 210 QColor lightc;
194 if ( ps->acStatus() == PowerStatus::Offline ) { 211 if ( ps->acStatus() == PowerStatus::Offline ) {
195 c = blue.light(120); 212 c = blue.light(120);
196 darkc = c.dark(280); 213 darkc = c.dark(280);
197 lightc = c.light(145); 214 lightc = c.light(145);
198 } else if ( ps->acStatus() == PowerStatus::Online ) { 215 } else if ( ps->acStatus() == PowerStatus::Online ) {
199 c = green.dark(130); 216 c = green.dark(130);
200 darkc = c.dark(200); 217 darkc = c.dark(200);
201 lightc = c.light(220); 218 lightc = c.light(220);
202 } else { 219 } else {
203 c = red; 220 c = red;
204 darkc = c.dark(280); 221 darkc = c.dark(280);
205 lightc = c.light(140); 222 lightc = c.light(140);
206 } 223 }
207 if ( percent < 0 ) 224 if ( percent < 0 )
208 return; 225 return;
209 226
210 int rightEnd1 = width() - 47; 227 int rightEnd1 = width() - 47;
211 int rightEnd2 = width() - 35; 228 int rightEnd2 = width() - 35;
212 int percent2 = (percent * rightEnd1) / 100; 229 int percent2 = (percent * rightEnd1) / 100;
213 p.setPen( black ); 230 p.setPen( black );
214 qDrawShadePanel( &p, 9, 10, rightEnd1 , 39, colorGroup(), TRUE, 1, NULL); 231 qDrawShadePanel( &p, 9, 10, rightEnd1 , 39, colorGroup(), TRUE, 1, NULL);
215 qDrawShadePanel( &p, rightEnd2, 17, 12, 24, colorGroup(), TRUE, 1, NULL); 232 qDrawShadePanel( &p, rightEnd2, 17, 12, 24, colorGroup(), TRUE, 1, NULL);
216 drawSegment( &p, QRect( 10, 10, percent2, 40 ), lightc, darkc, lightc.light(115), 6 ); 233 drawSegment( &p, QRect( 10, 10, percent2, 40 ), lightc, darkc, lightc.light(115), 6 );
217 drawSegment( &p, QRect( 11 + percent2, 10, rightEnd1 - percent2, 40 ), white.light(80), black, white.light(90), 6 ); 234 drawSegment( &p, QRect( 11 + percent2, 10, rightEnd1 - percent2, 40 ), white.light(80), black, white.light(90), 6 );
218 drawSegment( &p, QRect( rightEnd2, 17, 10, 25 ), white.light(80), black, white.light(90), 2 ); 235 drawSegment( &p, QRect( rightEnd2, 17, 10, 25 ), white.light(80), black, white.light(90), 2 );
219 p.setPen( black); 236 p.setPen( black);
220 237
221 if ( ODevice::inst ( )-> series ( ) == Model_iPAQ && bat2 ) { 238 if ( ODevice::inst ( )-> series ( ) == Model_iPAQ && bat2 ) {
222 239
223 p.drawText( 15, 30, tr ("Ipaq ") + ipaqChem ); 240 p.drawText( 15, 30, tr("Ipaq %1").arg( ipaqChem ) );
224 241
225 QString jacketMsg; 242 QString jacketMsg;
226 if (bat2) { 243 if (bat2) {
227 p.setPen(black); 244 p.setPen(black);
228 QString text = statusTextIpaq(); 245 QString text = statusTextIpaq();
229 p.drawText( 10, 150, text ); 246 p.drawText( 10, 150, text );
230 jacketMsg = tr("Jacket ") + jackChem; 247 jacketMsg = tr("Jacket ").arg( jackChem );
231 } else { 248 } else {
232 jackPercent = 0; 249 jackPercent = 0;
233 jacketMsg = tr("No jacket with battery inserted"); 250 jacketMsg = tr("No jacket with battery inserted");
234 } 251 }
235 252
236 int jackPerc = ( jackPercent * ( width() - 47 ) ) / 100; 253 int jackPerc = ( jackPercent * ( width() - 47 ) ) / 100;
237 254
238 qDrawShadePanel( &p, 9, 90, rightEnd1, 39, colorGroup(), TRUE, 1, NULL); 255 qDrawShadePanel( &p, 9, 90, rightEnd1, 39, colorGroup(), TRUE, 1, NULL);
239 qDrawShadePanel( &p, rightEnd2, 97, 12, 24, colorGroup(), TRUE, 1, NULL); 256 qDrawShadePanel( &p, rightEnd2, 97, 12, 24, colorGroup(), TRUE, 1, NULL);
240 drawSegment( &p, QRect( 10, 90, jackPerc, 40 ), lightc, darkc, lightc.light(115), 6 ); 257 drawSegment( &p, QRect( 10, 90, jackPerc, 40 ), lightc, darkc, lightc.light(115), 6 );
241 drawSegment( &p, QRect( 11 + jackPerc, 90, rightEnd1 - jackPerc, 40 ), white.light(80), black, white.light(90), 6 ); 258 drawSegment( &p, QRect( 11 + jackPerc, 90, rightEnd1 - jackPerc, 40 ), white.light(80), black, white.light(90), 6 );
242 drawSegment( &p, QRect( rightEnd2, 97, 10, 25 ), white.light(80), black, white.light(90), 2 ); 259 drawSegment( &p, QRect( rightEnd2, 97, 10, 25 ), white.light(80), black, white.light(90), 2 );
243 p.setPen( black ); 260 p.setPen( black );
244 p.drawText(15, 100, width() - 20, 20 , AlignVCenter, jacketMsg); 261 p.drawText(15, 100, width() - 20, 20 , AlignVCenter, jacketMsg);
245 } 262 }
246 QFrame::paintEvent(ev); 263 QFrame::paintEvent(ev);
247} 264}
248 265
249QSize BatteryStatus::sizeHint() const { 266QSize BatteryStatus::sizeHint() const {
250 QString text = statusText(); 267 QString text = statusText();
251 QString text2 = statusTextIpaq(); 268 QString text2 = statusTextIpaq();
252 QFontMetrics fm = fontMetrics(); 269 QFontMetrics fm = fontMetrics();
253 QRect r=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text ); 270 QRect r=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text );
254 QRect r2=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text2 ); 271 QRect r2=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text2 );
255 272
256 if ( bat2 ) { 273 if ( bat2 ) {
257 return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ), 274 return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ),
258 r.width()+2*10 ), 2 * 10 + 80 + r.height() + r2.height() ); 275 r.width()+2*10 ), 2 * 10 + 80 + r.height() + r2.height() );
259 } 276 }
260 return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ), 277 return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ),
261 r.width()+2*10 ), 2 * 10 + 40 + r.height() ); 278 r.width()+2*10 ), 2 * 10 + 40 + r.height() );
262} 279}