shared_widget.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <functional>
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include <QString>
  9. #include <QStringLiteral>
  10. #include <QWidget>
  11. #include <qobjectdefs.h>
  12. #include "yuzu/configuration/shared_translation.h"
  13. class QCheckBox;
  14. class QComboBox;
  15. class QDateTimeEdit;
  16. class QLabel;
  17. class QLineEdit;
  18. class QObject;
  19. class QPushButton;
  20. class QSlider;
  21. class QSpinBox;
  22. namespace Settings {
  23. class BasicSetting;
  24. } // namespace Settings
  25. namespace ConfigurationShared {
  26. enum class RequestType {
  27. Default,
  28. ComboBox,
  29. SpinBox,
  30. Slider,
  31. ReverseSlider,
  32. LineEdit,
  33. HexEdit,
  34. DateTimeEdit,
  35. MaxEnum,
  36. };
  37. class Widget : public QWidget {
  38. Q_OBJECT
  39. public:
  40. /**
  41. * @param setting The primary Setting to create the Widget for
  42. * @param translations Map of translations to display on the left side label/checkbox
  43. * @param combobox_translations Map of translations for enumerating combo boxes
  44. * @param parent Qt parent
  45. * @param runtime_lock Emulated guest powered on state, for use on settings that should be
  46. * configured during guest execution
  47. * @param apply_funcs_ List to append, functions to run to apply the widget state to the setting
  48. * @param request What type of data representation component to create -- not always respected
  49. * for the Setting data type
  50. * @param managed Set true if the caller will set up component data and handling
  51. * @param multiplier Value to multiply the slider feedback label
  52. * @param other_setting Second setting to modify, to replace the label with a checkbox
  53. * @param suffix Set to specify formats for Slider feedback labels or SpinBox
  54. */
  55. explicit Widget(Settings::BasicSetting* setting, const TranslationMap& translations,
  56. const ComboboxTranslationMap& combobox_translations, QWidget* parent,
  57. bool runtime_lock, std::vector<std::function<void(bool)>>& apply_funcs_,
  58. RequestType request = RequestType::Default, bool managed = true,
  59. float multiplier = 1.0f, Settings::BasicSetting* other_setting = nullptr,
  60. const QString& suffix = QStringLiteral(""));
  61. virtual ~Widget();
  62. /**
  63. * @returns True if the Widget successfully created the components for the setting
  64. */
  65. bool Valid() const;
  66. /**
  67. * Creates a button to appear when a setting has been modified. This exists for custom
  68. * configurations and wasn't designed to work for the global configuration. It has public access
  69. * for settings that need to be unmanaged but can be custom.
  70. *
  71. * @param using_global The global state of the setting this button is for
  72. * @param parent QWidget parent
  73. */
  74. [[nodiscard]] static QPushButton* CreateRestoreGlobalButton(bool using_global, QWidget* parent);
  75. // Direct handles to sub components created
  76. QPushButton* restore_button{}; ///< Restore button for custom configurations
  77. QLineEdit* line_edit{}; ///< QLineEdit, used for LineEdit and HexEdit
  78. QSpinBox* spinbox{};
  79. QCheckBox* checkbox{};
  80. QSlider* slider{};
  81. QComboBox* combobox{};
  82. QDateTimeEdit* date_time_edit{};
  83. private:
  84. void SetupComponent(const QString& label, std::function<void()>& load_func, bool managed,
  85. RequestType request, float multiplier,
  86. Settings::BasicSetting* other_setting, const QString& suffix);
  87. QLabel* CreateLabel(const QString& text);
  88. QWidget* CreateCheckBox(Settings::BasicSetting* bool_setting, const QString& label,
  89. std::function<std::string()>& serializer,
  90. std::function<void()>& restore_func,
  91. const std::function<void()>& touch);
  92. QWidget* CreateCombobox(std::function<std::string()>& serializer,
  93. std::function<void()>& restore_func,
  94. const std::function<void()>& touch);
  95. QWidget* CreateLineEdit(std::function<std::string()>& serializer,
  96. std::function<void()>& restore_func, const std::function<void()>& touch,
  97. bool managed = true);
  98. QWidget* CreateHexEdit(std::function<std::string()>& serializer,
  99. std::function<void()>& restore_func, const std::function<void()>& touch);
  100. QWidget* CreateSlider(bool reversed, float multiplier, const QString& suffix,
  101. std::function<std::string()>& serializer,
  102. std::function<void()>& restore_func, const std::function<void()>& touch);
  103. QWidget* CreateDateTimeEdit(bool disabled, bool restrict,
  104. std::function<std::string()>& serializer,
  105. std::function<void()>& restore_func,
  106. const std::function<void()>& touch);
  107. QWidget* CreateSpinBox(const QString& suffix, std::function<std::string()>& serializer,
  108. std::function<void()>& restore_func, const std::function<void()>& touch);
  109. QWidget* parent;
  110. const TranslationMap& translations;
  111. const ComboboxTranslationMap& combobox_enumerations;
  112. Settings::BasicSetting& setting;
  113. std::vector<std::function<void(bool)>>& apply_funcs;
  114. bool created{false};
  115. bool runtime_lock{false};
  116. };
  117. class Builder {
  118. public:
  119. explicit Builder(QWidget* parent, bool runtime_lock);
  120. ~Builder();
  121. Widget* BuildWidget(Settings::BasicSetting* setting,
  122. std::vector<std::function<void(bool)>>& apply_funcs,
  123. RequestType request = RequestType::Default, bool managed = true,
  124. float multiplier = 1.0f, Settings::BasicSetting* other_setting = nullptr,
  125. const QString& suffix = QStringLiteral("")) const;
  126. Widget* BuildWidget(Settings::BasicSetting* setting,
  127. std::vector<std::function<void(bool)>>& apply_funcs,
  128. Settings::BasicSetting* other_setting,
  129. RequestType request = RequestType::Default,
  130. const QString& suffix = QStringLiteral("")) const;
  131. const ComboboxTranslationMap& ComboboxTranslations() const;
  132. private:
  133. std::unique_ptr<TranslationMap> translations;
  134. std::unique_ptr<ComboboxTranslationMap> combobox_translations;
  135. QWidget* parent;
  136. const bool runtime_lock;
  137. };
  138. } // namespace ConfigurationShared