added lightning bolt
This commit is contained in:
@@ -54,11 +54,16 @@ Window {
|
|||||||
// ─────────────────────────────────────────────────────────────
|
// ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
property int batteryPercent: systemInfo.batteryPercent
|
property int batteryPercent: systemInfo.batteryPercent
|
||||||
|
property bool charging: systemInfo.charging
|
||||||
|
|
||||||
Item {
|
Item {
|
||||||
id: battery
|
id: battery
|
||||||
|
|
||||||
width: batteryIcon.width + 18 + batteryText.width
|
width: batteryIcon.width
|
||||||
|
+ (root.charging ? chargingIcon.width + 12 : 0)
|
||||||
|
+ 18
|
||||||
|
+ batteryText.width
|
||||||
|
|
||||||
height: 72
|
height: 72
|
||||||
|
|
||||||
anchors {
|
anchors {
|
||||||
@@ -134,14 +139,38 @@ Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
id: batteryText
|
id: chargingIcon
|
||||||
|
|
||||||
anchors {
|
anchors {
|
||||||
left: batteryIcon.right
|
left: batteryIcon.right
|
||||||
leftMargin: 18
|
leftMargin: 12
|
||||||
verticalCenter: parent.verticalCenter
|
verticalCenter: parent.verticalCenter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
visible: root.charging
|
||||||
|
text: "⚡"
|
||||||
|
|
||||||
|
color: "#ffffff"
|
||||||
|
|
||||||
|
font {
|
||||||
|
pixelSize: 38
|
||||||
|
weight: Font.Bold
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Text {
|
||||||
|
id: batteryText
|
||||||
|
|
||||||
|
anchors {
|
||||||
|
left: root.charging
|
||||||
|
? chargingIcon.right
|
||||||
|
: batteryIcon.right
|
||||||
|
|
||||||
|
leftMargin: root.charging ? 12 : 18
|
||||||
|
|
||||||
|
verticalCenter: parent.verticalCenter
|
||||||
|
}
|
||||||
|
|
||||||
text: root.batteryPercent + "%"
|
text: root.batteryPercent + "%"
|
||||||
|
|
||||||
color: "#ffffff"
|
color: "#ffffff"
|
||||||
|
|||||||
+49
-16
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
SystemInfo::SystemInfo(QObject *parent)
|
SystemInfo::SystemInfo(QObject *parent)
|
||||||
@@ -16,7 +17,8 @@ SystemInfo::SystemInfo(QObject *parent)
|
|||||||
this,
|
this,
|
||||||
&SystemInfo::updateBattery);
|
&SystemInfo::updateBattery);
|
||||||
|
|
||||||
// Battery percentage doesn't need millisecond precision.
|
// Battery percentage and charging state don't need
|
||||||
|
// millisecond precision.
|
||||||
m_batteryTimer.start(10000);
|
m_batteryTimer.start(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +27,11 @@ int SystemInfo::batteryPercent() const
|
|||||||
return m_batteryPercent;
|
return m_batteryPercent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SystemInfo::charging() const
|
||||||
|
{
|
||||||
|
return m_charging;
|
||||||
|
}
|
||||||
|
|
||||||
QString SystemInfo::findBatteryCapacityPath() const
|
QString SystemInfo::findBatteryCapacityPath() const
|
||||||
{
|
{
|
||||||
QDir powerSupply("/sys/class/power_supply");
|
QDir powerSupply("/sys/class/power_supply");
|
||||||
@@ -64,26 +71,52 @@ void SystemInfo::updateBattery()
|
|||||||
if (m_capacityPath.isEmpty())
|
if (m_capacityPath.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────
|
||||||
|
// Battery percentage
|
||||||
|
// ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
QFile capacityFile(m_capacityPath);
|
QFile capacityFile(m_capacityPath);
|
||||||
|
|
||||||
if (!capacityFile.open(QIODevice::ReadOnly | QIODevice::Text))
|
if (capacityFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
return;
|
bool ok = false;
|
||||||
|
|
||||||
bool ok = false;
|
int percent =
|
||||||
|
QString::fromUtf8(capacityFile.readAll())
|
||||||
|
.trimmed()
|
||||||
|
.toInt(&ok);
|
||||||
|
|
||||||
int percent =
|
if (ok) {
|
||||||
QString::fromUtf8(capacityFile.readAll())
|
percent = qBound(0, percent, 100);
|
||||||
.trimmed()
|
|
||||||
.toInt(&ok);
|
|
||||||
|
|
||||||
if (!ok)
|
if (percent != m_batteryPercent) {
|
||||||
return;
|
m_batteryPercent = percent;
|
||||||
|
emit batteryPercentChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
percent = qBound(0, percent, 100);
|
// ─────────────────────────────────────────────────────────────
|
||||||
|
// Charging state
|
||||||
|
// ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
if (percent == m_batteryPercent)
|
// m_capacityPath is something like:
|
||||||
return;
|
// /sys/class/power_supply/battery/capacity
|
||||||
|
//
|
||||||
|
// The status file lives beside it.
|
||||||
|
const QString batteryDirectory =
|
||||||
|
QFileInfo(m_capacityPath).absolutePath();
|
||||||
|
|
||||||
m_batteryPercent = percent;
|
QFile statusFile(batteryDirectory + "/status");
|
||||||
emit batteryPercentChanged();
|
|
||||||
}
|
if (statusFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
const QString status =
|
||||||
|
QString::fromUtf8(statusFile.readAll()).trimmed();
|
||||||
|
|
||||||
|
const bool charging = (status == "Charging");
|
||||||
|
|
||||||
|
if (charging != m_charging) {
|
||||||
|
m_charging = charging;
|
||||||
|
emit chargingChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-2
@@ -8,17 +8,25 @@ class SystemInfo : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(int batteryPercent
|
Q_PROPERTY(int batteryPercent
|
||||||
READ batteryPercent
|
READ batteryPercent
|
||||||
NOTIFY batteryPercentChanged)
|
NOTIFY batteryPercentChanged)
|
||||||
|
|
||||||
|
Q_PROPERTY(bool charging
|
||||||
|
READ charging
|
||||||
|
NOTIFY chargingChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit SystemInfo(QObject *parent = nullptr);
|
explicit SystemInfo(QObject *parent = nullptr);
|
||||||
|
|
||||||
int batteryPercent() const;
|
int batteryPercent() const;
|
||||||
|
|
||||||
|
bool charging() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void batteryPercentChanged();
|
void batteryPercentChanged();
|
||||||
|
|
||||||
|
void chargingChanged();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void updateBattery();
|
void updateBattery();
|
||||||
|
|
||||||
@@ -28,4 +36,6 @@ private:
|
|||||||
int m_batteryPercent = -1;
|
int m_batteryPercent = -1;
|
||||||
QString m_capacityPath;
|
QString m_capacityPath;
|
||||||
QTimer m_batteryTimer;
|
QTimer m_batteryTimer;
|
||||||
|
|
||||||
|
bool m_charging = false;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user