src/SimulatorWindow.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:51:08 +0200
branchv_0
changeset 10 da93f3667a52
parent 6 f81d43b74209
permissions -rw-r--r--
fix license version: GNU GPLv3
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
10
da93f3667a52 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * 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
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * 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
    15
 * 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
    16
 */
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
4
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    18
#include <QFormLayout>
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    19
#include <QLabel>
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    20
#include <QGridLayout>
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    21
#include <QObject>
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include "SimulatorWindow.h"
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    25
Q_DECLARE_METATYPE(MotionEvent);
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    26
Q_DECLARE_METATYPE(ButtonEvent);
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    27
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
SimulatorWindow::SimulatorWindow() {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	resize(640, 480);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	setWindowTitle("Spacenav Simulator");
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	QWidget* centralwidget = new QWidget(this);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	QFormLayout* formLayout = new QFormLayout(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	int f = 0;
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Buttons:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	std::vector<QString> buttonLabels = {"0", "1"};
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    40
	for (int i = 0; i < buttonLabels.size(); i++) {
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		QCheckBox* button = new QCheckBox(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		buttons.push_back(button);
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    43
		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
    44
		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
    45
		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
    46
			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
    47
		});
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	}
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
	std::vector<QString> axisLabels = {"X", "Y", "Z"};
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
6
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    52
	timer = new QTimer(centralwidget);
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    53
	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
    54
	timer->setInterval(20);
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    55
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Motion:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	for (QString a : axisLabels) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		QSlider* slider = createSlider(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		motions.push_back(slider);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		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
    62
		connect(slider, &QSlider::valueChanged, this, &SimulatorWindow::sendMotionEvent);
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
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
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Rotation:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	for (QString a : axisLabels) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		QSlider* slider = createSlider(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		rotations.push_back(slider);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		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
    72
		connect(slider, &QSlider::valueChanged, this, &SimulatorWindow::sendMotionEvent);
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	}
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
	formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Actions:", centralwidget));
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	centerButton = new QPushButton("Center all", centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	connect(centerButton, &QPushButton::clicked, this, &SimulatorWindow::centerAll);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	formLayout->setWidget(f++, QFormLayout::FieldRole, centerButton);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
6
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    80
	
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    81
	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
    82
	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
    83
	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
    84
		if (enabled) timer->start();
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    85
		else timer->stop();
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    86
	});
f81d43b74209 Option: Periodic updates (each 20 ms)
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    87
	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
    88
1
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	setCentralWidget(centralwidget);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
}
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
SimulatorWindow::SimulatorWindow(const SimulatorWindow& orig) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
}
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
SimulatorWindow::~SimulatorWindow() {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}
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
QSlider* SimulatorWindow::createSlider(QWidget* parent) {
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	QSlider* slider = new QSlider(Qt::Orientation::Horizontal, parent);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	slider->setRange(-500, 500);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	slider->setValue(0);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
	slider->setTickPosition(QSlider::TicksBelow);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	slider->setTickInterval(100);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	slider->setSingleStep(1);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	return slider;
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
}
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
void SimulatorWindow::centerAll() {
4
a874deb6a536 tidy up includes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   110
	// 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
   111
	for (QSlider* s : motions) s->setValue(0);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	for (QSlider* s : rotations) s->setValue(0);
9cd86b92aabb GUI draft
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
}
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   114
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   115
void SimulatorWindow::sendMotionEvent() {
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   116
	MotionEvent event;
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   117
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   118
	event.x = motions[0]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   119
	event.y = motions[1]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   120
	event.z = motions[2]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   121
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   122
	event.rx = rotations[0]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   123
	event.ry = rotations[1]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   124
	event.rz = rotations[2]->value();
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   125
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   126
	event.period = 0; // TODO: period?
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   127
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   128
	emit motionEvent(event);
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   129
}