diff --git a/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.cpp b/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.cpp index 66e6eb7416..3308b81f0a 100644 --- a/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.cpp +++ b/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.cpp @@ -6,6 +6,7 @@ #include "Common/Cpp/Containers/Pimpl.tpp" #include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/Concurrency/SpinLock.h" #include "LabelCellOption.h" //#include @@ -17,7 +18,7 @@ namespace PokemonAutomation{ struct LabelCellOption::Data{ -// mutable SpinLock m_lock; + mutable SpinLock m_lock; std::string m_text; // ImageRGB32 m_icon_owner; ImageViewRGB32 m_icon; @@ -80,6 +81,7 @@ LabelCellOption::LabelCellOption( // : m_data(CONSTRUCT_TOKEN, std::move(text), std::move(icon)) //{} const std::string& LabelCellOption::text() const{ + ReadSpinLock lg(m_data->m_lock); return m_data->m_text; } const ImageViewRGB32& LabelCellOption::icon() const{ @@ -94,6 +96,18 @@ JsonValue LabelCellOption::to_json() const{ return JsonValue(); } +void LabelCellOption::set_text(std::string x){ + // sanitize(x); + { + WriteSpinLock lg(m_data->m_lock); + if (m_data->m_text == x){ + return; + } + m_data->m_text = std::move(x); + } + report_value_changed(this); +} + diff --git a/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.h b/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.h index f7afe0b27e..300964d6d3 100644 --- a/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.h +++ b/SerialPrograms/Source/CommonFramework/Options/LabelCellOption.h @@ -45,6 +45,8 @@ class LabelCellOption : public ConfigOptionImpl{ const ImageViewRGB32& icon() const; Resolution resolution() const; + void set_text(std::string x); + virtual void load_json(const JsonValue& json) override; virtual JsonValue to_json() const override; diff --git a/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.cpp b/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.cpp index 4964284466..834b9f9032 100644 --- a/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.cpp @@ -20,11 +20,12 @@ template class RegisterConfigWidget; LabelCellWidget::~LabelCellWidget(){ + m_value.remove_listener(*this); } LabelCellWidget::LabelCellWidget(QWidget& parent, LabelCellOption& value) : QWidget(&parent) , ConfigWidget(value, *this) -// , m_value(value) + , m_value(value) { QHBoxLayout* layout = new QHBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); @@ -49,6 +50,17 @@ LabelCellWidget::LabelCellWidget(QWidget& parent, LabelCellOption& value) layout->addWidget(m_text, 1); // text->setTextInteractionFlags(Qt::TextBrowserInteraction); // m_text->setOpenExternalLinks(true); + + m_value.add_listener(*this); +} + +void LabelCellWidget::update_value(){ + m_text->setText(QString::fromStdString(m_value.text())); +} +void LabelCellWidget::on_config_value_changed(void* object){ + QMetaObject::invokeMethod(m_text, [this]{ + update_value(); + }, Qt::QueuedConnection); } diff --git a/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.h b/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.h index 77dda8709b..14442fda62 100644 --- a/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.h +++ b/SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.h @@ -25,8 +25,11 @@ class LabelCellWidget : public QWidget, public ConfigWidget{ ~LabelCellWidget(); LabelCellWidget(QWidget& parent, LabelCellOption& value); + virtual void update_value() override; + virtual void on_config_value_changed(void* object) override; + private: -// LabelCellOption& m_value; + LabelCellOption& m_value; QLabel* m_icon = nullptr; QLabel* m_text; };