summaryrefslogtreecommitdiff
path: root/core/applets/batteryapplet/battery.cpp
blob: 480d2618d2261329ff2d6e5487a4e5d51488b2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "battery.h"
#include "batterystatus.h"

#include <qpe/applnk.h>
#include <qpe/config.h>
#include <qpe/power.h>
#include <qpe/qpeapplication.h>

#include <qpainter.h>
#include <qtimer.h>


BatteryMeter::BatteryMeter( QWidget *parent )
    : QWidget( parent ), charging(false)
{
    ps = new PowerStatus;
    startTimer( 10000 );
    setFixedHeight( AppLnk::smallIconSize() );
    chargeTimer = new QTimer( this );
    connect( chargeTimer, SIGNAL(timeout()), this, SLOT(chargeTimeout()) );
    timerEvent(0);
    QPEApplication::setStylusOperation( this, QPEApplication::RightOnHold );
    Config c( "qpe" );
    c.setGroup( "Battery" );
    style = c.readNumEntry( "Style", 0 );
}

BatteryMeter::~BatteryMeter()
{
    delete ps;
}

QSize BatteryMeter::sizeHint() const
{
    return QSize(10, height() );
}

void BatteryMeter::mousePressEvent( QMouseEvent* e )
{
    if ( e->button() == RightButton )
    {
        style = 1-style;
        Config c( "qpe" );
        c.setGroup( "Battery" );
        c.writeEntry( "Style", style );
        repaint( true );
    }
    QWidget::mousePressEvent( e );
}

void BatteryMeter::mouseReleaseEvent( QMouseEvent* e)
{
    if ( batteryView && batteryView->isVisible() ) {
        delete (QWidget *) batteryView;
    } else {
        if ( !batteryView ) batteryView = new BatteryStatus( ps );
        batteryView->showMaximized();
        batteryView->raise();
        batteryView->show();
    }
}

void BatteryMeter::timerEvent( QTimerEvent * )
{
    PowerStatus prev = *ps;

    *ps = PowerStatusManager::readStatus();

    if ( prev != *ps ) {
    percent = ps->batteryPercentRemaining();
    if ( !charging && ps->batteryStatus() == PowerStatus::Charging && percent < 0 ) {
        percent = 0;
        charging = true;
        chargeTimer->start( 500 );
    } else if ( charging && ps->batteryStatus() != PowerStatus::Charging ) {
        charging = false;
        chargeTimer->stop();
        if ( batteryView )
        batteryView->updatePercent( percent );
    }
    repaint( style != 0 );
    if ( batteryView )
        batteryView->repaint();
    }
}

void BatteryMeter::chargeTimeout()
{
    percent += 20;
    if ( percent > 100 )
	percent = 0;

    repaint(FALSE);
    if ( batteryView )
	batteryView->updatePercent( percent );
}

void BatteryMeter::paintEvent( QPaintEvent* )
{
    if ( style == 1 )
    {
        QPainter p(this);
        QFont f( "Fixed", AppLnk::smallIconSize()/2 );
        QFontMetrics fm( f );
        p.setFont( f );
        if ( percent > 98 ) {
            p.drawText( 0, 0, width(), height(), Qt::AlignCenter, tr( "F" ) );
        }
        else if ( percent < 5 )
        {
            p.drawText( 0, 0, width(), height(), Qt::AlignCenter, tr( "E" ) );
        }
        else
        {
            p.drawText( 0, AppLnk::smallIconSize()/2, QString::number( percent ) );
            p.drawText( AppLnk::smallIconSize()/4, AppLnk::smallIconSize(), "%" );
        }
        return;
    }

    QPainter p(this);
    QColor color;
    QColor g = gray.light( 160 );
    switch ( ps->acStatus() )
    {
        case PowerStatus::Offline: color = blue.light( 150 ); break;
        case PowerStatus::Online: color = green.dark( 130 ).light( 180 ); break;
        default: color = red.light( 160 );
    }

    int w = height() / 2;
    if ( !(w%2) ) w--; // should have an odd value to get a real middle line
    int h = height() - 4;
    int pix = (percent * h) / 100;
    int y2 = height() -2;
    int y = y2 - pix;
    int x1 = (width() - w ) / 2;

    p.setPen(QColor(80,80,80));
    p.drawLine(x1+w/4,0,x1+w/4+w/2+1,0);  // header
    p.drawRect(x1,1,w,height()-1);      // corpus
    p.setBrush(color);

    int extra = ((percent * h) % 100)/(100/4);

    int middle = w/2;
    for ( int i = 0; i < middle; i++ )
    {
        p.setPen( gray.dark( 100+i*20 ) );
        p.drawLine( x1+middle-i, 2, x1+middle-i, y-1 );
        p.drawLine( x1+middle+i, 2, x1+middle+i, y-1 );
        p.setPen( color.dark( 100+i*20 ) );
        p.drawLine( x1+middle-i, y, x1+middle-i, y2 );
        p.drawLine( x1+middle+i, y, x1+middle+i, y2 );
    }
}