diff --git a/qgroundcontrol.pro b/qgroundcontrol.pro index 0b986f47e3189d866687cf18dec6a8b1b6c0f5a8..936272b4f1d73a63006fd9a09727fc8e0f10c3db 100644 --- a/qgroundcontrol.pro +++ b/qgroundcontrol.pro @@ -233,7 +233,9 @@ FORMS += src/ui/MainWindow.ui \ src/ui/uas/UASActionsWidget.ui \ src/ui/QGCTabbedInfoView.ui \ src/ui/UASRawStatusView.ui \ - src/ui/uas/QGCMessageView.ui + src/ui/uas/QGCMessageView.ui \ + src/ui/JoystickButton.ui \ + src/ui/JoystickAxis.ui INCLUDEPATH += src \ src/ui \ src/ui/linechart \ @@ -391,7 +393,9 @@ HEADERS += src/MG.h \ src/ui/QGCTabbedInfoView.h \ src/ui/UASRawStatusView.h \ src/ui/PrimaryFlightDisplay.h \ - src/ui/uas/QGCMessageView.h + src/ui/uas/QGCMessageView.h \ + src/ui/JoystickButton.h \ + src/ui/JoystickAxis.h # Google Earth is only supported on Mac OS and Windows with Visual Studio Compiler macx|macx-g++|macx-g++42|win32-msvc2008|win32-msvc2010|win32-msvc2012::HEADERS += src/ui/map3D/QGCGoogleEarthView.h @@ -567,7 +571,9 @@ SOURCES += src/main.cc \ src/ui/QGCTabbedInfoView.cpp \ src/ui/UASRawStatusView.cpp \ src/ui/PrimaryFlightDisplay.cpp \ - src/ui/uas/QGCMessageView.cc + src/ui/uas/QGCMessageView.cc \ + src/ui/JoystickButton.cc \ + src/ui/JoystickAxis.cc # Enable Google Earth only on Mac OS and Windows with Visual Studio compiler macx|macx-g++|macx-g++42|win32-msvc2008|win32-msvc2010|win32-msvc2012::SOURCES += src/ui/map3D/QGCGoogleEarthView.cc diff --git a/src/input/JoystickInput.cc b/src/input/JoystickInput.cc index d261cccc53fc20360bab3199348d6953561cfc0d..3c0aae4e5d9cb01c086921ddf2ded1401155152f 100644 --- a/src/input/JoystickInput.cc +++ b/src/input/JoystickInput.cc @@ -332,7 +332,8 @@ void JoystickInput::setActiveJoystick(int id) { joystickName = QString(SDL_JoystickName(joystickID)); joystickButtons = SDL_JoystickNumButtons(joystick); - qDebug() << QString("Switching to joystick '%1' with %2 buttons").arg(joystickName, QString::number(joystickButtons)); + joystickAxes = SDL_JoystickNumAxes(joystick); + qDebug() << QString("Switching to joystick '%1' with %2 buttons/%3 axes").arg(joystickName, QString::number(joystickButtons), QString::number(joystickAxes)); } buttonState = 0; } diff --git a/src/input/JoystickInput.h b/src/input/JoystickInput.h index fb2aea95152b72b85ac8c3f7c3b8ecb51c395e69..96896083eb7ba20a79dbf8e19d08143807cabe08 100644 --- a/src/input/JoystickInput.h +++ b/src/input/JoystickInput.h @@ -109,6 +109,11 @@ public: return joystickButtons; } + int getJoystickNumAxes() const + { + return joystickAxes; + } + int getJoystickID() const { return joystickID; @@ -147,6 +152,7 @@ protected: int stabilizeButtonMapping; SDL_Event event; QString joystickName; + int joystickAxes; int joystickButtons; int joystickID; int joysticksFound; diff --git a/src/ui/JoystickAxis.cc b/src/ui/JoystickAxis.cc new file mode 100644 index 0000000000000000000000000000000000000000..4a65de5d25ec503722248c554b003ef3fb300e69 --- /dev/null +++ b/src/ui/JoystickAxis.cc @@ -0,0 +1,21 @@ +#include "JoystickAxis.h" +#include "ui_JoystickAxis.h" +#include + +JoystickAxis::JoystickAxis(int id, QWidget *parent) : + QWidget(parent), + ui(new Ui::JoystickAxis) +{ + ui->setupUi(this); + ui->label->setText(QString::number(id)); +} + +JoystickAxis::~JoystickAxis() +{ + delete ui; +} + +void JoystickAxis::setValue(int value) +{ + ui->progressBar->setValue(value); +} diff --git a/src/ui/JoystickAxis.h b/src/ui/JoystickAxis.h new file mode 100644 index 0000000000000000000000000000000000000000..34393655059c503d99848bd67d33b4e1479fc23c --- /dev/null +++ b/src/ui/JoystickAxis.h @@ -0,0 +1,25 @@ +#ifndef JOYSTICKAXIS_H +#define JOYSTICKAXIS_H + +#include + +namespace Ui { +class JoystickAxis; +} + +class JoystickAxis : public QWidget +{ + Q_OBJECT + +public: + explicit JoystickAxis(int id, QWidget *parent = 0); + ~JoystickAxis(); + +public slots: + void setValue(int value); + +private: + Ui::JoystickAxis *ui; +}; + +#endif // JOYSTICKAXIS_H diff --git a/src/ui/JoystickAxis.ui b/src/ui/JoystickAxis.ui new file mode 100644 index 0000000000000000000000000000000000000000..925727a3bc0fddb77f759044fc1a6bf1900639df --- /dev/null +++ b/src/ui/JoystickAxis.ui @@ -0,0 +1,101 @@ + + + JoystickAxis + + + + 0 + 0 + 40 + 200 + + + + + 0 + 0 + + + + + 40 + 200 + + + + Form + + + + QLayout::SetMinimumSize + + + 1 + + + 2 + + + 1 + + + 2 + + + + + + + + + + + + + 0 + 0 + + + + + -- + + + + + + + + false + + + + 0 + 0 + + + + + 0 + 100 + + + + 0 + + + Qt::AlignCenter + + + false + + + Qt::Vertical + + + + + + + + diff --git a/src/ui/JoystickButton.cc b/src/ui/JoystickButton.cc new file mode 100644 index 0000000000000000000000000000000000000000..d07852b9b43593447d37de9318c61849153b4d36 --- /dev/null +++ b/src/ui/JoystickButton.cc @@ -0,0 +1,16 @@ +#include "JoystickButton.h" +#include "ui_JoystickButton.h" + +JoystickButton::JoystickButton(int id, QWidget *parent) : + QWidget(parent), + id(id), + m_ui(new Ui::JoystickButton) +{ + m_ui->setupUi(this); + m_ui->joystickButtonLabel->setText(QString::number(id)); +} + +JoystickButton::~JoystickButton() +{ + delete m_ui; +} diff --git a/src/ui/JoystickButton.h b/src/ui/JoystickButton.h new file mode 100644 index 0000000000000000000000000000000000000000..4c1bbaebf047c4c4a0aecc6de19f8080a119cf0f --- /dev/null +++ b/src/ui/JoystickButton.h @@ -0,0 +1,21 @@ +#ifndef JOYSTICKBUTTON_H +#define JOYSTICKBUTTON_H + +#include + +namespace Ui +{ +class JoystickButton; +} + +class JoystickButton : public QWidget +{ + Q_OBJECT +public: + explicit JoystickButton(int id, QWidget *parent = 0); + virtual ~JoystickButton(); +private: + int id; + Ui::JoystickButton *m_ui; +}; +#endif // JOYSTICKBUTTON_H diff --git a/src/ui/JoystickButton.ui b/src/ui/JoystickButton.ui new file mode 100644 index 0000000000000000000000000000000000000000..de82b9b5bd6c33c25889d15335aa26b4512c1ad7 --- /dev/null +++ b/src/ui/JoystickButton.ui @@ -0,0 +1,90 @@ + + + JoystickButton + + + + 0 + 0 + 125 + 27 + + + + + 0 + 0 + + + + + 50 + 0 + + + + Form + + + + QLayout::SetMinimumSize + + + 2 + + + 1 + + + 2 + + + 1 + + + + + + -- + + + + + Yaw + + + + + Pitch + + + + + Roll + + + + + Throttle + + + + + + + + + 0 + 0 + + + + + + + + + + + + diff --git a/src/ui/JoystickWidget.cc b/src/ui/JoystickWidget.cc index aa186d7dab2939e22a6757623c4f34af082b9857..b8c408ec6b20a839f2f98fd5494909631dcda8b7 100644 --- a/src/ui/JoystickWidget.cc +++ b/src/ui/JoystickWidget.cc @@ -1,6 +1,8 @@ #include "JoystickWidget.h" #include "MainWindow.h" #include "ui_JoystickWidget.h" +#include "JoystickButton.h" +#include "JoystickAxis.h" #include #include @@ -25,15 +27,18 @@ JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) : connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int))); // Watch for changes to the button/axis mappings - 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))); +// 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))); // Update the UI if the joystick changes. connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int))); + // Enable/disable the UI based on the enable checkbox + connect(m_ui->enableCheckBox, SIGNAL(toggled(bool)), m_ui->joystickFrame, SLOT(setEnabled(bool))); + // 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))); @@ -55,6 +60,11 @@ void JoystickWidget::initUI() m_ui->joystickNameComboBox->addItem(joystick->getJoystickNameById(i)); } m_ui->joystickNameComboBox->setCurrentIndex(joystick->getJoystickID()); + // And if joystick support is enabled, show the UI. + if (m_ui->enableCheckBox->isChecked()) + { + m_ui->joystickFrame->setEnabled(true); + } } else { @@ -104,56 +114,71 @@ void JoystickWidget::changeEvent(QEvent *e) void JoystickWidget::updateUIForJoystick(int id) { - // Delete all the old buttonlabels - foreach (QLabel* l, buttonLabels) + // Delete all the old UI elements + foreach (JoystickButton* b, buttons) { - delete l; + delete b; } - buttonLabels.clear(); + buttons.clear(); + foreach (JoystickAxis* a, axes) + { + delete a; + } + axes.clear(); // 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++) + // And add the necessary button displays for this joystick. + int newButtons = joystick->getJoystickNumButtons(); + for (int i = 0; i < newButtons; i++) { - QLabel* buttonLabel = new QLabel(m_ui->buttonLabelBox); - buttonLabel->setText(QString::number(i)); - buttonLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); - buttonLabel->setAlignment(Qt::AlignCenter); + JoystickButton* button = new JoystickButton(i, m_ui->buttonBox); // And make sure we insert BEFORE the vertical spacer. - m_ui->buttonLabelLayout->insertWidget(i, buttonLabel); - buttonLabels.append(buttonLabel); + m_ui->buttonLayout->insertWidget(i, button); + buttons.append(button); } - // 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()); -} - -void JoystickWidget::setThrottle(float thrust) -{ - m_ui->thrust->setValue(thrust*100); + // 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); + } } void JoystickWidget::setX(float x) { - m_ui->xSlider->setValue(x*100); - m_ui->xValue->display(x*100); + if (axes.size() > 0) + { + axes.at(0)->setValue(x * 100); + } } void JoystickWidget::setY(float y) { - m_ui->ySlider->setValue(y*100); - m_ui->yValue->display(y*100); + if (axes.size() > 1) + { + axes.at(1)->setValue(y * 100); + } } void JoystickWidget::setZ(float z) { - m_ui->dial->setValue(z*100); + 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); + } } void JoystickWidget::setHat(float x, float y) @@ -164,15 +189,14 @@ void JoystickWidget::setHat(float x, float y) void JoystickWidget::joystickButtonPressed(int key) { QString colorStyle = QString("QLabel { background-color: %1;}").arg(buttonLabelColor.name()); - buttonLabels.at(key)->setStyleSheet(colorStyle); + buttons.at(key)->setStyleSheet(colorStyle); } void JoystickWidget::joystickButtonReleased(int key) { - buttonLabels.at(key)->setStyleSheet(""); + buttons.at(key)->setStyleSheet(""); } void JoystickWidget::updateStatus(const QString& status) { - m_ui->statusLabel->setText(status); } diff --git a/src/ui/JoystickWidget.h b/src/ui/JoystickWidget.h index ba9f2067c7a3cf952ff8332ab63ef9ff79c3d43f..63d050cedcb2ed4c475cb39f7153342f22e9c124 100644 --- a/src/ui/JoystickWidget.h +++ b/src/ui/JoystickWidget.h @@ -35,6 +35,8 @@ This file is part of the PIXHAWK project #include #include "JoystickInput.h" #include "MainWindow.h" +#include "JoystickAxis.h" +#include "JoystickButton.h" namespace Ui { @@ -61,14 +63,14 @@ public slots: * @param yHat hat vector in left-right direction, -1 left, 0 center, +1 right */ void updateJoystick(double roll, double pitch, double yaw, double thrust, int xHat, int yHat); - /** @brief Throttle lever */ - void setThrottle(float thrust); /** @brief Back/forth movement */ void setX(float x); /** @brief Left/right movement */ void setY(float y); /** @brief Wrist rotation */ void setZ(float z); + /** @brief Throttle lever */ + void setThrottle(float thrust); /** @brief Hat switch position */ void setHat(float x, float y); /** @brief Trigger a UI change based on a button being pressed */ @@ -87,7 +89,9 @@ protected: virtual void changeEvent(QEvent *e); JoystickInput* joystick; ///< Reference to the joystick /** @brief a list of all button labels generated for this joystick. */ - QList buttonLabels; + QList buttons; + /** @brief a lit of all joystick axes generated for this joystick. */ + QList axes; /** @brief The color to use for button labels when their corresponding button is pressed */ QColor buttonLabelColor; diff --git a/src/ui/JoystickWidget.ui b/src/ui/JoystickWidget.ui index b499cda0452d3858e709ce567176016074d42fc2..2d74aa4d05213528b96420e86c9eaeec447e2f6d 100644 --- a/src/ui/JoystickWidget.ui +++ b/src/ui/JoystickWidget.ui @@ -6,10 +6,16 @@ 0 0 - 497 - 448 + 368 + 274 + + + 0 + 0 + + 368 @@ -19,7 +25,13 @@ Joystick Settings - + + + 8 + + + QLayout::SetFixedSize + 8 @@ -32,380 +44,159 @@ 8 - - 8 - - - - - - 0 - 0 - - - - - 40 - 16777215 - - - - Buttons + + + + true - - Qt::AlignCenter - - - false + + Enable joysticks - - - 1 - - - 3 - - - 3 - - - 3 - - - 3 - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - Mappings + + + + true - - Qt::AlignCenter + + + 0 + 0 + - - - - - Throttle - - - - - - - Stabilized Button - - - - - - - Auto Button - - - - - - - Manual Button - - - - - - - Pitch Axis - - - - - - - Yaw Axis - - - - - - - Roll Axis - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Stick + + + + false - - Qt::AlignCenter + + QFrame::StyledPanel - - - 6 - - - 6 - - - 6 - - - 6 + + QFrame::Sunken + + + + QLayout::SetMinimumSize - - + + + + + 0 + 0 + + - 0 + 100 0 - 40 - 24 + 16777215 + 16777215 - - QFrame::Plain - - - true - - - 3 + + Buttons - - QLCDNumber::Flat + + Qt::AlignCenter - - - - - + false - - -100 - - - 100 - - - Qt::Vertical - + + + 1 + + + QLayout::SetMinimumSize + + + 3 + + + 3 + + + 3 + + + 3 + + - - - - true + + + + + 0 + 0 + - + - 40 - 24 + 100 + 0 - - QFrame::Plain - - - true - - - 3 - - - QLCDNumber::Flat - - - - - - - false - - - -100 - - - 100 - - - - - - - Y + + Axes - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - false - - - -100 - - - 100 - - - Qt::Horizontal - - - - - - - X + Qt::AlignCenter + + + QLayout::SetMinimumSize + + - - + + + + + 0 + 0 + + + + + 50 + 0 + + - 60 + 16777215 16777215 - - Throttle + + Qt::Horizontal + + + QDialogButtonBox::Ok - - - 0 - - - 2 - - - 2 - - - 2 - - - 2 - - - - - false - - - - 40 - 20 - - - - 0 - - - Qt::Vertical - - - - - - - - - - 1 - - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Ok - - - - - - - - - buttonBox + dialogButtonsBox accepted() JoystickWidget accept()