configuration_shared.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <forward_list>
  5. #include <iterator>
  6. #include <memory>
  7. #include <QCheckBox>
  8. #include <QComboBox>
  9. #include <QWidget>
  10. #include <qobjectdefs.h>
  11. #include "common/settings.h"
  12. #include "yuzu/configuration/shared_translation.h"
  13. namespace ConfigurationShared {
  14. class Tab : public QWidget {
  15. Q_OBJECT
  16. public:
  17. explicit Tab(std::shared_ptr<std::forward_list<Tab*>> group_, QWidget* parent = nullptr);
  18. ~Tab();
  19. virtual void ApplyConfiguration() = 0;
  20. virtual void SetConfiguration() = 0;
  21. private:
  22. std::shared_ptr<std::forward_list<Tab*>> group;
  23. };
  24. constexpr int USE_GLOBAL_INDEX = 0;
  25. constexpr int USE_GLOBAL_SEPARATOR_INDEX = 1;
  26. constexpr int USE_GLOBAL_OFFSET = 2;
  27. // CheckBoxes require a tracker for their state since we emulate a tristate CheckBox
  28. enum class CheckState {
  29. Off, // Checkbox overrides to off/false
  30. On, // Checkbox overrides to on/true
  31. Global, // Checkbox defers to the global state
  32. Count, // Simply the number of states, not a valid checkbox state
  33. };
  34. enum class RequestType {
  35. Default,
  36. ComboBox,
  37. SpinBox,
  38. Slider,
  39. MaxEnum,
  40. };
  41. std::pair<QWidget*, void*> CreateWidget(Settings::BasicSetting* setting,
  42. const TranslationMap& translations, QWidget* parent,
  43. bool runtime_lock,
  44. std::forward_list<std::function<void(bool)>>& apply_funcs,
  45. std::list<CheckState>& trackers,
  46. RequestType request = RequestType::Default);
  47. // Global-aware apply and set functions
  48. // ApplyPerGameSetting, given a Settings::Setting and a Qt UI element, properly applies a Setting
  49. void ApplyPerGameSetting(Settings::SwitchableSetting<bool>* setting, const QCheckBox* checkbox,
  50. const CheckState& tracker);
  51. template <typename Type, bool ranged>
  52. void ApplyPerGameSetting(Settings::SwitchableSetting<Type, ranged>* setting,
  53. const QComboBox* combobox) {
  54. if (Settings::IsConfiguringGlobal() && setting->UsingGlobal()) {
  55. setting->SetValue(static_cast<Type>(combobox->currentIndex()));
  56. } else if (!Settings::IsConfiguringGlobal()) {
  57. if (combobox->currentIndex() == ConfigurationShared::USE_GLOBAL_INDEX) {
  58. setting->SetGlobal(true);
  59. } else {
  60. setting->SetGlobal(false);
  61. setting->SetValue(static_cast<Type>(combobox->currentIndex() -
  62. ConfigurationShared::USE_GLOBAL_OFFSET));
  63. }
  64. }
  65. }
  66. // Sets a Qt UI element given a Settings::Setting
  67. void SetPerGameSetting(QCheckBox* checkbox, const Settings::SwitchableSetting<bool>* setting);
  68. template <typename Type, bool ranged>
  69. void SetPerGameSetting(QComboBox* combobox,
  70. const Settings::SwitchableSetting<Type, ranged>* setting) {
  71. combobox->setCurrentIndex(setting->UsingGlobal() ? ConfigurationShared::USE_GLOBAL_INDEX
  72. : static_cast<int>(setting->GetValue()) +
  73. ConfigurationShared::USE_GLOBAL_OFFSET);
  74. }
  75. // (Un)highlights a Qt UI element
  76. void SetHighlight(QWidget* widget, bool highlighted);
  77. // Sets up a QCheckBox like a tristate one, given a Setting
  78. template <bool ranged, bool save>
  79. void SetColoredTristate(QCheckBox* checkbox,
  80. const Settings::SwitchableSetting<bool, ranged, save>& setting,
  81. CheckState& tracker) {
  82. if (setting.UsingGlobal()) {
  83. tracker = CheckState::Global;
  84. } else {
  85. tracker = (setting.GetValue() == setting.GetValue(true)) ? CheckState::On : CheckState::Off;
  86. }
  87. SetHighlight(checkbox, tracker != CheckState::Global);
  88. QObject::connect(checkbox, &QCheckBox::clicked, checkbox, [checkbox, setting, &tracker] {
  89. tracker = static_cast<CheckState>((static_cast<int>(tracker) + 1) %
  90. static_cast<int>(CheckState::Count));
  91. if (tracker == CheckState::Global) {
  92. checkbox->setChecked(setting.GetValue(true));
  93. }
  94. SetHighlight(checkbox, tracker != CheckState::Global);
  95. });
  96. }
  97. void SetColoredTristate(QCheckBox* checkbox, bool global, bool state, bool global_state,
  98. CheckState& tracker);
  99. // Sets up coloring of a QWidget `target` based on the state of a QComboBox, and calls
  100. // InsertGlobalItem
  101. void SetColoredComboBox(QComboBox* combobox, QWidget* target, int global);
  102. // Adds the "Use Global Configuration" selection and separator to the beginning of a QComboBox
  103. void InsertGlobalItem(QComboBox* combobox, int global_index);
  104. // Returns the correct index of a QComboBox taking into account global configuration
  105. int GetComboboxIndex(int global_setting_index, const QComboBox* combobox);
  106. } // namespace ConfigurationShared