From 950fa3d10501abce591cb9cfe1b51a9914d3a8f9 Mon Sep 17 00:00:00 2001 From: elecpower Date: Wed, 17 Mar 2021 21:40:13 +1100 Subject: [PATCH] Add support for rawsource and rawswitch in autocombobox --- companion/src/shared/autocombobox.h | 72 ++++++++++++++++++++++++----- companion/src/shared/autolineedit.h | 4 +- companion/src/shared/autotimeedit.h | 4 +- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/companion/src/shared/autocombobox.h b/companion/src/shared/autocombobox.h index a257f35d5..ca6054555 100644 --- a/companion/src/shared/autocombobox.h +++ b/companion/src/shared/autocombobox.h @@ -22,6 +22,8 @@ #include #include "genericpanel.h" +#include "rawsource.h" +#include "rawswitch.h" class AutoComboBox: public QComboBox { @@ -34,7 +36,9 @@ class AutoComboBox: public QComboBox panel(nullptr), next(0), lock(false), - hasModel(false) + hasModel(false), + rawSource(nullptr), + rawSwitch(nullptr) { connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int))); } @@ -77,6 +81,8 @@ class AutoComboBox: public QComboBox void setField(unsigned int & field, GenericPanel * panel = nullptr) { this->field = (int *)&field; + this->rawSource = nullptr; + this->rawSwitch = nullptr; this->panel = panel; updateValue(); } @@ -84,6 +90,26 @@ class AutoComboBox: public QComboBox void setField(int & field, GenericPanel * panel = nullptr) { this->field = &field; + this->rawSource = nullptr; + this->rawSwitch = nullptr; + this->panel = panel; + updateValue(); + } + + void setField(RawSource & field, GenericPanel * panel = nullptr) + { + this->rawSource = &field; + this->rawSwitch = nullptr; + this->field = nullptr; + this->panel = panel; + updateValue(); + } + + void setField(RawSwitch & field, GenericPanel * panel = nullptr) + { + this->rawSwitch = &field; + this->rawSource = nullptr; + this->field = nullptr; this->panel = panel; updateValue(); } @@ -108,10 +134,18 @@ class AutoComboBox: public QComboBox void updateValue() { - if (!field) + if (!field && !rawSource && !rawSwitch) return; + lock = true; - setCurrentIndex(findData(*field)); + + if (field) + setCurrentIndex(findData(*field)); + else if (rawSource) + setCurrentIndex(findData(rawSource->toValue())); + else if (rawSwitch) + setCurrentIndex(findData(rawSwitch->toValue())); + lock = false; } @@ -123,15 +157,29 @@ class AutoComboBox: public QComboBox { if (panel && panel->lock) return; - if (index > -1) { - const int val = itemData(index).toInt(); - if (field && !lock) { - *field = val; - emit currentDataChanged(val); - if (panel) - emit panel->modified(); - } + if (lock || index < 0) + return; + + bool ok; + const int val = itemData(index).toInt(&ok); + if (!ok) + return; + + if (field && *field != val) { + *field = val; } + else if (rawSource && rawSource->toValue() != val) { + *rawSource = RawSource(val); + } + else if (rawSwitch && rawSwitch->toValue() != val) { + *rawSwitch = RawSwitch(val); + } + else + return; + + emit currentDataChanged(val); + if (panel) + emit panel->modified(); } protected: @@ -140,4 +188,6 @@ class AutoComboBox: public QComboBox int next = 0; bool lock = false; bool hasModel = false; + RawSource *rawSource = nullptr; + RawSwitch *rawSwitch = nullptr; }; diff --git a/companion/src/shared/autolineedit.h b/companion/src/shared/autolineedit.h index 173d16e95..b2815796c 100644 --- a/companion/src/shared/autolineedit.h +++ b/companion/src/shared/autolineedit.h @@ -76,7 +76,9 @@ class AutoLineEdit: public QLineEdit protected slots: void onEdited() { - if ((panel && panel->lock) || lock) + if (panel && panel->lock) + return; + if (lock) return; if (field) diff --git a/companion/src/shared/autotimeedit.h b/companion/src/shared/autotimeedit.h index 8e2dff2d8..2ba75d70e 100644 --- a/companion/src/shared/autotimeedit.h +++ b/companion/src/shared/autotimeedit.h @@ -77,7 +77,9 @@ class AutoTimeEdit: public QTimeEdit protected slots: void onTimeChanged(QTime time) { - if ((panel && panel->lock) || !field || lock) + if (panel && panel->lock) + return; + if (!field || lock) return; unsigned int val = time.hour() * 3600 + time.minute() * 60 + time.second();