/**
* 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, version 3 of the License.
*
* 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 <QFormLayout>
#include <QLabel>
#include <QGridLayout>
#include <QObject>
#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"};
timer = new QTimer(centralwidget);
connect(timer, &QTimer::timeout, this, &SimulatorWindow::sendMotionEvent);
timer->setInterval(20);
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);
formLayout->setWidget(f++, QFormLayout::LabelRole, new QLabel("Options:", centralwidget));
periodicButton = new QCheckBox("Periodic updates", centralwidget);
connect(periodicButton, &QCheckBox::clicked, [this](bool enabled) {
if (enabled) timer->start();
else timer->stop();
});
formLayout->setWidget(f++, QFormLayout::FieldRole, periodicButton);
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() {
// 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?
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);
}