fixed little logical bug
[qbat.git] / batteryicon.cpp
blob0cf1f7670355e97a3f135d10bde842947743dba1
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),
22 m_energyUnits(false)
24 m_Icon.fill(Qt::transparent);
25 setIcon(m_Icon);
26 setContextMenu(contextMenu);
27 //updateData();
28 show();
31 CBatteryIcon::~CBatteryIcon() {
34 void CBatteryIcon::updateIcon() {
35 m_Icon.fill(Qt::transparent);
36 QPainter painter(&m_Icon);
38 if (m_ChargeNow != m_ChargeFull) {
39 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN]));
40 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_EMPTY]));
41 painter.drawRect(0, 4, 27, 23);
43 int chargedPixels = (int)(22 * m_RelativeCharge / 100.0);
45 painter.fillRect(1, 5 + 22 - chargedPixels, 26, chargedPixels, QColor(m_Settings->colors[UI_COLOR_BRUSH_CHARGED]));
47 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE]));
49 else {
50 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN_FULL]));
51 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_FULL]));
52 painter.drawRect(0, 4, 27, 23);
54 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE_FULL]));
56 painter.drawRect(7, 0, 13, 4);
58 painter.setBrush(Qt::NoBrush);
60 if (m_RelativeCharge < 100)
61 ((QFont&)painter.font()).setPixelSize(15);
62 else
63 ((QFont&)painter.font()).setPixelSize(12);
65 painter.setRenderHint(QPainter::TextAntialiasing);
66 ((QFont&)painter.font()).setBold(true);
67 painter.drawText(1, 9, 26, 16, Qt::AlignHCenter, QString::number(m_RelativeCharge));
69 setIcon(m_Icon);
72 void CBatteryIcon::updateToolTip() {
73 QString newToolTip = tr("QBat - %1: %2%").arg(m_BatteryName) +'\n';
75 if (m_RelativeCharge == -1)
76 newToolTip = newToolTip.arg('-');
77 else
78 newToolTip = newToolTip.arg(m_RelativeCharge);
80 switch (m_Status) {
81 case UI_BATTERY_DISCHARGING:
82 newToolTip += tr("status: %1").arg(tr("dischaging"));
83 if (m_CurrentNow) {
84 newToolTip += '\n';
85 qreal remainingTime = (qreal)m_ChargeNow / m_CurrentNow;
86 int remainingHours = (int)remainingTime;
87 int remainungMinutes = (int)(remainingTime * 60) % 60;
88 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
90 break;
91 case UI_BATTERY_CHARGING:
92 newToolTip += tr("status: %1").arg(tr("charging"));
93 if (m_CurrentNow && m_ChargeFull) {
94 newToolTip += '\n';
95 qreal remainingTime = (qreal)(m_ChargeFull - m_ChargeNow) / m_CurrentNow;
96 int remainingHours = (int)remainingTime;
97 int remainungMinutes = (int)(remainingTime * 60) % 60;
98 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
100 break;
101 case UI_BATTERY_FULL:
102 newToolTip += tr("status: %1").arg(tr("full"));
103 break;
104 default:
105 newToolTip += tr("status: %1").arg(tr("unknown"));
106 break;
108 newToolTip += '\n';
110 if (m_energyUnits) {
111 if (m_Status != UI_BATTERY_FULL)
112 newToolTip += tr("current rate: %1W").arg(qRound(m_CurrentNow / 100000.0) / 10.0) + '\n';
114 newToolTip += tr("current capacity: %2mWh").arg(m_ChargeNow / 1000);
116 if (m_ChargeFull)
117 newToolTip += '\n' + tr("last full capacity: %3mWh").arg(m_ChargeFull / 1000);
119 if (m_ChargeFullDesign)
120 newToolTip += '\n' + tr("design capacity: %4mWh").arg(m_ChargeFullDesign / 1000);
122 else
124 if (m_Status != UI_BATTERY_FULL)
125 newToolTip += tr("current rate: %1A").arg(qRound(m_CurrentNow / 100000.0) / 10.0) + '\n';
127 newToolTip += tr("current capacity: %2mAh").arg(m_ChargeNow / 1000);
129 if (m_ChargeFull)
130 newToolTip += '\n' + tr("last full capacity: %3mAh").arg(m_ChargeFull / 1000);
132 if (m_ChargeFullDesign)
133 newToolTip += '\n' + tr("design capacity: %4mAh").arg(m_ChargeFullDesign / 1000);
135 setToolTip(newToolTip);
138 void CBatteryIcon::updateData(int chargeFull, int chargeFullDesign, int chargeNow, int currentNow, int status, bool energyUnits) {
139 bool noupdate = true;
140 m_energyUnits = energyUnits;
142 if (chargeNow != m_ChargeNow) {
143 noupdate = false;
144 m_ChargeNow = chargeNow;
147 if (currentNow != m_CurrentNow) {
148 noupdate = false;
149 m_CurrentNow = currentNow;
152 if (status != m_Status) {
153 noupdate = false;
154 m_Status = status;
157 if (chargeFull != m_ChargeFullDesign) {
158 noupdate = false;
159 m_ChargeFull = chargeFull;
162 if (chargeFullDesign != m_ChargeFullDesign) {
163 noupdate = false;
164 m_ChargeFullDesign = chargeFullDesign;
167 if (noupdate)
168 return;
170 qint8 newRelativeCharge = 0;
172 if (m_ChargeFull)
173 newRelativeCharge = (qint8)(100.0 * m_ChargeNow / m_ChargeFull);
174 else
175 newRelativeCharge = -1;
177 if (newRelativeCharge != m_RelativeCharge) {
178 m_RelativeCharge = newRelativeCharge;
179 updateIcon();
181 updateToolTip();