configure_system.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <chrono>
  6. #include <optional>
  7. #include <QFileDialog>
  8. #include <QGraphicsItem>
  9. #include <QMessageBox>
  10. #include "common/assert.h"
  11. #include "common/file_util.h"
  12. #include "core/core.h"
  13. #include "core/settings.h"
  14. #include "ui_configure_system.h"
  15. #include "yuzu/configuration/configuration_shared.h"
  16. #include "yuzu/configuration/configure_system.h"
  17. ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureSystem) {
  18. ui->setupUi(this);
  19. connect(ui->button_regenerate_console_id, &QPushButton::clicked, this,
  20. &ConfigureSystem::RefreshConsoleID);
  21. connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  22. ui->rng_seed_edit->setEnabled(state == Qt::Checked);
  23. if (state != Qt::Checked) {
  24. ui->rng_seed_edit->setText(QStringLiteral("00000000"));
  25. }
  26. });
  27. connect(ui->custom_rtc_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  28. ui->custom_rtc_edit->setEnabled(state == Qt::Checked);
  29. if (state != Qt::Checked) {
  30. ui->custom_rtc_edit->setDateTime(QDateTime::currentDateTime());
  31. }
  32. });
  33. ui->label_console_id->setVisible(Settings::configuring_global);
  34. ui->button_regenerate_console_id->setVisible(Settings::configuring_global);
  35. SetupPerGameUI();
  36. SetConfiguration();
  37. }
  38. ConfigureSystem::~ConfigureSystem() = default;
  39. void ConfigureSystem::changeEvent(QEvent* event) {
  40. if (event->type() == QEvent::LanguageChange) {
  41. RetranslateUI();
  42. }
  43. QWidget::changeEvent(event);
  44. }
  45. void ConfigureSystem::RetranslateUI() {
  46. ui->retranslateUi(this);
  47. }
  48. void ConfigureSystem::SetConfiguration() {
  49. enabled = !Core::System::GetInstance().IsPoweredOn();
  50. const auto rng_seed =
  51. QStringLiteral("%1")
  52. .arg(Settings::values.rng_seed.GetValue().value_or(0), 8, 16, QLatin1Char{'0'})
  53. .toUpper();
  54. const auto rtc_time = Settings::values.custom_rtc.GetValue().value_or(
  55. std::chrono::seconds(QDateTime::currentSecsSinceEpoch()));
  56. ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.GetValue().has_value());
  57. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.GetValue().has_value() &&
  58. Settings::values.rng_seed.UsingGlobal());
  59. ui->rng_seed_edit->setText(rng_seed);
  60. ui->custom_rtc_checkbox->setChecked(Settings::values.custom_rtc.GetValue().has_value());
  61. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.GetValue().has_value() &&
  62. Settings::values.rng_seed.UsingGlobal());
  63. ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time.count()));
  64. if (Settings::configuring_global) {
  65. ui->combo_language->setCurrentIndex(Settings::values.language_index.GetValue());
  66. ui->combo_region->setCurrentIndex(Settings::values.region_index.GetValue());
  67. ui->combo_time_zone->setCurrentIndex(Settings::values.time_zone_index.GetValue());
  68. ui->combo_sound->setCurrentIndex(Settings::values.sound_index.GetValue());
  69. } else {
  70. ConfigurationShared::SetPerGameSetting(ui->combo_language,
  71. &Settings::values.language_index);
  72. ConfigurationShared::SetPerGameSetting(ui->combo_region, &Settings::values.region_index);
  73. ConfigurationShared::SetPerGameSetting(ui->combo_time_zone,
  74. &Settings::values.time_zone_index);
  75. ConfigurationShared::SetPerGameSetting(ui->combo_sound, &Settings::values.sound_index);
  76. ConfigurationShared::SetHighlight(ui->label_language, "label_language", !Settings::values.language_index.UsingGlobal());
  77. ConfigurationShared::SetHighlight(ui->label_region, "label_region", !Settings::values.region_index.UsingGlobal());
  78. ConfigurationShared::SetHighlight(ui->label_timezone, "label_timezone", !Settings::values.time_zone_index.UsingGlobal());
  79. ConfigurationShared::SetHighlight(ui->label_sound, "label_sound", !Settings::values.sound_index.UsingGlobal());
  80. }
  81. }
  82. void ConfigureSystem::ReadSystemSettings() {}
  83. void ConfigureSystem::ApplyConfiguration() {
  84. if (!enabled) {
  85. return;
  86. }
  87. if (Settings::configuring_global) {
  88. // Guard if during game and set to game-specific value
  89. if (Settings::values.language_index.UsingGlobal()) {
  90. Settings::values.language_index.SetValue(ui->combo_language->currentIndex());
  91. }
  92. if (Settings::values.region_index.UsingGlobal()) {
  93. Settings::values.region_index.SetValue(ui->combo_region->currentIndex());
  94. }
  95. if (Settings::values.time_zone_index.UsingGlobal()) {
  96. Settings::values.time_zone_index.SetValue(ui->combo_time_zone->currentIndex());
  97. }
  98. if (Settings::values.sound_index.UsingGlobal()) {
  99. Settings::values.sound_index.SetValue(ui->combo_sound->currentIndex());
  100. }
  101. if (Settings::values.rng_seed.UsingGlobal()) {
  102. if (ui->rng_seed_checkbox->isChecked()) {
  103. Settings::values.rng_seed.SetValue(
  104. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  105. } else {
  106. Settings::values.rng_seed.SetValue(std::nullopt);
  107. }
  108. }
  109. if (Settings::values.custom_rtc.UsingGlobal()) {
  110. if (ui->custom_rtc_checkbox->isChecked()) {
  111. Settings::values.custom_rtc.SetValue(
  112. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  113. } else {
  114. Settings::values.custom_rtc.SetValue(std::nullopt);
  115. }
  116. }
  117. } else {
  118. ConfigurationShared::ApplyPerGameSetting(&Settings::values.language_index,
  119. ui->combo_language);
  120. ConfigurationShared::ApplyPerGameSetting(&Settings::values.region_index, ui->combo_region);
  121. ConfigurationShared::ApplyPerGameSetting(&Settings::values.time_zone_index,
  122. ui->combo_time_zone);
  123. ConfigurationShared::ApplyPerGameSetting(&Settings::values.sound_index, ui->combo_sound);
  124. switch (ConfigurationShared::trackers.use_rng_seed) {
  125. case ConfigurationShared::CheckState::On:
  126. case ConfigurationShared::CheckState::Off:
  127. Settings::values.rng_seed.SetGlobal(false);
  128. if (ui->rng_seed_checkbox->isChecked()) {
  129. Settings::values.rng_seed.SetValue(
  130. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  131. } else {
  132. Settings::values.rng_seed.SetValue(std::nullopt);
  133. }
  134. break;
  135. case ConfigurationShared::CheckState::Global:
  136. Settings::values.rng_seed.SetGlobal(false);
  137. Settings::values.rng_seed.SetValue(std::nullopt);
  138. Settings::values.rng_seed.SetGlobal(true);
  139. break;
  140. case ConfigurationShared::CheckState::Count:;
  141. }
  142. switch (ConfigurationShared::trackers.use_custom_rtc) {
  143. case ConfigurationShared::CheckState::On:
  144. case ConfigurationShared::CheckState::Off:
  145. Settings::values.custom_rtc.SetGlobal(false);
  146. if (ui->custom_rtc_checkbox->isChecked()) {
  147. Settings::values.custom_rtc.SetValue(
  148. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  149. } else {
  150. Settings::values.custom_rtc.SetValue(std::nullopt);
  151. }
  152. break;
  153. case ConfigurationShared::CheckState::Global:
  154. Settings::values.custom_rtc.SetGlobal(false);
  155. Settings::values.custom_rtc.SetValue(std::nullopt);
  156. Settings::values.custom_rtc.SetGlobal(true);
  157. break;
  158. case ConfigurationShared::CheckState::Count:;
  159. }
  160. }
  161. Settings::Apply();
  162. }
  163. void ConfigureSystem::RefreshConsoleID() {
  164. QMessageBox::StandardButton reply;
  165. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  166. "Your current virtual Switch will not be recoverable. "
  167. "This might have unexpected effects in games. This might fail, "
  168. "if you use an outdated config savegame. Continue?");
  169. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  170. QMessageBox::No | QMessageBox::Yes);
  171. if (reply == QMessageBox::No) {
  172. return;
  173. }
  174. u64 console_id{};
  175. ui->label_console_id->setText(
  176. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  177. }
  178. void ConfigureSystem::SetupPerGameUI() {
  179. if (Settings::configuring_global) {
  180. ui->combo_language->setEnabled(Settings::values.language_index.UsingGlobal());
  181. ui->combo_region->setEnabled(Settings::values.region_index.UsingGlobal());
  182. ui->combo_time_zone->setEnabled(Settings::values.time_zone_index.UsingGlobal());
  183. ui->combo_sound->setEnabled(Settings::values.sound_index.UsingGlobal());
  184. ui->rng_seed_checkbox->setEnabled(Settings::values.rng_seed.UsingGlobal());
  185. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.UsingGlobal());
  186. ui->custom_rtc_checkbox->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  187. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  188. return;
  189. }
  190. ConfigurationShared::SetColoredComboBox(ui->combo_language, ui->label_language,
  191. "label_language",
  192. Settings::values.language_index.GetValue(true));
  193. ConfigurationShared::SetColoredComboBox(ui->combo_region, ui->label_region, "label_region",
  194. Settings::values.region_index.GetValue(true));
  195. ConfigurationShared::SetColoredComboBox(ui->combo_time_zone, ui->label_timezone,
  196. "label_timezone",
  197. Settings::values.time_zone_index.GetValue(true));
  198. ConfigurationShared::SetColoredComboBox(ui->combo_sound, ui->label_sound, "label_sound",
  199. Settings::values.sound_index.GetValue(true));
  200. ConfigurationShared::SetColoredTristate(ui->rng_seed_checkbox, "rng_seed_checkbox",
  201. Settings::values.rng_seed.UsingGlobal(),
  202. Settings::values.rng_seed.GetValue().has_value(),
  203. Settings::values.rng_seed.GetValue(true).has_value(),
  204. ConfigurationShared::trackers.use_rng_seed);
  205. ConfigurationShared::SetColoredTristate(ui->custom_rtc_checkbox, "custom_rtc_checkbox",
  206. Settings::values.custom_rtc.UsingGlobal(),
  207. Settings::values.custom_rtc.GetValue().has_value(),
  208. Settings::values.custom_rtc.GetValue(true).has_value(),
  209. ConfigurationShared::trackers.use_custom_rtc);
  210. }