configure_ui.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <utility>
  6. #include <QFileDialog>
  7. #include <QDirIterator>
  8. #include "common/common_types.h"
  9. #include "common/fs/path_util.h"
  10. #include "common/settings.h"
  11. #include "core/core.h"
  12. #include "ui_configure_ui.h"
  13. #include "yuzu/configuration/configure_ui.h"
  14. #include "yuzu/uisettings.h"
  15. namespace {
  16. constexpr std::array default_icon_sizes{
  17. std::make_pair(0, QT_TRANSLATE_NOOP("ConfigureUI", "None")),
  18. std::make_pair(32, QT_TRANSLATE_NOOP("ConfigureUI", "Small (32x32)")),
  19. std::make_pair(64, QT_TRANSLATE_NOOP("ConfigureUI", "Standard (64x64)")),
  20. std::make_pair(128, QT_TRANSLATE_NOOP("ConfigureUI", "Large (128x128)")),
  21. std::make_pair(256, QT_TRANSLATE_NOOP("ConfigureUI", "Full Size (256x256)")),
  22. };
  23. // clang-format off
  24. constexpr std::array row_text_names{
  25. QT_TRANSLATE_NOOP("ConfigureUI", "Filename"),
  26. QT_TRANSLATE_NOOP("ConfigureUI", "Filetype"),
  27. QT_TRANSLATE_NOOP("ConfigureUI", "Title ID"),
  28. QT_TRANSLATE_NOOP("ConfigureUI", "Title Name"),
  29. QT_TRANSLATE_NOOP("ConfigureUI", "None"),
  30. };
  31. // clang-format on
  32. QString GetTranslatedIconSize(size_t index) {
  33. return QCoreApplication::translate("ConfigureUI", default_icon_sizes[index].second);
  34. }
  35. QString GetTranslatedRowTextName(size_t index) {
  36. return QCoreApplication::translate("ConfigureUI", row_text_names[index]);
  37. }
  38. } // Anonymous namespace
  39. ConfigureUi::ConfigureUi(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureUi) {
  40. ui->setupUi(this);
  41. InitializeLanguageComboBox();
  42. for (const auto& theme : UISettings::themes) {
  43. ui->theme_combobox->addItem(QString::fromUtf8(theme.first),
  44. QString::fromUtf8(theme.second));
  45. }
  46. InitializeIconSizeComboBox();
  47. InitializeRowComboBoxes();
  48. SetConfiguration();
  49. // Force game list reload if any of the relevant settings are changed.
  50. connect(ui->show_add_ons, &QCheckBox::stateChanged, this, &ConfigureUi::RequestGameListUpdate);
  51. connect(ui->icon_size_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  52. &ConfigureUi::RequestGameListUpdate);
  53. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  54. &ConfigureUi::RequestGameListUpdate);
  55. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  56. &ConfigureUi::RequestGameListUpdate);
  57. // Update text ComboBoxes after user interaction.
  58. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::activated),
  59. [this] { ConfigureUi::UpdateSecondRowComboBox(); });
  60. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::activated),
  61. [this] { ConfigureUi::UpdateFirstRowComboBox(); });
  62. // Set screenshot path to user specification.
  63. connect(ui->screenshot_path_button, &QToolButton::pressed, this, [this] {
  64. auto dir =
  65. QFileDialog::getExistingDirectory(this, tr("Select Screenshots Path..."),
  66. QString::fromStdString(Common::FS::GetYuzuPathString(
  67. Common::FS::YuzuPath::ScreenshotsDir)));
  68. if (!dir.isEmpty()) {
  69. if (dir.back() != QChar::fromLatin1('/')) {
  70. dir.append(QChar::fromLatin1('/'));
  71. }
  72. ui->screenshot_path_edit->setText(dir);
  73. }
  74. });
  75. }
  76. ConfigureUi::~ConfigureUi() = default;
  77. void ConfigureUi::ApplyConfiguration() {
  78. UISettings::values.theme =
  79. ui->theme_combobox->itemData(ui->theme_combobox->currentIndex()).toString();
  80. UISettings::values.show_add_ons = ui->show_add_ons->isChecked();
  81. UISettings::values.icon_size = ui->icon_size_combobox->currentData().toUInt();
  82. UISettings::values.row_1_text_id = ui->row_1_text_combobox->currentData().toUInt();
  83. UISettings::values.row_2_text_id = ui->row_2_text_combobox->currentData().toUInt();
  84. UISettings::values.enable_screenshot_save_as = ui->enable_screenshot_save_as->isChecked();
  85. Common::FS::SetYuzuPath(Common::FS::YuzuPath::ScreenshotsDir,
  86. ui->screenshot_path_edit->text().toStdString());
  87. Core::System::GetInstance().ApplySettings();
  88. }
  89. void ConfigureUi::RequestGameListUpdate() {
  90. UISettings::values.is_game_list_reload_pending.exchange(true);
  91. }
  92. void ConfigureUi::SetConfiguration() {
  93. ui->theme_combobox->setCurrentIndex(ui->theme_combobox->findData(UISettings::values.theme));
  94. ui->language_combobox->setCurrentIndex(
  95. ui->language_combobox->findData(UISettings::values.language));
  96. ui->show_add_ons->setChecked(UISettings::values.show_add_ons);
  97. ui->icon_size_combobox->setCurrentIndex(
  98. ui->icon_size_combobox->findData(UISettings::values.icon_size));
  99. ui->enable_screenshot_save_as->setChecked(UISettings::values.enable_screenshot_save_as);
  100. ui->screenshot_path_edit->setText(QString::fromStdString(
  101. Common::FS::GetYuzuPathString(Common::FS::YuzuPath::ScreenshotsDir)));
  102. }
  103. void ConfigureUi::changeEvent(QEvent* event) {
  104. if (event->type() == QEvent::LanguageChange) {
  105. RetranslateUI();
  106. }
  107. QWidget::changeEvent(event);
  108. }
  109. void ConfigureUi::RetranslateUI() {
  110. ui->retranslateUi(this);
  111. for (int i = 0; i < ui->icon_size_combobox->count(); i++) {
  112. ui->icon_size_combobox->setItemText(i, GetTranslatedIconSize(static_cast<size_t>(i)));
  113. }
  114. for (int i = 0; i < ui->row_1_text_combobox->count(); i++) {
  115. const QString name = GetTranslatedRowTextName(static_cast<size_t>(i));
  116. ui->row_1_text_combobox->setItemText(i, name);
  117. ui->row_2_text_combobox->setItemText(i, name);
  118. }
  119. }
  120. void ConfigureUi::InitializeLanguageComboBox() {
  121. ui->language_combobox->addItem(tr("<System>"), QString{});
  122. ui->language_combobox->addItem(tr("English"), QStringLiteral("en"));
  123. QDirIterator it(QStringLiteral(":/languages"), QDirIterator::NoIteratorFlags);
  124. while (it.hasNext()) {
  125. QString locale = it.next();
  126. locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
  127. locale.remove(0, locale.lastIndexOf(QLatin1Char{'/'}) + 1);
  128. const QString lang = QLocale::languageToString(QLocale(locale).language());
  129. ui->language_combobox->addItem(lang, locale);
  130. }
  131. // Unlike other configuration changes, interface language changes need to be reflected on the
  132. // interface immediately. This is done by passing a signal to the main window, and then
  133. // retranslating when passing back.
  134. connect(ui->language_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  135. &ConfigureUi::OnLanguageChanged);
  136. }
  137. void ConfigureUi::InitializeIconSizeComboBox() {
  138. for (size_t i = 0; i < default_icon_sizes.size(); i++) {
  139. const auto size = default_icon_sizes[i].first;
  140. ui->icon_size_combobox->addItem(GetTranslatedIconSize(i), size);
  141. }
  142. }
  143. void ConfigureUi::InitializeRowComboBoxes() {
  144. UpdateFirstRowComboBox(true);
  145. UpdateSecondRowComboBox(true);
  146. }
  147. void ConfigureUi::UpdateFirstRowComboBox(bool init) {
  148. const int currentIndex =
  149. init ? UISettings::values.row_1_text_id
  150. : ui->row_1_text_combobox->findData(ui->row_1_text_combobox->currentData());
  151. ui->row_1_text_combobox->clear();
  152. for (std::size_t i = 0; i < row_text_names.size(); i++) {
  153. const QString row_text_name = GetTranslatedRowTextName(i);
  154. ui->row_1_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  155. }
  156. ui->row_1_text_combobox->setCurrentIndex(ui->row_1_text_combobox->findData(currentIndex));
  157. ui->row_1_text_combobox->removeItem(4); // None
  158. ui->row_1_text_combobox->removeItem(
  159. ui->row_1_text_combobox->findData(ui->row_2_text_combobox->currentData()));
  160. }
  161. void ConfigureUi::UpdateSecondRowComboBox(bool init) {
  162. const int currentIndex =
  163. init ? UISettings::values.row_2_text_id
  164. : ui->row_2_text_combobox->findData(ui->row_2_text_combobox->currentData());
  165. ui->row_2_text_combobox->clear();
  166. for (std::size_t i = 0; i < row_text_names.size(); ++i) {
  167. const QString row_text_name = GetTranslatedRowTextName(i);
  168. ui->row_2_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  169. }
  170. ui->row_2_text_combobox->setCurrentIndex(ui->row_2_text_combobox->findData(currentIndex));
  171. ui->row_2_text_combobox->removeItem(
  172. ui->row_2_text_combobox->findData(ui->row_1_text_combobox->currentData()));
  173. }
  174. void ConfigureUi::OnLanguageChanged(int index) {
  175. if (index == -1)
  176. return;
  177. emit LanguageChanged(ui->language_combobox->itemData(index).toString());
  178. }