v0.1.0 Release
[qbat.git] / batteryicon.cpp
blob5c81425ebb39b2c2a0c8a9d0ef063e74178bf261
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, Settings * settings, QMenu * contextMenu, QObject * parent) :
18 QSystemTrayIcon(parent),
19 m_BatteryName(batteryName),
20 m_Icon(32, 32),
21 m_Settings(settings)
23 m_Icon.fill(Qt::transparent);
24 setIcon(m_Icon);
25 setContextMenu(contextMenu);
26 //updateData();
27 show();
30 CBatteryIcon::~CBatteryIcon() {
33 void CBatteryIcon::updateData(int chargeFull, int chargeFullDesign, int chargeNow, int currentNow, int status) {
34 QString newToolTip = tr("QBat - %1: %2%").arg(m_BatteryName) +'\n';
36 if (chargeFull)
37 newToolTip = newToolTip.arg((int)(100.0 * chargeNow / chargeFull));
38 else
39 newToolTip = newToolTip.arg('-');
41 switch (status) {
42 case UI_BATTERY_DISCHARGING:
43 newToolTip += tr("status: %1").arg(tr("dischaging"));
44 if (currentNow) {
45 newToolTip += '\n';
46 qreal remainingTime = (qreal)chargeNow / (qreal)currentNow;
47 int remainingHours = (int)remainingTime;
48 int remainungMinutes = (int)(remainingTime * 60) % 60;
49 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
51 break;
52 case UI_BATTERY_CHARGING:
53 newToolTip += tr("status: %1").arg(tr("charging"));
54 break;
55 case UI_BATTERY_FULL:
56 newToolTip += tr("status: %1").arg(tr("full"));
57 break;
58 default:
59 newToolTip += tr("status: %1").arg(tr("unknown"));
60 break;
62 newToolTip += '\n';
64 if (status != UI_BATTERY_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 if (chargeFull)
70 newToolTip += tr("last full capacity: %3mAh").arg(chargeFull / 1000) + '\n';
72 if (chargeFullDesign)
73 newToolTip += tr("design capacity: %4mAh").arg(chargeFullDesign / 1000);
75 setToolTip(newToolTip);
76 m_Icon.fill(Qt::transparent);
78 QPainter painter(&m_Icon);
80 if (chargeNow != chargeFull) {
81 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN]));
82 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_EMPTY]));
83 painter.drawRect(0, 5, 31, 26);
85 painter.setPen(Qt::NoPen);
86 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_CHARGED]));
87 painter.drawRect(1, 6 + 25 - (int)(25.0 * chargeNow / chargeFull), 30, (int)(25.0 * chargeNow / chargeFull));
89 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN]));
90 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE]));
92 else {
93 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN_FULL]));
94 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_FULL]));
95 painter.drawRect(0, 5, 31, 26);
97 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE_FULL]));
99 painter.drawRect(9, 0, 13, 5);
101 painter.setBrush(Qt::NoBrush);
102 painter.font().setPixelSize(16);
103 painter.drawText(1, 12, 30, 16, Qt::AlignHCenter, QString::number((int)(100.0 * chargeNow / chargeFull)));
105 setIcon(m_Icon);