Skip to content

Commit 742119b

Browse files
author
Gin
committed
fix READ_ONLY for real
1 parent 454c1c0 commit 742119b

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

Common/Cpp/Options/ConfigOption.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum class LockMode{
2727
};
2828
enum class ConfigOptionState{
2929
ENABLED,
30-
DISABLED,
30+
DISABLED, // aka locked
3131
HIDDEN,
3232
};
3333

@@ -93,10 +93,12 @@ class ConfigOption{
9393
}
9494

9595
public:
96-
// Return the lock mode: how locking works on this option. It can be:
97-
// - UNLOCK_WHILE_RUNNING,
98-
// - LOCK_WHILE_RUNNING,
99-
// - READ_ONLY,
96+
// Return the lock mode: how locking works on this option. It can be:
97+
// - UNLOCK_WHILE_RUNNING,
98+
// - LOCK_WHILE_RUNNING,
99+
// - READ_ONLY, (aka always locked)
100+
// This value is const throughout the ConfigOption lifetime. It is set
101+
// when constructing the ConfigOption.
100102
LockMode lock_mode() const;
101103

102104
// Returns error message if invalid. Otherwise returns empty string.
@@ -108,7 +110,17 @@ class ConfigOption{
108110
// transient state that the option object may have.
109111
virtual void reset_state(){};
110112

113+
// Thread-safe: return the current visibility state. It can be:
114+
// - ENABLED
115+
// - DISABLED
116+
// - HIDDEN
111117
ConfigOptionState visibility() const;
118+
// Thread-safe: set the option's visibility state. It can be:
119+
// - ENABLED
120+
// - DISABLED
121+
// - HIDDEN
122+
// If visibility changed, all attached listeners' on_config_visibility_changed()
123+
// will be called.
112124
virtual void set_visibility(ConfigOptionState visibility);
113125

114126

Common/Qt/Options/ConfigWidget.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ void ConfigWidget::update_visibility(){
4646
// cout << "ConfigWidget::update_visibility(): " << (int)m_value.visibility() << endl;
4747
switch (m_value.visibility()){
4848
case ConfigOptionState::ENABLED:
49+
// setEnable(false) only happens when the lock mode is LOCK_WHILE_RUNNING and the program is running.
50+
// Ideally we should handle lock mode READ_ONLY here too, but m_widget as a QWidget does not have such
51+
// function. Only some of its derived classes, like QLineEdit, can call setReadOnly().
52+
// So here we treat READ_ONLY as enabled and let derived classes of ConfigWidget that have the
53+
// functionality to set read-only (e.g. StringOptionWidget) to handle READ_ONLY.
4954
m_widget->setEnabled(m_value.lock_mode() != LockMode::LOCK_WHILE_RUNNING || !m_program_is_running);
5055
m_widget->setVisible(true);
5156
break;

Common/Qt/Options/ConfigWidget.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ class ConfigWidget : protected ConfigOption::Listener{
3636

3737
// Needs to be called on the UI thread.
3838
virtual void update_value(){}
39+
// Needs to be called on the UI thread. Update QWidget's visibility based on
40+
// ConfigOption m_value's visibility setting.
41+
// Note this function is unable to set READ_ONLY visibility state. The derived
42+
// classes of ConfigWidget who have the functionality to set read-only should
43+
// overwrite on_config_visibility_changed() to handle that.
3944
virtual void update_visibility();
4045
void update_visibility(bool program_is_running);
4146
void update_all(bool program_is_running);
4247

4348
protected:
49+
// Overwrite ConfigOption::Listener::on_config_visibility_changed().
50+
// Called when the listened config option's visibility is changed.
51+
// This function invokes this->update_visibility() on the Qt UI thread.
52+
// Note due to update_visibility() is unable to set READ_ONLY visbility state.
53+
// It is up to the derived classes of ConfigWidget who have the functionality
54+
// to set read-only (e.g. StringOptionWidget) to overwrite this function to
55+
// handle read-only.
4456
virtual void on_config_visibility_changed() override;
4557
virtual void on_program_state_changed(bool program_is_running) override;
4658

Common/Qt/Options/StringWidget.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include <QLineEdit>
1010
#include "StringWidget.h"
1111

12-
//#include <iostream>
13-
//using std::cout;
14-
//using std::endl;
12+
// #include <iostream>
13+
// using std::cout;
14+
// using std::endl;
1515

1616
namespace PokemonAutomation{
1717

@@ -59,9 +59,22 @@ void StringCellWidget::on_config_value_changed(void* object){
5959
}, Qt::QueuedConnection);
6060
}
6161
void StringCellWidget::on_config_visibility_changed(){
62+
// Overwrite ConfigWidget::on_config_visibility_changed() because ConfigWidget cannot handle
63+
// READ_ONLY state.
64+
this->setEnabled(true);
6265
QMetaObject::invokeMethod(this, [this]{
63-
update_visibility();
64-
setReadOnly(m_value.lock_mode() == LockMode::READ_ONLY || m_value.is_locked());
66+
const ConfigOptionState visibility = m_value.visibility();
67+
this->setReadOnly(visibility != ConfigOptionState::ENABLED ||
68+
m_value.lock_mode() == LockMode::READ_ONLY || m_value.is_locked());
69+
switch (visibility){
70+
case ConfigOptionState::ENABLED:
71+
case ConfigOptionState::DISABLED:
72+
this->setVisible(true);
73+
break;
74+
case ConfigOptionState::HIDDEN:
75+
this->setVisible(false);
76+
break;
77+
}
6578
}, Qt::QueuedConnection);
6679
}
6780

@@ -109,9 +122,22 @@ void StringOptionWidget::on_config_value_changed(void* object){
109122
}, Qt::QueuedConnection);
110123
}
111124
void StringOptionWidget::on_config_visibility_changed(){
112-
QMetaObject::invokeMethod(m_box, [this]{
113-
update_visibility();
114-
m_box->setReadOnly(m_value.is_locked());
125+
// Overwrite ConfigWidget::on_config_visibility_changed() because ConfigWidget cannot handle
126+
// READ_ONLY state.
127+
this->setEnabled(true);
128+
QMetaObject::invokeMethod(this, [this]{
129+
const ConfigOptionState visibility = m_value.visibility();
130+
m_box->setReadOnly(visibility != ConfigOptionState::ENABLED ||
131+
m_value.lock_mode() == LockMode::READ_ONLY || m_value.is_locked());
132+
switch (visibility){
133+
case ConfigOptionState::ENABLED:
134+
case ConfigOptionState::DISABLED:
135+
this->setVisible(true);
136+
break;
137+
case ConfigOptionState::HIDDEN:
138+
this->setVisible(false);
139+
break;
140+
}
115141
}, Qt::QueuedConnection);
116142
}
117143

SerialPrograms/Source/ML/Programs/ML_LabelImages.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,10 @@ void LabelImages::on_config_value_changed(void* object){
513513
FORM_LABEL.set_visibility(ConfigOptionState::HIDDEN);
514514
CUSTOM_SET_LABEL.set_visibility(ConfigOptionState::ENABLED);
515515
MANUAL_LABEL.set_visibility(ConfigOptionState::HIDDEN);
516-
} else {
516+
} else { // value == 2
517517
FORM_LABEL.set_visibility(ConfigOptionState::HIDDEN);
518518
CUSTOM_SET_LABEL.set_visibility(ConfigOptionState::HIDDEN);
519519
MANUAL_LABEL.set_visibility(ConfigOptionState::ENABLED);
520-
// cout << "MANUAL_LABEL enabeld" << endl;
521520
}
522521
}
523522

0 commit comments

Comments
 (0)