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