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

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

    connect(this->joystick, SIGNAL(joystickChanged(double,double,double,double,int,int)), this, SLOT(updateJoystick(double,double,double,double,int,int)));
    connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(pressKey(int)));

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

pixhawk's avatar
pixhawk committed
19 20 21 22 23 24 25 26 27 28 29 30
    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
31
    setZ(yaw);
pixhawk's avatar
pixhawk committed
32
    setThrottle(thrust);
pixhawk's avatar
pixhawk committed
33
    setHat(xHat, yHat);
pixhawk's avatar
pixhawk committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
}

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
52

pixhawk's avatar
pixhawk committed
53 54 55 56 57
void JoystickWidget::setX(float x)
{
    m_ui->xSlider->setValue(x*100);
    m_ui->xValue->display(x*100);
}
pixhawk's avatar
pixhawk committed
58

pixhawk's avatar
pixhawk committed
59 60 61 62 63
void JoystickWidget::setY(float y)
{
    m_ui->ySlider->setValue(y*100);
    m_ui->yValue->display(y*100);
}
pixhawk's avatar
pixhawk committed
64 65 66 67 68 69 70 71 72

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

void JoystickWidget::setHat(float x, float y)
{
    qDebug() << __FILE__ << __LINE__ << "HAT X:" << x << "HAT Y:" << y;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    updateStatus(tr("Hat position: x: %1, y: %2").arg(x, y));
}

void JoystickWidget::clearKeys()
{
    QString colorstyle;
    QColor buttonStyleColor = QColor(200, 20, 20);
    colorstyle = QString("QGroupBox { border: 1px solid #EEEEEE; border-radius: 4px; padding: 0px; margin: 0px; background-color: %1;}").arg(buttonStyleColor.name());

    m_ui->buttonLabel0->setStyleSheet(colorstyle);
    m_ui->buttonLabel1->setStyleSheet(colorstyle);
    m_ui->buttonLabel2->setStyleSheet(colorstyle);
    m_ui->buttonLabel3->setStyleSheet(colorstyle);
    m_ui->buttonLabel4->setStyleSheet(colorstyle);
    m_ui->buttonLabel5->setStyleSheet(colorstyle);
    m_ui->buttonLabel6->setStyleSheet(colorstyle);
    m_ui->buttonLabel7->setStyleSheet(colorstyle);
    m_ui->buttonLabel8->setStyleSheet(colorstyle);
    m_ui->buttonLabel9->setStyleSheet(colorstyle);
pixhawk's avatar
pixhawk committed
92 93
}

pixhawk's avatar
pixhawk committed
94 95 96
void JoystickWidget::pressKey(int key)
{
    QString colorstyle;
97 98
    QColor buttonStyleColor = QColor(20, 200, 20);
    colorstyle = QString("QGroupBox { border: 1px solid #EEEEEE; border-radius: 4px; padding: 0px; margin: 0px; background-color: %1;}").arg(buttonStyleColor.name());
99
    switch(key) {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    case 0:
        m_ui->buttonLabel0->setStyleSheet(colorstyle);
        break;
    case 1:
        m_ui->buttonLabel1->setStyleSheet(colorstyle);
        break;
    case 2:
        m_ui->buttonLabel2->setStyleSheet(colorstyle);
        break;
    case 3:
        m_ui->buttonLabel3->setStyleSheet(colorstyle);
        break;
    case 4:
        m_ui->buttonLabel4->setStyleSheet(colorstyle);
        break;
    case 5:
        m_ui->buttonLabel5->setStyleSheet(colorstyle);
        break;
    case 6:
        m_ui->buttonLabel6->setStyleSheet(colorstyle);
        break;
    case 7:
        m_ui->buttonLabel7->setStyleSheet(colorstyle);
        break;
    case 8:
        m_ui->buttonLabel8->setStyleSheet(colorstyle);
        break;
    case 9:
        m_ui->buttonLabel9->setStyleSheet(colorstyle);
        break;
    }
    QTimer::singleShot(20, this, SLOT(clearKeys()));
pixhawk's avatar
pixhawk committed
132
    qDebug() << __FILE__ << __LINE__ << "KEY" << key << " pressed on joystick";
133 134 135 136 137
    updateStatus(tr("Key %1 pressed").arg(key));
}

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