From 9a07b83b001f1010ecfc66e708d5de548050b626 Mon Sep 17 00:00:00 2001 From: elecpower Date: Thu, 9 Dec 2021 21:39:57 +1100 Subject: [PATCH] Use item models for ui lists --- companion/src/firmwares/modeldata.cpp | 74 +++++++++++++++++++++++++++ companion/src/firmwares/modeldata.h | 24 +++++++++ companion/src/modeledit/setup.cpp | 13 ++--- 3 files changed, 103 insertions(+), 8 deletions(-) diff --git a/companion/src/firmwares/modeldata.cpp b/companion/src/firmwares/modeldata.cpp index a5aa59b10..2fd3c9ce4 100644 --- a/companion/src/firmwares/modeldata.cpp +++ b/companion/src/firmwares/modeldata.cpp @@ -1653,3 +1653,77 @@ AbstractStaticItemModel * ModelData::trainerModeItemModel(const GeneralSettings mdl->loadItemList(); return mdl; } + +QString ModelData::funcSwitchConfigToString(unsigned int index) const +{ + if (index < CPN_MAX_FUNCTION_SWITCHES) + return funcSwitchConfigToString((functionSwitchConfig >> (2 * index)) & 0x03); + else + return CPN_STR_UNKNOWN_ITEM; +} + +// static +QString ModelData::funcSwitchConfigToString(int value) +{ + switch (value) { + case FUNC_SWITCH_CONFIG_NONE: + return tr("NONE"); + case FUNC_SWITCH_CONFIG_TOGGLE: + return tr("TOGGLE"); + case FUNC_SWITCH_CONFIG_2POS: + return tr("2POS"); + default: + return CPN_STR_UNKNOWN_ITEM; + } +} + +// static +AbstractStaticItemModel * ModelData::funcSwitchConfigItemModel() +{ + AbstractStaticItemModel * mdl = new AbstractStaticItemModel(); + mdl->setName(AIM_MODELDATA_FUNCSWITCHCONFIG); + + for (int i = FUNC_SWITCH_CONFIG_FIRST; i <= FUNC_SWITCH_CONFIG_LAST; i++) { + mdl->appendToItemList(funcSwitchConfigToString(i), i); + } + + mdl->loadItemList(); + return mdl; +} + +QString ModelData::funcSwitchStartToString(unsigned int index) const +{ + if (index < CPN_MAX_FUNCTION_SWITCHES) + return funcSwitchStartToString((functionSwitchStartConfig >> (2 * index)) & 0x03); + else + return CPN_STR_UNKNOWN_ITEM; +} + +// static +QString ModelData::funcSwitchStartToString(int value) +{ + switch (value) { + case FUNC_SWITCH_START_INACTIVE: + return CPN_STR_SW_INDICATOR_UP; + case FUNC_SWITCH_START_ACTIVE: + return CPN_STR_SW_INDICATOR_DN; + case FUNC_SWITCH_START_PREVIOUS: + return "="; + default: + return CPN_STR_UNKNOWN_ITEM; + } +} + +// static +AbstractStaticItemModel * ModelData::funcSwitchStartItemModel() +{ + AbstractStaticItemModel * mdl = new AbstractStaticItemModel(); + mdl->setName(AIM_MODELDATA_FUNCSWITCHSTART); + + for (int i = FUNC_SWITCH_START_FIRST; i <= FUNC_SWITCH_START_LAST; i++) { + mdl->appendToItemList(funcSwitchStartToString(i), i); + } + + mdl->loadItemList(); + return mdl; +} diff --git a/companion/src/firmwares/modeldata.h b/companion/src/firmwares/modeldata.h index e79e47739..2be1a7ff4 100644 --- a/companion/src/firmwares/modeldata.h +++ b/companion/src/firmwares/modeldata.h @@ -44,6 +44,8 @@ class RadioDataConversionState; class AbstractStaticItemModel; constexpr char AIM_MODELDATA_TRAINERMODE[] {"modeldata.trainermode"}; +constexpr char AIM_MODELDATA_FUNCSWITCHCONFIG[] {"modeldata.funcswitchconfig"}; +constexpr char AIM_MODELDATA_FUNCSWITCHSTART[] {"modeldata.funcswitchstart"}; #define CHAR_FOR_NAMES " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-." #define CHAR_FOR_NAMES_REGEX "[ A-Za-z0-9_.-,]*" @@ -172,6 +174,22 @@ class ModelData { char registrationId[8+1]; + enum FunctionSwitchConfig { + FUNC_SWITCH_CONFIG_NONE, + FUNC_SWITCH_CONFIG_FIRST = FUNC_SWITCH_CONFIG_NONE, + FUNC_SWITCH_CONFIG_TOGGLE, + FUNC_SWITCH_CONFIG_2POS, + FUNC_SWITCH_CONFIG_LAST = FUNC_SWITCH_CONFIG_2POS + }; + + enum FunctionSwitchStart { + FUNC_SWITCH_START_INACTIVE, + FUNC_SWITCH_START_FIRST = FUNC_SWITCH_START_INACTIVE, + FUNC_SWITCH_START_ACTIVE, + FUNC_SWITCH_START_PREVIOUS, + FUNC_SWITCH_START_LAST = FUNC_SWITCH_START_PREVIOUS + }; + // Function switches unsigned int functionSwitchConfig; unsigned int functionSwitchGroup; @@ -268,6 +286,12 @@ class ModelData { static QString trainerModeToString(const int value); static bool isTrainerModeAvailable(const GeneralSettings & generalSettings, const Firmware * firmware, const int value); static AbstractStaticItemModel * trainerModeItemModel(const GeneralSettings & generalSettings, const Firmware * firmware); + QString funcSwitchConfigToString(unsigned int index) const; + static QString funcSwitchConfigToString(const int value); + static AbstractStaticItemModel * funcSwitchConfigItemModel(); + QString funcSwitchStartToString(unsigned int index) const; + static QString funcSwitchStartToString(const int value); + static AbstractStaticItemModel * funcSwitchStartItemModel(); protected: void removeGlobalVar(int & var); diff --git a/companion/src/modeledit/setup.cpp b/companion/src/modeledit/setup.cpp index 30cf3bbe2..f12d83a74 100644 --- a/companion/src/modeledit/setup.cpp +++ b/companion/src/modeledit/setup.cpp @@ -990,6 +990,9 @@ FunctionSwitchesPanel::FunctionSwitchesPanel(QWidget * parent, ModelData & model { ui->setupUi(this); + AbstractStaticItemModel *fsConfig = ModelData::funcSwitchConfigItemModel(); + AbstractStaticItemModel *fsStart = ModelData::funcSwitchStartItemModel(); + lock = true; QRegExp rx(CHAR_FOR_NAMES_REGEX); @@ -1005,19 +1008,13 @@ FunctionSwitchesPanel::FunctionSwitchesPanel(QWidget * parent, ModelData & model aleName->setValidator(new QRegExpValidator(rx, this)); aleName->setField((char *)model.functionSwitchNames[i], 3); - // TODO itemmodel QComboBox * cboConfig = new QComboBox(this); cboConfig->setProperty("index", i); - cboConfig->addItem(tr("NONE")); - cboConfig->addItem(tr("TOGGLE")); - cboConfig->addItem(tr("2POS")); + cboConfig->setModel(fsConfig); - // TODO itemmodel QComboBox * cboStartPosn = new QComboBox(this); cboStartPosn->setProperty("index", i); - cboStartPosn->addItem(CPN_STR_SW_INDICATOR_UP); - cboStartPosn->addItem(CPN_STR_SW_INDICATOR_DN); - cboStartPosn->addItem("="); + cboStartPosn->setModel(fsStart); QSpinBox * sbGroup = new QSpinBox(this); sbGroup->setProperty("index", i);