Newer
Older
#include "JoystickWidget.h"
#include "ui_JoystickWidget.h"
#include <QDebug>
JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) :
QDialog(parent),
m_ui(new Ui::JoystickWidget)
m_ui->rollMapSpinBox->setValue(joystick->getMappingXAxis());
m_ui->pitchMapSpinBox->setValue(joystick->getMappingYAxis());
m_ui->yawMapSpinBox->setValue(joystick->getMappingYawAxis());
m_ui->throttleMapSpinBox->setValue(joystick->getMappingThrustAxis());
m_ui->autoMapSpinBox->setValue(joystick->getMappingAutoButton());
connect(this->joystick, SIGNAL(joystickChanged(double,double,double,double,int,int,int)), this, SLOT(updateJoystick(double,double,double,double,int,int)));
connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(pressKey(int)));
connect(m_ui->rollMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingXAxis(int)));
connect(m_ui->pitchMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingYAxis(int)));
connect(m_ui->yawMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingYawAxis(int)));
connect(m_ui->throttleMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingThrustAxis(int)));
connect(m_ui->autoMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingAutoButton(int)));
// Display the widget
this->window()->setWindowTitle(tr("Joystick Settings"));
if (joystick) updateStatus(tr("Found joystick: %1").arg(joystick->getName()));
this->show();
}
JoystickWidget::~JoystickWidget()
{
delete m_ui;
}
void JoystickWidget::updateJoystick(double roll, double pitch, double yaw, double thrust, int xHat, int yHat)
{
setX(roll);
setY(pitch);
}
void JoystickWidget::changeEvent(QEvent *e)
{
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
void JoystickWidget::setThrottle(float thrust)
{
m_ui->thrust->setValue(thrust*100);
}
void JoystickWidget::setX(float x)
{
m_ui->xSlider->setValue(x*100);
m_ui->xValue->display(x*100);
}
void JoystickWidget::setY(float y)
{
m_ui->ySlider->setValue(y*100);
m_ui->yValue->display(y*100);
}
void JoystickWidget::setZ(float z)
{
m_ui->dial->setValue(z*100);
}
void JoystickWidget::setHat(float x, float y)
{
updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
}
void JoystickWidget::clearKeys()
{
QString colorstyle;
QColor buttonStyleColor = QColor(200, 20, 20);
colorstyle = QString("QLabel { border: 1px solid #EEEEEE; border-radius: 4px; padding: 0px; margin: 0px; background-color: %1;}").arg(buttonStyleColor.name());
m_ui->button0->setStyleSheet(colorstyle);
m_ui->button1->setStyleSheet(colorstyle);
m_ui->button2->setStyleSheet(colorstyle);
m_ui->button3->setStyleSheet(colorstyle);
m_ui->button4->setStyleSheet(colorstyle);
m_ui->button5->setStyleSheet(colorstyle);
m_ui->button6->setStyleSheet(colorstyle);
m_ui->button7->setStyleSheet(colorstyle);
m_ui->button8->setStyleSheet(colorstyle);
m_ui->button9->setStyleSheet(colorstyle);
m_ui->button10->setStyleSheet(colorstyle);
void JoystickWidget::pressKey(int key)
{
QString colorstyle;
QColor buttonStyleColor = QColor(20, 200, 20);
colorstyle = QString("QLabel { border: 1px solid #EEEEEE; border-radius: 4px; padding: 0px; margin: 0px; background-color: %1;}").arg(buttonStyleColor.name());
m_ui->button0->setStyleSheet(colorstyle);
m_ui->button1->setStyleSheet(colorstyle);
m_ui->button2->setStyleSheet(colorstyle);
m_ui->button3->setStyleSheet(colorstyle);
m_ui->button4->setStyleSheet(colorstyle);
m_ui->button5->setStyleSheet(colorstyle);
m_ui->button6->setStyleSheet(colorstyle);
m_ui->button7->setStyleSheet(colorstyle);
m_ui->button8->setStyleSheet(colorstyle);
m_ui->button9->setStyleSheet(colorstyle);
break;
case 10:
m_ui->button10->setStyleSheet(colorstyle);
break;
}
QTimer::singleShot(20, this, SLOT(clearKeys()));
updateStatus(tr("Key %1 pressed").arg(key));
}
void JoystickWidget::updateStatus(const QString& status)
{
m_ui->statusLabel->setText(status);