JoystickWidget.cc 5.69 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 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)));
pixhawk's avatar
pixhawk committed
28

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

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

35 36 37 38
    // 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)));

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

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

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

74 75 76 77 78 79 80 81 82 83 84 85
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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
JoystickWidget::~JoystickWidget()
{
    delete m_ui;
}

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

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

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

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

129 130 131 132
    // 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);
133 134
        axis->setValue(joystick->getCurrentValueForAxis(i));
        connect(axis, SIGNAL(mappingChanged(int,int)), this, SLOT(setMappingAxis(int,int)));
135 136 137 138
        // And make sure we insert BEFORE the vertical spacer.
        m_ui->axesLayout->insertWidget(i, axis);
        axes.append(axis);
    }
pixhawk's avatar
pixhawk committed
139
}
pixhawk's avatar
pixhawk committed
140

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

149
void JoystickWidget::setHat(float x, float y)
pixhawk's avatar
pixhawk committed
150
{
151
    updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
152 153
}

154
void JoystickWidget::setMappingAxis(int axisID, int newMapping)
155
{
156
    switch (newMapping)
157
    {
158 159 160 161 162 163 164 165 166 167 168 169
        case JoystickAxis::JOYSTICK_AXIS_MAPPING_ROLL:
            joystick->setMappingRollAxis(axisID);
            break;
        case JoystickAxis::JOYSTICK_AXIS_MAPPING_PITCH:
            joystick->setMappingPitchAxis(axisID);
            break;
        case JoystickAxis::JOYSTICK_AXIS_MAPPING_YAW:
            joystick->setMappingYawAxis(axisID);
            break;
        case JoystickAxis::JOYSTICK_AXIS_MAPPING_THROTTLE:
            joystick->setMappingThrottleAxis(axisID);
            break;
170
    }
pixhawk's avatar
pixhawk committed
171 172
}

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

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

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