2023-05-08 22:37:03 -04:00
|
|
|
#include <functional>
|
2023-05-10 17:57:25 -04:00
|
|
|
#include <limits>
|
2023-05-08 22:37:03 -04:00
|
|
|
#include <QCheckBox>
|
2023-05-18 23:05:21 -04:00
|
|
|
#include <QComboBox>
|
2023-05-10 17:57:25 -04:00
|
|
|
#include <QDateTimeEdit>
|
2023-05-08 22:37:03 -04:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSizePolicy>
|
2023-05-18 23:05:21 -04:00
|
|
|
#include <QSlider>
|
2023-05-09 15:46:07 -04:00
|
|
|
#include <QSpinBox>
|
2023-05-18 23:05:21 -04:00
|
|
|
#include <QStyle>
|
2023-05-08 22:37:03 -04:00
|
|
|
#include <QWidget>
|
|
|
|
#include <qabstractbutton.h>
|
2023-05-10 17:57:25 -04:00
|
|
|
#include <qabstractspinbox.h>
|
2023-06-09 16:53:26 -04:00
|
|
|
#include <qboxlayout.h>
|
2023-05-10 17:57:25 -04:00
|
|
|
#include <qnamespace.h>
|
2023-06-09 16:53:26 -04:00
|
|
|
#include <qpushbutton.h>
|
2023-05-18 23:19:08 -07:00
|
|
|
#include <qsizepolicy.h>
|
2023-05-10 17:57:25 -04:00
|
|
|
#include <qvalidator.h>
|
2023-05-09 16:36:09 -04:00
|
|
|
#include "common/common_types.h"
|
2023-05-08 22:37:03 -04:00
|
|
|
#include "common/settings.h"
|
2023-05-09 16:36:09 -04:00
|
|
|
#include "yuzu/configuration/configuration_shared.h"
|
2023-05-08 22:37:03 -04:00
|
|
|
#include "yuzu/configuration/shared_translation.h"
|
|
|
|
#include "yuzu/configuration/shared_widget.h"
|
|
|
|
|
|
|
|
namespace ConfigurationShared {
|
|
|
|
|
2023-05-18 23:19:08 -07:00
|
|
|
static int restore_button_count = 0;
|
|
|
|
|
2023-05-18 17:54:22 -04:00
|
|
|
QPushButton* Widget::CreateRestoreGlobalButton(bool using_global, QWidget* parent) {
|
2023-05-18 23:19:08 -07:00
|
|
|
restore_button_count++;
|
|
|
|
|
2023-05-09 14:26:03 -04:00
|
|
|
QStyle* style = parent->style();
|
2023-05-18 16:07:48 -04:00
|
|
|
QIcon* icon = new QIcon(style->standardIcon(QStyle::SP_LineEditClearButton));
|
2023-05-09 14:26:03 -04:00
|
|
|
QPushButton* restore_button = new QPushButton(*icon, QStringLiteral(""), parent);
|
2023-05-18 23:19:08 -07:00
|
|
|
restore_button->setObjectName(QStringLiteral("RestoreButton%1").arg(restore_button_count));
|
|
|
|
restore_button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
|
|
|
|
|
|
|
// Workaround for dark theme causing min-width to be much larger than 0
|
|
|
|
restore_button->setStyleSheet(
|
|
|
|
QStringLiteral("QAbstractButton#%1 { min-width: 0px }").arg(restore_button->objectName()));
|
2023-05-08 22:37:03 -04:00
|
|
|
|
|
|
|
QSizePolicy sp_retain = restore_button->sizePolicy();
|
|
|
|
sp_retain.setRetainSizeWhenHidden(true);
|
|
|
|
restore_button->setSizePolicy(sp_retain);
|
|
|
|
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button->setEnabled(!using_global);
|
|
|
|
restore_button->setVisible(!using_global);
|
2023-05-09 14:26:03 -04:00
|
|
|
|
|
|
|
return restore_button;
|
2023-05-08 22:37:03 -04:00
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QLabel* Widget::CreateLabel(const QString& text) {
|
|
|
|
QLabel* qt_label = new QLabel(text, this->parent);
|
|
|
|
qt_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
|
|
return qt_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
QHBoxLayout* Widget::CreateCheckBox(Settings::BasicSetting* bool_setting, const QString& label,
|
|
|
|
std::function<void()>& load_func, bool managed) {
|
2023-05-08 22:37:03 -04:00
|
|
|
created = true;
|
|
|
|
|
|
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
|
|
|
|
|
|
checkbox = new QCheckBox(label, this);
|
2023-06-09 16:53:26 -04:00
|
|
|
checkbox->setCheckState(bool_setting->ToString() == "true" ? Qt::CheckState::Checked
|
|
|
|
: Qt::CheckState::Unchecked);
|
|
|
|
checkbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-05-18 17:54:22 -04:00
|
|
|
if (!bool_setting->Save() && !Settings::IsConfiguringGlobal() && runtime_lock) {
|
|
|
|
checkbox->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:37:03 -04:00
|
|
|
layout->addWidget(checkbox);
|
2023-06-09 16:53:26 -04:00
|
|
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
|
|
if (!managed) {
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:37:03 -04:00
|
|
|
if (Settings::IsConfiguringGlobal()) {
|
|
|
|
load_func = [=]() {
|
2023-06-09 16:53:26 -04:00
|
|
|
bool_setting->LoadString(checkbox->checkState() == Qt::Checked ? "true" : "false");
|
2023-05-08 22:37:03 -04:00
|
|
|
};
|
|
|
|
} else {
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button =
|
|
|
|
CreateRestoreGlobalButton(bool_setting->UsingGlobal() && setting.UsingGlobal(), this);
|
2023-05-08 22:37:03 -04:00
|
|
|
layout->addWidget(restore_button);
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QObject::connect(checkbox, &QCheckBox::stateChanged, [=](int) {
|
2023-05-08 22:37:03 -04:00
|
|
|
restore_button->setVisible(true);
|
|
|
|
restore_button->setEnabled(true);
|
|
|
|
});
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QObject::connect(restore_button, &QAbstractButton::clicked, [=](bool) {
|
|
|
|
checkbox->setCheckState(bool_setting->ToStringGlobal() == "true" ? Qt::Checked
|
|
|
|
: Qt::Unchecked);
|
2023-05-08 22:37:03 -04:00
|
|
|
restore_button->setEnabled(false);
|
|
|
|
restore_button->setVisible(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
load_func = [=]() {
|
|
|
|
bool using_global = !restore_button->isEnabled();
|
2023-06-09 16:53:26 -04:00
|
|
|
bool_setting->SetGlobal(using_global);
|
2023-05-08 22:37:03 -04:00
|
|
|
if (!using_global) {
|
2023-06-09 16:53:26 -04:00
|
|
|
bool_setting->LoadString(checkbox->checkState() == Qt::Checked ? "true" : "false");
|
2023-05-08 22:37:03 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
return layout;
|
2023-05-08 22:37:03 -04:00
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
void Widget::CreateCombobox(const QString& label, std::function<void()>& load_func, bool managed,
|
|
|
|
Settings::BasicSetting* const other_setting) {
|
2023-05-08 22:37:03 -04:00
|
|
|
created = true;
|
|
|
|
|
|
|
|
const auto type = setting.TypeId();
|
|
|
|
|
|
|
|
QLayout* layout = new QHBoxLayout(this);
|
|
|
|
|
2023-05-18 23:19:08 -07:00
|
|
|
QLabel* qt_label = CreateLabel(label);
|
2023-05-08 22:37:03 -04:00
|
|
|
combobox = new QComboBox(this);
|
2023-05-18 23:19:08 -07:00
|
|
|
combobox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2023-05-08 22:37:03 -04:00
|
|
|
|
|
|
|
layout->addWidget(qt_label);
|
|
|
|
layout->addWidget(combobox);
|
|
|
|
|
|
|
|
layout->setSpacing(6);
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
2023-05-18 22:17:36 -04:00
|
|
|
const ComboboxTranslations* enumeration{nullptr};
|
|
|
|
if (combobox_enumerations.contains(type)) {
|
|
|
|
enumeration = &combobox_enumerations.at(type);
|
|
|
|
for (const auto& [id, name] : *enumeration) {
|
|
|
|
combobox->addItem(name);
|
|
|
|
}
|
2023-06-09 16:53:26 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 22:17:36 -04:00
|
|
|
if (!managed || enumeration == nullptr) {
|
|
|
|
return;
|
2023-05-18 16:06:54 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 22:17:36 -04:00
|
|
|
const auto find_index = [=](u32 value) -> int {
|
|
|
|
for (u32 i = 0; i < enumeration->size(); i++) {
|
|
|
|
if (enumeration->at(i).first == value) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32 setting_value = std::stoi(setting.ToString());
|
|
|
|
combobox->setCurrentIndex(find_index(setting_value));
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
if (Settings::IsConfiguringGlobal()) {
|
2023-05-18 22:17:36 -04:00
|
|
|
load_func = [=]() {
|
|
|
|
int current = combobox->currentIndex();
|
|
|
|
setting.LoadString(std::to_string(enumeration->at(current).first));
|
|
|
|
};
|
2023-06-09 16:53:26 -04:00
|
|
|
} else {
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
|
2023-05-08 22:37:03 -04:00
|
|
|
layout->addWidget(restore_button);
|
|
|
|
|
2023-05-18 22:17:36 -04:00
|
|
|
QObject::connect(restore_button, &QAbstractButton::clicked, [=](bool) {
|
2023-05-08 22:37:03 -04:00
|
|
|
restore_button->setEnabled(false);
|
|
|
|
restore_button->setVisible(false);
|
|
|
|
|
2023-05-18 22:17:36 -04:00
|
|
|
const u32 global_value = std::stoi(setting.ToStringGlobal());
|
|
|
|
combobox->setCurrentIndex(find_index(global_value));
|
2023-05-08 22:37:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(combobox, QOverload<int>::of(&QComboBox::activated), [=](int) {
|
|
|
|
restore_button->setEnabled(true);
|
|
|
|
restore_button->setVisible(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
load_func = [=]() {
|
|
|
|
bool using_global = !restore_button->isEnabled();
|
|
|
|
setting.SetGlobal(using_global);
|
|
|
|
if (!using_global) {
|
2023-05-18 22:17:36 -04:00
|
|
|
int current = combobox->currentIndex();
|
|
|
|
setting.LoadString(std::to_string(enumeration->at(current).first));
|
2023-05-08 22:37:03 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
void Widget::CreateLineEdit(const QString& label, std::function<void()>& load_func, bool managed,
|
|
|
|
Settings::BasicSetting* other_setting) {
|
|
|
|
const bool has_checkbox = other_setting != nullptr;
|
|
|
|
if (has_checkbox && other_setting->TypeId() != typeid(bool)) {
|
|
|
|
LOG_WARNING(Frontend, "Extra setting requested but setting is not boolean");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:37:03 -04:00
|
|
|
created = true;
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QHBoxLayout* layout{nullptr};
|
|
|
|
std::function<void()> checkbox_load_func = []() {};
|
|
|
|
|
|
|
|
if (has_checkbox) {
|
|
|
|
layout = CreateCheckBox(other_setting, label, checkbox_load_func, managed);
|
|
|
|
} else {
|
|
|
|
layout = new QHBoxLayout(this);
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
QLabel* q_label = CreateLabel(label);
|
|
|
|
layout->addWidget(q_label);
|
|
|
|
}
|
2023-05-08 22:37:03 -04:00
|
|
|
|
|
|
|
const QString text = QString::fromStdString(setting.ToString());
|
2023-06-09 16:53:26 -04:00
|
|
|
line_edit = new QLineEdit(this);
|
2023-05-08 22:37:03 -04:00
|
|
|
line_edit->setText(text);
|
|
|
|
|
|
|
|
layout->addWidget(line_edit);
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
if (!managed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Settings::IsConfiguringGlobal()) {
|
2023-05-08 22:37:03 -04:00
|
|
|
load_func = [=]() {
|
2023-06-09 16:53:26 -04:00
|
|
|
checkbox_load_func();
|
|
|
|
|
2023-05-08 22:37:03 -04:00
|
|
|
std::string load_text = line_edit->text().toStdString();
|
|
|
|
setting.LoadString(load_text);
|
|
|
|
};
|
2023-06-09 16:53:26 -04:00
|
|
|
} else {
|
|
|
|
if (!has_checkbox) {
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
|
2023-06-09 16:53:26 -04:00
|
|
|
layout->addWidget(restore_button);
|
|
|
|
}
|
2023-05-08 22:37:03 -04:00
|
|
|
|
|
|
|
QObject::connect(restore_button, &QAbstractButton::clicked, [&](bool) {
|
|
|
|
restore_button->setEnabled(false);
|
|
|
|
restore_button->setVisible(false);
|
|
|
|
|
|
|
|
line_edit->setText(QString::fromStdString(setting.ToStringGlobal()));
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(line_edit, &QLineEdit::textChanged, [&](QString) {
|
|
|
|
restore_button->setEnabled(true);
|
|
|
|
restore_button->setVisible(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
load_func = [=]() {
|
2023-06-09 16:53:26 -04:00
|
|
|
checkbox_load_func();
|
|
|
|
|
2023-05-08 22:37:03 -04:00
|
|
|
bool using_global = !restore_button->isEnabled();
|
|
|
|
setting.SetGlobal(using_global);
|
|
|
|
if (!using_global) {
|
|
|
|
setting.LoadString(line_edit->text().toStdString());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
void Widget::CreateSlider(const QString& label, bool reversed, float multiplier,
|
2023-05-18 17:54:22 -04:00
|
|
|
std::function<void()>& load_func, bool managed, const QString& format,
|
2023-06-09 16:53:26 -04:00
|
|
|
Settings::BasicSetting* const other_setting) {
|
2023-05-08 22:37:03 -04:00
|
|
|
created = true;
|
|
|
|
|
|
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
|
|
slider = new QSlider(Qt::Horizontal, this);
|
2023-06-09 16:53:26 -04:00
|
|
|
QLabel* qt_label = new QLabel(label, this);
|
2023-05-08 22:37:03 -04:00
|
|
|
QLabel* feedback = new QLabel(this);
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
layout->addWidget(qt_label);
|
2023-05-08 22:37:03 -04:00
|
|
|
layout->addWidget(slider);
|
|
|
|
layout->addWidget(feedback);
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
qt_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2023-05-08 22:37:03 -04:00
|
|
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
|
|
int max_val = std::stoi(setting.MaxVal());
|
|
|
|
|
2023-05-18 17:54:22 -04:00
|
|
|
const QString use_format = format == QStringLiteral("") ? QStringLiteral("%1") : format;
|
|
|
|
|
2023-05-08 22:37:03 -04:00
|
|
|
QObject::connect(slider, &QAbstractSlider::valueChanged, [=](int value) {
|
2023-05-18 17:54:22 -04:00
|
|
|
int present = (reversed ? max_val - value : value) * multiplier + 0.5f;
|
|
|
|
feedback->setText(use_format.arg(QVariant::fromValue(present).value<QString>()));
|
2023-05-08 22:37:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
slider->setMinimum(std::stoi(setting.MinVal()));
|
|
|
|
slider->setMaximum(max_val);
|
2023-05-18 17:54:22 -04:00
|
|
|
slider->setValue(std::stoi(setting.ToString()));
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
slider->setInvertedAppearance(reversed);
|
|
|
|
|
|
|
|
if (!managed) {
|
|
|
|
return;
|
2023-05-08 22:37:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Settings::IsConfiguringGlobal()) {
|
|
|
|
load_func = [=]() { setting.LoadString(std::to_string(slider->value())); };
|
|
|
|
} else {
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
|
2023-05-08 22:37:03 -04:00
|
|
|
layout->addWidget(restore_button);
|
|
|
|
|
|
|
|
QObject::connect(restore_button, &QAbstractButton::clicked, [=](bool) {
|
|
|
|
slider->setValue(std::stoi(setting.ToStringGlobal()));
|
|
|
|
|
|
|
|
restore_button->setEnabled(false);
|
|
|
|
restore_button->setVisible(false);
|
|
|
|
});
|
|
|
|
|
2023-05-18 16:07:48 -04:00
|
|
|
QObject::connect(slider, &QAbstractSlider::valueChanged, [=]() {
|
2023-05-08 22:37:03 -04:00
|
|
|
restore_button->setEnabled(true);
|
|
|
|
restore_button->setVisible(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
load_func = [=]() {
|
|
|
|
bool using_global = !restore_button->isEnabled();
|
|
|
|
setting.SetGlobal(using_global);
|
|
|
|
if (!using_global) {
|
|
|
|
setting.LoadString(std::to_string(slider->value()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
void Widget::CreateSpinBox(const QString& label, std::function<void()>& load_func, bool managed,
|
2023-05-18 17:54:22 -04:00
|
|
|
const QString& suffix, Settings::BasicSetting* other_setting) {
|
2023-06-09 16:53:26 -04:00
|
|
|
const bool has_checkbox = other_setting != nullptr;
|
|
|
|
if (has_checkbox && other_setting->TypeId() != typeid(bool)) {
|
|
|
|
LOG_WARNING(Frontend, "Extra setting requested but setting is not boolean");
|
2023-05-10 17:57:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
created = true;
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QHBoxLayout* layout{nullptr};
|
|
|
|
std::function<void()> checkbox_load_func = []() {};
|
|
|
|
QLabel* q_label{nullptr};
|
2023-05-10 17:57:25 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
if (has_checkbox) {
|
|
|
|
layout = CreateCheckBox(other_setting, label, checkbox_load_func, managed);
|
|
|
|
} else {
|
|
|
|
layout = new QHBoxLayout(this);
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
q_label = CreateLabel(label);
|
|
|
|
layout->addWidget(q_label);
|
|
|
|
}
|
2023-05-10 17:57:25 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
const int min_val = std::stoi(setting.MinVal());
|
|
|
|
const int max_val = std::stoi(setting.MaxVal());
|
|
|
|
const int default_val = std::stoi(setting.ToString());
|
2023-05-10 17:57:25 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
spinbox = new QSpinBox(this);
|
|
|
|
spinbox->setRange(min_val, max_val);
|
|
|
|
spinbox->setValue(default_val);
|
2023-05-18 17:54:22 -04:00
|
|
|
spinbox->setSuffix(suffix);
|
2023-06-09 16:53:26 -04:00
|
|
|
spinbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2023-05-10 17:57:25 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
layout->insertWidget(1, spinbox);
|
2023-05-10 17:57:25 -04:00
|
|
|
|
|
|
|
if (Settings::IsConfiguringGlobal()) {
|
|
|
|
load_func = [=]() {
|
|
|
|
checkbox_load_func();
|
2023-06-09 16:53:26 -04:00
|
|
|
setting.LoadString(std::to_string(spinbox->value()));
|
2023-05-10 17:57:25 -04:00
|
|
|
};
|
|
|
|
} else {
|
2023-06-09 16:53:26 -04:00
|
|
|
if (!has_checkbox) {
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
|
|
|
|
layout->addWidget(restore_button);
|
2023-06-09 16:53:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QObject::connect(restore_button, &QAbstractButton::clicked,
|
|
|
|
[this](bool) { spinbox->setValue(std::stoi(setting.ToStringGlobal())); });
|
2023-05-10 17:57:25 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QObject::connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), [this](int) {
|
2023-05-10 17:57:25 -04:00
|
|
|
restore_button->setEnabled(true);
|
|
|
|
restore_button->setVisible(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
load_func = [=]() {
|
|
|
|
checkbox_load_func();
|
|
|
|
|
|
|
|
const bool using_global = !restore_button->isEnabled();
|
2023-06-09 16:53:26 -04:00
|
|
|
setting.SetGlobal(using_global);
|
2023-05-10 17:57:25 -04:00
|
|
|
if (!using_global) {
|
2023-06-09 16:53:26 -04:00
|
|
|
setting.LoadString(std::to_string(spinbox->value()));
|
2023-05-10 17:57:25 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
void Widget::CreateHexEdit(const QString& label, std::function<void()>& load_func, bool managed,
|
|
|
|
Settings::BasicSetting* const other_setting) {
|
|
|
|
CreateLineEdit(label, load_func, false, other_setting);
|
|
|
|
if (!created || !managed) {
|
2023-05-09 16:36:09 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QLayout* layout = this->layout();
|
|
|
|
|
|
|
|
auto to_hex = [=](const std::string& input) {
|
|
|
|
return QString::fromStdString(fmt::format("{:08x}", std::stoi(input)));
|
|
|
|
};
|
|
|
|
|
|
|
|
QRegExpValidator* regex =
|
|
|
|
new QRegExpValidator{QRegExp{QStringLiteral("^[0-9a-fA-F]{0,8}$")}, line_edit};
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
const QString default_val = to_hex(setting.ToString());
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-05-09 16:36:09 -04:00
|
|
|
line_edit->setText(default_val);
|
2023-06-09 16:53:26 -04:00
|
|
|
line_edit->setMaxLength(8);
|
|
|
|
line_edit->setValidator(regex);
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
auto hex_to_dec = [=]() -> std::string {
|
|
|
|
return std::to_string(std::stoul(line_edit->text().toStdString(), nullptr, 16));
|
|
|
|
};
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-05-09 16:36:09 -04:00
|
|
|
if (Settings::IsConfiguringGlobal()) {
|
|
|
|
load_func = [=]() {
|
2023-06-09 16:53:26 -04:00
|
|
|
other_setting->LoadString(checkbox->checkState() == Qt::Checked ? "true" : "false");
|
|
|
|
setting.LoadString(hex_to_dec());
|
2023-05-09 16:36:09 -04:00
|
|
|
};
|
|
|
|
} else {
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
|
2023-06-09 16:53:26 -04:00
|
|
|
layout->addWidget(restore_button);
|
|
|
|
|
2023-05-10 17:57:25 -04:00
|
|
|
QObject::connect(restore_button, &QAbstractButton::clicked, [=](bool) {
|
2023-06-09 16:53:26 -04:00
|
|
|
line_edit->setText(to_hex(setting.ToStringGlobal()));
|
|
|
|
checkbox->setCheckState(other_setting->ToStringGlobal() == "true" ? Qt::Checked
|
|
|
|
: Qt::Unchecked);
|
|
|
|
|
|
|
|
restore_button->setEnabled(false);
|
|
|
|
restore_button->setVisible(false);
|
2023-05-10 17:57:25 -04:00
|
|
|
});
|
2023-05-08 22:37:03 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QObject::connect(line_edit, &QLineEdit::textEdited, [&]() {
|
2023-05-08 22:37:03 -04:00
|
|
|
restore_button->setEnabled(true);
|
|
|
|
restore_button->setVisible(true);
|
|
|
|
});
|
2023-05-09 16:36:09 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QObject::connect(checkbox, &QAbstractButton::clicked, [&]() {
|
2023-05-09 15:46:07 -04:00
|
|
|
restore_button->setEnabled(true);
|
|
|
|
restore_button->setVisible(true);
|
|
|
|
});
|
2023-05-09 16:36:09 -04:00
|
|
|
|
|
|
|
load_func = [=]() {
|
2023-05-10 17:57:25 -04:00
|
|
|
const bool using_global = !restore_button->isEnabled();
|
2023-05-09 16:36:09 -04:00
|
|
|
other_setting->SetGlobal(using_global);
|
2023-06-09 16:53:26 -04:00
|
|
|
setting.SetGlobal(using_global);
|
|
|
|
|
2023-05-09 16:36:09 -04:00
|
|
|
if (!using_global) {
|
2023-06-09 16:53:26 -04:00
|
|
|
other_setting->LoadString(checkbox->checkState() == Qt::Checked ? "true" : "false");
|
|
|
|
setting.LoadString(hex_to_dec());
|
2023-05-09 16:36:09 -04:00
|
|
|
}
|
|
|
|
};
|
2023-05-09 15:46:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
void Widget::CreateDateTimeEdit(const QString& label, std::function<void()>& load_func,
|
|
|
|
bool managed, bool restrict,
|
|
|
|
Settings::BasicSetting* const other_setting) {
|
|
|
|
const bool has_checkbox = other_setting != nullptr;
|
|
|
|
if ((restrict && !has_checkbox) || (has_checkbox && other_setting->TypeId() != typeid(bool))) {
|
|
|
|
LOG_WARNING(Frontend, "Extra setting or restrict requested but is not boolean");
|
2023-05-10 17:57:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
created = true;
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
QHBoxLayout* layout{nullptr};
|
|
|
|
std::function<void()> checkbox_load_func = []() {};
|
|
|
|
|
|
|
|
if (has_checkbox) {
|
|
|
|
layout = CreateCheckBox(other_setting, label, checkbox_load_func, managed);
|
|
|
|
} else {
|
|
|
|
layout = new QHBoxLayout(this);
|
|
|
|
QLabel* q_label = CreateLabel(label);
|
|
|
|
layout->addWidget(q_label);
|
|
|
|
}
|
2023-05-10 17:57:25 -04:00
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
const bool disabled = other_setting->ToString() != "true";
|
2023-05-10 17:57:25 -04:00
|
|
|
const long long current_time = QDateTime::currentSecsSinceEpoch();
|
2023-06-09 16:53:26 -04:00
|
|
|
const s64 the_time = disabled ? current_time : std::stoll(setting.ToString());
|
2023-05-10 17:57:25 -04:00
|
|
|
const auto default_val = QDateTime::fromSecsSinceEpoch(the_time);
|
|
|
|
|
|
|
|
date_time_edit = new QDateTimeEdit(this);
|
|
|
|
date_time_edit->setDateTime(default_val);
|
|
|
|
date_time_edit->setMinimumDateTime(QDateTime::fromSecsSinceEpoch(0));
|
|
|
|
date_time_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
|
|
|
|
|
|
layout->insertWidget(1, date_time_edit);
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
if (!managed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-10 17:57:25 -04:00
|
|
|
if (Settings::IsConfiguringGlobal()) {
|
|
|
|
load_func = [=]() {
|
|
|
|
checkbox_load_func();
|
2023-06-09 16:53:26 -04:00
|
|
|
if (restrict && checkbox->checkState() == Qt::Unchecked) {
|
2023-05-10 17:57:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:07:48 -04:00
|
|
|
setting.LoadString(std::to_string(date_time_edit->dateTime().toSecsSinceEpoch()));
|
2023-05-10 17:57:25 -04:00
|
|
|
};
|
|
|
|
} else {
|
2023-06-09 16:53:26 -04:00
|
|
|
if (!has_checkbox) {
|
2023-05-18 17:54:22 -04:00
|
|
|
restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
|
2023-06-09 16:53:26 -04:00
|
|
|
layout->addWidget(restore_button);
|
|
|
|
}
|
|
|
|
|
2023-05-10 17:57:25 -04:00
|
|
|
auto get_clear_val = [=]() {
|
|
|
|
return QDateTime::fromSecsSinceEpoch([=]() {
|
2023-06-09 16:53:26 -04:00
|
|
|
if (restrict && checkbox->checkState() == Qt::Checked) {
|
2023-05-18 16:07:48 -04:00
|
|
|
return std::stoll(setting.ToStringGlobal());
|
2023-05-10 17:57:25 -04:00
|
|
|
}
|
|
|
|
return current_time;
|
|
|
|
}());
|
|
|
|
};
|
|
|
|
|
|
|
|
QObject::connect(restore_button, &QAbstractButton::clicked,
|
|
|
|
[=](bool) { date_time_edit->setDateTime(get_clear_val()); });
|
|
|
|
|
|
|
|
QObject::connect(date_time_edit, &QDateTimeEdit::editingFinished, [=]() {
|
|
|
|
if (date_time_edit->dateTime() != get_clear_val()) {
|
|
|
|
restore_button->setEnabled(true);
|
|
|
|
restore_button->setVisible(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
load_func = [=]() {
|
|
|
|
checkbox_load_func();
|
2023-06-09 16:53:26 -04:00
|
|
|
if (restrict && checkbox->checkState() == Qt::Unchecked) {
|
2023-05-10 17:57:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool using_global = !restore_button->isEnabled();
|
|
|
|
other_setting->SetGlobal(using_global);
|
|
|
|
if (!using_global) {
|
2023-05-18 16:07:48 -04:00
|
|
|
setting.LoadString(std::to_string(date_time_edit->dateTime().toSecsSinceEpoch()));
|
2023-05-10 17:57:25 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:37:03 -04:00
|
|
|
bool Widget::Valid() {
|
|
|
|
return created;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::~Widget() = default;
|
|
|
|
|
2023-06-09 16:53:26 -04:00
|
|
|
Widget::Widget(Settings::BasicSetting* setting_, const TranslationMap& translations_,
|
2023-05-18 22:17:36 -04:00
|
|
|
const ComboboxTranslationMap& combobox_translations_, QWidget* parent_,
|
|
|
|
bool runtime_lock_, std::forward_list<std::function<void(bool)>>& apply_funcs_,
|
|
|
|
RequestType request, bool managed, float multiplier,
|
|
|
|
Settings::BasicSetting* other_setting, const QString& string)
|
|
|
|
: QWidget(parent_), parent{parent_}, translations{translations_},
|
|
|
|
combobox_enumerations{combobox_translations_}, setting{*setting_}, apply_funcs{apply_funcs_},
|
|
|
|
runtime_lock{runtime_lock_} {
|
2023-05-08 22:37:03 -04:00
|
|
|
if (!Settings::IsConfiguringGlobal() && !setting.Switchable()) {
|
|
|
|
LOG_DEBUG(Frontend, "\"{}\" is not switchable, skipping...", setting.GetLabel());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto type = setting.TypeId();
|
|
|
|
const int id = setting.Id();
|
|
|
|
|
|
|
|
const auto [label, tooltip] = [&]() {
|
|
|
|
const auto& setting_label = setting.GetLabel();
|
|
|
|
if (translations.contains(id)) {
|
|
|
|
return std::pair{translations.at(id).first, translations.at(id).second};
|
|
|
|
}
|
|
|
|
LOG_WARNING(Frontend, "Translation table lacks entry for \"{}\"", setting_label);
|
|
|
|
return std::pair{QString::fromStdString(setting_label), QStringLiteral("")};
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (label == QStringLiteral("")) {
|
|
|
|
LOG_DEBUG(Frontend, "Translation table has emtpy entry for \"{}\", skipping...",
|
|
|
|
setting.GetLabel());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void()> load_func = []() {};
|
|
|
|
|
|
|
|
if (type == typeid(bool)) {
|
2023-05-18 17:54:22 -04:00
|
|
|
CreateCheckBox(&setting, label, load_func, managed);
|
2023-05-08 22:37:03 -04:00
|
|
|
} else if (setting.IsEnum()) {
|
2023-06-09 16:53:26 -04:00
|
|
|
CreateCombobox(label, load_func, managed);
|
|
|
|
} else if (type == typeid(u32) || type == typeid(int) || type == typeid(u16) ||
|
2023-05-18 17:54:22 -04:00
|
|
|
type == typeid(s64) || type == typeid(u8)) {
|
2023-05-08 22:37:03 -04:00
|
|
|
switch (request) {
|
|
|
|
case RequestType::Slider:
|
|
|
|
case RequestType::ReverseSlider:
|
2023-06-09 16:53:26 -04:00
|
|
|
CreateSlider(label, request == RequestType::ReverseSlider, multiplier, load_func,
|
2023-05-18 17:54:22 -04:00
|
|
|
managed, string);
|
2023-05-08 22:37:03 -04:00
|
|
|
break;
|
|
|
|
case RequestType::LineEdit:
|
|
|
|
case RequestType::Default:
|
2023-06-09 16:53:26 -04:00
|
|
|
CreateLineEdit(label, load_func, managed);
|
2023-05-08 22:37:03 -04:00
|
|
|
break;
|
|
|
|
case RequestType::ComboBox:
|
2023-06-09 16:53:26 -04:00
|
|
|
CreateCombobox(label, load_func, managed);
|
2023-05-08 22:37:03 -04:00
|
|
|
break;
|
2023-05-10 17:57:25 -04:00
|
|
|
case RequestType::DateTimeEdit:
|
2023-06-09 16:53:26 -04:00
|
|
|
CreateDateTimeEdit(label, load_func, managed, true, other_setting);
|
|
|
|
break;
|
2023-05-08 22:37:03 -04:00
|
|
|
case RequestType::SpinBox:
|
2023-06-09 16:53:26 -04:00
|
|
|
CreateSpinBox(label, load_func, managed, string, other_setting);
|
|
|
|
break;
|
2023-05-10 17:57:25 -04:00
|
|
|
case RequestType::HexEdit:
|
2023-06-09 16:53:26 -04:00
|
|
|
CreateHexEdit(label, load_func, managed, other_setting);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_WARNING(Frontend, "Requested widget is unimplemented.");
|
2023-05-08 22:37:03 -04:00
|
|
|
break;
|
|
|
|
}
|
2023-05-10 17:57:25 -04:00
|
|
|
} else if (type == typeid(std::string)) {
|
2023-05-18 17:54:22 -04:00
|
|
|
switch (request) {
|
|
|
|
case RequestType::Default:
|
|
|
|
case RequestType::LineEdit:
|
|
|
|
CreateLineEdit(label, load_func, managed);
|
|
|
|
break;
|
|
|
|
case RequestType::ComboBox:
|
|
|
|
CreateCombobox(label, load_func, false);
|
|
|
|
break;
|
|
|
|
case RequestType::SpinBox:
|
|
|
|
case RequestType::Slider:
|
|
|
|
case RequestType::ReverseSlider:
|
|
|
|
case RequestType::HexEdit:
|
|
|
|
case RequestType::DateTimeEdit:
|
|
|
|
case RequestType::MaxEnum:
|
|
|
|
LOG_WARNING(Frontend, "Requested widget is unimplemented.");
|
|
|
|
break;
|
|
|
|
}
|
2023-05-08 22:37:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!created) {
|
2023-05-09 16:36:09 -04:00
|
|
|
LOG_WARNING(Frontend, "No widget was created for \"{}\"", setting.GetLabel());
|
2023-05-08 22:37:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
apply_funcs.push_front([load_func, setting_](bool powered_on) {
|
|
|
|
if (setting_->RuntimeModfiable() || !powered_on) {
|
|
|
|
load_func();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
bool enable = runtime_lock || setting.RuntimeModfiable();
|
|
|
|
if (setting.Switchable() && Settings::IsConfiguringGlobal() && !runtime_lock) {
|
|
|
|
enable &= setting.UsingGlobal();
|
|
|
|
}
|
|
|
|
this->setEnabled(enable);
|
|
|
|
|
|
|
|
this->setVisible(Settings::IsConfiguringGlobal() || setting.Switchable());
|
|
|
|
|
|
|
|
this->setToolTip(tooltip);
|
|
|
|
}
|
|
|
|
} // namespace ConfigurationShared
|