fixed relative battery charge and added caching of battery states
[qbat.git] / batteryicon.cpp
blobdb60a722191004c9b712ef52b6e722ae56a1706f
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::updateIcon() {
34 m_Icon.fill(Qt::transparent);
35 QPainter painter(&m_Icon);
37 if (m_ChargeNow != m_ChargeFull) {
38 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN]));
39 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_EMPTY]));
40 painter.drawRect(0, 4, 27, 23);
42 int chargedPixels = (int)(21 * m_RelativeCharge / 100.0);
44 painter.fillRect(1, 1 + 26 - chargedPixels, 26, chargedPixels, QColor(m_Settings->colors[UI_COLOR_BRUSH_CHARGED]));
46 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE]));
48 else {
49 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN_FULL]));
50 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_FULL]));
51 painter.drawRect(0, 4, 27, 23);
53 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE_FULL]));
55 painter.drawRect(8, 0, 11, 4);
57 painter.setBrush(Qt::NoBrush);
59 ((QFont&)painter.font()).setPixelSize(14);
60 ((QFont&)painter.font()).setBold(true);
61 painter.drawText(1, 10, 26, 15, Qt::AlignHCenter, QString::number(m_RelativeCharge));
63 setIcon(m_Icon);
66 void CBatteryIcon::updateToolTip() {
67 QString newToolTip = tr("QBat - %1: %2%").arg(m_BatteryName) +'\n';
69 if (m_RelativeCharge == -1)
70 newToolTip = newToolTip.arg('-');
71 else
72 newToolTip = newToolTip.arg(m_RelativeCharge);
74 switch (m_Status) {
75 case UI_BATTERY_DISCHARGING:
76 newToolTip += tr("status: %1").arg(tr("dischaging"));
77 if (m_CurrentNow) {
78 newToolTip += '\n';
79 qreal remainingTime = (qreal)m_ChargeNow / m_CurrentNow;
80 int remainingHours = (int)remainingTime;
81 int remainungMinutes = (int)(remainingTime * 60) % 60;
82 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
84 break;
85 case UI_BATTERY_CHARGING:
86 newToolTip += tr("status: %1").arg(tr("charging"));
87 break;
88 case UI_BATTERY_FULL:
89 newToolTip += tr("status: %1").arg(tr("full"));
90 break;
91 default:
92 newToolTip += tr("status: %1").arg(tr("unknown"));
93 break;
95 newToolTip += '\n';
97 if (m_Status != UI_BATTERY_FULL)
98 newToolTip += tr("current rate: %1A").arg(qRound(m_CurrentNow / 100000.0) / 10.0) + '\n';
100 newToolTip += tr("current capacity: %2mAh").arg(m_ChargeNow / 1000) + '\n';
102 if (m_ChargeFull)
103 newToolTip += tr("last full capacity: %3mAh").arg(m_ChargeFull / 1000) + '\n';
105 if (m_ChargeFullDesign)
106 newToolTip += tr("design capacity: %4mAh").arg(m_ChargeFullDesign / 1000);
108 setToolTip(newToolTip);
111 void CBatteryIcon::updateData(int chargeFull, int chargeFullDesign, int chargeNow, int currentNow, int status) {
112 bool update = false;
113 if (chargeNow == m_ChargeNow)
114 update = true;
115 else
116 m_ChargeNow = chargeNow;
118 if (currentNow == m_CurrentNow)
119 update = true;
120 else
121 m_CurrentNow = currentNow;
123 if (status == m_Status)
124 update = true;
125 else
126 m_Status = status;
128 if (chargeFull == m_ChargeFullDesign)
129 update = true;
130 else
131 m_ChargeFullDesign = chargeFull;
133 if (chargeFullDesign == m_ChargeFullDesign)
134 update = true;
135 else
136 m_ChargeFullDesign = chargeFullDesign;
138 if (!update)
139 return;
141 qint8 newRelativeCharge = 0;
143 if (m_ChargeFull)
144 newRelativeCharge = (qint8)(100.0 * m_ChargeNow / m_ChargeFull);
145 else
146 newRelativeCharge = -1;
148 if (newRelativeCharge != m_RelativeCharge) {
149 m_RelativeCharge = newRelativeCharge;
150 updateIcon();
153 updateToolTip();