configure_system.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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",
  77. !Settings::values.language_index.UsingGlobal());
  78. ConfigurationShared::SetHighlight(ui->label_region, "label_region",
  79. !Settings::values.region_index.UsingGlobal());
  80. ConfigurationShared::SetHighlight(ui->label_timezone, "label_timezone",
  81. !Settings::values.time_zone_index.UsingGlobal());
  82. ConfigurationShared::SetHighlight(ui->label_sound, "label_sound",
  83. !Settings::values.sound_index.UsingGlobal());
  84. }
  85. }
  86. void ConfigureSystem::ReadSystemSettings() {}
  87. void ConfigureSystem::ApplyConfiguration() {
  88. if (!enabled) {
  89. return;
  90. }
  91. if (Settings::configuring_global) {
  92. // Guard if during game and set to game-specific value
  93. if (Settings::values.language_index.UsingGlobal()) {
  94. Settings::values.language_index.SetValue(ui->combo_language->currentIndex());
  95. }
  96. if (Settings::values.region_index.UsingGlobal()) {
  97. Settings::values.region_index.SetValue(ui->combo_region->currentIndex());
  98. }
  99. if (Settings::values.time_zone_index.UsingGlobal()) {
  100. Settings::values.time_zone_index.SetValue(ui->combo_time_zone->currentIndex());
  101. }
  102. if (Settings::values.sound_index.UsingGlobal()) {
  103. Settings::values.sound_index.SetValue(ui->combo_sound->currentIndex());
  104. }
  105. if (Settings::values.rng_seed.UsingGlobal()) {
  106. if (ui->rng_seed_checkbox->isChecked()) {
  107. Settings::values.rng_seed.SetValue(
  108. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  109. } else {
  110. Settings::values.rng_seed.SetValue(std::nullopt);
  111. }
  112. }
  113. if (Settings::values.custom_rtc.UsingGlobal()) {
  114. if (ui->custom_rtc_checkbox->isChecked()) {
  115. Settings::values.custom_rtc.SetValue(
  116. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  117. } else {
  118. Settings::values.custom_rtc.SetValue(std::nullopt);
  119. }
  120. }
  121. } else {
  122. ConfigurationShared::ApplyPerGameSetting(&Settings::values.language_index,
  123. ui->combo_language);
  124. ConfigurationShared::ApplyPerGameSetting(&Settings::values.region_index, ui->combo_region);
  125. ConfigurationShared::ApplyPerGameSetting(&Settings::values.time_zone_index,
  126. ui->combo_time_zone);
  127. ConfigurationShared::ApplyPerGameSetting(&Settings::values.sound_index, ui->combo_sound);
  128. switch (trackers.use_rng_seed) {
  129. case ConfigurationShared::CheckState::On:
  130. case ConfigurationShared::CheckState::Off:
  131. Settings::values.rng_seed.SetGlobal(false);
  132. if (ui->rng_seed_checkbox->isChecked()) {
  133. Settings::values.rng_seed.SetValue(
  134. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  135. } else {
  136. Settings::values.rng_seed.SetValue(std::nullopt);
  137. }
  138. break;
  139. case ConfigurationShared::CheckState::Global:
  140. Settings::values.rng_seed.SetGlobal(false);
  141. Settings::values.rng_seed.SetValue(std::nullopt);
  142. Settings::values.rng_seed.SetGlobal(true);
  143. break;
  144. case ConfigurationShared::CheckState::Count:
  145. break;
  146. }
  147. switch (trackers.use_custom_rtc) {
  148. case ConfigurationShared::CheckState::On:
  149. case ConfigurationShared::CheckState::Off:
  150. Settings::values.custom_rtc.SetGlobal(false);
  151. if (ui->custom_rtc_checkbox->isChecked()) {
  152. Settings::values.custom_rtc.SetValue(
  153. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  154. } else {
  155. Settings::values.custom_rtc.SetValue(std::nullopt);
  156. }
  157. break;
  158. case ConfigurationShared::CheckState::Global:
  159. Settings::values.custom_rtc.SetGlobal(false);
  160. Settings::values.custom_rtc.SetValue(std::nullopt);
  161. Settings::values.custom_rtc.SetGlobal(true);
  162. break;
  163. case ConfigurationShared::CheckState::Count:
  164. break;
  165. }
  166. }
  167. Settings::Apply();
  168. }
  169. void ConfigureSystem::RefreshConsoleID() {
  170. QMessageBox::StandardButton reply;
  171. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  172. "Your current virtual Switch will not be recoverable. "
  173. "This might have unexpected effects in games. This might fail, "
  174. "if you use an outdated config savegame. Continue?");
  175. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  176. QMessageBox::No | QMessageBox::Yes);
  177. if (reply == QMessageBox::No) {
  178. return;
  179. }
  180. u64 console_id{};
  181. ui->label_console_id->setText(
  182. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  183. }
  184. void ConfigureSystem::SetupPerGameUI() {
  185. if (Settings::configuring_global) {
  186. ui->combo_language->setEnabled(Settings::values.language_index.UsingGlobal());
  187. ui->combo_region->setEnabled(Settings::values.region_index.UsingGlobal());
  188. ui->combo_time_zone->setEnabled(Settings::values.time_zone_index.UsingGlobal());
  189. ui->combo_sound->setEnabled(Settings::values.sound_index.UsingGlobal());
  190. ui->rng_seed_checkbox->setEnabled(Settings::values.rng_seed.UsingGlobal());
  191. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.UsingGlobal());
  192. ui->custom_rtc_checkbox->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  193. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  194. return;
  195. }
  196. ConfigurationShared::SetColoredComboBox(ui->combo_language, ui->label_language,
  197. "label_language",
  198. Settings::values.language_index.GetValue(true));
  199. ConfigurationShared::SetColoredComboBox(ui->combo_region, ui->label_region, "label_region",
  200. Settings::values.region_index.GetValue(true));
  201. ConfigurationShared::SetColoredComboBox(ui->combo_time_zone, ui->label_timezone,
  202. "label_timezone",
  203. Settings::values.time_zone_index.GetValue(true));
  204. ConfigurationShared::SetColoredComboBox(ui->combo_sound, ui->label_sound, "label_sound",
  205. Settings::values.sound_index.GetValue(true));
  206. ConfigurationShared::SetColoredTristate(ui->rng_seed_checkbox, "rng_seed_checkbox",
  207. Settings::values.rng_seed.UsingGlobal(),
  208. Settings::values.rng_seed.GetValue().has_value(),
  209. Settings::values.rng_seed.GetValue(true).has_value(),
  210. trackers.use_rng_seed);
  211. ConfigurationShared::SetColoredTristate(ui->custom_rtc_checkbox, "custom_rtc_checkbox",
  212. Settings::values.custom_rtc.UsingGlobal(),
  213. Settings::values.custom_rtc.GetValue().has_value(),
  214. Settings::values.custom_rtc.GetValue(true).has_value(),
  215. trackers.use_custom_rtc);
  216. }