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
@@ -23,6 +23,7 @@
|
||||
#include "ui_setup_function_switches.h"
|
||||
#include "namevalidator.h"
|
||||
#include "autolineedit.h"
|
||||
#include <QColorDialog>
|
||||
|
||||
#include <QDir>
|
||||
|
||||
@@ -42,13 +43,16 @@ bool FilteredGroupSwitchesModel::filterAcceptsRow(int sourceRow, const QModelInd
|
||||
bool FilteredSwitchGroupsModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
|
||||
{
|
||||
if (sourceRow == 0) return true;
|
||||
if (m_model->getFuncSwitchConfig(m_switch) == ModelData::FUNC_SWITCH_CONFIG_NONE) return false;
|
||||
if (m_model->getFuncSwitchConfig(m_switch) == Board::SWITCH_NOT_AVAILABLE) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FilteredSwitchConfigsModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
|
||||
{
|
||||
if (sourceRow == ModelData::FUNC_SWITCH_CONFIG_NONE || sourceRow == ModelData::FUNC_SWITCH_CONFIG_2POS) return true;
|
||||
if (sourceRow == Board::SWITCH_3POS)
|
||||
return false;
|
||||
if (sourceRow == Board::SWITCH_NOT_AVAILABLE || sourceRow == Board::SWITCH_2POS || sourceRow == Board::SWITCH_GLOBAL)
|
||||
return true;
|
||||
return !m_model->getFuncSwitchAlwaysOnGroupForSwitch(m_switch);
|
||||
}
|
||||
|
||||
@@ -70,51 +74,122 @@ FunctionSwitchesPanel::FunctionSwitchesPanel(QWidget * parent, ModelData & model
|
||||
|
||||
fsGroupStart = ModelData::funcSwitchGroupStartSwitchModel(switchcnt);
|
||||
|
||||
for (int i = 0; i < switchcnt; i++) {
|
||||
QLabel * lblSwitchId = new QLabel(this);
|
||||
lblSwitchId->setText(tr("SW%1").arg(i + 1));
|
||||
|
||||
AutoLineEdit * aleName = new AutoLineEdit(this);
|
||||
aleName->setProperty("index", i);
|
||||
aleName->setValidator(new NameValidator(board, this));
|
||||
aleName->setField((char *)model.functionSwitchNames[i], 3);
|
||||
|
||||
QComboBox * cboConfig = new QComboBox(this);
|
||||
cboConfig->setProperty("index", i);
|
||||
auto configFilter = new FilteredSwitchConfigsModel(fsConfig, i, &model);
|
||||
cboConfig->setModel(configFilter);
|
||||
filterSwitchConfigs << configFilter;
|
||||
|
||||
QComboBox * cboStartPosn = new QComboBox(this);
|
||||
cboStartPosn->setProperty("index", i);
|
||||
cboStartPosn->setModel(fsStart);
|
||||
|
||||
QComboBox * cboGroup = new QComboBox(this);
|
||||
cboGroup->setProperty("index", i);
|
||||
auto groupFilter = new FilteredSwitchGroupsModel(fsGroups, i, &model);
|
||||
cboGroup->setModel(groupFilter);
|
||||
filterSwitchGroups << groupFilter;
|
||||
|
||||
int row = 0;
|
||||
int coloffset = 1;
|
||||
ui->gridSwitches->addWidget(lblSwitchId, row++, i + coloffset);
|
||||
ui->gridSwitches->addWidget(aleName, row++, i + coloffset);
|
||||
ui->gridSwitches->addWidget(cboConfig, row++, i + coloffset);
|
||||
ui->gridSwitches->addWidget(cboStartPosn, row++, i + coloffset);
|
||||
ui->gridSwitches->addWidget(cboGroup, row++, i + coloffset);
|
||||
|
||||
connect(aleName, &AutoLineEdit::currentDataChanged, this, &FunctionSwitchesPanel::on_nameEditingFinished);
|
||||
connect(cboConfig, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FunctionSwitchesPanel::on_configCurrentIndexChanged);
|
||||
connect(cboStartPosn, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FunctionSwitchesPanel::on_startPosnCurrentIndexChanged);
|
||||
connect(cboGroup, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FunctionSwitchesPanel::on_groupChanged);
|
||||
|
||||
aleNames << aleName;
|
||||
cboConfigs << cboConfig;
|
||||
cboStartupPosns << cboStartPosn;
|
||||
cboGroups << cboGroup;
|
||||
if (Boards::getCapability(board, Board::FunctionSwitchColors)) {
|
||||
QLabel * lblOffColor = new QLabel(this);
|
||||
lblOffColor->setText(tr("Off color"));
|
||||
ui->gridSwitches->addWidget(lblOffColor, 5, 0);
|
||||
QLabel * lblOffLua = new QLabel(this);
|
||||
lblOffLua->setText(tr(" Allow Lua override"));
|
||||
ui->gridSwitches->addWidget(lblOffLua, 6, 0);
|
||||
QLabel * lblOnColor = new QLabel(this);
|
||||
lblOnColor->setText(tr("On color"));
|
||||
ui->gridSwitches->addWidget(lblOnColor, 7, 0);
|
||||
QLabel * lblOnLua = new QLabel(this);
|
||||
lblOnLua->setText(tr(" Allow Lua override"));
|
||||
ui->gridSwitches->addWidget(lblOnLua, 8, 0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i += 1) {
|
||||
for (int sw = 0, col = 0; sw < Boards::getCapability(board, Board::Switches); sw++) {
|
||||
int i = Boards::getCFSIndexForSwitch(sw);
|
||||
if (i >= 0) {
|
||||
QLabel * lblSwitchId = new QLabel(this);
|
||||
lblSwitchId->setText(Boards::getSwitchName(sw));
|
||||
|
||||
AutoLineEdit * aleName = new AutoLineEdit(this);
|
||||
aleName->setProperty("index", i);
|
||||
aleName->setValidator(new NameValidator(board, this));
|
||||
aleName->setField((char *)model.customSwitches[i].name, 3);
|
||||
|
||||
QComboBox * cboConfig = new QComboBox(this);
|
||||
cboConfig->setProperty("index", i);
|
||||
auto configFilter = new FilteredSwitchConfigsModel(fsConfig, i, &model);
|
||||
cboConfig->setModel(configFilter);
|
||||
filterSwitchConfigs << configFilter;
|
||||
|
||||
QComboBox * cboStartPosn = new QComboBox(this);
|
||||
cboStartPosn->setProperty("index", i);
|
||||
cboStartPosn->setModel(fsStart);
|
||||
|
||||
QComboBox * cboGroup = new QComboBox(this);
|
||||
cboGroup->setProperty("index", i);
|
||||
auto groupFilter = new FilteredSwitchGroupsModel(fsGroups, i, &model);
|
||||
cboGroup->setModel(groupFilter);
|
||||
filterSwitchGroups << groupFilter;
|
||||
|
||||
int row = 0;
|
||||
int coloffset = 1;
|
||||
ui->gridSwitches->addWidget(lblSwitchId, row++, col + coloffset);
|
||||
ui->gridSwitches->addWidget(aleName, row++, col + coloffset);
|
||||
ui->gridSwitches->addWidget(cboConfig, row++, col + coloffset);
|
||||
ui->gridSwitches->addWidget(cboStartPosn, row++, col + coloffset);
|
||||
ui->gridSwitches->addWidget(cboGroup, row++, col + coloffset);
|
||||
|
||||
if (Boards::getCapability(board, Board::FunctionSwitchColors)) {
|
||||
const QString qss = QString("border-style: outset; border-width: 2px; border-radius: 5px; border-color: darkgrey; padding: 2px; background-color: %1;");
|
||||
QPushButton * btnOffColor = new QPushButton(tr(""));
|
||||
QColor off = this->model->customSwitches[i].offColor.getQColor();
|
||||
btnOffColor->setStyleSheet(QString(qss).arg(off.name()));
|
||||
connect(btnOffColor, &QPushButton::clicked, [=]() {
|
||||
QColorDialog *dlg = new QColorDialog();
|
||||
QColor color = dlg->getColor(this->model->customSwitches[i].offColor.getQColor());
|
||||
if (color.isValid()) {
|
||||
this->model->customSwitches[i].offColor.setColor(color.red(), color.green(), color.blue());
|
||||
btnOffColor->setStyleSheet(QString(qss).arg(color.name()));
|
||||
emit modified();
|
||||
}
|
||||
});
|
||||
ui->gridSwitches->addWidget(btnOffColor, row++, col + coloffset);
|
||||
btnOffColors << btnOffColor;
|
||||
|
||||
QCheckBox * cbOffLuaOverride = new QCheckBox(this);
|
||||
cbOffLuaOverride->setChecked(this->model->customSwitches[i].offColorLuaOverride);
|
||||
connect(cbOffLuaOverride, &QCheckBox::toggled, [=](int value) {
|
||||
this->model->customSwitches[i].offColorLuaOverride = value;
|
||||
emit modified();
|
||||
});
|
||||
ui->gridSwitches->addWidget(cbOffLuaOverride, row++, col + coloffset);
|
||||
cbOffLuaOverrides << cbOffLuaOverride;
|
||||
|
||||
QPushButton * btnOnColor = new QPushButton(tr(""));
|
||||
QColor on = this->model->customSwitches[i].onColor.getQColor();
|
||||
btnOnColor->setStyleSheet(QString(qss).arg(on.name()));
|
||||
connect(btnOnColor, &QPushButton::clicked, [=]() {
|
||||
QColorDialog *dlg = new QColorDialog();
|
||||
QColor color = dlg->getColor(this->model->customSwitches[i].onColor.getQColor());
|
||||
if (color.isValid()) {
|
||||
this->model->customSwitches[i].onColor.setColor(color.red(), color.green(), color.blue());
|
||||
btnOnColor->setStyleSheet(QString(qss).arg(color.name()));
|
||||
emit modified();
|
||||
}
|
||||
});
|
||||
ui->gridSwitches->addWidget(btnOnColor, row++, col + coloffset);
|
||||
btnOnColors << btnOnColor;
|
||||
|
||||
QCheckBox * cbOnLuaOverride = new QCheckBox(this);
|
||||
cbOnLuaOverride->setChecked(this->model->customSwitches[i].onColorLuaOverride);
|
||||
connect(cbOnLuaOverride, &QCheckBox::toggled, [=](int value) {
|
||||
this->model->customSwitches[i].onColorLuaOverride = value;
|
||||
emit modified();
|
||||
});
|
||||
ui->gridSwitches->addWidget(cbOnLuaOverride, row++, col + coloffset);
|
||||
cbOnLuaOverrides << cbOnLuaOverride;
|
||||
}
|
||||
|
||||
connect(aleName, &AutoLineEdit::currentDataChanged, this, &FunctionSwitchesPanel::on_nameEditingFinished);
|
||||
connect(cboConfig, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FunctionSwitchesPanel::on_configCurrentIndexChanged);
|
||||
connect(cboStartPosn, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FunctionSwitchesPanel::on_startPosnCurrentIndexChanged);
|
||||
connect(cboGroup, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FunctionSwitchesPanel::on_groupChanged);
|
||||
|
||||
aleNames << aleName;
|
||||
cboConfigs << cboConfig;
|
||||
cboStartupPosns << cboStartPosn;
|
||||
cboGroups << cboGroup;
|
||||
|
||||
col += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < Boards::getCapability(board, Board::FunctionSwitchGroups); i += 1) {
|
||||
QLabel * lblGroupId = new QLabel(this);
|
||||
lblGroupId->setText(tr("Group %1").arg(i + 1));
|
||||
|
||||
@@ -154,22 +229,37 @@ void FunctionSwitchesPanel::update()
|
||||
{
|
||||
lock = true;
|
||||
|
||||
for (int i = 0; i < switchcnt; i++) {
|
||||
filterSwitchConfigs[i]->invalidate();
|
||||
filterSwitchGroups[i]->invalidate();
|
||||
Board::Type board = firmware->getBoard();
|
||||
|
||||
aleNames[i]->update();
|
||||
cboConfigs[i]->setCurrentIndex(filterSwitchConfigs[i]->mapFromSource(fsConfig->index(model->getFuncSwitchConfig(i), 0)).row());
|
||||
cboStartupPosns[i]->setCurrentIndex(model->getFuncSwitchStart(i));
|
||||
unsigned int grp = model->getFuncSwitchGroup(i);
|
||||
cboGroups[i]->setCurrentIndex(grp);
|
||||
for (int sw = 0, col = 0; sw < Boards::getCapability(firmware->getBoard(), Board::Switches); sw++) {
|
||||
int i = Boards::getCFSIndexForSwitch(sw);
|
||||
if (i >= 0) {
|
||||
filterSwitchConfigs[col]->invalidate();
|
||||
filterSwitchGroups[col]->invalidate();
|
||||
|
||||
cboStartupPosns[i]->setEnabled(cboConfigs[i]->currentIndex() >= ModelData::FUNC_SWITCH_CONFIG_2POS && (grp == 0));
|
||||
unsigned int cfg = model->getFuncSwitchConfig(i);
|
||||
unsigned int grp = model->getFuncSwitchGroup(i);
|
||||
|
||||
cboGroups[i]->setEnabled(cboConfigs[i]->currentIndex() >= ModelData::FUNC_SWITCH_CONFIG_TOGGLE);
|
||||
aleNames[col]->update();
|
||||
cboConfigs[col]->setCurrentIndex(filterSwitchConfigs[col]->mapFromSource(fsConfig->index(cfg, 0)).row());
|
||||
cboStartupPosns[col]->setCurrentIndex(model->getFuncSwitchStart(i));
|
||||
cboGroups[col]->setCurrentIndex(grp);
|
||||
|
||||
cboStartupPosns[col]->setEnabled(cfg == Board::SWITCH_2POS && (grp == 0));
|
||||
cboGroups[col]->setEnabled(cfg >= Board::SWITCH_TOGGLE && cfg < Board::SWITCH_GLOBAL);
|
||||
|
||||
if (Boards::getCapability(board, Board::FunctionSwitchColors)) {
|
||||
btnOffColors[col]->setEnabled(cfg != Board::SWITCH_NOT_AVAILABLE && cfg != Board::SWITCH_GLOBAL);
|
||||
cbOffLuaOverrides[col]->setEnabled(cfg != Board::SWITCH_NOT_AVAILABLE && cfg != Board::SWITCH_GLOBAL);
|
||||
btnOnColors[col]->setEnabled(cfg != Board::SWITCH_NOT_AVAILABLE && cfg != Board::SWITCH_GLOBAL);
|
||||
cbOnLuaOverrides[col]->setEnabled(cfg != Board::SWITCH_NOT_AVAILABLE && cfg != Board::SWITCH_GLOBAL);
|
||||
}
|
||||
|
||||
col += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i += 1) {
|
||||
for (int i = 0; i < Boards::getCapability(board, Board::FunctionSwitchGroups); i += 1) {
|
||||
filterGroupSwitches[i]->invalidate();
|
||||
|
||||
model->setGroupSwitchState(i + 1, switchcnt);
|
||||
@@ -200,10 +290,10 @@ void FunctionSwitchesPanel::on_configCurrentIndexChanged(int index)
|
||||
unsigned int sw = sender()->property("index").toInt(&ok);
|
||||
unsigned int config = filterSwitchConfigs[sw]->mapToSource(filterSwitchConfigs[sw]->index(index, 0)).row();
|
||||
if (ok && model->getFuncSwitchConfig(sw) != config) {
|
||||
model->setFuncSwitchConfig(sw, config);
|
||||
if (config != ModelData::FUNC_SWITCH_CONFIG_2POS) {
|
||||
model->setFuncSwitchConfig(sw, (Board::SwitchType)config);
|
||||
if (config != Board::SWITCH_2POS && config != Board::SWITCH_GLOBAL) {
|
||||
model->setFuncSwitchStart(sw, ModelData::FUNC_SWITCH_START_PREVIOUS);
|
||||
if ((config == ModelData::FUNC_SWITCH_CONFIG_NONE) || model->getFuncSwitchAlwaysOnGroupForSwitch(sw))
|
||||
if ((config == Board::SWITCH_NOT_AVAILABLE) || model->getFuncSwitchAlwaysOnGroupForSwitch(sw))
|
||||
model->setFuncSwitchGroup(sw, 0);
|
||||
}
|
||||
update();
|
||||
@@ -270,8 +360,8 @@ void FunctionSwitchesPanel::on_groupChanged(int value)
|
||||
if (ok && model->getFuncSwitchGroup(sw) != grp) {
|
||||
unsigned oldGrp = model->getFuncSwitchGroup(sw);
|
||||
if (model->getFuncSwitchAlwaysOnGroup(grp)) {
|
||||
if (model->getFuncSwitchConfig(sw) == ModelData::FUNC_SWITCH_CONFIG_TOGGLE)
|
||||
model->setFuncSwitchConfig(sw, ModelData::FUNC_SWITCH_CONFIG_2POS);
|
||||
if (model->getFuncSwitchConfig(sw) == Board::SWITCH_TOGGLE)
|
||||
model->setFuncSwitchConfig(sw, Board::SWITCH_2POS);
|
||||
}
|
||||
if ((grp == 0) || (model->getFuncGroupSwitchStart(grp, switchcnt) == 0))
|
||||
model->setFuncSwitchStart(sw, ModelData::FUNC_SWITCH_START_PREVIOUS);
|
||||
@@ -304,8 +394,8 @@ void FunctionSwitchesPanel::on_alwaysOnGroupChanged(int value)
|
||||
model->setFuncSwitchAlwaysOnGroup(grp, (unsigned int)value);
|
||||
if (value) {
|
||||
for (int i = 0; i < switchcnt; i += 1) {
|
||||
if (((int)model->getFuncSwitchGroup(i) == grp) && (model->getFuncSwitchConfig(i) == ModelData::FUNC_SWITCH_CONFIG_TOGGLE))
|
||||
model->setFuncSwitchConfig(i, ModelData::FUNC_SWITCH_CONFIG_2POS);
|
||||
if (((int)model->getFuncSwitchGroup(i) == grp) && (model->getFuncSwitchConfig(i) == Board::SWITCH_TOGGLE))
|
||||
model->setFuncSwitchConfig(i, Board::SWITCH_2POS);
|
||||
}
|
||||
if ((int)model->getFuncGroupSwitchStart(grp, switchcnt) == switchcnt + 1) {
|
||||
for (int i = 0; i < switchcnt; i += 1) {
|
||||
|
||||
Reference in New Issue
Block a user