feat(radio): refactor of switch handling to better support customisable switches (#6095)
Co-authored-by: philmoz <phil.a.mitchell@gmail.com> Co-authored-by: 3djc <3djc@gh.com> Co-authored-by: elecpower <elecpower@users.noreply.github.com>
This commit is contained in:
co-authored by
philmoz
3djc
elecpower
parent
28833f42e6
commit
0c8c39b134
@@ -193,6 +193,7 @@ class SimulatorInterface : public QObject
|
||||
void auxSerialStart(const quint8 port_num);
|
||||
void auxSerialStop(const quint8 port_num);
|
||||
void txBatteryVoltageChanged(const int voltage);
|
||||
void fsColorChange(quint8 index, qint32 color);
|
||||
};
|
||||
|
||||
class SimulatorFactory {
|
||||
|
||||
@@ -519,6 +519,9 @@ void SimulatorWidget::setupRadioWidgets()
|
||||
case RadioWidget::RADIO_WIDGET_KNOB :
|
||||
ui->radioWidgetsHTLayout->removeWidget(rw);
|
||||
break;
|
||||
case RadioWidget::RADIO_WIDGET_FUNC_SWITCH:
|
||||
ui->radioWidgetsCSLayout->removeWidget(rw);
|
||||
break;
|
||||
case RadioWidget::RADIO_WIDGET_FADER :
|
||||
case RadioWidget::RADIO_WIDGET_TRIM :
|
||||
ui->VCGridLayout->removeWidget(rw);
|
||||
@@ -534,22 +537,36 @@ void SimulatorWidget::setupRadioWidgets()
|
||||
|
||||
// Now set up new widgets.
|
||||
|
||||
// switches
|
||||
Board::SwitchType swcfg;
|
||||
for (i = 0; i < ttlSwitches; ++i) {
|
||||
if (!radioSettings.isSwitchAvailable(i) || Boards::isSwitchFunc(i))
|
||||
continue;
|
||||
|
||||
swcfg = Board::SwitchType(radioSettings.switchConfig[i].type);
|
||||
wname = RawSource(RawSourceType::SOURCE_TYPE_SWITCH, i + 1).toString(nullptr, &radioSettings, Board::BOARD_UNKNOWN, false);
|
||||
RadioSwitchWidget * sw = new RadioSwitchWidget(swcfg, wname, -1, ui->radioWidgetsHT);
|
||||
sw->setIndex(i);
|
||||
ui->radioWidgetsHTLayout->addWidget(sw);
|
||||
|
||||
m_radioWidgets.append(sw);
|
||||
if (!Boards::getCapability(m_board, Board::FunctionSwitches)) {
|
||||
ui->radioWidgetsCS->hide();
|
||||
}
|
||||
|
||||
midpos = (int)floorf(m_radioWidgets.size() / 2.0f);
|
||||
// switches
|
||||
int swCnt = 0;
|
||||
for (i = 0; i < ttlSwitches; ++i) {
|
||||
if (radioSettings.isSwitchAvailable(i)) {
|
||||
wname = RawSource(RawSourceType::SOURCE_TYPE_SWITCH, i + 1).toString(nullptr, &radioSettings, Board::BOARD_UNKNOWN, false);
|
||||
RadioWidget * sw;
|
||||
Board::SwitchType swcfg;
|
||||
|
||||
if (Boards::isSwitchFunc(i)) {
|
||||
swcfg = Board::SWITCH_2POS; // TODO: get this from model settings
|
||||
sw = new RadioFuncSwitchWidget(simulator, swcfg, wname, -1, ui->radioWidgetsCS);
|
||||
ui->radioWidgetsCSLayout->addWidget(sw);
|
||||
} else {
|
||||
swcfg = Board::SwitchType(radioSettings.switchConfig[i].type);
|
||||
sw = new RadioSwitchWidget(swcfg, wname, -1, ui->radioWidgetsHT);
|
||||
ui->radioWidgetsHTLayout->addWidget(sw);
|
||||
swCnt += 1;
|
||||
}
|
||||
|
||||
sw->setIndex(i);
|
||||
|
||||
m_radioWidgets.append(sw);
|
||||
}
|
||||
}
|
||||
|
||||
midpos = (int)floorf(swCnt / 2.0f);
|
||||
|
||||
// pots in middle of switches
|
||||
for (i = 0; i < ttlInputs; ++i) {
|
||||
@@ -746,7 +763,7 @@ void SimulatorWidget::onPhaseChanged(qint32 phase, const QString & name)
|
||||
|
||||
void SimulatorWidget::onRadioWidgetValueChange(const RadioWidget::RadioWidgetType type, int index, int value)
|
||||
{
|
||||
//qDebug() << type << index << value;
|
||||
// qDebug() << type << index << value;
|
||||
if (!simulator || index < 0)
|
||||
return;
|
||||
|
||||
@@ -765,6 +782,10 @@ void SimulatorWidget::onRadioWidgetValueChange(const RadioWidget::RadioWidgetTyp
|
||||
inpType = SimulatorInterface::INPUT_SRC_SWITCH;
|
||||
break;
|
||||
|
||||
case RadioWidget::RADIO_WIDGET_FUNC_SWITCH:
|
||||
inpType = SimulatorInterface::INPUT_SRC_SWITCH;
|
||||
break;
|
||||
|
||||
case RadioWidget::RADIO_WIDGET_KNOB :
|
||||
inpType = SimulatorInterface::INPUT_SRC_KNOB;
|
||||
break;
|
||||
|
||||
@@ -114,7 +114,43 @@
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2">
|
||||
<item row="1" column="0" colspan="2" alignment="Qt::AlignBottom">
|
||||
<widget class="QFrame" name="radioWidgetsCS">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::Panel</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Raised</enum>
|
||||
</property>
|
||||
<property name="midLineWidth">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="radioWidgetsCSLayout">
|
||||
<property name="spacing">
|
||||
<number>10</number>
|
||||
</property>
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>9</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>9</number>
|
||||
</property>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<widget class="QFrame" name="controlsLower">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
|
||||
@@ -24,8 +24,10 @@
|
||||
#include "radiowidget.h"
|
||||
#include "boards.h"
|
||||
#include "simulator.h"
|
||||
#include "simulatorinterface.h"
|
||||
|
||||
#include <QSlider>
|
||||
#include <QPushButton>
|
||||
#include <QTimer>
|
||||
#include <QToolButton>
|
||||
|
||||
@@ -143,5 +145,62 @@ class RadioSwitchWidget : public RadioWidget
|
||||
|
||||
QSlider * m_slider;
|
||||
quint16 m_stepSize;
|
||||
};
|
||||
|
||||
class RadioFuncSwitchWidget : public RadioWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit RadioFuncSwitchWidget(SimulatorInterface *simulator, Board::SwitchType type, const QString & labelText, int value, QWidget * parent, Qt::WindowFlags f = Qt::WindowFlags()) :
|
||||
RadioWidget(labelText, value, parent, f)
|
||||
{
|
||||
init(simulator, type);
|
||||
}
|
||||
|
||||
void init(SimulatorInterface *simulator, Board::SwitchType swType)
|
||||
{
|
||||
m_type = RADIO_WIDGET_FUNC_SWITCH;
|
||||
|
||||
m_button = new QPushButton("");
|
||||
m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
|
||||
m_button->setFixedWidth(30);
|
||||
|
||||
setWidget(m_button);
|
||||
|
||||
// TODO: connect custom function switches to model data?
|
||||
|
||||
connect(m_button, &QPushButton::pressed, this, &RadioFuncSwitchWidget::onPressed);
|
||||
connect(m_button, &QPushButton::released, this, &RadioFuncSwitchWidget::onReleased);
|
||||
connect(this, &RadioWidget::valueChanged, m_button, [=](int value) {
|
||||
m_value = value;
|
||||
});
|
||||
connect(simulator, &SimulatorInterface::fsColorChange, this, &RadioFuncSwitchWidget::onFsColorChange);
|
||||
}
|
||||
|
||||
void onPressed()
|
||||
{
|
||||
setValue(1);
|
||||
}
|
||||
|
||||
void onReleased()
|
||||
{
|
||||
setValue(-1);
|
||||
}
|
||||
|
||||
private slots:
|
||||
void onFsColorChange(quint8 index, qint32 color)
|
||||
{
|
||||
if (index == m_index && color != lastColor) {
|
||||
lastColor = color;
|
||||
QColor c = QColor((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
|
||||
QString qss = QString("background-color: %1; border: none;").arg(c.name());
|
||||
m_button->setStyleSheet(qss);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
QPushButton * m_button;
|
||||
qint32 lastColor = -1;
|
||||
};
|
||||
|
||||
@@ -46,6 +46,7 @@ class RadioWidget : public QWidget
|
||||
RADIO_WIDGET_TRIM, // trim axis, usually 2 buttons & slider
|
||||
RADIO_WIDGET_STICK, // actually one axis of a stick
|
||||
RADIO_WIDGET_KEY, // UI key/pushbutton
|
||||
RADIO_WIDGET_FUNC_SWITCH,
|
||||
RADIO_WIDGET_ENUM_COUNT
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user