author | František Kučera <franta-hg@frantovo.cz> |
Thu, 24 Oct 2019 21:51:08 +0200 | |
branch | v_0 |
changeset 10 | da93f3667a52 |
parent 6 | f81d43b74209 |
permissions | -rw-r--r-- |
1 | 1 |
/** |
2 |
* Spacenav Simulator Qt |
|
3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
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 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
4 | 18 |
#include <QFormLayout> |
19 |
#include <QLabel> |
|
20 |
#include <QGridLayout> |
|
21 |
#include <QObject> |
|
1 | 22 |
|
23 |
#include "SimulatorWindow.h" |
|
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 | 28 |
SimulatorWindow::SimulatorWindow() { |
29 |
resize(640, 480); |
|
30 |
setWindowTitle("Spacenav Simulator"); |
|
31 |
||
32 |
QWidget* centralwidget = new QWidget(this); |
|
33 |
QFormLayout* formLayout = new QFormLayout(centralwidget); |
|
34 |
||
35 |
int f = 0; |
|
36 |
||
37 |
formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Buttons:", centralwidget)); |
|
38 |
std::vector<QString> buttonLabels = {"0", "1"}; |
|
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 | 41 |
QCheckBox* button = new QCheckBox(centralwidget); |
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 | 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 | 48 |
} |
49 |
||
50 |
std::vector<QString> axisLabels = {"X", "Y", "Z"}; |
|
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 | 56 |
formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Motion:", centralwidget)); |
57 |
for (QString a : axisLabels) { |
|
58 |
QSlider* slider = createSlider(centralwidget); |
|
59 |
motions.push_back(slider); |
|
60 |
formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget)); |
|
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 | 63 |
} |
64 |
||
65 |
||
66 |
formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Rotation:", centralwidget)); |
|
67 |
for (QString a : axisLabels) { |
|
68 |
QSlider* slider = createSlider(centralwidget); |
|
69 |
rotations.push_back(slider); |
|
70 |
formLayout->setWidget(f, QFormLayout::LabelRole, new QLabel(a, centralwidget)); |
|
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 | 73 |
} |
74 |
||
75 |
formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Actions:", centralwidget)); |
|
76 |
centerButton = new QPushButton("Center all", centralwidget); |
|
77 |
connect(centerButton, &QPushButton::clicked, this, &SimulatorWindow::centerAll); |
|
78 |
formLayout->setWidget(f++, QFormLayout::FieldRole, centerButton); |
|
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 | 89 |
|
90 |
setCentralWidget(centralwidget); |
|
91 |
} |
|
92 |
||
93 |
SimulatorWindow::SimulatorWindow(const SimulatorWindow& orig) { |
|
94 |
} |
|
95 |
||
96 |
SimulatorWindow::~SimulatorWindow() { |
|
97 |
} |
|
98 |
||
99 |
QSlider* SimulatorWindow::createSlider(QWidget* parent) { |
|
100 |
QSlider* slider = new QSlider(Qt::Orientation::Horizontal, parent); |
|
101 |
slider->setRange(-500, 500); |
|
102 |
slider->setValue(0); |
|
103 |
slider->setTickPosition(QSlider::TicksBelow); |
|
104 |
slider->setTickInterval(100); |
|
105 |
slider->setSingleStep(1); |
|
106 |
return slider; |
|
107 |
} |
|
108 |
||
109 |
void SimulatorWindow::centerAll() { |
|
4 | 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 | 111 |
for (QSlider* s : motions) s->setValue(0); |
112 |
for (QSlider* s : rotations) s->setValue(0); |
|
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 |
} |