forked from suyu/suyu
shared_widget: Implement radio groups
This commit is contained in:
parent
bc4ad5e62d
commit
8a4cb3f902
2 changed files with 73 additions and 1 deletions
|
@ -23,6 +23,7 @@
|
|||
#include <QLineEdit>
|
||||
#include <QObject>
|
||||
#include <QPushButton>
|
||||
#include <QRadioButton>
|
||||
#include <QRegularExpression>
|
||||
#include <QSizePolicy>
|
||||
#include <QSlider>
|
||||
|
@ -171,6 +172,65 @@ QWidget* Widget::CreateCombobox(std::function<std::string()>& serializer,
|
|||
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,
|
||||
std::function<void()>& restore_func,
|
||||
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;
|
||||
case Settings::Specialization::Countable:
|
||||
return RequestType::SpinBox;
|
||||
case Settings::Specialization::Radio:
|
||||
return RequestType::RadioGroup;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -438,7 +500,11 @@ void Widget::SetupComponent(const QString& label, std::function<void()>& load_fu
|
|||
if (setting.TypeId() == typeid(bool)) {
|
||||
data_component = CreateCheckBox(&setting, label, serializer, restore_func, touch);
|
||||
} 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) ||
|
||||
type == typeid(s64) || type == typeid(u8)) {
|
||||
switch (request) {
|
||||
|
|
|
@ -22,6 +22,7 @@ class QObject;
|
|||
class QPushButton;
|
||||
class QSlider;
|
||||
class QSpinBox;
|
||||
class QRadioButton;
|
||||
|
||||
namespace Settings {
|
||||
class BasicSetting;
|
||||
|
@ -38,6 +39,7 @@ enum class RequestType {
|
|||
LineEdit,
|
||||
HexEdit,
|
||||
DateTimeEdit,
|
||||
RadioGroup,
|
||||
MaxEnum,
|
||||
};
|
||||
|
||||
|
@ -91,6 +93,7 @@ public:
|
|||
QSlider* slider{};
|
||||
QComboBox* combobox{};
|
||||
QDateTimeEdit* date_time_edit{};
|
||||
std::vector<std::pair<u32, QRadioButton*>> radio_buttons{};
|
||||
|
||||
private:
|
||||
void SetupComponent(const QString& label, std::function<void()>& load_func, bool managed,
|
||||
|
@ -106,6 +109,9 @@ private:
|
|||
QWidget* CreateCombobox(std::function<std::string()>& serializer,
|
||||
std::function<void()>& restore_func,
|
||||
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,
|
||||
std::function<void()>& restore_func, const std::function<void()>& touch,
|
||||
bool managed = true);
|
||||
|
|
Loading…
Reference in a new issue