Skip to content

Commit

Permalink
Add battery percent and charging status indication for device
Browse files Browse the repository at this point in the history
Fixes #163
  • Loading branch information
z3ntu committed Apr 28, 2024
1 parent 7272060 commit 183aa16
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/devicewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QProgressBar>
#include <QPushButton>
#include <QSlider>
#include <QTextEdit>
Expand All @@ -40,6 +41,46 @@ DeviceWidget::DeviceWidget(const QString &name, const QDBusObjectPath &devicePat
header->setFont(titleFont);
verticalLayout->addWidget(header);

/* Battery */
if (device->hasFeature("battery")) {
auto *batteryHeaderHBox = new QHBoxLayout();

QLabel *batterHeader = new QLabel(tr("Battery"), this);
batterHeader->setFont(headerFont);

bool charging = false;
try {
charging = device->isCharging();
} catch (const libopenrazer::DBusException &e) {
qWarning("Failed to get charging status");
}

QLabel *chargingLabel = new QLabel(this);
if (charging) {
chargingLabel->setText(tr("Charging"));
} else {
chargingLabel->setText(tr("Not Charging"));
}

batteryHeaderHBox->addWidget(batterHeader);
batteryHeaderHBox->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
batteryHeaderHBox->addWidget(chargingLabel);

verticalLayout->addLayout(batteryHeaderHBox);

double percent = 0.0;
try {
percent = device->getBatteryPercent();
} catch (const libopenrazer::DBusException &e) {
qWarning("Failed to get battery charge percentage");
}

auto *progressBar = new QProgressBar;
progressBar->setValue(percent);

verticalLayout->addWidget(progressBar);
}

// Lighting header
if (leds.size() != 0) {
QLabel *lightingHeader = new QLabel(tr("Lighting"), this);
Expand Down

0 comments on commit 183aa16

Please sign in to comment.