-
Notifications
You must be signed in to change notification settings - Fork 0
/
qbattmain.cpp
161 lines (134 loc) · 4.91 KB
/
qbattmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <QSystemTrayIcon>
#include <QMessageBox>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QDebug>
#include "qbattmain.h"
#include "qbattstats.h"
QBattMain::QBattMain(QWidget *parent) :
QMainWindow(parent)
{
trayFont = QFont("Hack", 10);
trayPixmap = QPixmap(16, 16);
trayPixmap.fill(Qt::white);
trayIcon = new QBattTray_T (this);
stats = new QBattStats();
trayTimer = new QTimer(this);
trayTimer->setInterval(3000);
trayTimer->start();
QObject::connect(trayTimer, SIGNAL(timeout()),
this, SLOT(updateTrayLabel()));
QObject::connect(trayIcon->getSystemTray (), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(exitApplication(QSystemTrayIcon::ActivationReason)));
}
QBattMain::~QBattMain()
{
if (trayTimer->isActive())
trayTimer->stop();
// Release memory
delete stats;
delete trayTimer;
delete trayIcon;
}
void QBattMain::updateTrayLabel()
{
// Update PSU information first
int currentRate = -1;
float battery_health = 0.0;
stats->updatePowerSupplyInfo();
int trayCapacity = stats->getBatteryCapacity();
trayIcon->batteryLevelChanged(trayCapacity);
QString battStatus = stats->getBatteryStatus();
bool adapterStatus = stats->getACOnline();
trayIcon->batteryStatusChanged (adapterStatus);
currentRate = stats->getBatteryCurrentNow();
if (currentRate != -1) {
currentRate /= 1000;
} else {
if (stats->getBatteryVoltageNow() > 0)
currentRate = (stats->getBatteryPowerNow() / (stats->getBatteryVoltageNow() / 1000));
else
currentRate = -1;
}
trayToolTipText.clear();
trayToolTipText.append("Status: ");
if ((!QString().compare(battStatus, BATT_STATUS_UNKNOWN)) and
(adapterStatus)) {
trayToolTipText.append("On-line");
} else {
trayToolTipText.append(battStatus);
}
trayText.clear();
if (trayCapacity >= 100) {
/*
* Capacity value might be greater than 100% when
* the battery has accumulated greater energy amount
* at its last charge in compare to its previous charge.
* So just show that it's completely charged.
*/
trayText.append("F");
} else {
if ((!QString().compare(battStatus, BATT_STATUS_CHARGING)) or
(!QString().compare(battStatus, BATT_STATUS_DISCHARGING))) {
trayToolTipText.append(QString().sprintf("\nRate: %d mAh",
currentRate));
trayToolTipText.append("\nTime left: ");
trayToolTipText.append(stats->getTimeLeft());
}
trayText.sprintf("%d", trayCapacity);
}
if (trayCapacity != -1)
trayToolTipText.append(QString().sprintf("\nCapacity: %d%%", trayCapacity));
else
trayToolTipText.append(QString().sprintf("n/a"));
/*
* Possible ways of getting the information about battery
* health are (at least):
* - comparing full charge and designed full charge
* - comparing stored energy value to its design value
*/
if ((stats->getBatteryChargeFull() > 0) &&
(stats->getBatteryChargeFullDesign() > 0)) {
battery_health = (stats->getBatteryChargeFull() * 100.0) /
stats->getBatteryChargeFullDesign() * 1.0;
trayToolTipText.append(QString().sprintf("\nHealth: %.1f%%",
battery_health));
} else if ((stats->getBatteryEnergyFull() > 0) &&
(stats->getBatteryEnergyFullDesign() > 0)) {
battery_health = (stats->getBatteryEnergyFull() * 100.0) /
stats->getBatteryEnergyFullDesign() * 1.0;
trayToolTipText.append(QString().sprintf("\nHealth: %.1f%%",
battery_health));
}
trayPainter = new QPainter(&trayPixmap);
trayPainter->setFont(trayFont);
trayPainter->eraseRect(trayPixmap.rect());
trayPainter->drawText(trayPixmap.rect(), Qt::AlignCenter, trayText);
trayIcon->getSystemTray ()->setToolTip(trayToolTipText);
delete trayPainter;
}
void QBattMain::exitApplication(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::DoubleClick) {
QMessageBox *msg = new QMessageBox(QMessageBox::Information,
"Exit qbatt:", "Do you really want to exit?",
QMessageBox::Yes | QMessageBox::Cancel, NULL);
int ret = msg->exec();
delete msg;
switch (ret) {
case QMessageBox::Yes:
// Stop the timer
trayTimer->stop();
// Release memory
delete stats;
delete trayTimer;
delete trayIcon;
exit(0);
break;
case QMessageBox::Cancel:
break;
}
}
return;
}