Skip to content

Commit a40ba8f

Browse files
authored
LabelCellWidget/Option (#1196)
1 parent 45c8ea4 commit a40ba8f

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

SerialPrograms/Source/CommonFramework/Options/LabelCellOption.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "Common/Cpp/Containers/Pimpl.tpp"
88
#include "Common/Cpp/Json/JsonValue.h"
9+
#include "Common/Cpp/Concurrency/SpinLock.h"
910
#include "LabelCellOption.h"
1011

1112
//#include <iostream>
@@ -17,7 +18,7 @@ namespace PokemonAutomation{
1718

1819

1920
struct LabelCellOption::Data{
20-
// mutable SpinLock m_lock;
21+
mutable SpinLock m_lock;
2122
std::string m_text;
2223
// ImageRGB32 m_icon_owner;
2324
ImageViewRGB32 m_icon;
@@ -80,6 +81,7 @@ LabelCellOption::LabelCellOption(
8081
// : m_data(CONSTRUCT_TOKEN, std::move(text), std::move(icon))
8182
//{}
8283
const std::string& LabelCellOption::text() const{
84+
ReadSpinLock lg(m_data->m_lock);
8385
return m_data->m_text;
8486
}
8587
const ImageViewRGB32& LabelCellOption::icon() const{
@@ -94,6 +96,18 @@ JsonValue LabelCellOption::to_json() const{
9496
return JsonValue();
9597
}
9698

99+
void LabelCellOption::set_text(std::string x){
100+
// sanitize(x);
101+
{
102+
WriteSpinLock lg(m_data->m_lock);
103+
if (m_data->m_text == x){
104+
return;
105+
}
106+
m_data->m_text = std::move(x);
107+
}
108+
report_value_changed(this);
109+
}
110+
97111

98112

99113

SerialPrograms/Source/CommonFramework/Options/LabelCellOption.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class LabelCellOption : public ConfigOptionImpl<LabelCellOption>{
4545
const ImageViewRGB32& icon() const;
4646
Resolution resolution() const;
4747

48+
void set_text(std::string x);
49+
4850
virtual void load_json(const JsonValue& json) override;
4951
virtual JsonValue to_json() const override;
5052

SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ template class RegisterConfigWidget<LabelCellWidget>;
2020

2121

2222
LabelCellWidget::~LabelCellWidget(){
23+
m_value.remove_listener(*this);
2324
}
2425
LabelCellWidget::LabelCellWidget(QWidget& parent, LabelCellOption& value)
2526
: QWidget(&parent)
2627
, ConfigWidget(value, *this)
27-
// , m_value(value)
28+
, m_value(value)
2829
{
2930
QHBoxLayout* layout = new QHBoxLayout(this);
3031
layout->setContentsMargins(0, 0, 0, 0);
@@ -49,6 +50,17 @@ LabelCellWidget::LabelCellWidget(QWidget& parent, LabelCellOption& value)
4950
layout->addWidget(m_text, 1);
5051
// text->setTextInteractionFlags(Qt::TextBrowserInteraction);
5152
// m_text->setOpenExternalLinks(true);
53+
54+
m_value.add_listener(*this);
55+
}
56+
57+
void LabelCellWidget::update_value(){
58+
m_text->setText(QString::fromStdString(m_value.text()));
59+
}
60+
void LabelCellWidget::on_config_value_changed(void* object){
61+
QMetaObject::invokeMethod(m_text, [this]{
62+
update_value();
63+
}, Qt::QueuedConnection);
5264
}
5365

5466

SerialPrograms/Source/CommonFramework/Options/QtWidget/LabelCellWidget.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ class LabelCellWidget : public QWidget, public ConfigWidget{
2525
~LabelCellWidget();
2626
LabelCellWidget(QWidget& parent, LabelCellOption& value);
2727

28+
virtual void update_value() override;
29+
virtual void on_config_value_changed(void* object) override;
30+
2831
private:
29-
// LabelCellOption& m_value;
32+
LabelCellOption& m_value;
3033
QLabel* m_icon = nullptr;
3134
QLabel* m_text;
3235
};

0 commit comments

Comments
 (0)