added charging time information
[qbat.git] / batteryicon.cpp
blob80fd9dc07f9f0a3f8078d0e2a5a4037257affd52
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)(22 * m_RelativeCharge / 100.0);
44 painter.fillRect(1, 5 + 22 - 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(7, 0, 13, 4);
57 painter.setBrush(Qt::NoBrush);
59 if (m_RelativeCharge < 100)
60 ((QFont&)painter.font()).setPixelSize(15);
61 else
62 ((QFont&)painter.font()).setPixelSize(12);
64 painter.setRenderHint(QPainter::TextAntialiasing);
65 ((QFont&)painter.font()).setBold(true);
66 painter.drawText(1, 9, 26, 16, Qt::AlignHCenter, QString::number(m_RelativeCharge));
68 setIcon(m_Icon);
71 void CBatteryIcon::updateToolTip() {
72 QString newToolTip = tr("QBat - %1: %2%").arg(m_BatteryName) +'\n';
74 if (m_RelativeCharge == -1)
75 newToolTip = newToolTip.arg('-');
76 else
77 newToolTip = newToolTip.arg(m_RelativeCharge);
79 switch (m_Status) {
80 case UI_BATTERY_DISCHARGING:
81 newToolTip += tr("status: %1").arg(tr("dischaging"));
82 if (m_CurrentNow) {
83 newToolTip += '\n';
84 qreal remainingTime = (qreal)m_ChargeNow / m_CurrentNow;
85 int remainingHours = (int)remainingTime;
86 int remainungMinutes = (int)(remainingTime * 60) % 60;
87 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
89 break;
90 case UI_BATTERY_CHARGING:
91 newToolTip += tr("status: %1").arg(tr("charging"));
92 if (m_CurrentNow && m_ChargeFull) {
93 newToolTip += '\n';
94 qreal remainingTime = (qreal)(m_ChargeFull - m_ChargeNow) / m_CurrentNow;
95 int remainingHours = (int)remainingTime;
96 int remainungMinutes = (int)(remainingTime * 60) % 60;
97 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
99 break;
100 case UI_BATTERY_FULL:
101 newToolTip += tr("status: %1").arg(tr("full"));
102 break;
103 default:
104 newToolTip += tr("status: %1").arg(tr("unknown"));
105 break;
107 newToolTip += '\n';
109 if (m_Status != UI_BATTERY_FULL)
110 newToolTip += tr("current rate: %1A").arg(qRound(m_CurrentNow / 100000.0) / 10.0) + '\n';
112 newToolTip += tr("current capacity: %2mAh").arg(m_ChargeNow / 1000) + '\n';
114 if (m_ChargeFull)
115 newToolTip += tr("last full capacity: %3mAh").arg(m_ChargeFull / 1000) + '\n';
117 if (m_ChargeFullDesign)
118 newToolTip += tr("design capacity: %4mAh").arg(m_ChargeFullDesign / 1000);
120 setToolTip(newToolTip);
123 void CBatteryIcon::updateData(int chargeFull, int chargeFullDesign, int chargeNow, int currentNow, int status) {
124 bool noupdate = true;
125 if (chargeNow != m_ChargeNow) {
126 noupdate = false;
127 m_ChargeNow = chargeNow;
130 if (currentNow != m_CurrentNow) {
131 noupdate = false;
132 m_CurrentNow = currentNow;
135 if (status != m_Status) {
136 noupdate = false;
137 m_Status = status;
140 if (chargeFull != m_ChargeFullDesign) {
141 noupdate = false;
142 m_ChargeFull = chargeFull;
145 if (chargeFullDesign != m_ChargeFullDesign) {
146 noupdate = false;
147 m_ChargeFullDesign = chargeFullDesign;
150 if (noupdate)
151 return;
153 qint8 newRelativeCharge = 0;
155 if (m_ChargeFull)
156 newRelativeCharge = (qint8)(100.0 * m_ChargeNow / m_ChargeFull);
157 else
158 newRelativeCharge = -1;
160 if (newRelativeCharge != m_RelativeCharge) {
161 m_RelativeCharge = newRelativeCharge;
162 updateIcon();
164 updateToolTip();