src/SimulatorWindow.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 14 Mar 2019 21:37:43 +0100
branchv_0
changeset 6 f81d43b74209
parent 4 a874deb6a536
child 10 da93f3667a52
permissions -rw-r--r--
Option: Periodic updates (each 20 ms)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Spacenav Simulator Qt
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
4
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    19
#include <QFormLayout>
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    20
#include <QLabel>
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    21
#include <QGridLayout>
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    22
#include <QObject>
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include "SimulatorWindow.h"
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    26
Q_DECLARE_METATYPE(MotionEvent);
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    27
Q_DECLARE_METATYPE(ButtonEvent);
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    28
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
SimulatorWindow::SimulatorWindow() {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	resize(640, 480);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	setWindowTitle("Spacenav Simulator");
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	QWidget* centralwidget = new QWidget(this);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	QFormLayout* formLayout = new QFormLayout(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	int f = 0;
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Buttons:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	std::vector<QString> buttonLabels = {"0", "1"};
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    41
	for (int i = 0; i < buttonLabels.size(); i++) {
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		QCheckBox* button = new QCheckBox(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		buttons.push_back(button);
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    44
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(buttonLabels[i], centralwidget));
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		formLayout->setWidget(f++, QFormLayout::FieldRole, button);
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
		connect(button, &QCheckBox::stateChanged, [ i, this ](int state) {
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
			emit buttonEvent({i, state == Qt::CheckState::Checked});
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    48
		});
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	}
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	std::vector<QString> axisLabels = {"X", "Y", "Z"};
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
6
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    53
	timer = new QTimer(centralwidget);
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    54
	connect(timer, &QTimer::timeout, this, &SimulatorWindow::sendMotionEvent);
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    55
	timer->setInterval(20);
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    56
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Motion:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	for (QString a : axisLabels) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		QSlider* slider = createSlider(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		motions.push_back(slider);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		formLayout->setWidget(f++, QFormLayout::FieldRole, slider);
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    63
		connect(slider, &QSlider::valueChanged, this, &SimulatorWindow::sendMotionEvent);
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Rotation:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	for (QString a : axisLabels) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		QSlider* slider = createSlider(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		rotations.push_back(slider);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		formLayout->setWidget(f++, QFormLayout::FieldRole, slider);
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    73
		connect(slider, &QSlider::valueChanged, this, &SimulatorWindow::sendMotionEvent);
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Actions:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	centerButton = new QPushButton("Center all", centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	connect(centerButton, &QPushButton::clicked, this, &SimulatorWindow::centerAll);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	formLayout->setWidget(f++, QFormLayout::FieldRole, centerButton);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
6
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    81
	
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    82
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Options:", centralwidget));
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    83
	periodicButton = new QCheckBox("Periodic updates", centralwidget);
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    84
	connect(periodicButton, &QCheckBox::clicked, [this](bool enabled) {
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    85
		if (enabled) timer->start();
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    86
		else timer->stop();
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    87
	});
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    88
	formLayout->setWidget(f++, QFormLayout::FieldRole, periodicButton);
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    89
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	setCentralWidget(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
}
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
SimulatorWindow::SimulatorWindow(const SimulatorWindow& orig) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
}
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
SimulatorWindow::~SimulatorWindow() {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
}
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
QSlider* SimulatorWindow::createSlider(QWidget* parent) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	QSlider* slider = new QSlider(Qt::Orientation::Horizontal, parent);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	slider->setRange(-500, 500);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
	slider->setValue(0);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	slider->setTickPosition(QSlider::TicksBelow);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	slider->setTickInterval(100);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	slider->setSingleStep(1);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	return slider;
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
}
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
void SimulatorWindow::centerAll() {
4
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   111
	// TODO: add option for suppressing events in sendMotionEvent() and call this method from here instead of through sliders signals, so only one event will be emitted?
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	for (QSlider* s : motions) s->setValue(0);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
	for (QSlider* s : rotations) s->setValue(0);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
}
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   115
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   116
void SimulatorWindow::sendMotionEvent() {
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   117
	MotionEvent event;
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   118
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   119
	event.x = motions[0]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   120
	event.y = motions[1]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   121
	event.z = motions[2]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   122
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   123
	event.rx = rotations[0]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   124
	event.ry = rotations[1]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   125
	event.rz = rotations[2]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   126
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   127
	event.period = 0; // TODO: period?
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   128
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   129
	emit motionEvent(event);
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   130
}