From 6f3ad758211a7cd3b7c0c7ddea3a9b03ffac4bb9 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Fri, 18 Mar 2022 07:48:56 +0100 Subject: [PATCH] Serial port settings are mutually exclusive --- companion/src/generaledit/hardware.cpp | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/companion/src/generaledit/hardware.cpp b/companion/src/generaledit/hardware.cpp index d7bc3a12d..abd7d4a1d 100644 --- a/companion/src/generaledit/hardware.cpp +++ b/companion/src/generaledit/hardware.cpp @@ -35,6 +35,50 @@ constexpr char FIM_SWITCHTYPE2POS[] {"Switch Type 2 Pos"}; constexpr char FIM_SWITCHTYPE3POS[] {"Switch Type 3 Pos"}; constexpr char FIM_INTERNALMODULES[] {"Internal Modules"}; +class ExclusiveComboGroup: public QObject +{ + static constexpr auto _role = Qt::UserRole + 500; + + QList combos; + std::function filter; + +public: + ExclusiveComboGroup(QObject *parent, std::function filter) : + QObject(parent), filter(std::move(filter)) + { + } + + void addCombo(QComboBox *comboBox) + { + connect(comboBox, QOverload::of(&QComboBox::activated), + [=](int index) { this->handleActivated(comboBox, index); }); + combos.append(comboBox); + } + + void handleActivated(QComboBox* target, int index) { + auto data = target->itemData(index); + auto targetidx = combos.indexOf(target); + for (auto combo : combos) { + if (target == combo) continue; + auto view = dynamic_cast(combo->view()); + Q_ASSERT(view); + + auto previous = combo->findData(targetidx, _role); + if (previous >= 0) { + view->setRowHidden(previous, false); + combo->setItemData(previous, QVariant(), _role); + } + if (!filter(data)) { + auto idx = combo->findData(data); + if (idx >= 0) { + view->setRowHidden(idx, true); + combo->setItemData(idx, targetidx, _role); + } + } + } + } +}; + HardwarePanel::HardwarePanel(QWidget * parent, GeneralSettings & generalSettings, Firmware * firmware, CompoundItemModelFactory * sharedItemModels): GeneralPanel(parent, generalSettings, firmware), board(firmware->getBoard()), @@ -150,6 +194,10 @@ HardwarePanel::HardwarePanel(QWidget * parent, GeneralSettings & generalSettings addParams(row, maxBaudRate); } + // All values except 0 are mutually exclusive + ExclusiveComboGroup *exclGroup = new ExclusiveComboGroup( + this, [=](const QVariant &value) { return value == 0; }); + if (firmware->getCapability(HasAuxSerialMode)) { QString lbl = "Serial Port"; if (IS_RADIOMASTER_TX16S(board)) @@ -158,6 +206,7 @@ HardwarePanel::HardwarePanel(QWidget * parent, GeneralSettings & generalSettings AutoComboBox *serialPortMode = new AutoComboBox(this); serialPortMode->setModel(editorItemModels->getItemModel(auxmodelid)); serialPortMode->setField(generalSettings.serialPort[GeneralSettings::SP_AUX1]); + exclGroup->addCombo(serialPortMode); addParams(row, serialPortMode); } @@ -169,6 +218,7 @@ HardwarePanel::HardwarePanel(QWidget * parent, GeneralSettings & generalSettings AutoComboBox *serialPortMode = new AutoComboBox(this); serialPortMode->setModel(editorItemModels->getItemModel(auxmodelid)); serialPortMode->setField(generalSettings.serialPort[GeneralSettings::SP_AUX2]); + exclGroup->addCombo(serialPortMode); addParams(row, serialPortMode); } @@ -177,6 +227,7 @@ HardwarePanel::HardwarePanel(QWidget * parent, GeneralSettings & generalSettings AutoComboBox *serialPortMode = new AutoComboBox(this); serialPortMode->setModel(editorItemModels->getItemModel(vcpmodelid)); serialPortMode->setField(generalSettings.serialPort[GeneralSettings::SP_VCP]); + exclGroup->addCombo(serialPortMode); addParams(row, serialPortMode); }