-
Notifications
You must be signed in to change notification settings - Fork 2
/
batterywidget.cpp
125 lines (100 loc) · 3.62 KB
/
batterywidget.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
#include "batterywidget.h"
#include "ui_batterywidget.h"
#include <QPainter>
#include <QRegularExpression>
BatteryWidget::BatteryWidget(int iMseconds, int iMode, QWidget *parent) : QWidget(parent), ui(new Ui::BatteryWidget)
{
ui->setupUi(this);
this->_mseconds = iMseconds;
this->_mode = iMode;
this->setFocusPolicy(Qt::FocusPolicy::NoFocus);
_timer = new QTimer(this);
const int ph = this->geometry().height();
const int px = this->geometry().x();
const int py = this->geometry().y();
const int dw = this->width();
const int dh = this->height();
this->setGeometry(px, py + ph - dh, dw, dh);
switch (this->_mode) {
case BatteryOnly:
this->resize(QSize(21, 9));
break;
case BatteryAndTimeHorizontal:
this->resize(QSize(44, 9));
break;
case BatteryAndTimeVertical:
this->resize(QSize(21, 18));
break;
}
setWindowFlags(Qt::Widget | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
setParent(nullptr); // Set to TopLevel-Widget
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
this->showNormal();
this->UpdateLabel();
QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(UpdateLabel()));
_timer->start(iMseconds);
}
QString BatteryWidget::GetBatteryValue()
{
QString mPercentage;
_process.start("upower",
QStringList() << "-i"
<< "/org/freedesktop/UPower/devices/battery_axp20x_battery");
_process.waitForFinished();
QRegularExpression re
= QRegularExpression("^\\s+percentage:\\s+([0-9]{1,3}%)$", QRegularExpression::MultilineOption);
QRegularExpressionMatch match = re.match(_process.readAllStandardOutput().trimmed());
if (Q_LIKELY(match.hasMatch()))
mPercentage = match.captured(1);
else
mPercentage = GetBatteryValueFromSys();
return mPercentage;
}
QString BatteryWidget::GetBatteryValueFromSys()
{
QFile inputFile(QString("/sys/class/power_supply/axp20x-battery/uevent"), this);
inputFile.open(QIODevice::ReadOnly);
if (!inputFile.isOpen()) return nullptr;
QTextStream mStream(&inputFile);
skipLine(mStream, 4);
const double voltage_now = mStream.readLine().split("=")[1].toInt();
skipLine(mStream, 4);
const double voltage_max = mStream.readLine().split("=")[1].toInt();
const double voltage_min = mStream.readLine().split("=")[1].toInt();
QString mPercentage = QString::number(((voltage_now - voltage_min) / (voltage_max - voltage_min)) * 100);
mPercentage.contains(".") ? mPercentage = mPercentage.split(".")[0].append("%")
: mPercentage = mPercentage.append("%");
inputFile.close();
return mPercentage;
}
QString BatteryWidget::GetTime()
{
_process.start("sh",
QStringList() << "-c"
<< "date '+%H:%M'");
_process.waitForFinished();
return _process.readAllStandardOutput().trimmed();
}
void BatteryWidget::UpdateLabel()
{
QString mText;
switch (_mode) {
case BatteryOnly:
mText = GetBatteryValue();
break;
case BatteryAndTimeHorizontal:
mText = GetBatteryValue().append(QString(" ").append(GetTime()));
break;
case BatteryAndTimeVertical:
mText = GetBatteryValue().append(QString("\n").append(GetTime()));
break;
}
ui->label->setText(mText);
ui->label->adjustSize();
this->raise();
}
BatteryWidget::~BatteryWidget()
{
delete ui;
}