mirror of
				https://git.suyu.dev/suyu/suyu.git
				synced 2025-10-30 22:36:46 +08:00 
			
		
		
		
	shared_widget: Implement radio groups
This commit is contained in:
		
							parent
							
								
									bc4ad5e62d
								
							
						
					
					
						commit
						8a4cb3f902
					
				| @ -23,6 +23,7 @@ | |||||||
| #include <QLineEdit> | #include <QLineEdit> | ||||||
| #include <QObject> | #include <QObject> | ||||||
| #include <QPushButton> | #include <QPushButton> | ||||||
|  | #include <QRadioButton> | ||||||
| #include <QRegularExpression> | #include <QRegularExpression> | ||||||
| #include <QSizePolicy> | #include <QSizePolicy> | ||||||
| #include <QSlider> | #include <QSlider> | ||||||
| @ -171,6 +172,65 @@ QWidget* Widget::CreateCombobox(std::function<std::string()>& serializer, | |||||||
|     return combobox; |     return combobox; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | QWidget* Widget::CreateRadioGroup(std::function<std::string()>& serializer, | ||||||
|  |                                   std::function<void()>& restore_func, | ||||||
|  |                                   const std::function<void()>& touch) { | ||||||
|  |     const auto type = setting.EnumIndex(); | ||||||
|  | 
 | ||||||
|  |     QWidget* group = new QWidget(this); | ||||||
|  |     QHBoxLayout* layout = new QHBoxLayout(group); | ||||||
|  |     layout->setContentsMargins(0, 0, 0, 0); | ||||||
|  |     group->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | ||||||
|  | 
 | ||||||
|  |     const ComboboxTranslations* enumeration{nullptr}; | ||||||
|  |     if (combobox_enumerations.contains(type)) { | ||||||
|  |         enumeration = &combobox_enumerations.at(type); | ||||||
|  |         for (const auto& [id, name] : *enumeration) { | ||||||
|  |             QRadioButton* radio_button = new QRadioButton(name, group); | ||||||
|  |             layout->addWidget(radio_button); | ||||||
|  |             radio_buttons.push_back({id, radio_button}); | ||||||
|  |         } | ||||||
|  |     } else { | ||||||
|  |         return group; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const auto get_selected = [=]() -> u32 { | ||||||
|  |         for (const auto& [id, button] : radio_buttons) { | ||||||
|  |             if (button->isChecked()) { | ||||||
|  |                 return id; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return -1; | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     const auto set_index = [=](u32 value) { | ||||||
|  |         for (const auto& [id, button] : radio_buttons) { | ||||||
|  |             button->setChecked(id == value); | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     const u32 setting_value = std::stoi(setting.ToString()); | ||||||
|  |     set_index(setting_value); | ||||||
|  | 
 | ||||||
|  |     serializer = [get_selected]() { | ||||||
|  |         int current = get_selected(); | ||||||
|  |         return std::to_string(current); | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     restore_func = [this, set_index]() { | ||||||
|  |         const u32 global_value = std::stoi(RelevantDefault(setting)); | ||||||
|  |         set_index(global_value); | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     if (!Settings::IsConfiguringGlobal()) { | ||||||
|  |         for (const auto& [id, button] : radio_buttons) { | ||||||
|  |             QObject::connect(button, &QAbstractButton::clicked, [touch]() { touch(); }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return group; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| QWidget* Widget::CreateLineEdit(std::function<std::string()>& serializer, | QWidget* Widget::CreateLineEdit(std::function<std::string()>& serializer, | ||||||
|                                 std::function<void()>& restore_func, |                                 std::function<void()>& restore_func, | ||||||
|                                 const std::function<void()>& touch, bool managed) { |                                 const std::function<void()>& touch, bool managed) { | ||||||
| @ -410,6 +470,8 @@ void Widget::SetupComponent(const QString& label, std::function<void()>& load_fu | |||||||
|             return RequestType::Slider; |             return RequestType::Slider; | ||||||
|         case Settings::Specialization::Countable: |         case Settings::Specialization::Countable: | ||||||
|             return RequestType::SpinBox; |             return RequestType::SpinBox; | ||||||
|  |         case Settings::Specialization::Radio: | ||||||
|  |             return RequestType::RadioGroup; | ||||||
|         default: |         default: | ||||||
|             break; |             break; | ||||||
|         } |         } | ||||||
| @ -438,7 +500,11 @@ void Widget::SetupComponent(const QString& label, std::function<void()>& load_fu | |||||||
|     if (setting.TypeId() == typeid(bool)) { |     if (setting.TypeId() == typeid(bool)) { | ||||||
|         data_component = CreateCheckBox(&setting, label, serializer, restore_func, touch); |         data_component = CreateCheckBox(&setting, label, serializer, restore_func, touch); | ||||||
|     } else if (setting.IsEnum()) { |     } else if (setting.IsEnum()) { | ||||||
|         data_component = CreateCombobox(serializer, restore_func, touch); |         if (request == RequestType::RadioGroup) { | ||||||
|  |             data_component = CreateRadioGroup(serializer, restore_func, touch); | ||||||
|  |         } else { | ||||||
|  |             data_component = CreateCombobox(serializer, restore_func, touch); | ||||||
|  |         } | ||||||
|     } else if (type == typeid(u32) || type == typeid(int) || type == typeid(u16) || |     } else if (type == typeid(u32) || type == typeid(int) || type == typeid(u16) || | ||||||
|                type == typeid(s64) || type == typeid(u8)) { |                type == typeid(s64) || type == typeid(u8)) { | ||||||
|         switch (request) { |         switch (request) { | ||||||
|  | |||||||
| @ -22,6 +22,7 @@ class QObject; | |||||||
| class QPushButton; | class QPushButton; | ||||||
| class QSlider; | class QSlider; | ||||||
| class QSpinBox; | class QSpinBox; | ||||||
|  | class QRadioButton; | ||||||
| 
 | 
 | ||||||
| namespace Settings { | namespace Settings { | ||||||
| class BasicSetting; | class BasicSetting; | ||||||
| @ -38,6 +39,7 @@ enum class RequestType { | |||||||
|     LineEdit, |     LineEdit, | ||||||
|     HexEdit, |     HexEdit, | ||||||
|     DateTimeEdit, |     DateTimeEdit, | ||||||
|  |     RadioGroup, | ||||||
|     MaxEnum, |     MaxEnum, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| @ -91,6 +93,7 @@ public: | |||||||
|     QSlider* slider{}; |     QSlider* slider{}; | ||||||
|     QComboBox* combobox{}; |     QComboBox* combobox{}; | ||||||
|     QDateTimeEdit* date_time_edit{}; |     QDateTimeEdit* date_time_edit{}; | ||||||
|  |     std::vector<std::pair<u32, QRadioButton*>> radio_buttons{}; | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     void SetupComponent(const QString& label, std::function<void()>& load_func, bool managed, |     void SetupComponent(const QString& label, std::function<void()>& load_func, bool managed, | ||||||
| @ -106,6 +109,9 @@ private: | |||||||
|     QWidget* CreateCombobox(std::function<std::string()>& serializer, |     QWidget* CreateCombobox(std::function<std::string()>& serializer, | ||||||
|                             std::function<void()>& restore_func, |                             std::function<void()>& restore_func, | ||||||
|                             const std::function<void()>& touch); |                             const std::function<void()>& touch); | ||||||
|  |     QWidget* CreateRadioGroup(std::function<std::string()>& serializer, | ||||||
|  |                               std::function<void()>& restore_func, | ||||||
|  |                               const std::function<void()>& touch); | ||||||
|     QWidget* CreateLineEdit(std::function<std::string()>& serializer, |     QWidget* CreateLineEdit(std::function<std::string()>& serializer, | ||||||
|                             std::function<void()>& restore_func, const std::function<void()>& touch, |                             std::function<void()>& restore_func, const std::function<void()>& touch, | ||||||
|                             bool managed = true); |                             bool managed = true); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user