src/SimulatorWindow.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 14 Mar 2019 16:46:16 +0100
branchv_0
changeset 3 42d64bd73232
parent 2 a27850958a67
child 4 a874deb6a536
permissions -rw-r--r--
connect slots, print events to stdout

/**
 * Spacenav Simulator Qt
 * 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 <QtWidgets/QFormLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QGridLayout>
#include <qt5/QtCore/qobject.h>

#include "SimulatorWindow.h"

Q_DECLARE_METATYPE(MotionEvent);
Q_DECLARE_METATYPE(ButtonEvent);

SimulatorWindow::SimulatorWindow() {
	resize(640, 480);
	setWindowTitle("Spacenav Simulator");

	QWidget* centralwidget = new QWidget(this);
	QFormLayout* formLayout = new QFormLayout(centralwidget);

	int f = 0;

	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Buttons:", centralwidget));
	std::vector<QString> buttonLabels = {"0", "1"};

	for (int i = 0; i < buttonLabels.size(); i++) {
		QCheckBox* button = new QCheckBox(centralwidget);
		buttons.push_back(button);
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(buttonLabels[i], centralwidget));
		formLayout->setWidget(f++, QFormLayout::FieldRole, button);
		connect(button, &QCheckBox::stateChanged, [ i, this ](int state) {
			emit buttonEvent({i, state == Qt::CheckState::Checked});
		});
	}

	std::vector<QString> axisLabels = {"X", "Y", "Z"};

	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Motion:", centralwidget));
	for (QString a : axisLabels) {
		QSlider* slider = createSlider(centralwidget);
		motions.push_back(slider);
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget));
		formLayout->setWidget(f++, QFormLayout::FieldRole, slider);
		connect(slider, &QSlider::valueChanged, this, &SimulatorWindow::sendMotionEvent);
	}


	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Rotation:", centralwidget));
	for (QString a : axisLabels) {
		QSlider* slider = createSlider(centralwidget);
		rotations.push_back(slider);
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget));
		formLayout->setWidget(f++, QFormLayout::FieldRole, slider);
		connect(slider, &QSlider::valueChanged, this, &SimulatorWindow::sendMotionEvent);
	}

	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Actions:", centralwidget));
	centerButton = new QPushButton("Center all", centralwidget);
	connect(centerButton, &QPushButton::clicked, this, &SimulatorWindow::centerAll);
	formLayout->setWidget(f++, QFormLayout::FieldRole, centerButton);


	setCentralWidget(centralwidget);
}

SimulatorWindow::SimulatorWindow(const SimulatorWindow& orig) {
}

SimulatorWindow::~SimulatorWindow() {
}

QSlider* SimulatorWindow::createSlider(QWidget* parent) {
	QSlider* slider = new QSlider(Qt::Orientation::Horizontal, parent);
	slider->setRange(-500, 500);
	slider->setValue(0);
	slider->setTickPosition(QSlider::TicksBelow);
	slider->setTickInterval(100);
	slider->setSingleStep(1);
	return slider;
}

void SimulatorWindow::centerAll() {
	for (QSlider* s : motions) s->setValue(0);
	for (QSlider* s : rotations) s->setValue(0);
}

void SimulatorWindow::sendMotionEvent() {
	MotionEvent event;

	event.x = motions[0]->value();
	event.y = motions[1]->value();
	event.z = motions[2]->value();

	event.rx = rotations[0]->value();
	event.ry = rotations[1]->value();
	event.rz = rotations[2]->value();

	event.period = 0; // TODO: period?

	emit motionEvent(event);
}