Commit 295b8a25 authored by Bryant's avatar Bryant

The joystick settings window has now been reworked to be a fixed minimum size...

The joystick settings window has now been reworked to be a fixed minimum size and dynamically scale with both the number of buttons and axes on a given joystick/controller. Mapping functionality is incomplete at this point still, though the UI exists for it.
parent f4c1eaab
......@@ -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
......
......@@ -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;
}
......
......@@ -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;
......
#include "JoystickAxis.h"
#include "ui_JoystickAxis.h"
#include <QString>
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);
}
#ifndef JOYSTICKAXIS_H
#define JOYSTICKAXIS_H
#include <QWidget>
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
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>JoystickAxis</class>
<widget class="QWidget" name="JoystickAxis">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>40</width>
<height>200</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>40</width>
<height>200</height>
</size>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<property name="leftMargin">
<number>1</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string>--</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<property name="value">
<number>0</number>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
#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;
}
#ifndef JOYSTICKBUTTON_H
#define JOYSTICKBUTTON_H
#include <QWidget>
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
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>JoystickButton</class>
<widget class="QWidget" name="JoystickButton">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>125</width>
<height>27</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<property name="leftMargin">
<number>2</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>1</number>
</property>
<item>
<widget class="QComboBox" name="joystickAction">
<item>
<property name="text">
<string>--</string>
</property>
</item>
<item>
<property name="text">
<string>Yaw</string>
</property>
</item>
<item>
<property name="text">
<string>Pitch</string>
</property>
</item>
<item>
<property name="text">
<string>Roll</string>
</property>
</item>
<item>
<property name="text">
<string>Throttle</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QLabel" name="joystickButtonLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
#include "JoystickWidget.h"
#include "MainWindow.h"
#include "ui_JoystickWidget.h"
#include "JoystickButton.h"
#include "JoystickAxis.h"
#include <QDebug>
#include <QDesktopWidget>
......@@ -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);
}
......@@ -35,6 +35,8 @@ This file is part of the PIXHAWK project
#include <QLabel>
#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<QLabel*> buttonLabels;
QList<JoystickButton*> buttons;
/** @brief a lit of all joystick axes generated for this joystick. */
QList<JoystickAxis*> axes;
/** @brief The color to use for button labels when their corresponding button is pressed */
QColor buttonLabelColor;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment