[Simulator] Further Simulator improvements. (#4282)

* Refactor CompStoreObj to use templates. Clean up some formatting.

* [Simulator] Add SimulatorOptions type to replace individual simulator options; Fix storage handler for custom types; Cleanup some redundant code.

* [Simulator][Joystick] Rework the joystick options/calibration dialog.  Can now enable/disable/select the Joystick from there. Delay start of calibration until Start is pressed (otherwise looks like last calibration didn't work). And generally refactor a lot of it to be smarter.

* Cosmetics.

* [Simulator] Add state handling for RadioWidget class. Split code to cpp file.

* [Simulator] Add SimulatorStartupDialog as its own UI class.

* [Simulator] Multiple changes and new features:
    All Horus data source options are now fully supported with data properly saved after use (SD path, .otx file, or custom data path);
    Loading .otx files now works for all radio types (but not saving data back to .otx);
    Switch and knob/slider states are now saved to per radio profile;
    Joystick configuration/calibration is now available directly from inside Simulator;
    Due to refactoring, all previously saved per-profile simulator settings are now invalid;
    Further consolidated simulator dialog startup/shutdown, now handles temp. paths internally, etc;
    With WIN_USE_CONSOLE defined, Simulator and Companion when started from Windows command line will now use that console for all messages (instead of opening a new one). This allows full debug output to be seen at all stages of execution and after exit.

* [Simulator] Fix build w/out SDL Joystick support.

* [Simulator] Fix bogus throttle warning on simulator start; Add forgotten RadioWidgetState flags to stream handlers; Add debug state saver in SimulatorOptions.
This commit is contained in:
Max Paperno
2017-01-25 16:20:45 +01:00
committed by Bertrand Songis
parent b53ec7392c
commit 6e2474081f
22 changed files with 2869 additions and 1785 deletions
@@ -30,12 +30,12 @@ class RadioFaderWidget : public RadioWidget
public:
explicit RadioFaderWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
explicit RadioFaderWidget(QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(parent, f)
{
init();
}
explicit RadioFaderWidget(const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
explicit RadioFaderWidget(const QString & labelText, int value = 0, QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(labelText, value, parent, f)
{
init();
@@ -22,7 +22,7 @@
#define _RADIOKNOBWIDGET_H_
#include "radiowidget.h"
#include "radiodata.h"
#include "boards.h"
#include <QDial>
#include <QMouseEvent>
@@ -68,7 +68,7 @@ class RadioKnobWidget : public RadioWidget
class KnobWidget : public QDial
{
friend class RadioKnobWidget;
friend class RadioKnobWidget;
public:
@@ -22,7 +22,7 @@
#define _RADIOSWITCHWIDGET_H_
#include "radiowidget.h"
#include "radiodata.h"
#include "boards.h"
#include <QSlider>
#include <QDebug>
@@ -33,15 +33,15 @@ class RadioSwitchWidget : public RadioWidget
public:
explicit RadioSwitchWidget(Board::SwitchType type = Board::SWITCH_3POS, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
explicit RadioSwitchWidget(Board::SwitchType type = Board::SWITCH_3POS, QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(parent, f),
switchConfig(type)
swType(type)
{
init();
}
explicit RadioSwitchWidget(Board::SwitchType type, const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
explicit RadioSwitchWidget(Board::SwitchType type, const QString & labelText, int value = 0, QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(labelText, value, parent, f),
switchConfig(type)
swType(type)
{
init();
}
@@ -58,7 +58,7 @@ class RadioSwitchWidget : public RadioWidget
sl->setInvertedControls(true);
sl->setTickPosition(QSlider::TicksBothSides);
sl->setPageStep(1);
sl->setMinimum((switchConfig == Board::SWITCH_3POS ? -1 : 0));
sl->setMinimum((swType == Board::SWITCH_3POS ? -1 : 0));
sl->setMaximum(1);
sl->setTickInterval(1);
sl->setValue(m_value);
@@ -70,7 +70,7 @@ class RadioSwitchWidget : public RadioWidget
}
private:
Board::SwitchType switchConfig;
Board::SwitchType swType;
};
@@ -0,0 +1,201 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "radiowidget.h"
RadioWidget::RadioWidget(QWidget * parent, Qt::WindowFlags f) :
QWidget(parent, f),
m_value(0),
m_flags(0),
m_invertValue(false),
m_showLabel(false),
m_labelText(""),
m_type(RADIO_WIDGET_NONE)
{
init();
}
RadioWidget::RadioWidget(const QString & labelText, int value, QWidget * parent, Qt::WindowFlags f) :
QWidget(parent, f),
m_value(value),
m_flags(0),
m_invertValue(false),
m_showLabel(true),
m_labelText(labelText),
m_type(RADIO_WIDGET_NONE)
{
init();
}
void RadioWidget::setIndex(int index)
{
m_index = index;
}
void RadioWidget::setInvertValue(bool invertValue)
{
m_invertValue = invertValue;
}
void RadioWidget::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(m_value);
}
}
void RadioWidget::setFlags(quint16 flags)
{
if (flags != m_flags) {
m_flags = flags;
emit flagsChanged(flags);
}
}
void RadioWidget::setShowLabel(bool show)
{
if (show != m_showLabel) {
m_showLabel = show;
addLabel();
}
}
void RadioWidget::setLabelText(const QString & labelText, bool showLabel)
{
m_labelText = labelText;
m_showLabel = showLabel;
addLabel();
}
void RadioWidget::setStateData(const QByteArray & data)
{
RadioWidgetState state;
QDataStream stream(data);
stream >> state;
//setIndex(state.index);
setValue(state.value);
setFlags(state.flags);
}
void RadioWidget::changeVisibility(bool visible)
{
static QSizePolicy oldSP = sizePolicy();
setVisible(visible);
if (visible) {
setSizePolicy(oldSP);
}
else {
oldSP = sizePolicy();
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
}
}
int RadioWidget::getValue() const
{
return m_value * (m_invertValue ? -1 : 1);
}
int RadioWidget::getIndex() const
{
return m_index;
}
int RadioWidget::getType() const
{
return m_type;
}
QByteArray RadioWidget::getStateData() const
{
QByteArray ba;
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << getState();
return ba;
}
RadioWidget::RadioWidgetState RadioWidget::getState() const
{
return RadioWidgetState(quint8(m_type), qint8(m_index), qint16(m_value));;
}
void RadioWidget::init()
{
setIndex(0);
m_controlWidget = NULL;
m_nameLabel = NULL;
m_gridLayout= new QGridLayout(this);
m_gridLayout->setContentsMargins(0, 0, 0, 0);
m_gridLayout->setVerticalSpacing(4);
m_gridLayout->setHorizontalSpacing(0);
addLabel();
}
void RadioWidget::addLabel()
{
if (m_nameLabel) {
m_gridLayout->removeWidget(m_nameLabel);
m_nameLabel->deleteLater();
}
if (m_showLabel && !m_labelText.isEmpty()) {
m_nameLabel = new QLabel(m_labelText, this);
m_nameLabel->setAlignment(Qt::AlignCenter);
m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
m_gridLayout->addWidget(m_nameLabel, 1, 0, 1, 1, Qt::AlignTop);
m_gridLayout->setRowStretch(1, 0);
}
}
void RadioWidget::setWidget(QWidget * widget)
{
if (m_controlWidget) {
m_gridLayout->removeWidget(m_controlWidget);
m_controlWidget->deleteLater();
}
m_controlWidget = widget;
if (widget) {
m_gridLayout->addWidget(widget, 0, 0, 1, 1, Qt::AlignHCenter);
m_gridLayout->setRowStretch(0, 1);
}
}
QDataStream & operator << (QDataStream &out, const RadioWidget::RadioWidgetState & o)
{
out << o._version << o.type << o.index << o.value << o.flags;
return out;
}
QDataStream & operator >> (QDataStream &in, RadioWidget::RadioWidgetState & o)
{
if (o._version <= RADIO_WIDGET_STATE_VERSION)
in >> o._version >> o.type >> o.index >> o.value >> o.flags;
return in;
}
QDebug operator << (QDebug d, const RadioWidget::RadioWidgetState & o)
{
QDebugStateSaver saver(d);
d << "RadioWidget::RadioWidgetState: type=" << o.type << "; index=" << o.index
<< "; value=" << o.value << "; flags=0x" << hex << o.flags;
return d;
}
+53 -106
View File
@@ -21,146 +21,93 @@
#ifndef _RADIOWIDGET_H_
#define _RADIOWIDGET_H_
#include <QWidget>
#include <QLabel>
#include <QGridLayout>
#include <QDebug>
#include <QLabel>
#include <QWidget>
#define RADIO_WIDGET_STATE_VERSION 1
class RadioWidget : public QWidget
{
Q_OBJECT
public:
enum RadioWidgetType { RADIO_WIDGET_NONE, RADIO_WIDGET_SWITCH, RADIO_WIDGET_KNOB, RADIO_WIDGET_FADER };
explicit RadioWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
QWidget(parent, f),
m_value(0),
m_invertValue(false),
m_showLabel(false),
m_labelText(""),
m_type(RADIO_WIDGET_NONE)
{
init();
}
explicit RadioWidget(const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
QWidget(parent, f),
m_value(value),
m_invertValue(false),
m_showLabel(true),
m_labelText(labelText),
m_type(RADIO_WIDGET_NONE)
{
init();
}
// These are saved to persistent user settings in RadioWidgetState,
// so only append entires here, do not change index value if it can be avoided.
enum RadioWidgetType {
RADIO_WIDGET_NONE = 0,
RADIO_WIDGET_SWITCH,
RADIO_WIDGET_KNOB,
RADIO_WIDGET_FADER,
RADIO_WIDGET_STICK // actually one axis of a stick
};
void setIndex(int index) { m_index = index; }
void setInvertValue(bool invertValue) { m_invertValue = invertValue; }
struct RadioWidgetState {
public:
RadioWidgetState(quint8 type = 0, qint8 index = 0, qint16 value = 0, quint16 flags = 0) :
type(type), index(index), value(value), flags(flags),
_version(RADIO_WIDGET_STATE_VERSION)
{ }
void setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(m_value);
}
}
quint8 type;
qint8 index;
qint16 value;
quint16 flags;
void setShowLabel(bool show)
{
if (show != m_showLabel) {
m_showLabel = show;
addLabel();
}
}
friend QDataStream & operator << (QDataStream &out, const RadioWidgetState & o);
friend QDataStream & operator >> (QDataStream &in, RadioWidgetState & o);
friend QDebug operator << (QDebug d, const RadioWidgetState & o);
void setLabelText(const QString &labelText, bool showLabel = true)
{
m_labelText = labelText;
m_showLabel = showLabel;
addLabel();
}
private:
quint8 _version = RADIO_WIDGET_STATE_VERSION; // structure definition version
};
void changeVisibility(bool visible)
{
static QSizePolicy oldSP = sizePolicy();
setVisible(visible);
if (visible) {
setSizePolicy(oldSP);
}
else {
oldSP = sizePolicy();
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
}
}
explicit RadioWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
explicit RadioWidget(const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
int getValue() const { return m_value * (m_invertValue ? -1 : 1); }
int getIndex() const { return m_index; }
int getType() const { return m_type; }
void setIndex(int index);
void setInvertValue(bool invertValue);
void setValue(int value);
void setFlags(quint16 flags);
void setShowLabel(bool show);
void setLabelText(const QString &labelText, bool showLabel = true);
void setStateData(const QByteArray & data);
void changeVisibility(bool visible);
int getValue() const;
int getIndex() const;
int getType() const;
QByteArray getStateData() const;
RadioWidgetState getState() const;
protected:
void init()
{
setIndex(0);
m_controlWidget = NULL;
m_nameLabel = NULL;
m_gridLayout= new QGridLayout(this);
m_gridLayout->setContentsMargins(0, 0, 0, 0);
m_gridLayout->setVerticalSpacing(4);
m_gridLayout->setHorizontalSpacing(0);
addLabel();
}
void addLabel()
{
if (m_nameLabel) {
m_gridLayout->removeWidget(m_nameLabel);
m_nameLabel->deleteLater();
}
if (m_showLabel && !m_labelText.isEmpty()) {
m_nameLabel = new QLabel(m_labelText, this);
m_nameLabel->setAlignment(Qt::AlignCenter);
m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
m_gridLayout->addWidget(m_nameLabel, 1, 0, 1, 1, Qt::AlignTop);
m_gridLayout->setRowStretch(1, 0);
}
}
void setWidget(QWidget * widget = NULL)
{
if (m_controlWidget) {
m_gridLayout->removeWidget(m_controlWidget);
m_controlWidget->deleteLater();
}
m_controlWidget = widget;
if (widget) {
m_gridLayout->addWidget(widget, 0, 0, 1, 1, Qt::AlignHCenter);
m_gridLayout->setRowStretch(0, 1);
}
}
void init();
void addLabel();
void setWidget(QWidget * widget = NULL);
int m_value;
int m_index;
quint16 m_flags;
bool m_invertValue;
bool m_showLabel;
QString m_labelText;
RadioWidgetType m_type;
private:
QGridLayout * m_gridLayout;
QWidget * m_controlWidget;
QLabel * m_nameLabel;
signals:
void valueChanged(int m_value);
void flagsChanged(quint16 flags);
};
#endif // _RADIOWIDGET_H_