JoystickWidget.cc 5.77 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
    QDialog(parent),
8
    joystick(joystick),
9
    m_ui(new Ui::JoystickWidget)
pixhawk's avatar
pixhawk committed
10 11
{
    m_ui->setupUi(this);
12 13 14 15 16 17

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

18 19
    // Initialize the UI based on the current joystick
    initUI();
Lorenz Meier's avatar
Lorenz Meier committed
20

21
    // Watch for input events from the joystick
22
    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
23
    connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(pressKey(int)));
24 25

    // Watch for changes to the button/axis mappings
Lorenz Meier's avatar
Lorenz Meier committed
26 27 28 29 30
    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
31

32 33
    // Update the UI if the joystick changes.
    connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int)));
34

35 36
    // Display the widget above all other windows.
    this->raise();
pixhawk's avatar
pixhawk committed
37 38 39
    this->show();
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
void JoystickWidget::initUI()
{
    // Add the joysticks to the top combobox. They're indexed by their item number.
    // And set the currently-selected combobox item to the current joystick.
    int joysticks = joystick->getNumJoysticks();
    if (joysticks)
    {
        for (int i = 0; i < joysticks; i++)
        {
            m_ui->joystickNameComboBox->addItem(joystick->getJoystickNameById(i));
        }
        m_ui->joystickNameComboBox->setCurrentIndex(joystick->getJoystickID());
    }
    else
    {
        m_ui->joystickNameComboBox->addItem(tr("No joysticks found. Connect and restart QGC to add one."));
    }

    // Add any missing buttons
    updateUIForJoystick(joystick->getJoystickID());
}

pixhawk's avatar
pixhawk committed
62 63 64 65 66 67 68 69 70
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
71
    setZ(yaw);
pixhawk's avatar
pixhawk committed
72
    setThrottle(thrust);
pixhawk's avatar
pixhawk committed
73
    setHat(xHat, yHat);
pixhawk's avatar
pixhawk committed
74 75 76 77 78 79 80 81 82 83 84 85 86
}

void JoystickWidget::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
void JoystickWidget::updateUIForJoystick(int id)
{
    // Delete all the old buttonlabels
    foreach (QLabel* l, m_ui->buttonLabelBox->findChildren<QLabel*>())
    {
        delete l;
    }

    // Set the JoystickInput to listen to the new joystick instead.
    joystick->setActiveJoystick(id);

    // And add new ones for every new button found.
    for (int i = 0; i < joystick->getJoystickNumButtons(); i++)
    {
        QLabel* buttonLabel = new QLabel(m_ui->buttonLabelBox);
        buttonLabel->setText(QString::number(i));
        buttonLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        buttonLabel->setAlignment(Qt::AlignCenter);
        // And make sure we insert BEFORE the vertical spacer.
        m_ui->buttonLabelLayout->insertWidget(i, buttonLabel);
    }

    // Update the mapping UI
    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());
}
pixhawk's avatar
pixhawk committed
116 117 118 119 120

void JoystickWidget::setThrottle(float thrust)
{
    m_ui->thrust->setValue(thrust*100);
}
pixhawk's avatar
pixhawk committed
121

pixhawk's avatar
pixhawk committed
122 123 124 125 126
void JoystickWidget::setX(float x)
{
    m_ui->xSlider->setValue(x*100);
    m_ui->xValue->display(x*100);
}
pixhawk's avatar
pixhawk committed
127

pixhawk's avatar
pixhawk committed
128 129 130 131 132
void JoystickWidget::setY(float y)
{
    m_ui->ySlider->setValue(y*100);
    m_ui->yValue->display(y*100);
}
pixhawk's avatar
pixhawk committed
133 134 135 136 137 138 139 140

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

void JoystickWidget::setHat(float x, float y)
{
141
    updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
142 143
}

pixhawk's avatar
pixhawk committed
144 145
void JoystickWidget::pressKey(int key)
{
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
//    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());
//    switch(key) {
//    case 0:
//        m_ui->button0->setStyleSheet(colorstyle);
//        break;
//    case 1:
//        m_ui->button1->setStyleSheet(colorstyle);
//        break;
//    case 2:
//        m_ui->button2->setStyleSheet(colorstyle);
//        break;
//    case 3:
//        m_ui->button3->setStyleSheet(colorstyle);
//        break;
//    case 4:
//        m_ui->button4->setStyleSheet(colorstyle);
//        break;
//    case 5:
//        m_ui->button5->setStyleSheet(colorstyle);
//        break;
//    case 6:
//        m_ui->button6->setStyleSheet(colorstyle);
//        break;
//    case 7:
//        m_ui->button7->setStyleSheet(colorstyle);
//        break;
//    case 8:
//        m_ui->button8->setStyleSheet(colorstyle);
//        break;
//    case 9:
//        m_ui->button9->setStyleSheet(colorstyle);
//        break;
//    case 10:
//        m_ui->button10->setStyleSheet(colorstyle);
//        break;
//    }
184 185 186 187 188
    updateStatus(tr("Key %1 pressed").arg(key));
}

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