remaining time bugfix
[qbat.git] / batteryicon.cpp
blob3f446cd206f8bc2fa6a4b93c45b365df1e915e74
1 //
2 // C++ Implementation: batteryicon
3 //
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
5 //
6 // Copyright: See COPYING file that comes with this distribution
7 //
8 #include "batteryicon.h"
9 #include "common.h"
10 #include <string>
11 #include <QDir>
12 #include <QPainter>
14 namespace qbat {
15 using namespace std;
17 CBatteryIcon::CBatteryIcon(QString batteryName, QMenu * contextMenu, QObject * parent) :
18 QSystemTrayIcon(parent),
19 m_BatteryName(batteryName),
20 m_Icon(32, 32)
22 m_Icon.fill(Qt::transparent);
23 setContextMenu(contextMenu);
24 updateData();
25 show();
28 CBatteryIcon::~CBatteryIcon() {
31 void CBatteryIcon::updateData() {
32 QDir workDir(UI_PATH_SYSFS_DIR + ('/' + m_BatteryName));
33 int chargeFull = readIntSysFile(workDir.filePath("charge_full").toAscii().constData());
34 int chargeFullDesign = readIntSysFile(workDir.filePath("charge_full_design").toAscii().constData());
35 int chargeNow = readIntSysFile(workDir.filePath("charge_now").toAscii().constData());
36 int currentNow = readIntSysFile(workDir.filePath("current_now").toAscii().constData());
37 string status = readStringSysFile(workDir.filePath("status").toAscii().constData());
39 QString newToolTip = tr("QBat - %1: %2%").arg(m_BatteryName) +'\n';
40 if (chargeFull)
41 newToolTip = newToolTip.arg((int)(100.0 * chargeNow / chargeFull));
42 else
43 newToolTip = newToolTip.arg('-');
45 if (status == "Discharging") {
46 newToolTip += tr("status: %1").arg(tr("dischaging"));
47 if (currentNow) {
48 newToolTip += '\n';
49 qreal remainingTime = (qreal)chargeNow / (qreal)currentNow;
50 int remainingHours = (int)remainingTime;
51 int remainungMinutes = (int)(remainingTime * 60) % 60;
52 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
55 else if (status == "Charging")
56 newToolTip += tr("status: %1").arg(tr("charging"));
57 else if (status == "Full")
58 newToolTip += tr("status: %1").arg(tr("full"));
59 else
60 newToolTip += tr("status: %1").arg(tr("unknown"));
62 newToolTip += '\n';
64 if (status != "Full")
65 newToolTip += tr("current rate: %1A").arg(qRound(currentNow / 100000.0) / 10.0) + '\n';
67 newToolTip += tr("current capacity: %2mAh").arg(chargeNow / 1000) + '\n';
69 newToolTip += tr("last full capacity: %3mAh").arg(chargeFull / 1000) + '\n';
70 newToolTip += tr("design capacity: %4mAh").arg(chargeFullDesign / 1000);
72 setToolTip(newToolTip);
73 m_Icon.fill(Qt::transparent);
75 QPainter painter(&m_Icon);
77 if (chargeNow != chargeFull) {
78 painter.setPen(Qt::black);
79 painter.setBrush(Qt::white);
80 painter.drawRect(0, 5, 31, 26);
82 painter.setPen(Qt::NoPen);
83 painter.setBrush(Qt::green);
84 painter.drawRect(1, 6 + 25 - (int)(25.0 * chargeNow / chargeFull), 30, (int)(25.0 * chargeNow / chargeFull));
86 painter.setPen(Qt::black);
87 painter.setBrush(Qt::yellow);
89 else {
90 painter.setPen(Qt::black);
91 painter.setBrush(Qt::green);
92 painter.drawRect(0, 5, 31, 26);
94 painter.setBrush(Qt::blue);
96 painter.drawRect(9, 0, 13, 5);
98 painter.setBrush(Qt::NoBrush);
99 painter.font().setPixelSize(16);
100 painter.drawText(1, 12, 30, 16, Qt::AlignHCenter, QString::number((int)(100.0 * chargeNow / chargeFull)));
102 setIcon(m_Icon);