shared_widget.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "yuzu/configuration/shared_widget.h"
  4. #include <functional>
  5. #include <limits>
  6. #include <typeindex>
  7. #include <typeinfo>
  8. #include <utility>
  9. #include <vector>
  10. #include <QAbstractButton>
  11. #include <QAbstractSlider>
  12. #include <QBoxLayout>
  13. #include <QCheckBox>
  14. #include <QComboBox>
  15. #include <QDateTime>
  16. #include <QDateTimeEdit>
  17. #include <QIcon>
  18. #include <QLabel>
  19. #include <QLayout>
  20. #include <QLineEdit>
  21. #include <QObject>
  22. #include <QPushButton>
  23. #include <QRadioButton>
  24. #include <QRegularExpression>
  25. #include <QSizePolicy>
  26. #include <QSlider>
  27. #include <QSpinBox>
  28. #include <QStyle>
  29. #include <QValidator>
  30. #include <QVariant>
  31. #include <QtCore/qglobal.h>
  32. #include <QtCore/qobjectdefs.h>
  33. #include <fmt/core.h>
  34. #include <qglobal.h>
  35. #include <qnamespace.h>
  36. #include "common/assert.h"
  37. #include "common/common_types.h"
  38. #include "common/logging/log.h"
  39. #include "common/settings.h"
  40. #include "common/settings_common.h"
  41. #include "yuzu/configuration/shared_translation.h"
  42. namespace ConfigurationShared {
  43. static int restore_button_count = 0;
  44. static std::string RelevantDefault(const Settings::BasicSetting& setting) {
  45. return Settings::IsConfiguringGlobal() ? setting.DefaultToString() : setting.ToStringGlobal();
  46. }
  47. static QString DefaultSuffix(QWidget* parent, Settings::BasicSetting& setting) {
  48. const auto tr = [parent](const char* text, const char* context) {
  49. return parent->tr(text, context);
  50. };
  51. if ((setting.Specialization() & Settings::SpecializationAttributeMask) ==
  52. Settings::Specialization::Percentage) {
  53. std::string context{fmt::format("{} percentage (e.g. 50%)", setting.GetLabel())};
  54. return tr("%", context.c_str());
  55. }
  56. return default_suffix;
  57. }
  58. QPushButton* Widget::CreateRestoreGlobalButton(bool using_global, QWidget* parent) {
  59. restore_button_count++;
  60. QStyle* style = parent->style();
  61. QIcon* icon = new QIcon(style->standardIcon(QStyle::SP_LineEditClearButton));
  62. QPushButton* restore_button = new QPushButton(*icon, QStringLiteral(), parent);
  63. restore_button->setObjectName(QStringLiteral("RestoreButton%1").arg(restore_button_count));
  64. restore_button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  65. // Workaround for dark theme causing min-width to be much larger than 0
  66. restore_button->setStyleSheet(
  67. QStringLiteral("QAbstractButton#%1 { min-width: 0px }").arg(restore_button->objectName()));
  68. QSizePolicy sp_retain = restore_button->sizePolicy();
  69. sp_retain.setRetainSizeWhenHidden(true);
  70. restore_button->setSizePolicy(sp_retain);
  71. restore_button->setEnabled(!using_global);
  72. restore_button->setVisible(!using_global);
  73. return restore_button;
  74. }
  75. QLabel* Widget::CreateLabel(const QString& text) {
  76. QLabel* qt_label = new QLabel(text, this->parent);
  77. qt_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  78. return qt_label;
  79. }
  80. QWidget* Widget::CreateCheckBox(Settings::BasicSetting* bool_setting, const QString& label,
  81. std::function<std::string()>& serializer,
  82. std::function<void()>& restore_func,
  83. const std::function<void()>& touch) {
  84. checkbox = new QCheckBox(label, this);
  85. checkbox->setCheckState(bool_setting->ToString() == "true" ? Qt::CheckState::Checked
  86. : Qt::CheckState::Unchecked);
  87. checkbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  88. if (!bool_setting->Save() && !Settings::IsConfiguringGlobal() && runtime_lock) {
  89. checkbox->setEnabled(false);
  90. }
  91. serializer = [this]() {
  92. return checkbox->checkState() == Qt::CheckState::Checked ? "true" : "false";
  93. };
  94. restore_func = [this, bool_setting]() {
  95. checkbox->setCheckState(RelevantDefault(*bool_setting) == "true" ? Qt::Checked
  96. : Qt::Unchecked);
  97. };
  98. if (!Settings::IsConfiguringGlobal()) {
  99. QObject::connect(checkbox, &QCheckBox::clicked, [touch]() { touch(); });
  100. }
  101. return checkbox;
  102. }
  103. QWidget* Widget::CreateCombobox(std::function<std::string()>& serializer,
  104. std::function<void()>& restore_func,
  105. const std::function<void()>& touch) {
  106. const auto type = setting.EnumIndex();
  107. combobox = new QComboBox(this);
  108. combobox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  109. const ComboboxTranslations* enumeration{nullptr};
  110. if (combobox_enumerations.contains(type)) {
  111. enumeration = &combobox_enumerations.at(type);
  112. for (const auto& [id, name] : *enumeration) {
  113. combobox->addItem(name);
  114. }
  115. } else {
  116. return combobox;
  117. }
  118. const auto find_index = [=](u32 value) -> int {
  119. for (u32 i = 0; i < enumeration->size(); i++) {
  120. if (enumeration->at(i).first == value) {
  121. return i;
  122. }
  123. }
  124. return -1;
  125. };
  126. const u32 setting_value = std::strtoul(setting.ToString().c_str(), nullptr, 0);
  127. combobox->setCurrentIndex(find_index(setting_value));
  128. serializer = [this, enumeration]() {
  129. int current = combobox->currentIndex();
  130. return std::to_string(enumeration->at(current).first);
  131. };
  132. restore_func = [this, find_index]() {
  133. const u32 global_value = std::strtoul(RelevantDefault(setting).c_str(), nullptr, 0);
  134. combobox->setCurrentIndex(find_index(global_value));
  135. };
  136. if (!Settings::IsConfiguringGlobal()) {
  137. QObject::connect(combobox, QOverload<int>::of(&QComboBox::activated),
  138. [touch]() { touch(); });
  139. }
  140. return combobox;
  141. }
  142. QWidget* Widget::CreateRadioGroup(std::function<std::string()>& serializer,
  143. std::function<void()>& restore_func,
  144. const std::function<void()>& touch) {
  145. const auto type = setting.EnumIndex();
  146. QWidget* group = new QWidget(this);
  147. QHBoxLayout* layout = new QHBoxLayout(group);
  148. layout->setContentsMargins(0, 0, 0, 0);
  149. group->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  150. const ComboboxTranslations* enumeration{nullptr};
  151. if (combobox_enumerations.contains(type)) {
  152. enumeration = &combobox_enumerations.at(type);
  153. for (const auto& [id, name] : *enumeration) {
  154. QRadioButton* radio_button = new QRadioButton(name, group);
  155. layout->addWidget(radio_button);
  156. radio_buttons.push_back({id, radio_button});
  157. }
  158. } else {
  159. return group;
  160. }
  161. const auto get_selected = [=]() -> int {
  162. for (const auto& [id, button] : radio_buttons) {
  163. if (button->isChecked()) {
  164. return id;
  165. }
  166. }
  167. return -1;
  168. };
  169. const auto set_index = [=](u32 value) {
  170. for (const auto& [id, button] : radio_buttons) {
  171. button->setChecked(id == value);
  172. }
  173. };
  174. const u32 setting_value = std::strtoul(setting.ToString().c_str(), nullptr, 0);
  175. set_index(setting_value);
  176. serializer = [get_selected]() {
  177. int current = get_selected();
  178. return std::to_string(current);
  179. };
  180. restore_func = [this, set_index]() {
  181. const u32 global_value = std::strtoul(RelevantDefault(setting).c_str(), nullptr, 0);
  182. set_index(global_value);
  183. };
  184. if (!Settings::IsConfiguringGlobal()) {
  185. for (const auto& [id, button] : radio_buttons) {
  186. QObject::connect(button, &QAbstractButton::clicked, [touch]() { touch(); });
  187. }
  188. }
  189. return group;
  190. }
  191. QWidget* Widget::CreateLineEdit(std::function<std::string()>& serializer,
  192. std::function<void()>& restore_func,
  193. const std::function<void()>& touch, bool managed) {
  194. const QString text = QString::fromStdString(setting.ToString());
  195. line_edit = new QLineEdit(this);
  196. line_edit->setText(text);
  197. serializer = [this]() { return line_edit->text().toStdString(); };
  198. if (!managed) {
  199. return line_edit;
  200. }
  201. restore_func = [this]() {
  202. line_edit->setText(QString::fromStdString(RelevantDefault(setting)));
  203. };
  204. if (!Settings::IsConfiguringGlobal()) {
  205. QObject::connect(line_edit, &QLineEdit::textChanged, [touch]() { touch(); });
  206. }
  207. return line_edit;
  208. }
  209. static void CreateIntSlider(Settings::BasicSetting& setting, bool reversed, float multiplier,
  210. QLabel* feedback, const QString& use_format, QSlider* slider,
  211. std::function<std::string()>& serializer,
  212. std::function<void()>& restore_func) {
  213. const int max_val = std::strtol(setting.MaxVal().c_str(), nullptr, 0);
  214. const auto update_feedback = [=](int value) {
  215. int present = (reversed ? max_val - value : value) * multiplier + 0.5f;
  216. feedback->setText(use_format.arg(QVariant::fromValue(present).value<QString>()));
  217. };
  218. QObject::connect(slider, &QAbstractSlider::valueChanged, update_feedback);
  219. update_feedback(std::strtol(setting.ToString().c_str(), nullptr, 0));
  220. slider->setMinimum(std::strtol(setting.MinVal().c_str(), nullptr, 0));
  221. slider->setMaximum(max_val);
  222. slider->setValue(std::strtol(setting.ToString().c_str(), nullptr, 0));
  223. serializer = [slider]() { return std::to_string(slider->value()); };
  224. restore_func = [slider, &setting]() {
  225. slider->setValue(std::strtol(RelevantDefault(setting).c_str(), nullptr, 0));
  226. };
  227. }
  228. static void CreateFloatSlider(Settings::BasicSetting& setting, bool reversed, float multiplier,
  229. QLabel* feedback, const QString& use_format, QSlider* slider,
  230. std::function<std::string()>& serializer,
  231. std::function<void()>& restore_func) {
  232. const float max_val = std::strtof(setting.MaxVal().c_str(), nullptr);
  233. const float min_val = std::strtof(setting.MinVal().c_str(), nullptr);
  234. const float use_multiplier =
  235. multiplier == default_multiplier ? default_float_multiplier : multiplier;
  236. const auto update_feedback = [=](float value) {
  237. int present = (reversed ? max_val - value : value) + 0.5f;
  238. feedback->setText(use_format.arg(QVariant::fromValue(present).value<QString>()));
  239. };
  240. QObject::connect(slider, &QAbstractSlider::valueChanged, update_feedback);
  241. update_feedback(std::strtof(setting.ToString().c_str(), nullptr));
  242. slider->setMinimum(min_val * use_multiplier);
  243. slider->setMaximum(max_val * use_multiplier);
  244. slider->setValue(std::strtof(setting.ToString().c_str(), nullptr) * use_multiplier);
  245. serializer = [slider, use_multiplier]() {
  246. return std::to_string(slider->value() / use_multiplier);
  247. };
  248. restore_func = [slider, &setting, use_multiplier]() {
  249. slider->setValue(std::strtof(RelevantDefault(setting).c_str(), nullptr) * use_multiplier);
  250. };
  251. }
  252. QWidget* Widget::CreateSlider(bool reversed, float multiplier, const QString& given_suffix,
  253. std::function<std::string()>& serializer,
  254. std::function<void()>& restore_func,
  255. const std::function<void()>& touch) {
  256. if (!setting.Ranged()) {
  257. LOG_ERROR(Frontend, "\"{}\" is not a ranged setting, but a slider was requested.",
  258. setting.GetLabel());
  259. return nullptr;
  260. }
  261. QWidget* container = new QWidget(this);
  262. QHBoxLayout* layout = new QHBoxLayout(container);
  263. slider = new QSlider(Qt::Horizontal, this);
  264. QLabel* feedback = new QLabel(this);
  265. layout->addWidget(slider);
  266. layout->addWidget(feedback);
  267. container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  268. layout->setContentsMargins(0, 0, 0, 0);
  269. QString suffix = given_suffix == default_suffix ? DefaultSuffix(this, setting) : given_suffix;
  270. const QString use_format = QStringLiteral("%1").append(suffix);
  271. if (setting.IsIntegral()) {
  272. CreateIntSlider(setting, reversed, multiplier, feedback, use_format, slider, serializer,
  273. restore_func);
  274. } else {
  275. CreateFloatSlider(setting, reversed, multiplier, feedback, use_format, slider, serializer,
  276. restore_func);
  277. }
  278. slider->setInvertedAppearance(reversed);
  279. if (!Settings::IsConfiguringGlobal()) {
  280. QObject::connect(slider, &QAbstractSlider::actionTriggered, [touch]() { touch(); });
  281. }
  282. return container;
  283. }
  284. QWidget* Widget::CreateSpinBox(const QString& given_suffix,
  285. std::function<std::string()>& serializer,
  286. std::function<void()>& restore_func,
  287. const std::function<void()>& touch) {
  288. const auto min_val = std::strtol(setting.MinVal().c_str(), nullptr, 0);
  289. const auto max_val = std::strtol(setting.MaxVal().c_str(), nullptr, 0);
  290. const auto default_val = std::strtol(setting.ToString().c_str(), nullptr, 0);
  291. QString suffix = given_suffix == default_suffix ? DefaultSuffix(this, setting) : given_suffix;
  292. spinbox = new QSpinBox(this);
  293. spinbox->setRange(min_val, max_val);
  294. spinbox->setValue(default_val);
  295. spinbox->setSuffix(suffix);
  296. spinbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  297. serializer = [this]() { return std::to_string(spinbox->value()); };
  298. restore_func = [this]() {
  299. auto value{std::strtol(RelevantDefault(setting).c_str(), nullptr, 0)};
  300. spinbox->setValue(value);
  301. };
  302. if (!Settings::IsConfiguringGlobal()) {
  303. QObject::connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), [this, touch]() {
  304. if (spinbox->value() != std::strtol(setting.ToStringGlobal().c_str(), nullptr, 0)) {
  305. touch();
  306. }
  307. });
  308. }
  309. return spinbox;
  310. }
  311. QWidget* Widget::CreateDoubleSpinBox(const QString& given_suffix,
  312. std::function<std::string()>& serializer,
  313. std::function<void()>& restore_func,
  314. const std::function<void()>& touch) {
  315. const auto min_val = std::strtod(setting.MinVal().c_str(), nullptr);
  316. const auto max_val = std::strtod(setting.MaxVal().c_str(), nullptr);
  317. const auto default_val = std::strtod(setting.ToString().c_str(), nullptr);
  318. QString suffix = given_suffix == default_suffix ? DefaultSuffix(this, setting) : given_suffix;
  319. double_spinbox = new QDoubleSpinBox(this);
  320. double_spinbox->setRange(min_val, max_val);
  321. double_spinbox->setValue(default_val);
  322. double_spinbox->setSuffix(suffix);
  323. double_spinbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  324. serializer = [this]() { return fmt::format("{:f}", double_spinbox->value()); };
  325. restore_func = [this]() {
  326. auto value{std::strtod(RelevantDefault(setting).c_str(), nullptr)};
  327. double_spinbox->setValue(value);
  328. };
  329. if (!Settings::IsConfiguringGlobal()) {
  330. QObject::connect(double_spinbox, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
  331. [this, touch]() {
  332. if (double_spinbox->value() !=
  333. std::strtod(setting.ToStringGlobal().c_str(), nullptr)) {
  334. touch();
  335. }
  336. });
  337. }
  338. return double_spinbox;
  339. }
  340. QWidget* Widget::CreateHexEdit(std::function<std::string()>& serializer,
  341. std::function<void()>& restore_func,
  342. const std::function<void()>& touch) {
  343. auto* data_component = CreateLineEdit(serializer, restore_func, touch, false);
  344. if (data_component == nullptr) {
  345. return nullptr;
  346. }
  347. auto to_hex = [=](const std::string& input) {
  348. return QString::fromStdString(
  349. fmt::format("{:08x}", std::strtoul(input.c_str(), nullptr, 0)));
  350. };
  351. QRegularExpressionValidator* regex = new QRegularExpressionValidator(
  352. QRegularExpression{QStringLiteral("^[0-9a-fA-F]{0,8}$")}, line_edit);
  353. const QString default_val = to_hex(setting.ToString());
  354. line_edit->setText(default_val);
  355. line_edit->setMaxLength(8);
  356. line_edit->setValidator(regex);
  357. auto hex_to_dec = [this]() -> std::string {
  358. return std::to_string(std::strtoul(line_edit->text().toStdString().c_str(), nullptr, 16));
  359. };
  360. serializer = [hex_to_dec]() { return hex_to_dec(); };
  361. restore_func = [this, to_hex]() { line_edit->setText(to_hex(RelevantDefault(setting))); };
  362. if (!Settings::IsConfiguringGlobal()) {
  363. QObject::connect(line_edit, &QLineEdit::textChanged, [touch]() { touch(); });
  364. }
  365. return line_edit;
  366. }
  367. QWidget* Widget::CreateDateTimeEdit(bool disabled, bool restrict,
  368. std::function<std::string()>& serializer,
  369. std::function<void()>& restore_func,
  370. const std::function<void()>& touch) {
  371. const long long current_time = QDateTime::currentSecsSinceEpoch();
  372. const s64 the_time =
  373. disabled ? current_time : std::strtoll(setting.ToString().c_str(), nullptr, 0);
  374. const auto default_val = QDateTime::fromSecsSinceEpoch(the_time);
  375. date_time_edit = new QDateTimeEdit(this);
  376. date_time_edit->setDateTime(default_val);
  377. date_time_edit->setMinimumDateTime(QDateTime::fromSecsSinceEpoch(0));
  378. date_time_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  379. serializer = [this]() { return std::to_string(date_time_edit->dateTime().toSecsSinceEpoch()); };
  380. auto get_clear_val = [this, restrict, current_time]() {
  381. return QDateTime::fromSecsSinceEpoch([this, restrict, current_time]() {
  382. if (restrict && checkbox->checkState() == Qt::Checked) {
  383. return std::strtoll(RelevantDefault(setting).c_str(), nullptr, 0);
  384. }
  385. return current_time;
  386. }());
  387. };
  388. restore_func = [this, get_clear_val]() { date_time_edit->setDateTime(get_clear_val()); };
  389. if (!Settings::IsConfiguringGlobal()) {
  390. QObject::connect(date_time_edit, &QDateTimeEdit::editingFinished,
  391. [this, get_clear_val, touch]() {
  392. if (date_time_edit->dateTime() != get_clear_val()) {
  393. touch();
  394. }
  395. });
  396. }
  397. return date_time_edit;
  398. }
  399. void Widget::SetupComponent(const QString& label, std::function<void()>& load_func, bool managed,
  400. RequestType request, float multiplier,
  401. Settings::BasicSetting* other_setting, const QString& suffix) {
  402. created = true;
  403. const auto type = setting.TypeId();
  404. QLayout* layout = new QHBoxLayout(this);
  405. layout->setContentsMargins(0, 0, 0, 0);
  406. if (other_setting == nullptr) {
  407. other_setting = setting.PairedSetting();
  408. }
  409. const bool require_checkbox =
  410. other_setting != nullptr && other_setting->TypeId() == typeid(bool);
  411. if (other_setting != nullptr && other_setting->TypeId() != typeid(bool)) {
  412. LOG_WARNING(
  413. Frontend,
  414. "Extra setting \"{}\" specified but is not bool, refusing to create checkbox for it.",
  415. other_setting->GetLabel());
  416. }
  417. std::function<std::string()> checkbox_serializer = []() -> std::string { return {}; };
  418. std::function<void()> checkbox_restore_func = []() {};
  419. std::function<void()> touch = []() {};
  420. std::function<std::string()> serializer = []() -> std::string { return {}; };
  421. std::function<void()> restore_func = []() {};
  422. QWidget* data_component{nullptr};
  423. request = [&]() {
  424. if (request != RequestType::Default) {
  425. return request;
  426. }
  427. switch (setting.Specialization() & Settings::SpecializationTypeMask) {
  428. case Settings::Specialization::Default:
  429. return RequestType::Default;
  430. case Settings::Specialization::Time:
  431. return RequestType::DateTimeEdit;
  432. case Settings::Specialization::Hex:
  433. return RequestType::HexEdit;
  434. case Settings::Specialization::RuntimeList:
  435. managed = false;
  436. [[fallthrough]];
  437. case Settings::Specialization::List:
  438. return RequestType::ComboBox;
  439. case Settings::Specialization::Scalar:
  440. return RequestType::Slider;
  441. case Settings::Specialization::Countable:
  442. return RequestType::SpinBox;
  443. case Settings::Specialization::Radio:
  444. return RequestType::RadioGroup;
  445. default:
  446. break;
  447. }
  448. return request;
  449. }();
  450. if (!Settings::IsConfiguringGlobal() && managed) {
  451. restore_button = CreateRestoreGlobalButton(setting.UsingGlobal(), this);
  452. touch = [this]() {
  453. LOG_DEBUG(Frontend, "Enabling custom setting for \"{}\"", setting.GetLabel());
  454. restore_button->setEnabled(true);
  455. restore_button->setVisible(true);
  456. };
  457. }
  458. if (require_checkbox) {
  459. QWidget* lhs =
  460. CreateCheckBox(other_setting, label, checkbox_serializer, checkbox_restore_func, touch);
  461. layout->addWidget(lhs);
  462. } else if (setting.TypeId() != typeid(bool)) {
  463. QLabel* qt_label = CreateLabel(label);
  464. layout->addWidget(qt_label);
  465. }
  466. if (setting.TypeId() == typeid(bool)) {
  467. data_component = CreateCheckBox(&setting, label, serializer, restore_func, touch);
  468. } else if (setting.IsEnum()) {
  469. if (request == RequestType::RadioGroup) {
  470. data_component = CreateRadioGroup(serializer, restore_func, touch);
  471. } else {
  472. data_component = CreateCombobox(serializer, restore_func, touch);
  473. }
  474. } else if (setting.IsIntegral()) {
  475. switch (request) {
  476. case RequestType::Slider:
  477. case RequestType::ReverseSlider:
  478. data_component = CreateSlider(request == RequestType::ReverseSlider, multiplier, suffix,
  479. serializer, restore_func, touch);
  480. break;
  481. case RequestType::Default:
  482. case RequestType::LineEdit:
  483. data_component = CreateLineEdit(serializer, restore_func, touch);
  484. break;
  485. case RequestType::DateTimeEdit:
  486. data_component = CreateDateTimeEdit(other_setting->ToString() != "true", true,
  487. serializer, restore_func, touch);
  488. break;
  489. case RequestType::SpinBox:
  490. data_component = CreateSpinBox(suffix, serializer, restore_func, touch);
  491. break;
  492. case RequestType::HexEdit:
  493. data_component = CreateHexEdit(serializer, restore_func, touch);
  494. break;
  495. case RequestType::ComboBox:
  496. data_component = CreateCombobox(serializer, restore_func, touch);
  497. break;
  498. default:
  499. UNIMPLEMENTED();
  500. }
  501. } else if (setting.IsFloatingPoint()) {
  502. switch (request) {
  503. case RequestType::Default:
  504. case RequestType::SpinBox:
  505. data_component = CreateDoubleSpinBox(suffix, serializer, restore_func, touch);
  506. break;
  507. case RequestType::Slider:
  508. case RequestType::ReverseSlider:
  509. data_component = CreateSlider(request == RequestType::ReverseSlider, multiplier, suffix,
  510. serializer, restore_func, touch);
  511. break;
  512. default:
  513. UNIMPLEMENTED();
  514. }
  515. } else if (type == typeid(std::string)) {
  516. switch (request) {
  517. case RequestType::Default:
  518. case RequestType::LineEdit:
  519. data_component = CreateLineEdit(serializer, restore_func, touch);
  520. break;
  521. case RequestType::ComboBox:
  522. data_component = CreateCombobox(serializer, restore_func, touch);
  523. break;
  524. default:
  525. UNIMPLEMENTED();
  526. }
  527. }
  528. if (data_component == nullptr) {
  529. LOG_ERROR(Frontend, "Failed to create widget for \"{}\"", setting.GetLabel());
  530. created = false;
  531. return;
  532. }
  533. layout->addWidget(data_component);
  534. if (!managed) {
  535. return;
  536. }
  537. if (Settings::IsConfiguringGlobal()) {
  538. load_func = [this, serializer, checkbox_serializer, require_checkbox, other_setting]() {
  539. if (require_checkbox && other_setting->UsingGlobal()) {
  540. other_setting->LoadString(checkbox_serializer());
  541. }
  542. if (setting.UsingGlobal()) {
  543. setting.LoadString(serializer());
  544. }
  545. };
  546. } else {
  547. layout->addWidget(restore_button);
  548. QObject::connect(restore_button, &QAbstractButton::clicked,
  549. [this, restore_func, checkbox_restore_func](bool) {
  550. LOG_DEBUG(Frontend, "Restore global state for \"{}\"",
  551. setting.GetLabel());
  552. restore_button->setEnabled(false);
  553. restore_button->setVisible(false);
  554. checkbox_restore_func();
  555. restore_func();
  556. });
  557. load_func = [this, serializer, require_checkbox, checkbox_serializer, other_setting]() {
  558. bool using_global = !restore_button->isEnabled();
  559. setting.SetGlobal(using_global);
  560. if (!using_global) {
  561. setting.LoadString(serializer());
  562. }
  563. if (require_checkbox) {
  564. other_setting->SetGlobal(using_global);
  565. if (!using_global) {
  566. other_setting->LoadString(checkbox_serializer());
  567. }
  568. }
  569. };
  570. }
  571. if (other_setting != nullptr) {
  572. const auto reset = [restore_func, data_component](int state) {
  573. data_component->setEnabled(state == Qt::Checked);
  574. if (state != Qt::Checked) {
  575. restore_func();
  576. }
  577. };
  578. connect(checkbox, &QCheckBox::stateChanged, reset);
  579. reset(checkbox->checkState());
  580. }
  581. }
  582. bool Widget::Valid() const {
  583. return created;
  584. }
  585. Widget::~Widget() = default;
  586. Widget::Widget(Settings::BasicSetting* setting_, const TranslationMap& translations_,
  587. const ComboboxTranslationMap& combobox_translations_, QWidget* parent_,
  588. bool runtime_lock_, std::vector<std::function<void(bool)>>& apply_funcs_,
  589. RequestType request, bool managed, float multiplier,
  590. Settings::BasicSetting* other_setting, const QString& suffix)
  591. : QWidget(parent_), parent{parent_}, translations{translations_},
  592. combobox_enumerations{combobox_translations_}, setting{*setting_}, apply_funcs{apply_funcs_},
  593. runtime_lock{runtime_lock_} {
  594. if (!Settings::IsConfiguringGlobal() && !setting.Switchable()) {
  595. LOG_DEBUG(Frontend, "\"{}\" is not switchable, skipping...", setting.GetLabel());
  596. return;
  597. }
  598. const int id = setting.Id();
  599. const auto [label, tooltip] = [&]() {
  600. const auto& setting_label = setting.GetLabel();
  601. if (translations.contains(id)) {
  602. return std::pair{translations.at(id).first, translations.at(id).second};
  603. }
  604. LOG_WARNING(Frontend, "Translation table lacks entry for \"{}\"", setting_label);
  605. return std::pair{QString::fromStdString(setting_label), QStringLiteral()};
  606. }();
  607. if (label == QStringLiteral()) {
  608. LOG_DEBUG(Frontend, "Translation table has empty entry for \"{}\", skipping...",
  609. setting.GetLabel());
  610. return;
  611. }
  612. std::function<void()> load_func = []() {};
  613. SetupComponent(label, load_func, managed, request, multiplier, other_setting, suffix);
  614. if (!created) {
  615. LOG_WARNING(Frontend, "No widget was created for \"{}\"", setting.GetLabel());
  616. return;
  617. }
  618. apply_funcs.push_back([load_func, setting_](bool powered_on) {
  619. if (setting_->RuntimeModfiable() || !powered_on) {
  620. load_func();
  621. }
  622. });
  623. bool enable = runtime_lock || setting.RuntimeModfiable();
  624. if (setting.Switchable() && Settings::IsConfiguringGlobal() && !runtime_lock) {
  625. enable &= setting.UsingGlobal();
  626. }
  627. this->setEnabled(enable);
  628. this->setToolTip(tooltip);
  629. }
  630. Builder::Builder(QWidget* parent_, bool runtime_lock_)
  631. : translations{InitializeTranslations(parent_)},
  632. combobox_translations{ComboboxEnumeration(parent_)}, parent{parent_}, runtime_lock{
  633. runtime_lock_} {}
  634. Builder::~Builder() = default;
  635. Widget* Builder::BuildWidget(Settings::BasicSetting* setting,
  636. std::vector<std::function<void(bool)>>& apply_funcs,
  637. RequestType request, bool managed, float multiplier,
  638. Settings::BasicSetting* other_setting, const QString& suffix) const {
  639. if (!Settings::IsConfiguringGlobal() && !setting->Switchable()) {
  640. return nullptr;
  641. }
  642. if (setting->Specialization() == Settings::Specialization::Paired) {
  643. LOG_DEBUG(Frontend, "\"{}\" has specialization Paired: ignoring", setting->GetLabel());
  644. return nullptr;
  645. }
  646. return new Widget(setting, *translations, *combobox_translations, parent, runtime_lock,
  647. apply_funcs, request, managed, multiplier, other_setting, suffix);
  648. }
  649. Widget* Builder::BuildWidget(Settings::BasicSetting* setting,
  650. std::vector<std::function<void(bool)>>& apply_funcs,
  651. Settings::BasicSetting* other_setting, RequestType request,
  652. const QString& suffix) const {
  653. return BuildWidget(setting, apply_funcs, request, true, 1.0f, other_setting, suffix);
  654. }
  655. const ComboboxTranslationMap& Builder::ComboboxTranslations() const {
  656. return *combobox_translations;
  657. }
  658. } // namespace ConfigurationShared