configure_system.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <chrono>
  4. #include <forward_list>
  5. #include <optional>
  6. #include <QCheckBox>
  7. #include <QComboBox>
  8. #include <QDateTimeEdit>
  9. #include <QFileDialog>
  10. #include <QGraphicsItem>
  11. #include <QLineEdit>
  12. #include <QMessageBox>
  13. #include "common/settings.h"
  14. #include "core/core.h"
  15. #include "core/hle/service/time/time_manager.h"
  16. #include "ui_configure_system.h"
  17. #include "yuzu/configuration/config.h"
  18. #include "yuzu/configuration/configuration_shared.h"
  19. #include "yuzu/configuration/configure_system.h"
  20. #include "yuzu/configuration/shared_widget.h"
  21. constexpr std::array<u32, 7> LOCALE_BLOCKLIST{
  22. // pzzefezrpnkzeidfej
  23. // thhsrnhutlohsternp
  24. // BHH4CG U
  25. // Raa1AB S
  26. // nn9
  27. // ts
  28. 0b0100011100001100000, // Japan
  29. 0b0000001101001100100, // Americas
  30. 0b0100110100001000010, // Europe
  31. 0b0100110100001000010, // Australia
  32. 0b0000000000000000000, // China
  33. 0b0100111100001000000, // Korea
  34. 0b0100111100001000000, // Taiwan
  35. };
  36. static bool IsValidLocale(u32 region_index, u32 language_index) {
  37. if (region_index >= LOCALE_BLOCKLIST.size()) {
  38. return false;
  39. }
  40. return ((LOCALE_BLOCKLIST.at(region_index) >> language_index) & 1) == 0;
  41. }
  42. ConfigureSystem::ConfigureSystem(
  43. Core::System& system_, std::shared_ptr<std::forward_list<ConfigurationShared::Tab*>> group,
  44. const ConfigurationShared::TranslationMap& translations_,
  45. const ConfigurationShared::ComboboxTranslationMap& combobox_translations_, QWidget* parent)
  46. : Tab(group, parent), ui{std::make_unique<Ui::ConfigureSystem>()}, system{system_},
  47. translations{translations_}, combobox_translations{combobox_translations_} {
  48. ui->setupUi(this);
  49. Setup();
  50. connect(rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  51. rng_seed_edit->setEnabled(state == Qt::Checked);
  52. if (state != Qt::Checked) {
  53. rng_seed_edit->setText(QStringLiteral("00000000"));
  54. }
  55. });
  56. connect(custom_rtc_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  57. custom_rtc_edit->setEnabled(state == Qt::Checked);
  58. if (state != Qt::Checked) {
  59. custom_rtc_edit->setDateTime(QDateTime::currentDateTime());
  60. }
  61. });
  62. const auto locale_check = [this]() {
  63. const auto region_index = combo_region->currentIndex();
  64. const auto language_index = combo_language->currentIndex();
  65. const bool valid_locale = IsValidLocale(region_index, language_index);
  66. ui->label_warn_invalid_locale->setVisible(!valid_locale);
  67. if (!valid_locale) {
  68. ui->label_warn_invalid_locale->setText(
  69. tr("Warning: \"%1\" is not a valid language for region \"%2\"")
  70. .arg(combo_language->currentText())
  71. .arg(combo_region->currentText()));
  72. }
  73. };
  74. connect(combo_language, qOverload<int>(&QComboBox::currentIndexChanged), this, locale_check);
  75. connect(combo_region, qOverload<int>(&QComboBox::currentIndexChanged), this, locale_check);
  76. ui->label_warn_invalid_locale->setVisible(false);
  77. locale_check();
  78. SetConfiguration();
  79. }
  80. ConfigureSystem::~ConfigureSystem() = default;
  81. void ConfigureSystem::changeEvent(QEvent* event) {
  82. if (event->type() == QEvent::LanguageChange) {
  83. RetranslateUI();
  84. }
  85. QWidget::changeEvent(event);
  86. }
  87. void ConfigureSystem::RetranslateUI() {
  88. ui->retranslateUi(this);
  89. }
  90. void ConfigureSystem::Setup() {
  91. const bool runtime_lock = !system.IsPoweredOn();
  92. auto& core_layout = *ui->core_widget->layout();
  93. auto& system_layout = *ui->system_widget->layout();
  94. std::map<u32, QWidget*> core_hold{};
  95. std::map<u32, QWidget*> system_hold{};
  96. std::forward_list<Settings::BasicSetting*> settings;
  97. auto push = [&settings](std::forward_list<Settings::BasicSetting*>& list) {
  98. for (auto setting : list) {
  99. settings.push_front(setting);
  100. }
  101. };
  102. push(Settings::values.linkage.by_category[Settings::Category::Core]);
  103. push(Settings::values.linkage.by_category[Settings::Category::System]);
  104. for (auto setting : settings) {
  105. if (!Settings::IsConfiguringGlobal() && !setting->Switchable()) {
  106. continue;
  107. }
  108. [[maybe_unused]] std::string label = setting->GetLabel();
  109. ConfigurationShared::Widget* widget = [=]() {
  110. if (setting->Id() == Settings::values.custom_rtc.Id()) {
  111. // custom_rtc needs a DateTimeEdit (default is LineEdit), and a checkbox to manage
  112. // it and custom_rtc_enabled
  113. return new ConfigurationShared::Widget(
  114. setting, translations, combobox_translations, this, runtime_lock, apply_funcs,
  115. &Settings::values.custom_rtc_enabled,
  116. ConfigurationShared::RequestType::DateTimeEdit);
  117. } else if (setting->Id() == Settings::values.rng_seed.Id()) {
  118. // rng_seed needs a HexEdit (default is LineEdit), and a checkbox to manage
  119. // it and rng_seed_enabled
  120. return new ConfigurationShared::Widget(
  121. setting, translations, combobox_translations, this, runtime_lock, apply_funcs,
  122. &Settings::values.rng_seed_enabled, ConfigurationShared::RequestType::HexEdit);
  123. } else {
  124. return new ConfigurationShared::Widget(setting, translations, combobox_translations,
  125. this, runtime_lock, apply_funcs);
  126. }
  127. }();
  128. if (!widget->Valid()) {
  129. delete widget;
  130. continue;
  131. }
  132. if (setting->Id() == Settings::values.rng_seed.Id()) {
  133. // Keep track of rng_seed's widgets to reset it with the checkbox state
  134. rng_seed_checkbox = widget->checkbox;
  135. rng_seed_edit = widget->line_edit;
  136. rng_seed_edit->setEnabled(Settings::values.rng_seed_enabled.GetValue());
  137. } else if (setting->Id() == Settings::values.custom_rtc.Id()) {
  138. // Keep track of custom_rtc's widgets to reset it with the checkbox state
  139. custom_rtc_checkbox = widget->checkbox;
  140. custom_rtc_edit = widget->date_time_edit;
  141. custom_rtc_edit->setEnabled(Settings::values.custom_rtc_enabled.GetValue());
  142. } else if (setting->Id() == Settings::values.region_index.Id()) {
  143. // Keep track of the region_index (and langauge_index) combobox to validate the selected
  144. // settings
  145. combo_region = widget->combobox;
  146. } else if (setting->Id() == Settings::values.language_index.Id()) {
  147. combo_language = widget->combobox;
  148. }
  149. switch (setting->Category()) {
  150. case Settings::Category::Core:
  151. core_hold.emplace(setting->Id(), widget);
  152. break;
  153. case Settings::Category::System:
  154. system_hold.emplace(setting->Id(), widget);
  155. break;
  156. default:
  157. delete widget;
  158. }
  159. }
  160. for (const auto& [label, widget] : core_hold) {
  161. core_layout.addWidget(widget);
  162. }
  163. for (const auto& [id, widget] : system_hold) {
  164. system_layout.addWidget(widget);
  165. }
  166. }
  167. void ConfigureSystem::SetConfiguration() {}
  168. void ConfigureSystem::ApplyConfiguration() {
  169. const bool powered_on = system.IsPoweredOn();
  170. for (const auto& func : apply_funcs) {
  171. func(powered_on);
  172. }
  173. }