cadMousePro-gui/src/MouseMainWindow.cpp
branchv_0
changeset 21 642f32f76c9d
parent 20 cfb5f62ea048
child 26 fff8e9a86e85
--- a/cadMousePro-gui/src/MouseMainWindow.cpp	Tue Sep 03 23:55:00 2019 +0200
+++ b/cadMousePro-gui/src/MouseMainWindow.cpp	Wed Sep 04 19:41:21 2019 +0200
@@ -17,11 +17,13 @@
  */
 #include <QLabel>
 #include <QPushButton>
+#include <qt5/QtDBus/qdbuspendingcall.h>
 
 #include "MouseMainWindow.h"
 
 MouseMainWindow::MouseMainWindow() {
 	proxy = new InfoGlobalcodeMouseCadMouseProInterface("info.globalcode.mouse.cadMousePro", "/info/globalcode/mouse/cadMousePro", connection, this);
+	upowerProxy = new OrgFreedesktopUPowerInterface("org.freedesktop.UPower", "/org/freedesktop/UPower", connection, this);
 
 	setWindowTitle("cadMousePro");
 
@@ -42,12 +44,19 @@
 	statusProxy->setEnabled(false);
 	statusUPower->setEnabled(false);
 	statusDevice->setEnabled(false);
+	statusBattery->setOrientation(Qt::Orientation::Horizontal);
+	statusBattery->setMinimum(0);
+	statusBattery->setMaximum(100);
+	
+	statusUPower->setToolTip("UPower interface is used for getting the battery level");
 	statusName->setToolTip("name of the USB device – usually the wireless adaptor");
+	statusBattery->setToolTip("battery charge level");
 
 	layout->setWidget(f++, QFormLayout::FieldRole, statusProxy);
 	layout->setWidget(f++, QFormLayout::FieldRole, statusUPower);
 	layout->setWidget(f++, QFormLayout::FieldRole, statusDevice);
 	layout->setWidget(f++, QFormLayout::FieldRole, statusName);
+	layout->setWidget(f++, QFormLayout::FieldRole, statusBattery);
 
 	QPushButton* refreshButton = new QPushButton("Refresh", panel);
 	layout->setWidget(f++, QFormLayout::FieldRole, refreshButton);
@@ -86,10 +95,10 @@
 void MouseMainWindow::appendAboutLine(QFormLayout* layout, const QString& label, const QString& value, QWidget* parent) {
 	QLabel* labelWidget = new QLabel(label, parent);
 	QLabel* textWidget = new QLabel(value, parent);
-	
+
 	textWidget->setTextInteractionFlags(Qt::TextInteractionFlag::TextSelectableByMouse | Qt::TextInteractionFlag::TextBrowserInteraction);
 	textWidget->setOpenExternalLinks(true);
-	
+
 	layout->addRow(labelWidget, textWidget);
 }
 
@@ -107,10 +116,29 @@
 	tabs->addTab(panel, "About");
 }
 
+/**
+ * Finds the first HID mouse managed by UPower and returns its battery level.
+ * 
+ * @return percentage of the current battery charge or 0 if no suitable mouse was found
+ */
+int MouseMainWindow::getBatteryLevel() {
+	auto devices = upowerProxy->EnumerateDevices();
+	devices.waitForFinished();
+	for (auto device : devices.value()) {
+		if (device.path().startsWith(upowerProxy->path() + "/devices/mouse_hid_") && device.path().endsWith("_battery")) {
+			OrgFreedesktopUPowerDeviceInterface battery(upowerProxy->service(), device.path(), connection, this);
+			if (battery.isValid()) return battery.percentage();
+		}
+	}
+	return 0; // mouse with battery not found
+}
+
 void MouseMainWindow::refresh() {
 	statusProxy->setChecked(proxy->isValid());
+	statusUPower->setChecked(upowerProxy->isValid());
 	statusDevice->setChecked(proxy->devicePresent());
 	statusName->setText(proxy->deviceName());
+	statusBattery->setValue(getBatteryLevel());
 }
 
 void MouseMainWindow::configure() {