summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar lat9nq2023-06-06 15:45:44 -0400
committerGravatar lat9nq2023-07-21 10:56:07 -0400
commitd7dd023409d01b09631e02a2dff591c847de32b3 (patch)
tree3054b8771ca058f18429799aa29c7b39c27ad900 /src
parentandroid-config: Adapt settings rework (diff)
downloadyuzu-d7dd023409d01b09631e02a2dff591c847de32b3.tar.gz
yuzu-d7dd023409d01b09631e02a2dff591c847de32b3.tar.xz
yuzu-d7dd023409d01b09631e02a2dff591c847de32b3.zip
shared_widget: Refactor again
Starting with combobox Putting code specific to the sub-widget in their own function.
Diffstat (limited to 'src')
-rw-r--r--src/yuzu/configuration/shared_widget.cpp163
-rw-r--r--src/yuzu/configuration/shared_widget.h10
2 files changed, 121 insertions, 52 deletions
diff --git a/src/yuzu/configuration/shared_widget.cpp b/src/yuzu/configuration/shared_widget.cpp
index d5b2bd60e..564c46e5e 100644
--- a/src/yuzu/configuration/shared_widget.cpp
+++ b/src/yuzu/configuration/shared_widget.cpp
@@ -115,23 +115,18 @@ QHBoxLayout* Widget::CreateCheckBox(Settings::BasicSetting* bool_setting, const
115 return layout; 115 return layout;
116} 116}
117 117
118void Widget::CreateCombobox(const QString& label, std::function<void()>& load_func, bool managed, 118QWidget* Widget::CreateCombobox(std::function<std::string()>& serializer,
119 Settings::BasicSetting* const other_setting) { 119 std::function<void()>& restore_func,
120 created = true; 120 const std::function<void()>& touched) {
121
122 const auto type = setting.TypeId(); 121 const auto type = setting.TypeId();
123 122
124 QLayout* layout = new QHBoxLayout(this);
125
126 QLabel* qt_label = CreateLabel(label);
127 combobox = new QComboBox(this); 123 combobox = new QComboBox(this);
128 combobox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 124 combobox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
129 125
130 layout->addWidget(qt_label); 126 if (!Settings::IsConfiguringGlobal()) {
131 layout->addWidget(combobox); 127 QObject::connect(combobox, QOverload<int>::of(&QComboBox::activated),
132 128 [touched]() { touched(); });
133 layout->setSpacing(6); 129 }
134 layout->setContentsMargins(0, 0, 0, 0);
135 130
136 const ComboboxTranslations* enumeration{nullptr}; 131 const ComboboxTranslations* enumeration{nullptr};
137 if (combobox_enumerations.contains(type)) { 132 if (combobox_enumerations.contains(type)) {
@@ -139,10 +134,8 @@ void Widget::CreateCombobox(const QString& label, std::function<void()>& load_fu
139 for (const auto& [id, name] : *enumeration) { 134 for (const auto& [id, name] : *enumeration) {
140 combobox->addItem(name); 135 combobox->addItem(name);
141 } 136 }
142 } 137 } else {
143 138 return combobox;
144 if (!managed || enumeration == nullptr) {
145 return;
146 } 139 }
147 140
148 const auto find_index = [=](u32 value) -> int { 141 const auto find_index = [=](u32 value) -> int {
@@ -157,37 +150,17 @@ void Widget::CreateCombobox(const QString& label, std::function<void()>& load_fu
157 const u32 setting_value = std::stoi(setting.ToString()); 150 const u32 setting_value = std::stoi(setting.ToString());
158 combobox->setCurrentIndex(find_index(setting_value)); 151 combobox->setCurrentIndex(find_index(setting_value));
159 152
160 if (Settings::IsConfiguringGlobal()) { 153 serializer = [this, enumeration]() {
161 load_func = [=]() { 154 int current = combobox->currentIndex();
162 int current = combobox->currentIndex(); 155 return std::to_string(enumeration->at(current).first);
163 setting.LoadString(std::to_string(enumeration->at(current).first)); 156 };
164 };
165 } else {
166 restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
167 layout->addWidget(restore_button);
168
169 QObject::connect(restore_button, &QAbstractButton::clicked, [=](bool) {
170 restore_button->setEnabled(false);
171 restore_button->setVisible(false);
172
173 const u32 global_value = std::stoi(setting.ToStringGlobal());
174 combobox->setCurrentIndex(find_index(global_value));
175 });
176 157
177 QObject::connect(combobox, QOverload<int>::of(&QComboBox::activated), [=](int) { 158 restore_func = [this, find_index]() {
178 restore_button->setEnabled(true); 159 const u32 global_value = std::stoi(setting.ToStringGlobal());
179 restore_button->setVisible(true); 160 combobox->setCurrentIndex(find_index(global_value));
180 }); 161 };
181 162
182 load_func = [=]() { 163 return combobox;
183 bool using_global = !restore_button->isEnabled();
184 setting.SetGlobal(using_global);
185 if (!using_global) {
186 int current = combobox->currentIndex();
187 setting.LoadString(std::to_string(enumeration->at(current).first));
188 }
189 };
190 }
191} 164}
192 165
193void Widget::CreateLineEdit(const QString& label, std::function<void()>& load_func, bool managed, 166void Widget::CreateLineEdit(const QString& label, std::function<void()>& load_func, bool managed,
@@ -542,7 +515,99 @@ void Widget::CreateDateTimeEdit(const QString& label, std::function<void()>& loa
542 } 515 }
543} 516}
544 517
545bool Widget::Valid() { 518void Widget::SetupComponent(const QString& label, std::function<void()>& load_func, bool managed,
519 RequestType request, Settings::BasicSetting* other_setting) {
520 created = true;
521 const auto type = setting.TypeId();
522
523 QLayout* layout = new QHBoxLayout(this);
524 layout->setContentsMargins(0, 0, 0, 0);
525
526 const bool require_checkbox =
527 other_setting != nullptr && other_setting->TypeId() == typeid(bool);
528
529 if (other_setting != nullptr && other_setting->TypeId() != typeid(bool)) {
530 LOG_WARNING(Frontend,
531 "Extra setting specified but is not bool, refusing to create checkbox for it.");
532 }
533
534 if (require_checkbox) {
535 } else {
536 QLabel* qt_label = CreateLabel(label);
537 layout->addWidget(qt_label);
538 }
539
540 std::function<void()> touched = []() {};
541 std::function<std::string()> serializer = []() -> std::string { return {}; };
542 std::function<void()> restore_func = []() {};
543
544 QWidget* data_component{nullptr};
545
546 if (!Settings::IsConfiguringGlobal()) {
547 restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
548
549 touched = [this]() {
550 restore_button->setEnabled(true);
551 restore_button->setVisible(true);
552 };
553 }
554
555 if (setting.IsEnum()) {
556 data_component = CreateCombobox(serializer, restore_func, touched);
557 } else if (type == typeid(u32) || type == typeid(int) || type == typeid(u16) ||
558 type == typeid(s64) || type == typeid(u8)) {
559 switch (request) {
560 case RequestType::ComboBox:
561 data_component = CreateCombobox(serializer, restore_func, touched);
562 break;
563 default:
564 UNIMPLEMENTED();
565 }
566 } else if (type == typeid(std::string)) {
567 switch (request) {
568 case RequestType::ComboBox:
569 data_component = CreateCombobox(serializer, restore_func, touched);
570 break;
571 default:
572 UNIMPLEMENTED();
573 }
574 }
575
576 if (data_component == nullptr) {
577 LOG_ERROR(Frontend, "Failed to create widget for {}", setting.GetLabel());
578 created = false;
579 return;
580 }
581
582 layout->addWidget(data_component);
583
584 if (!managed) {
585 return;
586 }
587
588 if (Settings::IsConfiguringGlobal()) {
589 load_func = [this, serializer]() { setting.LoadString(serializer()); };
590 } else {
591 layout->addWidget(restore_button);
592
593 QObject::connect(restore_button, &QAbstractButton::clicked, [this, restore_func](bool) {
594 restore_button->setEnabled(false);
595 restore_button->setVisible(false);
596
597 restore_func();
598 });
599
600 load_func = [this, serializer]() {
601 bool using_global = !restore_button->isEnabled();
602 setting.SetGlobal(using_global);
603 if (!using_global) {
604 setting.LoadString(serializer());
605 }
606 };
607 }
608}
609
610bool Widget::Valid() const {
546 return created; 611 return created;
547} 612}
548 613
@@ -584,7 +649,7 @@ Widget::Widget(Settings::BasicSetting* setting_, const TranslationMap& translati
584 if (type == typeid(bool)) { 649 if (type == typeid(bool)) {
585 CreateCheckBox(&setting, label, load_func, managed); 650 CreateCheckBox(&setting, label, load_func, managed);
586 } else if (setting.IsEnum()) { 651 } else if (setting.IsEnum()) {
587 CreateCombobox(label, load_func, managed); 652 SetupComponent(label, load_func, managed, request, other_setting);
588 } else if (type == typeid(u32) || type == typeid(int) || type == typeid(u16) || 653 } else if (type == typeid(u32) || type == typeid(int) || type == typeid(u16) ||
589 type == typeid(s64) || type == typeid(u8)) { 654 type == typeid(s64) || type == typeid(u8)) {
590 switch (request) { 655 switch (request) {
@@ -598,7 +663,7 @@ Widget::Widget(Settings::BasicSetting* setting_, const TranslationMap& translati
598 CreateLineEdit(label, load_func, managed); 663 CreateLineEdit(label, load_func, managed);
599 break; 664 break;
600 case RequestType::ComboBox: 665 case RequestType::ComboBox:
601 CreateCombobox(label, load_func, managed); 666 SetupComponent(label, load_func, managed, request, other_setting);
602 break; 667 break;
603 case RequestType::DateTimeEdit: 668 case RequestType::DateTimeEdit:
604 CreateDateTimeEdit(label, load_func, managed, true, other_setting); 669 CreateDateTimeEdit(label, load_func, managed, true, other_setting);
@@ -620,7 +685,7 @@ Widget::Widget(Settings::BasicSetting* setting_, const TranslationMap& translati
620 CreateLineEdit(label, load_func, managed); 685 CreateLineEdit(label, load_func, managed);
621 break; 686 break;
622 case RequestType::ComboBox: 687 case RequestType::ComboBox:
623 CreateCombobox(label, load_func, false); 688 SetupComponent(label, load_func, managed, request, other_setting);
624 break; 689 break;
625 case RequestType::SpinBox: 690 case RequestType::SpinBox:
626 case RequestType::Slider: 691 case RequestType::Slider:
diff --git a/src/yuzu/configuration/shared_widget.h b/src/yuzu/configuration/shared_widget.h
index 6077f045d..2ed738a06 100644
--- a/src/yuzu/configuration/shared_widget.h
+++ b/src/yuzu/configuration/shared_widget.h
@@ -43,7 +43,7 @@ public:
43 const QString& string = QStringLiteral("")); 43 const QString& string = QStringLiteral(""));
44 virtual ~Widget(); 44 virtual ~Widget();
45 45
46 bool Valid(); 46 bool Valid() const;
47 47
48 [[nodiscard]] static QPushButton* CreateRestoreGlobalButton(bool using_global, QWidget* parent); 48 [[nodiscard]] static QPushButton* CreateRestoreGlobalButton(bool using_global, QWidget* parent);
49 49
@@ -56,12 +56,16 @@ public:
56 QDateTimeEdit* date_time_edit{}; 56 QDateTimeEdit* date_time_edit{};
57 57
58private: 58private:
59 void SetupComponent(const QString& label, std::function<void()>& load_func, bool managed,
60 RequestType request, Settings::BasicSetting* other_setting);
61
59 QLabel* CreateLabel(const QString& text); 62 QLabel* CreateLabel(const QString& text);
60 QHBoxLayout* CreateCheckBox(Settings::BasicSetting* bool_setting, const QString& label, 63 QHBoxLayout* CreateCheckBox(Settings::BasicSetting* bool_setting, const QString& label,
61 std::function<void()>& load_func, bool managed); 64 std::function<void()>& load_func, bool managed);
62 65
63 void CreateCombobox(const QString& label, std::function<void()>& load_func, bool managed, 66 QWidget* CreateCombobox(std::function<std::string()>& serializer,
64 Settings::BasicSetting* const other_setting = nullptr); 67 std::function<void()>& restore_func,
68 const std::function<void()>& touched);
65 void CreateLineEdit(const QString& label, std::function<void()>& load_func, bool managed, 69 void CreateLineEdit(const QString& label, std::function<void()>& load_func, bool managed,
66 Settings::BasicSetting* const other_setting = nullptr); 70 Settings::BasicSetting* const other_setting = nullptr);
67 void CreateHexEdit(const QString& label, std::function<void()>& load_func, bool managed, 71 void CreateHexEdit(const QString& label, std::function<void()>& load_func, bool managed,