JoystickWidget.cc 5.9 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
#include "JoystickWidget.h"
2
#include "MainWindow.h"
pixhawk's avatar
pixhawk committed
3
#include "ui_JoystickWidget.h"
4 5
#include "JoystickButton.h"
#include "JoystickAxis.h"
pixhawk's avatar
pixhawk committed
6
#include <QDebug>
7
#include <QDesktopWidget>
pixhawk's avatar
pixhawk committed
8 9

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

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

21 22
    // Initialize the UI based on the current joystick
    initUI();
Lorenz Meier's avatar
Lorenz Meier committed
23

24
    // Watch for button, axis, and hat input events from the joystick.
25 26
    connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int)));
    connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int)));
27
    connect(this->joystick, SIGNAL(axisValueChanged(int,float)), this, SLOT(updateAxisValue(int,float)));
28
    connect(this->joystick, SIGNAL(hatDirectionChanged(int,int)), this, SLOT(setHat(int,int)));
pixhawk's avatar
pixhawk committed
29

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

33 34 35
    // Enable/disable the UI based on the enable checkbox
    connect(m_ui->enableCheckBox, SIGNAL(toggled(bool)), m_ui->joystickFrame, SLOT(setEnabled(bool)));

36 37 38 39
    // Update the button label colors based on the current theme and watch for future theme changes.
    styleChanged(MainWindow::instance()->getStyle());
    connect(MainWindow::instance(), SIGNAL(styleChanged(MainWindow::QGC_MAINWINDOW_STYLE)), this, SLOT(styleChanged(MainWindow::QGC_MAINWINDOW_STYLE)));

40 41
    // Display the widget above all other windows.
    this->raise();
pixhawk's avatar
pixhawk committed
42 43 44
    this->show();
}

45 46 47 48 49 50 51 52 53 54 55 56
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());
57 58 59 60 61
        // And if joystick support is enabled, show the UI.
        if (m_ui->enableCheckBox->isChecked())
        {
            m_ui->joystickFrame->setEnabled(true);
        }
62
    }
63
    // But if there're no joysticks, just disable everything.
64 65
    else
    {
66
        m_ui->enableCheckBox->setEnabled(false);
67
        m_ui->joystickNameComboBox->addItem(tr("No joysticks found. Connect and restart QGC to add one."));
68
        m_ui->joystickNameComboBox->setEnabled(false);
69 70 71 72 73 74
    }

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

75 76 77 78 79 80 81 82 83 84 85 86
void JoystickWidget::styleChanged(MainWindow::QGC_MAINWINDOW_STYLE newStyle)
{
    if (newStyle == MainWindow::QGC_MAINWINDOW_STYLE_LIGHT)
    {
        buttonLabelColor = QColor(0x73, 0xD9, 0x5D);
    }
    else
    {
        buttonLabelColor = QColor(0x14, 0xC6, 0x14);
    }
}

pixhawk's avatar
pixhawk committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
JoystickWidget::~JoystickWidget()
{
    delete m_ui;
}

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

103 104
void JoystickWidget::updateUIForJoystick(int id)
{
105 106
    // Delete all the old UI elements
    foreach (JoystickButton* b, buttons)
107
    {
108
        delete b;
109
    }
110 111 112 113 114 115
    buttons.clear();
    foreach (JoystickAxis* a, axes)
    {
        delete a;
    }
    axes.clear();
116 117 118 119

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

120 121 122
    // And add the necessary button displays for this joystick.
    int newButtons = joystick->getJoystickNumButtons();
    for (int i = 0; i < newButtons; i++)
123
    {
124
        JoystickButton* button = new JoystickButton(i, m_ui->buttonBox);
125
        // And make sure we insert BEFORE the vertical spacer.
126 127
        m_ui->buttonLayout->insertWidget(i, button);
        buttons.append(button);
128 129
    }

130 131 132 133
    // Do the same for the axes supported by this joystick.
    for (int i = 0; i < joystick->getJoystickNumAxes(); i++)
    {
        JoystickAxis* axis = new JoystickAxis(i, m_ui->axesBox);
134
        axis->setValue(joystick->getCurrentValueForAxis(i));
135
        connect(axis, SIGNAL(mappingChanged(int,JoystickInput::JOYSTICK_INPUT_MAPPING)), this, SLOT(setMappingAxis(int,JoystickInput::JOYSTICK_INPUT_MAPPING)));
136 137 138 139
        // And make sure we insert BEFORE the vertical spacer.
        m_ui->axesLayout->insertWidget(i, axis);
        axes.append(axis);
    }
pixhawk's avatar
pixhawk committed
140
}
pixhawk's avatar
pixhawk committed
141

142
void JoystickWidget::updateAxisValue(int axis, float value)
pixhawk's avatar
pixhawk committed
143
{
144
    if (axis < axes.size())
145
    {
146
        axes.at(axis)->setValue(value);
147
    }
pixhawk's avatar
pixhawk committed
148
}
pixhawk's avatar
pixhawk committed
149

150
void JoystickWidget::setHat(int x, int y)
pixhawk's avatar
pixhawk committed
151
{
152
    m_ui->statusLabel->setText(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
153 154
}

155
void JoystickWidget::setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping)
156
{
157
    switch (newMapping)
158
    {
159
        case JoystickInput::JOYSTICK_INPUT_MAPPING_ROLL:
160 161
            joystick->setMappingRollAxis(axisID);
            break;
162
        case JoystickInput::JOYSTICK_INPUT_MAPPING_PITCH:
163 164
            joystick->setMappingPitchAxis(axisID);
            break;
165
        case JoystickInput::JOYSTICK_INPUT_MAPPING_YAW:
166 167
            joystick->setMappingYawAxis(axisID);
            break;
168
        case JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE:
169 170
            joystick->setMappingThrottleAxis(axisID);
            break;
171 172 173
        case JoystickInput::JOYSTICK_INPUT_MAPPING_NONE:
        default:
            break;
174
    }
pixhawk's avatar
pixhawk committed
175 176
}

177 178 179
void JoystickWidget::joystickButtonPressed(int key)
{
    QString colorStyle = QString("QLabel { background-color: %1;}").arg(buttonLabelColor.name());
180
    buttons.at(key)->setStyleSheet(colorStyle);
181 182 183
}

void JoystickWidget::joystickButtonReleased(int key)
pixhawk's avatar
pixhawk committed
184
{
185
    buttons.at(key)->setStyleSheet("");
186
}