00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "battery.h"
00021 #include "batterystatus.h"
00022
00023
00024 #include <opie2/otaskbarapplet.h>
00025
00026 #include <qpe/qpeapplication.h>
00027 #include <qpe/applnk.h>
00028 #include <qpe/config.h>
00029 #include <qpe/power.h>
00030
00031
00032 #include <qpainter.h>
00033 #include <qtimer.h>
00034
00035
00036 using namespace Opie::Ui;
00037 BatteryMeter::BatteryMeter( QWidget *parent )
00038 : QWidget( parent ), charging(false) {
00039 ps = new PowerStatus;
00040 startTimer( 10000 );
00041
00042 setFixedWidth( QMAX(AppLnk::smallIconSize()*3/4, 6) );
00043 setFixedHeight( AppLnk::smallIconSize() );
00044
00045 chargeTimer = new QTimer( this );
00046 connect( chargeTimer, SIGNAL(timeout()), this, SLOT(chargeTimeout()) );
00047 timerEvent(0);
00048 QPEApplication::setStylusOperation( this, QPEApplication::RightOnHold );
00049 Config c( "qpe" );
00050 c.setGroup( "Battery" );
00051 style = c.readNumEntry( "Style", 0 );
00052 }
00053
00054 BatteryMeter::~BatteryMeter() {
00055 delete ps;
00056 }
00057
00058 QSize BatteryMeter::sizeHint() const {
00059 return QSize(QMAX(AppLnk::smallIconSize()*3/4, 6), height() );
00060 }
00061
00062 void BatteryMeter::mousePressEvent( QMouseEvent* e ) {
00063 if ( e->button() == RightButton ) {
00064 style = 1-style;
00065 Config c( "qpe" );
00066 c.setGroup( "Battery" );
00067 c.writeEntry( "Style", style );
00068 repaint( true );
00069 }
00070 QWidget::mousePressEvent( e );
00071 }
00072
00073 void BatteryMeter::mouseReleaseEvent( QMouseEvent* ) {
00074 if ( batteryView && batteryView->isVisible() ) {
00075 batteryView->hide();
00076 } else {
00077 if ( !batteryView ) {
00078 batteryView = new BatteryStatus( ps, 0, WStyle_StaysOnTop | WType_Popup );
00079 batteryView->setFrameStyle( QFrame::PopupPanel | QFrame::Raised );
00080 }
00081
00082 batteryView->UpdateBatteryStatus();
00083 QRect r(batteryView->pos(),batteryView->sizeHint());
00084 QPoint curPos = this->mapToGlobal ( rect().topLeft() );
00085
00086 int lp = qApp->desktop()->width() - batteryView->sizeHint().width();
00087 r.moveTopLeft( QPoint(lp, curPos.y()-batteryView->sizeHint().height()-1) );
00088 batteryView->setGeometry(r);
00089
00090 batteryView->raise();
00091 batteryView->show();
00092 }
00093 }
00094
00095
00096 void BatteryMeter::timerEvent( QTimerEvent * ) {
00097 PowerStatus prev = *ps;
00098
00099 *ps = PowerStatusManager::readStatus();
00100
00101 if ( prev != *ps ) {
00102 percent = ps->batteryPercentRemaining();
00103 if ( !charging && ps->batteryStatus() == PowerStatus::Charging ) {
00104 percent = 0;
00105 charging = true;
00106 chargeTimer->start( 500 );
00107 } else if ( charging && ps->batteryStatus() != PowerStatus::Charging ) {
00108 charging = false;
00109 chargeTimer->stop();
00110 if ( batteryView )
00111 batteryView->updatePercent( percent );
00112 }
00113 repaint( style != 0 );
00114 if ( batteryView )
00115 batteryView->repaint();
00116 }
00117 }
00118
00119 void BatteryMeter::chargeTimeout() {
00120 percent += 20;
00121 if ( percent > 100 )
00122 percent = 0;
00123
00124 repaint(FALSE);
00125 if ( batteryView )
00126 batteryView->updatePercent( percent );
00127 }
00128
00129 void BatteryMeter::paintEvent( QPaintEvent* ) {
00130
00131 if ( style == 1 ) {
00132 QPainter p(this);
00133 QFont f( "Fixed", AppLnk::smallIconSize()/2 );
00134 QFontMetrics fm( f );
00135
00136 p.eraseRect( 0, 0, 20, 20 );
00137 p.setFont( f );
00138 p.drawText( 0, height()/2, QString::number( percent ) );
00139 p.drawText( width()/4, height(), "%" );
00140 return;
00141 }
00142
00143 QPainter p(this);
00144 QColor color;
00145 QColor g = gray.light( 160 );
00146 switch ( ps->acStatus() ) {
00147 case PowerStatus::Offline:
00148 color = blue.light( 150 );
00149 break;
00150 case PowerStatus::Online:
00151 color = green.dark( 130 ).light( 180 );
00152 break;
00153 default:
00154 color = red.light( 160 );
00155 }
00156
00157 int w = height() / 2;
00158 if ( !(w%2) )
00159 w--;
00160 int h = height() - 4;
00161 int pix = (percent * h) / 100;
00162 int y2 = height() -2;
00163 int y = y2 - pix;
00164 int x1 = (width() - w ) / 2;
00165
00166 p.setPen(QColor(80,80,80));
00167 p.drawLine(x1+w/4,0,x1+w/4+w/2+1,0);
00168 p.drawRect(x1,1,w,height()-1);
00169 p.setBrush(color);
00170
00171
00172
00173 int middle = w/2;
00174 for ( int i = 0; i < middle; i++ ) {
00175 p.setPen( gray.dark( 100+i*20 ) );
00176 p.drawLine( x1+middle-i, 2, x1+middle-i, y-1 );
00177 p.drawLine( x1+middle+i, 2, x1+middle+i, y-1 );
00178 p.setPen( color.dark( 100+i*20 ) );
00179 p.drawLine( x1+middle-i, y, x1+middle-i, y2 );
00180 p.drawLine( x1+middle+i, y, x1+middle+i, y2 );
00181 }
00182 }
00183
00184 int BatteryMeter::position() {
00185 return 8;
00186 }
00187
00188 EXPORT_OPIE_APPLET_v1( BatteryMeter )
00189