configure_system.cpp 6.7 KB

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