JoystickWidget.cc 4.81 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3
#include "JoystickWidget.h"
#include "ui_JoystickWidget.h"
#include <QDebug>
4
#include <QDesktopWidget>
pixhawk's avatar
pixhawk committed
5 6

JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) :
7 8
    QDialog(parent),
    m_ui(new Ui::JoystickWidget)
pixhawk's avatar
pixhawk committed
9 10
{
    m_ui->setupUi(this);
11 12 13 14 15 16 17

    // Center the window on the screen.
    QRect position = frameGeometry();
    position.moveCenter(QDesktopWidget().availableGeometry().center());
    move(position.topLeft());

    clearKeys();
pixhawk's avatar
pixhawk committed
18 19
    this->joystick = joystick;

Lorenz Meier's avatar
Lorenz Meier committed
20 21 22 23 24 25
    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());

26
    connect(this->joystick, SIGNAL(joystickChanged(double,double,double,double,int,int,int)), this, SLOT(updateJoystick(double,double,double,double,int,int)));
pixhawk's avatar
pixhawk committed
27
    connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(pressKey(int)));
Lorenz Meier's avatar
Lorenz Meier committed
28 29 30 31 32
    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)));
pixhawk's avatar
pixhawk committed
33 34 35

    // Display the widget
    this->window()->setWindowTitle(tr("Joystick Settings"));
36 37
    if (joystick) updateStatus(tr("Found joystick: %1").arg(joystick->getName()));

pixhawk's avatar
pixhawk committed
38 39 40 41 42 43 44 45 46 47 48 49
    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);
pixhawk's avatar
pixhawk committed
50
    setZ(yaw);
pixhawk's avatar
pixhawk committed
51
    setThrottle(thrust);
pixhawk's avatar
pixhawk committed
52
    setHat(xHat, yHat);
pixhawk's avatar
pixhawk committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

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);
}
pixhawk's avatar
pixhawk committed
71

pixhawk's avatar
pixhawk committed
72 73 74 75 76
void JoystickWidget::setX(float x)
{
    m_ui->xSlider->setValue(x*100);
    m_ui->xValue->display(x*100);
}
pixhawk's avatar
pixhawk committed
77

pixhawk's avatar
pixhawk committed
78 79 80 81 82
void JoystickWidget::setY(float y)
{
    m_ui->ySlider->setValue(y*100);
    m_ui->yValue->display(y*100);
}
pixhawk's avatar
pixhawk committed
83 84 85 86 87 88 89 90

void JoystickWidget::setZ(float z)
{
    m_ui->dial->setValue(z*100);
}

void JoystickWidget::setHat(float x, float y)
{
91
    updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
92 93 94 95 96 97
}

void JoystickWidget::clearKeys()
{
    QString colorstyle;
    QColor buttonStyleColor = QColor(200, 20, 20);
98 99 100 101 102 103 104 105 106 107 108 109 110
    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);
pixhawk's avatar
pixhawk committed
111 112
}

pixhawk's avatar
pixhawk committed
113 114 115
void JoystickWidget::pressKey(int key)
{
    QString colorstyle;
116
    QColor buttonStyleColor = QColor(20, 200, 20);
117
    colorstyle = QString("QLabel { border: 1px solid #EEEEEE; border-radius: 4px; padding: 0px; margin: 0px; background-color: %1;}").arg(buttonStyleColor.name());
118
    switch(key) {
119
    case 0:
120
        m_ui->button0->setStyleSheet(colorstyle);
121 122
        break;
    case 1:
123
        m_ui->button1->setStyleSheet(colorstyle);
124 125
        break;
    case 2:
126
        m_ui->button2->setStyleSheet(colorstyle);
127 128
        break;
    case 3:
129
        m_ui->button3->setStyleSheet(colorstyle);
130 131
        break;
    case 4:
132
        m_ui->button4->setStyleSheet(colorstyle);
133 134
        break;
    case 5:
135
        m_ui->button5->setStyleSheet(colorstyle);
136 137
        break;
    case 6:
138
        m_ui->button6->setStyleSheet(colorstyle);
139 140
        break;
    case 7:
141
        m_ui->button7->setStyleSheet(colorstyle);
142 143
        break;
    case 8:
144
        m_ui->button8->setStyleSheet(colorstyle);
145 146
        break;
    case 9:
147 148 149 150
        m_ui->button9->setStyleSheet(colorstyle);
        break;
    case 10:
        m_ui->button10->setStyleSheet(colorstyle);
151 152 153 154 155 156 157 158
        break;
    }
    QTimer::singleShot(20, this, SLOT(clearKeys()));
    updateStatus(tr("Key %1 pressed").arg(key));
}

void JoystickWidget::updateStatus(const QString& status)
{
159
    m_ui->statusLabel->setText(status);
pixhawk's avatar
pixhawk committed
160
}