cadMousePro-gui/src/MouseMainWindow.cpp
branchv_0
changeset 21 642f32f76c9d
parent 20 cfb5f62ea048
child 26 fff8e9a86e85
equal deleted inserted replaced
20:cfb5f62ea048 21:642f32f76c9d
    15  * You should have received a copy of the GNU General Public License
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    17  */
    18 #include <QLabel>
    18 #include <QLabel>
    19 #include <QPushButton>
    19 #include <QPushButton>
       
    20 #include <qt5/QtDBus/qdbuspendingcall.h>
    20 
    21 
    21 #include "MouseMainWindow.h"
    22 #include "MouseMainWindow.h"
    22 
    23 
    23 MouseMainWindow::MouseMainWindow() {
    24 MouseMainWindow::MouseMainWindow() {
    24 	proxy = new InfoGlobalcodeMouseCadMouseProInterface("info.globalcode.mouse.cadMousePro", "/info/globalcode/mouse/cadMousePro", connection, this);
    25 	proxy = new InfoGlobalcodeMouseCadMouseProInterface("info.globalcode.mouse.cadMousePro", "/info/globalcode/mouse/cadMousePro", connection, this);
       
    26 	upowerProxy = new OrgFreedesktopUPowerInterface("org.freedesktop.UPower", "/org/freedesktop/UPower", connection, this);
    25 
    27 
    26 	setWindowTitle("cadMousePro");
    28 	setWindowTitle("cadMousePro");
    27 
    29 
    28 	initStatusPanel();
    30 	initStatusPanel();
    29 	initConfigurationPanel();
    31 	initConfigurationPanel();
    40 	int f = 0;
    42 	int f = 0;
    41 
    43 
    42 	statusProxy->setEnabled(false);
    44 	statusProxy->setEnabled(false);
    43 	statusUPower->setEnabled(false);
    45 	statusUPower->setEnabled(false);
    44 	statusDevice->setEnabled(false);
    46 	statusDevice->setEnabled(false);
       
    47 	statusBattery->setOrientation(Qt::Orientation::Horizontal);
       
    48 	statusBattery->setMinimum(0);
       
    49 	statusBattery->setMaximum(100);
       
    50 	
       
    51 	statusUPower->setToolTip("UPower interface is used for getting the battery level");
    45 	statusName->setToolTip("name of the USB device – usually the wireless adaptor");
    52 	statusName->setToolTip("name of the USB device – usually the wireless adaptor");
       
    53 	statusBattery->setToolTip("battery charge level");
    46 
    54 
    47 	layout->setWidget(f++, QFormLayout::FieldRole, statusProxy);
    55 	layout->setWidget(f++, QFormLayout::FieldRole, statusProxy);
    48 	layout->setWidget(f++, QFormLayout::FieldRole, statusUPower);
    56 	layout->setWidget(f++, QFormLayout::FieldRole, statusUPower);
    49 	layout->setWidget(f++, QFormLayout::FieldRole, statusDevice);
    57 	layout->setWidget(f++, QFormLayout::FieldRole, statusDevice);
    50 	layout->setWidget(f++, QFormLayout::FieldRole, statusName);
    58 	layout->setWidget(f++, QFormLayout::FieldRole, statusName);
       
    59 	layout->setWidget(f++, QFormLayout::FieldRole, statusBattery);
    51 
    60 
    52 	QPushButton* refreshButton = new QPushButton("Refresh", panel);
    61 	QPushButton* refreshButton = new QPushButton("Refresh", panel);
    53 	layout->setWidget(f++, QFormLayout::FieldRole, refreshButton);
    62 	layout->setWidget(f++, QFormLayout::FieldRole, refreshButton);
    54 	connect(refreshButton, &QPushButton::clicked, this, &MouseMainWindow::refresh);
    63 	connect(refreshButton, &QPushButton::clicked, this, &MouseMainWindow::refresh);
    55 
    64 
    84 }
    93 }
    85 
    94 
    86 void MouseMainWindow::appendAboutLine(QFormLayout* layout, const QString& label, const QString& value, QWidget* parent) {
    95 void MouseMainWindow::appendAboutLine(QFormLayout* layout, const QString& label, const QString& value, QWidget* parent) {
    87 	QLabel* labelWidget = new QLabel(label, parent);
    96 	QLabel* labelWidget = new QLabel(label, parent);
    88 	QLabel* textWidget = new QLabel(value, parent);
    97 	QLabel* textWidget = new QLabel(value, parent);
    89 	
    98 
    90 	textWidget->setTextInteractionFlags(Qt::TextInteractionFlag::TextSelectableByMouse | Qt::TextInteractionFlag::TextBrowserInteraction);
    99 	textWidget->setTextInteractionFlags(Qt::TextInteractionFlag::TextSelectableByMouse | Qt::TextInteractionFlag::TextBrowserInteraction);
    91 	textWidget->setOpenExternalLinks(true);
   100 	textWidget->setOpenExternalLinks(true);
    92 	
   101 
    93 	layout->addRow(labelWidget, textWidget);
   102 	layout->addRow(labelWidget, textWidget);
    94 }
   103 }
    95 
   104 
    96 void MouseMainWindow::initAboutPanel() {
   105 void MouseMainWindow::initAboutPanel() {
    97 	QWidget* panel = new QWidget(this);
   106 	QWidget* panel = new QWidget(this);
   105 	appendAboutLine(layout, "BTC:", "bc1qmt3qgzcf3a0f0tvnm70zjr0vhlchfyrjtnleqm", panel);
   114 	appendAboutLine(layout, "BTC:", "bc1qmt3qgzcf3a0f0tvnm70zjr0vhlchfyrjtnleqm", panel);
   106 
   115 
   107 	tabs->addTab(panel, "About");
   116 	tabs->addTab(panel, "About");
   108 }
   117 }
   109 
   118 
       
   119 /**
       
   120  * Finds the first HID mouse managed by UPower and returns its battery level.
       
   121  * 
       
   122  * @return percentage of the current battery charge or 0 if no suitable mouse was found
       
   123  */
       
   124 int MouseMainWindow::getBatteryLevel() {
       
   125 	auto devices = upowerProxy->EnumerateDevices();
       
   126 	devices.waitForFinished();
       
   127 	for (auto device : devices.value()) {
       
   128 		if (device.path().startsWith(upowerProxy->path() + "/devices/mouse_hid_") && device.path().endsWith("_battery")) {
       
   129 			OrgFreedesktopUPowerDeviceInterface battery(upowerProxy->service(), device.path(), connection, this);
       
   130 			if (battery.isValid()) return battery.percentage();
       
   131 		}
       
   132 	}
       
   133 	return 0; // mouse with battery not found
       
   134 }
       
   135 
   110 void MouseMainWindow::refresh() {
   136 void MouseMainWindow::refresh() {
   111 	statusProxy->setChecked(proxy->isValid());
   137 	statusProxy->setChecked(proxy->isValid());
       
   138 	statusUPower->setChecked(upowerProxy->isValid());
   112 	statusDevice->setChecked(proxy->devicePresent());
   139 	statusDevice->setChecked(proxy->devicePresent());
   113 	statusName->setText(proxy->deviceName());
   140 	statusName->setText(proxy->deviceName());
       
   141 	statusBattery->setValue(getBatteryLevel());
   114 }
   142 }
   115 
   143 
   116 void MouseMainWindow::configure() {
   144 void MouseMainWindow::configure() {
   117 	message->setText("…");
   145 	message->setText("…");
   118 
   146