cadMousePro-gui/src/MouseMainWindow.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 30 Aug 2019 01:22:12 +0200
branchv_0
changeset 16 2705911938b4
child 17 d37c6dd9aa20
permissions -rw-r--r--
GUI for mouse configuration over D-Bus daemon

/**
 * cadMousePro
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <QLabel>
#include <QPushButton>

#include "MouseMainWindow.h"

MouseMainWindow::MouseMainWindow() {
	proxy = new InfoGlobalcodeMouseCadMouseProInterface("info.globalcode.mouse.cadMousePro", "/info/globalcode/mouse/cadMousePro", connection, this);

	resize(640, 480);
	setWindowTitle("cadMousePro");
	centralwidget = new QWidget(this);
	formLayout = new QFormLayout(centralwidget);

	int f = 0;

	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("How does the mouse feel:", centralwidget));

	statusProxy->setEnabled(false);
	statusUPower->setEnabled(false);
	statusDevice->setEnabled(false);

	formLayout->setWidget(f++, QFormLayout::FieldRole, statusProxy);
	formLayout->setWidget(f++, QFormLayout::FieldRole, statusUPower);
	formLayout->setWidget(f++, QFormLayout::FieldRole, statusDevice);



	QPushButton* refreshButton = new QPushButton("Refresh", this);
	formLayout->setWidget(f++, QFormLayout::FieldRole, refreshButton);
	connect(refreshButton, &QPushButton::clicked, this, &MouseMainWindow::refresh);



	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Talk to your mouse:", centralwidget));

	configureLiftOffDetection->setChecked(true);
	formLayout->setWidget(f++, QFormLayout::FieldRole, configureLiftOffDetection);
	formLayout->setWidget(f++, QFormLayout::FieldRole, configureSmartScrolling);
	configureFrequency250->setChecked(true);
	formLayout->setWidget(f++, QFormLayout::FieldRole, configureFrequency125);
	formLayout->setWidget(f++, QFormLayout::FieldRole, configureFrequency250);
	formLayout->setWidget(f++, QFormLayout::FieldRole, configureFrequency500);
	formLayout->setWidget(f++, QFormLayout::FieldRole, configureFrequency1000);

	QPushButton* configureButton = new QPushButton("Configure", this);
	configureButton->setToolTip("n.b. current interface is write-only and the controls above just configures the mouse – does not show current configuration");
	formLayout->setWidget(f++, QFormLayout::FieldRole, configureButton);
	connect(configureButton, &QPushButton::clicked, this, &MouseMainWindow::configure);


	message->setReadOnly(true);
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Mouse responds:", this));
	formLayout->setWidget(f++, QFormLayout::FieldRole, message);

	setCentralWidget(centralwidget);
	refresh();
}

void MouseMainWindow::refresh() {
	statusProxy->setChecked(proxy->isValid());
	statusDevice->setChecked(proxy->devicePresent());

}

void MouseMainWindow::configure() {
	int frequency; // TODO: get value from the group
	if (configureFrequency125->isChecked()) frequency = 125;
	else if (configureFrequency250->isChecked()) frequency = 250;
	else if (configureFrequency500->isChecked()) frequency = 500;
	else if (configureFrequency1000->isChecked()) frequency = 1000;
	else frequency = 250;
	
	auto response = proxy->configure(configureLiftOffDetection->isChecked(), configureSmartScrolling->isChecked(), frequency);

	response.waitForFinished();
	if (response.isError()) message->setText("Error: " + response.error().message());
	else message->setText("mouse configured");

}