Skip to content

Commit ba71c67

Browse files
committed
Remove QWidget from ConfigOption.
1 parent 2599d53 commit ba71c67

File tree

11 files changed

+41
-45
lines changed

11 files changed

+41
-45
lines changed

Common/Cpp/Options/ConfigOption.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include "Common/Cpp/Containers/Pimpl.h"
1414
#include "Common/Cpp/UiWrapper.h"
1515

16-
class QWidget;
17-
1816
namespace PokemonAutomation{
1917

2018
class JsonValue;
@@ -139,7 +137,6 @@ class ConfigOption{
139137

140138
public:
141139
virtual UiWrapper make_UiComponent(void* params) = 0;
142-
ConfigWidget* make_QtWidget(QWidget& parent);
143140

144141
private:
145142
struct Data;
@@ -159,7 +156,13 @@ class ConfigOption{
159156
template <typename OptionType>
160157
using ConfigUiFactory = UiWrapper (*)(OptionType& option, void* params);
161158

162-
159+
//
160+
// This is a convenience class that implementations should inherit from instead
161+
// of directly inheriting from ConfigOption or another option type.
162+
//
163+
// This provides the per-type UI factory as well as the "make_UiComponent"
164+
// override. This saves a ton of copy-paste as those are the same eveywhere.
165+
//
163166
template <typename ConfigType, typename ParentType = ConfigOption>
164167
class ConfigOptionImpl : public ParentType{
165168
public:

Common/Cpp/UiWrapper.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,11 @@ class UiComponent{
2525
// Lightweight smart pointer to avoid pulling in <memory>.
2626
class UiWrapper{
2727
public:
28-
UiWrapper()
29-
: m_owns(false)
30-
, m_component(nullptr)
31-
{}
32-
UiWrapper(bool take_ownership, UiComponent* component)
33-
: m_owns(take_ownership)
34-
, m_component(component)
35-
{}
3628
~UiWrapper(){
3729
if (m_owns){
3830
delete m_component;
3931
}
4032
}
41-
4233
UiWrapper(UiWrapper&& x)
4334
: m_owns(x.m_owns)
4435
, m_component(x.m_component)
@@ -60,6 +51,25 @@ class UiWrapper{
6051
void operator=(const UiWrapper& x) = delete;
6152

6253

54+
public:
55+
UiWrapper()
56+
: m_owns(false)
57+
, m_component(nullptr)
58+
{}
59+
UiWrapper(bool take_ownership, UiComponent* component)
60+
: m_owns(take_ownership)
61+
, m_component(component)
62+
{}
63+
64+
bool is_owning() const{
65+
return m_owns;
66+
}
67+
UiComponent* release(){
68+
m_owns = false;
69+
return m_component;
70+
}
71+
72+
6373
public:
6474
operator bool() const{
6575
return m_component != nullptr;

Common/Qt/Options/ConfigWidget.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,15 @@ namespace PokemonAutomation{
1717

1818

1919
ConfigWidget* ConfigWidget::make_from_option(ConfigOption& option, QWidget* parent){
20-
ConfigWidget* widget = nullptr;
21-
22-
// This always returns a widget that is owned by a parent.
23-
// So the pointer can be released here.
2420
UiWrapper wrapper = option.make_UiComponent(parent);
25-
if (wrapper){
26-
widget = dynamic_cast<ConfigWidget*>(wrapper.get());
27-
}
28-
if (widget){
29-
return widget;
30-
}
31-
32-
if (widget == nullptr){
21+
if (!wrapper){
3322
throw InternalProgramError(
3423
nullptr,
3524
PA_CURRENT_FUNCTION,
3625
std::string("UI component not registered for type: ") + typeid(option).name()
3726
);
3827
}
39-
40-
return widget;
41-
}
42-
43-
44-
ConfigWidget* ConfigOption::make_QtWidget(QWidget& parent){
45-
return ConfigWidget::make_from_option(*this, &parent);
28+
return dynamic_cast<ConfigWidget*>(wrapper.release());
4629
}
4730

4831

Common/Qt/Options/ConfigWidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "Common/Cpp/Options/ConfigOption.h"
1111

12+
class QWidget;
13+
1214
namespace PokemonAutomation{
1315

1416

@@ -25,7 +27,6 @@ namespace PokemonAutomation{
2527
// pointer can get the actual QWidget.
2628
class ConfigWidget : public UiComponent, protected ConfigOption::Listener{
2729
public:
28-
// Temporary for refactoring.
2930
static ConfigWidget* make_from_option(ConfigOption& option, QWidget* parent);
3031

3132

SerialPrograms/Source/CommonFramework/Panels/UI/SettingsPanelWidget.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ QWidget* SettingsPanelWidget::make_options(QWidget& parent){
5656
QVBoxLayout* options_layout = new QVBoxLayout(options_widget);
5757
options_layout->setAlignment(Qt::AlignTop);
5858

59-
6059
SettingsPanelInstance& instance = static_cast<SettingsPanelInstance&>(m_instance);
61-
m_options = static_cast<BatchWidget*>(instance.m_options.make_QtWidget(parent));
60+
m_options = static_cast<BatchWidget*>(ConfigWidget::make_from_option(instance.m_options, &parent));
6261
options_layout->addWidget(m_options);
6362
options_layout->addStretch();
6463

SerialPrograms/Source/ComputerPrograms/Framework/ComputerProgramWidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ComputerProgramWidget::ComputerProgramWidget(
5959
QVBoxLayout* scroll_layout = new QVBoxLayout(scroll_inner);
6060
scroll_layout->setAlignment(Qt::AlignTop);
6161

62-
m_options = option.options().make_QtWidget(*this);
62+
m_options = ConfigWidget::make_from_option(option.options(), this);
6363
scroll_layout->addWidget(&m_options->widget());
6464

6565
scroll_layout->addStretch(1);

SerialPrograms/Source/ML/Programs/ML_LabelImagesWidget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ LabelImages_Widget::LabelImages_Widget(
112112
scroll_layout->addLayout(annotation_row);
113113

114114
// add a dropdown menu for user to pick whether to choose from pokemon form label or custom label
115-
ConfigWidget* label_type_widget = program.LABEL_TYPE.make_QtWidget(*scroll_inner);
115+
ConfigWidget* label_type_widget = ConfigWidget::make_from_option(program.LABEL_TYPE, scroll_inner);
116116
annotation_row->addWidget(&label_type_widget->widget(), 0);
117117

118-
ConfigWidget* pokemon_label_widget = program.FORM_LABEL.make_QtWidget(*scroll_inner);
118+
ConfigWidget* pokemon_label_widget = ConfigWidget::make_from_option(program.FORM_LABEL, scroll_inner);
119119
annotation_row->addWidget(&pokemon_label_widget->widget(), 2);
120-
ConfigWidget* custom_label_widget = program.CUSTOM_SET_LABEL.make_QtWidget(*scroll_inner);
120+
ConfigWidget* custom_label_widget = ConfigWidget::make_from_option(program.CUSTOM_SET_LABEL, scroll_inner);
121121
annotation_row->addWidget(&custom_label_widget->widget(), 2);
122-
ConfigWidget* manual_input_label_widget = program.MANUAL_LABEL.make_QtWidget(*scroll_inner);
122+
ConfigWidget* manual_input_label_widget = ConfigWidget::make_from_option(program.MANUAL_LABEL, scroll_inner);
123123
annotation_row->addWidget(&manual_input_label_widget->widget(), 2);
124124
QPushButton* load_custom_set_button = new QPushButton("Load Custom Set", scroll_inner);
125125
annotation_row->addWidget(load_custom_set_button, 2);

SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_CommandRow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ CommandRow::CommandRow(
5252
layout0->addLayout(layout1, CONSOLE_SETTINGS_STRETCH_L0_RIGHT);
5353
layout1->setContentsMargins(0, 0, 0, 0);
5454

55-
ConfigWidget* console_type_box = console_type.make_QtWidget(*this);
55+
ConfigWidget* console_type_box = ConfigWidget::make_from_option(console_type, this);
5656
layout1->addWidget(&console_type_box->widget());
5757

5858

SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_MultiSwitchProgramWidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ MultiSwitchProgramWidget2::MultiSwitchProgramWidget2(
9090
m_system = new MultiSwitchSystemWidget(*this, m_session.system(), m_session.instance_id());
9191
scroll_layout->addWidget(m_system);
9292

93-
m_options = option.options().make_QtWidget(*this);
93+
m_options = ConfigWidget::make_from_option(option.options(), this);
9494
scroll_layout->addWidget(&m_options->widget());
9595

9696
scroll_layout->addStretch(1);

SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SingleSwitchProgramWidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ SingleSwitchProgramWidget2::SingleSwitchProgramWidget2(
9393
);
9494
scroll_layout->addWidget(m_system);
9595

96-
m_options = option.options().make_QtWidget(*this);
96+
m_options = ConfigWidget::make_from_option(option.options(), this);
9797
scroll_layout->addWidget(&m_options->widget());
9898

9999
scroll_layout->addStretch(1);

0 commit comments

Comments
 (0)