cleanup
[qbat.git] / batteryicon.cpp
blob667f072347f60c6e3a7531483ec2bbd65e470e89
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 <QPainter>
12 namespace qbat {
13 QDir CBatteryIcon::sysfsDir(UI_PATH_SYSFS_DIR);
15 CBatteryIcon::CBatteryIcon(Settings * settings, QString batteryName, QObject * parent) :
16 QSystemTrayIcon(parent),
17 m_Icon(28, 28),
18 m_Settings(settings)
20 m_Data.name = batteryName;
23 CBatteryIcon::~CBatteryIcon() {
26 void CBatteryIcon::updateIcon() {
27 m_Icon.fill(Qt::transparent);
28 QPainter painter(&m_Icon);
30 if (m_Data.currentCapacity != m_Data.fullCapacity) {
31 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN]));
32 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_EMPTY]));
33 painter.drawRect(0, 4, 27, 23);
35 int chargedPixels = (int)(22 * m_Data.relativeCharge / 100.0);
37 painter.fillRect(1, 5 + 22 - chargedPixels, 26, chargedPixels, QColor(m_Settings->colors[UI_COLOR_BRUSH_CHARGED]));
39 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE]));
41 else {
42 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN_FULL]));
43 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_FULL]));
44 painter.drawRect(0, 4, 27, 23);
46 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE_FULL]));
48 painter.drawRect(7, 0, 13, 4);
50 painter.setBrush(Qt::NoBrush);
52 if (m_Data.relativeCharge < 100)
53 ((QFont&)painter.font()).setPixelSize(15);
54 else
55 ((QFont&)painter.font()).setPixelSize(12);
57 painter.setRenderHint(QPainter::TextAntialiasing);
58 ((QFont&)painter.font()).setBold(true);
59 if (m_Data.relativeCharge == -1)
60 painter.drawText(1, 9, 26, 16, Qt::AlignHCenter, QString('?'));
61 else
62 painter.drawText(1, 9, 26, 16, Qt::AlignHCenter, QString::number(m_Data.relativeCharge));
64 setIcon(m_Icon);
67 void CBatteryIcon::updateToolTip() {
68 QString newToolTip = tr("QBat - %1: %2%").arg(m_Data.name) +'\n';
70 if (m_Data.relativeCharge == -1)
71 newToolTip = newToolTip.arg('-');
72 else
73 newToolTip = newToolTip.arg(m_Data.relativeCharge);
75 switch (m_Data.status) {
76 case UI_BATTERY_DISCHARGING:
77 newToolTip += tr("status: %1").arg(tr("dischaging"));
78 if (m_Data.rate) {
79 newToolTip += '\n';
80 qreal remainingTime = (qreal)(m_Data.currentCapacity) / m_Data.rate;
81 int remainingHours = (int)remainingTime;
82 int remainungMinutes = (int)(remainingTime * 60) % 60;
83 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
85 break;
86 case UI_BATTERY_CHARGING:
87 newToolTip += tr("status: %1").arg(tr("charging"));
88 if (m_Data.rate && m_Data.fullCapacity) {
89 newToolTip += '\n';
90 qreal remainingTime = (qreal)(m_Data.fullCapacity - m_Data.currentCapacity) / m_Data.rate;
91 int remainingHours = (int)remainingTime;
92 int remainungMinutes = (int)(remainingTime * 60) % 60;
93 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
95 break;
96 case UI_BATTERY_FULL:
97 newToolTip += tr("status: %1").arg(tr("full"));
98 break;
99 default:
100 newToolTip += tr("status: %1").arg(tr("unknown"));
101 break;
103 newToolTip += '\n';
105 if (m_Data.energyUnits) {
106 if ((m_Data.rate) && (m_Data.status != UI_BATTERY_FULL)) {
107 double rateW = qRound(m_Data.rate / 100000.0) / 10.0;
108 double rateA = qRound((m_Data.rate / m_Data.voltage) / 1000.0) / 10.0;
109 newToolTip += tr("current rate: %1W / %2A").arg(rateW).arg(rateA) + '\n';
112 newToolTip += tr("current capacity: %1mWh").arg(m_Data.currentCapacity / 1000);
114 if (m_Data.fullCapacity)
115 newToolTip += '\n' + tr("last full capacity: %1mWh").arg(m_Data.fullCapacity / 1000);
117 if (m_Data.designCapacity)
118 newToolTip += '\n' + tr("design capacity: %1mWh").arg(m_Data.designCapacity / 1000);
120 else
122 if ((m_Data.rate) && (m_Data.status != UI_BATTERY_FULL)) {
123 double rateA = m_Data.rate / 100000.0;
124 double rateW = qRound(rateA * m_Data.voltage / 100.0) / 10.0;
125 newToolTip += tr("current rate: %1W / %2A").arg(rateW).arg(qRound(rateA) / 10.0) + '\n';
128 newToolTip += tr("current capacity: %1mAh").arg(m_Data.currentCapacity / 1000);
130 if (m_Data.fullCapacity)
131 newToolTip += '\n' + tr("last full capacity: %1mAh").arg(m_Data.fullCapacity / 1000);
133 if (m_Data.designCapacity)
134 newToolTip += '\n' + tr("design capacity: %1mAh").arg(m_Data.designCapacity / 1000);
136 setToolTip(newToolTip);
139 void CBatteryIcon::updateData(int currentCapacity, int fullCapacity, int designCapacity, int rate, int voltage, int status, bool energyUnits) {
140 m_Data.energyUnits = energyUnits;
142 bool noupdate = true;
144 if (rate != m_Data.rate) {
145 noupdate = false;
146 m_Data.rate = rate;
149 if (voltage != m_Data.voltage) {
150 noupdate = false;
151 m_Data.voltage = voltage;
154 if (status != m_Data.status) {
155 noupdate = false;
156 m_Data.status = status;
159 if (fullCapacity != m_Data.fullCapacity) {
160 noupdate = false;
161 m_Data.fullCapacity = fullCapacity;
164 if (designCapacity != m_Data.designCapacity) {
165 noupdate = false;
166 m_Data.designCapacity = designCapacity;
169 if (currentCapacity != m_Data.currentCapacity) {
170 noupdate = false;
171 m_Data.currentCapacity = currentCapacity;
174 if (noupdate)
175 return;
177 qint8 newRelativeCharge = calcRelativeDef(currentCapacity, fullCapacity);
179 if (newRelativeCharge != m_Data.relativeCharge) {
180 m_Data.relativeCharge = newRelativeCharge;
182 if (isVisible())
183 updateIcon();
186 if (isVisible())
187 updateToolTip();
190 void CBatteryIcon::updateData() {
191 int currentCapacity = 0;
192 int fullCapacity = 0;
193 int designCapacity = 0;
194 int rate = 0;
195 int voltage = 0;
196 int status = 0;
197 bool energyUnits = sysfsDir.exists(m_Data.name + UI_CAPTION_NOW(UI_CAPTION_ENERGY));
199 rate = readIntSysFile(sysfsDir.filePath(m_Data.name + "/current_now").toAscii().constData());
200 voltage = readIntSysFile(sysfsDir.filePath(m_Data.name + UI_CAPTION_NOW(UI_CAPTION_VOLTAGE)).toAscii().constData()) / 10000;
202 char buffer[BUF_SIZE];
203 readStringFromFile(buffer, sysfsDir.filePath(m_Data.name + "/status").toAscii().constData());
204 status = toStatusInt(buffer);
206 if (energyUnits) {
207 fullCapacity = readIntSysFile(sysfsDir.filePath(m_Data.name + UI_CAPTION_FULL(UI_CAPTION_ENERGY)).toAscii().constData());
208 designCapacity = readIntSysFile(sysfsDir.filePath(m_Data.name + UI_CAPTION_DESIGN(UI_CAPTION_ENERGY)).toAscii().constData());
209 currentCapacity = readIntSysFile(sysfsDir.filePath(m_Data.name + UI_CAPTION_NOW(UI_CAPTION_ENERGY)).toAscii().constData());
211 else {
212 fullCapacity = readIntSysFile(sysfsDir.filePath(m_Data.name + UI_CAPTION_FULL(UI_CAPTION_CHARGE)).toAscii().constData());
213 designCapacity = readIntSysFile(sysfsDir.filePath(m_Data.name + UI_CAPTION_DESIGN(UI_CAPTION_CHARGE)).toAscii().constData());
214 currentCapacity = readIntSysFile(sysfsDir.filePath(m_Data.name + UI_CAPTION_NOW(UI_CAPTION_CHARGE)).toAscii().constData());
217 updateData(currentCapacity, fullCapacity, designCapacity, rate, voltage, status, energyUnits);