added manpage
[qbat.git] / batteryicon.cpp
blob45f0a266840cf4a74871e181abdccd286ea5d02d
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(28, 28),
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);
77 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, 4, 27, 23);
85 painter.setPen(Qt::NoPen);
86 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_CHARGED]));
88 int chargedPixels = (int)(21 * (float)chargeNow / (float)chargeFull);
90 painter.drawRect(1, 1 + 26 - chargedPixels, 26, chargedPixels);
92 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN]));
93 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE]));
95 else {
96 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN_FULL]));
97 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_FULL]));
98 painter.drawRect(0, 4, 27, 23);
100 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE_FULL]));
102 painter.drawRect(8, 0, 11, 4);
104 painter.setBrush(Qt::NoBrush);
106 ((QFont&)painter.font()).setPixelSize(15);
107 painter.drawText(1, 10, 26, 15, Qt::AlignHCenter, QString::number((int)(100.0 * chargeNow / chargeFull)));
109 setIcon(m_Icon);