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 input events from the joystick
25
    connect(this->joystick, SIGNAL(joystickChanged(double,double,double,double,int,int,int)), this, SLOT(updateJoystick(double,double,double,double,int,int)));
26 27
    connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int)));
    connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int)));
28 29

    // Watch for changes to the button/axis mappings
30 31 32 33 34
//    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
35

36 37
    // Update the UI if the joystick changes.
    connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int)));
38

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

42 43 44 45
    // 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)));

46 47
    // Display the widget above all other windows.
    this->raise();
pixhawk's avatar
pixhawk committed
48 49 50
    this->show();
}

51 52 53 54 55 56 57 58 59 60 61 62
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());
63 64 65 66 67
        // And if joystick support is enabled, show the UI.
        if (m_ui->enableCheckBox->isChecked())
        {
            m_ui->joystickFrame->setEnabled(true);
        }
68 69 70 71 72 73 74 75 76 77
    }
    else
    {
        m_ui->joystickNameComboBox->addItem(tr("No joysticks found. Connect and restart QGC to add one."));
    }

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

78 79 80 81 82 83 84 85 86 87 88 89
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
90 91 92 93 94 95 96 97 98
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
99
    setZ(yaw);
pixhawk's avatar
pixhawk committed
100
    setThrottle(thrust);
pixhawk's avatar
pixhawk committed
101
    setHat(xHat, yHat);
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110 111 112 113 114
}

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

115 116
void JoystickWidget::updateUIForJoystick(int id)
{
117 118
    // Delete all the old UI elements
    foreach (JoystickButton* b, buttons)
119
    {
120
        delete b;
121
    }
122 123 124 125 126 127
    buttons.clear();
    foreach (JoystickAxis* a, axes)
    {
        delete a;
    }
    axes.clear();
128 129 130 131

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

132 133 134
    // And add the necessary button displays for this joystick.
    int newButtons = joystick->getJoystickNumButtons();
    for (int i = 0; i < newButtons; i++)
135
    {
136
        JoystickButton* button = new JoystickButton(i, m_ui->buttonBox);
137
        // And make sure we insert BEFORE the vertical spacer.
138 139
        m_ui->buttonLayout->insertWidget(i, button);
        buttons.append(button);
140 141
    }

142 143 144 145 146 147 148 149
    // 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);
        // And make sure we insert BEFORE the vertical spacer.
        m_ui->axesLayout->insertWidget(i, axis);
        axes.append(axis);
    }
pixhawk's avatar
pixhawk committed
150
}
pixhawk's avatar
pixhawk committed
151

pixhawk's avatar
pixhawk committed
152 153
void JoystickWidget::setX(float x)
{
154 155 156 157
    if (axes.size() > 0)
    {
        axes.at(0)->setValue(x * 100);
    }
pixhawk's avatar
pixhawk committed
158
}
pixhawk's avatar
pixhawk committed
159

pixhawk's avatar
pixhawk committed
160 161
void JoystickWidget::setY(float y)
{
162 163 164 165
    if (axes.size() > 1)
    {
        axes.at(1)->setValue(y * 100);
    }
pixhawk's avatar
pixhawk committed
166
}
pixhawk's avatar
pixhawk committed
167 168 169

void JoystickWidget::setZ(float z)
{
170 171 172 173 174 175 176 177 178 179 180 181
    if (axes.size() > 2)
    {
        axes.at(2)->setValue(z * 100);
    }
}

void JoystickWidget::setThrottle(float t)
{
    if (axes.size() > 3)
    {
        axes.at(3)->setValue(t * 100);
    }
pixhawk's avatar
pixhawk committed
182 183 184 185
}

void JoystickWidget::setHat(float x, float y)
{
186
    updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
187 188
}

189 190 191
void JoystickWidget::joystickButtonPressed(int key)
{
    QString colorStyle = QString("QLabel { background-color: %1;}").arg(buttonLabelColor.name());
192
    buttons.at(key)->setStyleSheet(colorStyle);
193 194 195
}

void JoystickWidget::joystickButtonReleased(int key)
pixhawk's avatar
pixhawk committed
196
{
197
    buttons.at(key)->setStyleSheet("");
198 199 200 201
}

void JoystickWidget::updateStatus(const QString& status)
{
pixhawk's avatar
pixhawk committed
202
}