From 3794e0031b03ea34545cb02b07dc88b0d5ac638c Mon Sep 17 00:00:00 2001 From: Carl Philipp Klemm Date: Mon, 13 Apr 2026 15:13:47 +0200 Subject: [PATCH] Finis support for ENUM item types --- src/items/item.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ src/items/item.h | 8 ++++++- src/ui/itemwidget.cpp | 27 ++++++++++++++++++++++ src/ui/itemwidget.ui | 10 +++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/items/item.cpp b/src/items/item.cpp index b902c32..14d31b3 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -60,6 +60,13 @@ void ItemData::storeWithChanges(QJsonObject& json, const ItemFieldChanges& chang json["Value"] = static_cast(value_); if(changes.groupName) json["GroupName"] = groupName_; + if(changes.valueNames) + { + QJsonArray valueNamesArray; + for(const QString& name : valueNames_) + valueNamesArray.append(name); + json["ValueNames"] = valueNamesArray; + } } void ItemData::load(const QJsonObject &json, const bool preserve) @@ -87,6 +94,19 @@ ItemFieldChanges ItemData::loadWithChanges(const QJsonObject& json, const bool p groupName_ = json["GroupName"].toString(); changes.groupName = true; } + if(json.contains("ValueType")) + { + type_ = static_cast(json["ValueType"].toInt()); + changes.type = true; + } + if(json.contains("ValueNames")) + { + valueNames_.clear(); + QJsonArray valueNamesArray = json["ValueNames"].toArray(); + for(int i = 0; i < valueNamesArray.size(); ++i) + valueNames_.push_back(valueNamesArray[i].toString()); + changes.valueNames = true; + } itemId_ = static_cast(json["ItemId"].toDouble(0)); } return changes; @@ -120,6 +140,8 @@ bool ItemData::hasChanged(const ItemData& other, const ItemFieldChanges& changes return true; if(changes.actors) return true; + if(changes.valueNames && other.getValueNames() != getValueNames()) + return true; return false; } @@ -148,6 +170,33 @@ void ItemData::setGroupName(QString groupName) groupName_ = groupName; } +std::vector ItemData::getValueNames() const +{ + return valueNames_; +} + +void ItemData::setValueNames(std::vector valueNames) +{ + valueNames_ = std::move(valueNames); +} + +int ItemData::valueNameToIndex(const QString& name) const +{ + for(size_t i = 0; i < valueNames_.size(); ++i) + { + if(valueNames_[i] == name) + return static_cast(i); + } + return -1; +} + +QString ItemData::indexToValueName(int index) const +{ + if(index >= 0 && static_cast(index) < valueNames_.size()) + return valueNames_[index]; + return QString(); +} + //item Item::Item(uint32_t itemIdIn, QString name, uint8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, @@ -205,6 +254,7 @@ Item& Item::operator=(const ItemData& other) itemId_ = other.id(); hidden_ = other.isHidden(); groupName_ = other.getGroupName(); + valueNames_ = other.getValueNames(); return *this; } @@ -248,6 +298,8 @@ void Item::requestUpdate(ItemUpdateRequest update) for(std::shared_ptr& actor : update.newActors) addActor(actor); } + if(update.changes.valueNames) + valueNames_ = update.payload.getValueNames(); update.payload = *this; updated(update); } diff --git a/src/items/item.h b/src/items/item.h index b12575c..4a70fae 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -71,6 +71,10 @@ public: item_value_type_t getValueType(); QString getGroupName() const; void setGroupName(QString groupName); + std::vector getValueNames() const; + void setValueNames(std::vector valueNames); + int valueNameToIndex(const QString& name) const; + QString indexToValueName(int index) const; void storeWithChanges(QJsonObject& json, const ItemFieldChanges& changes); ItemFieldChanges loadWithChanges(const QJsonObject& json, const bool preserve = false); virtual QString getName() const; @@ -130,6 +134,7 @@ struct ItemFieldChanges bool type :1; bool groupName :1; bool actors :1; + bool valueNames :1; ItemFieldChanges(bool defaultVal = false) { name = defaultVal; @@ -138,10 +143,11 @@ struct ItemFieldChanges type = defaultVal; groupName = defaultVal; actors = false; + valueNames = defaultVal; } inline bool isNone() const { - return !name && !value && !hidden && !type && !groupName && !actors; + return !name && !value && !hidden && !type && !groupName && !actors && !valueNames; } }; diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index de7544e..3846911 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -2,6 +2,7 @@ #include "ui_itemwidget.h" #include +#include #include #include #include "itemsettingsdialog.h" @@ -26,6 +27,16 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool noGroupEdit, QWidget *pare ui->checkBox->hide(); ui->slider->hide(); } + else if(workingItem->getValueType() == ITEM_VALUE_ENUM) + { + ui->slider->hide(); + ui->checkBox->hide(); + QStringList list; + for(const QString& name : workingItem->getValueNames()) + list.append(name); + ui->comboBox->addItems(list); + ui->comboBox->setCurrentIndex(workingItem->getValue()); + } else { ui->slider->hide(); @@ -37,6 +48,8 @@ ItemWidget::ItemWidget(std::weak_ptr item, bool noGroupEdit, QWidget *pare if(workingItem->getValueType() == ITEM_VALUE_UINT) connect(ui->slider, &QSlider::valueChanged, this, &ItemWidget::moveToValue); + else if(workingItem->getValueType() == ITEM_VALUE_ENUM) + connect(ui->comboBox, QOverload::of(&QComboBox::currentIndexChanged), this, &ItemWidget::moveToValue); else connect(ui->checkBox, &QCheckBox::toggled, this, &ItemWidget::moveToState); connect(ui->pushButton, &QPushButton::clicked, this, &ItemWidget::showSettingsDialog); @@ -84,6 +97,7 @@ void ItemWidget::disable() ui->checkBox->setEnabled(false); ui->label->setEnabled(false); ui->slider->setEnabled(false); + ui->comboBox->setEnabled(false); ui->pushButton_Remove->setEnabled(false); } @@ -115,6 +129,16 @@ std::weak_ptr ItemWidget::getItem() void ItemWidget::onItemUpdated(ItemUpdateRequest update) { + if(update.changes.valueNames) + { + ui->comboBox->blockSignals(true); + ui->comboBox->clear(); + QStringList list; + for(const QString& name : update.payload.getValueNames()) + list.append(name); + ui->comboBox->addItems(list); + ui->comboBox->blockSignals(false); + } stateChanged(update.payload.getValue()); } @@ -126,6 +150,9 @@ void ItemWidget::stateChanged(int state) ui->checkBox->blockSignals(true); ui->checkBox->setChecked(state); ui->checkBox->blockSignals(false); + ui->comboBox->blockSignals(true); + ui->comboBox->setCurrentIndex(state); + ui->comboBox->blockSignals(false); } ItemWidget::~ItemWidget() diff --git a/src/ui/itemwidget.ui b/src/ui/itemwidget.ui index 96b625e..a1e4bf0 100644 --- a/src/ui/itemwidget.ui +++ b/src/ui/itemwidget.ui @@ -59,6 +59,16 @@ + + + + + 0 + 0 + + + +