configure_ui.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <utility>
  5. #include <QFileDialog>
  6. #include <QDirIterator>
  7. #include "common/common_types.h"
  8. #include "common/fs/path_util.h"
  9. #include "common/logging/log.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_game_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. constexpr std::array default_folder_icon_sizes{
  24. std::make_pair(0, QT_TRANSLATE_NOOP("ConfigureUI", "None")),
  25. std::make_pair(24, QT_TRANSLATE_NOOP("ConfigureUI", "Small (24x24)")),
  26. std::make_pair(48, QT_TRANSLATE_NOOP("ConfigureUI", "Standard (48x48)")),
  27. std::make_pair(72, QT_TRANSLATE_NOOP("ConfigureUI", "Large (72x72)")),
  28. };
  29. // clang-format off
  30. constexpr std::array row_text_names{
  31. QT_TRANSLATE_NOOP("ConfigureUI", "Filename"),
  32. QT_TRANSLATE_NOOP("ConfigureUI", "Filetype"),
  33. QT_TRANSLATE_NOOP("ConfigureUI", "Title ID"),
  34. QT_TRANSLATE_NOOP("ConfigureUI", "Title Name"),
  35. QT_TRANSLATE_NOOP("ConfigureUI", "None"),
  36. };
  37. // clang-format on
  38. QString GetTranslatedGameIconSize(size_t index) {
  39. return QCoreApplication::translate("ConfigureUI", default_game_icon_sizes[index].second);
  40. }
  41. QString GetTranslatedFolderIconSize(size_t index) {
  42. return QCoreApplication::translate("ConfigureUI", default_folder_icon_sizes[index].second);
  43. }
  44. QString GetTranslatedRowTextName(size_t index) {
  45. return QCoreApplication::translate("ConfigureUI", row_text_names[index]);
  46. }
  47. } // Anonymous namespace
  48. ConfigureUi::ConfigureUi(Core::System& system_, QWidget* parent)
  49. : QWidget(parent), ui{std::make_unique<Ui::ConfigureUi>()}, system{system_} {
  50. ui->setupUi(this);
  51. InitializeLanguageComboBox();
  52. for (const auto& theme : UISettings::themes) {
  53. ui->theme_combobox->addItem(QString::fromUtf8(theme.first),
  54. QString::fromUtf8(theme.second));
  55. }
  56. InitializeIconSizeComboBox();
  57. InitializeRowComboBoxes();
  58. SetConfiguration();
  59. // Force game list reload if any of the relevant settings are changed.
  60. connect(ui->show_add_ons, &QCheckBox::stateChanged, this, &ConfigureUi::RequestGameListUpdate);
  61. connect(ui->show_compat, &QCheckBox::stateChanged, this, &ConfigureUi::RequestGameListUpdate);
  62. connect(ui->game_icon_size_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  63. &ConfigureUi::RequestGameListUpdate);
  64. connect(ui->folder_icon_size_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  65. this, &ConfigureUi::RequestGameListUpdate);
  66. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  67. &ConfigureUi::RequestGameListUpdate);
  68. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  69. &ConfigureUi::RequestGameListUpdate);
  70. // Update text ComboBoxes after user interaction.
  71. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::activated),
  72. [this] { ConfigureUi::UpdateSecondRowComboBox(); });
  73. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::activated),
  74. [this] { ConfigureUi::UpdateFirstRowComboBox(); });
  75. // Set screenshot path to user specification.
  76. connect(ui->screenshot_path_button, &QToolButton::pressed, this, [this] {
  77. auto dir =
  78. QFileDialog::getExistingDirectory(this, tr("Select Screenshots Path..."),
  79. QString::fromStdString(Common::FS::GetYuzuPathString(
  80. Common::FS::YuzuPath::ScreenshotsDir)));
  81. if (!dir.isEmpty()) {
  82. if (dir.back() != QChar::fromLatin1('/')) {
  83. dir.append(QChar::fromLatin1('/'));
  84. }
  85. ui->screenshot_path_edit->setText(dir);
  86. }
  87. });
  88. }
  89. ConfigureUi::~ConfigureUi() = default;
  90. void ConfigureUi::ApplyConfiguration() {
  91. UISettings::values.theme =
  92. ui->theme_combobox->itemData(ui->theme_combobox->currentIndex()).toString();
  93. UISettings::values.show_add_ons = ui->show_add_ons->isChecked();
  94. UISettings::values.show_compat = ui->show_compat->isChecked();
  95. UISettings::values.game_icon_size = ui->game_icon_size_combobox->currentData().toUInt();
  96. UISettings::values.folder_icon_size = ui->folder_icon_size_combobox->currentData().toUInt();
  97. UISettings::values.row_1_text_id = ui->row_1_text_combobox->currentData().toUInt();
  98. UISettings::values.row_2_text_id = ui->row_2_text_combobox->currentData().toUInt();
  99. UISettings::values.enable_screenshot_save_as = ui->enable_screenshot_save_as->isChecked();
  100. Common::FS::SetYuzuPath(Common::FS::YuzuPath::ScreenshotsDir,
  101. ui->screenshot_path_edit->text().toStdString());
  102. system.ApplySettings();
  103. }
  104. void ConfigureUi::RequestGameListUpdate() {
  105. UISettings::values.is_game_list_reload_pending.exchange(true);
  106. }
  107. void ConfigureUi::SetConfiguration() {
  108. ui->theme_combobox->setCurrentIndex(ui->theme_combobox->findData(UISettings::values.theme));
  109. ui->language_combobox->setCurrentIndex(
  110. ui->language_combobox->findData(UISettings::values.language));
  111. ui->show_add_ons->setChecked(UISettings::values.show_add_ons.GetValue());
  112. ui->show_compat->setChecked(UISettings::values.show_compat.GetValue());
  113. ui->game_icon_size_combobox->setCurrentIndex(
  114. ui->game_icon_size_combobox->findData(UISettings::values.game_icon_size.GetValue()));
  115. ui->folder_icon_size_combobox->setCurrentIndex(
  116. ui->folder_icon_size_combobox->findData(UISettings::values.folder_icon_size.GetValue()));
  117. ui->enable_screenshot_save_as->setChecked(
  118. UISettings::values.enable_screenshot_save_as.GetValue());
  119. ui->screenshot_path_edit->setText(QString::fromStdString(
  120. Common::FS::GetYuzuPathString(Common::FS::YuzuPath::ScreenshotsDir)));
  121. }
  122. void ConfigureUi::changeEvent(QEvent* event) {
  123. if (event->type() == QEvent::LanguageChange) {
  124. RetranslateUI();
  125. }
  126. QWidget::changeEvent(event);
  127. }
  128. void ConfigureUi::RetranslateUI() {
  129. ui->retranslateUi(this);
  130. for (int i = 0; i < ui->game_icon_size_combobox->count(); i++) {
  131. ui->game_icon_size_combobox->setItemText(i,
  132. GetTranslatedGameIconSize(static_cast<size_t>(i)));
  133. }
  134. for (int i = 0; i < ui->folder_icon_size_combobox->count(); i++) {
  135. ui->folder_icon_size_combobox->setItemText(
  136. i, GetTranslatedFolderIconSize(static_cast<size_t>(i)));
  137. }
  138. for (int i = 0; i < ui->row_1_text_combobox->count(); i++) {
  139. const QString name = GetTranslatedRowTextName(static_cast<size_t>(i));
  140. ui->row_1_text_combobox->setItemText(i, name);
  141. ui->row_2_text_combobox->setItemText(i, name);
  142. }
  143. }
  144. void ConfigureUi::InitializeLanguageComboBox() {
  145. // This is a list of lexicographically sorted languages, only the available translations are
  146. // shown to the user.
  147. static const struct {
  148. const QString name;
  149. const char* id;
  150. } languages[] = {
  151. // clang-format off
  152. {QStringLiteral(u"Bahasa Indonesia"), "id"}, // Indonesian
  153. {QStringLiteral(u"Bahasa Melayu"), "ms"}, // Malay
  154. {QStringLiteral(u"Catal\u00E0"), "ca"}, // Catalan
  155. {QStringLiteral(u"\u010Ce\u0161tina"), "cs"}, // Czech
  156. {QStringLiteral(u"Dansk"), "da"}, // Danish
  157. {QStringLiteral(u"Deutsch"), "de"}, // German
  158. {QStringLiteral(u"English"), "en"}, // English
  159. {QStringLiteral(u"Espa\u00F1ol"), "es"}, // Spanish
  160. {QStringLiteral(u"Fran\u00E7ais"), "fr"}, // French
  161. {QStringLiteral(u"Hrvatski"), "hr"}, // Croatian
  162. {QStringLiteral(u"Italiano"), "it"}, // Italian
  163. {QStringLiteral(u"Magyar"), "hu"}, // Hungarian
  164. {QStringLiteral(u"Nederlands"), "nl"}, // Dutch
  165. {QStringLiteral(u"Norsk bokm\u00E5l"), "nb"}, // Norwegian
  166. {QStringLiteral(u"Polski"), "pl"}, // Polish
  167. {QStringLiteral(u"Portugu\u00EAs"), "pt_PT"}, // Portuguese
  168. {QStringLiteral(u"Portugu\u00EAs (Brasil)"), "pt_BR"}, // Portuguese (Brazil)
  169. {QStringLiteral(u"Rom\u00E2n\u0103"), "ro"}, // Romanian
  170. {QStringLiteral(u"Srpski"), "sr"}, // Serbian
  171. {QStringLiteral(u"Suomi"), "fi"}, // Finnish
  172. {QStringLiteral(u"Svenska"), "sv"}, // Swedish
  173. {QStringLiteral(u"Ti\u1EBFng Vi\u1EC7t"), "vi"}, // Vietnamese
  174. {QStringLiteral(u"Ti\u1EBFng Vi\u1EC7t (Vi\u1EC7t Nam)"), "vi_VN"}, // Vietnamese
  175. {QStringLiteral(u"T\u00FCrk\u00E7e"), "tr_TR"}, // Turkish
  176. {QStringLiteral(u"\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC"), "el"}, // Greek
  177. {QStringLiteral(u"\u0420\u0443\u0441\u0441\u043A\u0438\u0439"), "ru_RU"}, // Russian
  178. {QStringLiteral(u"\u0423\u043A\u0440\u0430\u0457\u043D\u0441\u044C\u043A\u0430"),
  179. "uk"}, // Ukrainian
  180. {QStringLiteral(u"\u0627\u0644\u0639\u0631\u0628\u064A\u0629"), "ar"}, // Arabic
  181. {QStringLiteral(u"\u0641\u0627\u0631\u0633\u06CC"), "fa"}, // Farsi
  182. {QStringLiteral(u"\uD55C\uAD6D\uC5B4"), "ko_KR"}, // Korean
  183. {QStringLiteral(u"\u65E5\u672C\u8A9E"), "ja_JP"}, // Japanese
  184. {QStringLiteral(u"\u7B80\u4F53\u4E2D\u6587"), "zh_CN"}, // Simplified Chinese
  185. {QStringLiteral(u"\u7E41\u9AD4\u4E2D\u6587"), "zh_TW"}, // Traditional Chinese
  186. // clang-format on
  187. };
  188. ui->language_combobox->addItem(tr("<System>"), QString{});
  189. QDir languages_dir{QStringLiteral(":/languages")};
  190. QStringList language_files = languages_dir.entryList();
  191. for (const auto& lang : languages) {
  192. if (QString::fromLatin1(lang.id) == QStringLiteral("en")) {
  193. ui->language_combobox->addItem(lang.name, QStringLiteral("en"));
  194. language_files.removeOne(QStringLiteral("en.qm"));
  195. continue;
  196. }
  197. for (int i = 0; i < language_files.size(); ++i) {
  198. QString locale = language_files[i];
  199. locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
  200. if (QString::fromLatin1(lang.id) == locale) {
  201. ui->language_combobox->addItem(lang.name, locale);
  202. language_files.removeAt(i);
  203. break;
  204. }
  205. }
  206. }
  207. // Anything remaining will be at the bottom
  208. for (const QString& file : language_files) {
  209. LOG_CRITICAL(Frontend, "Unexpected Language File: {}", file.toStdString());
  210. QString locale = file;
  211. locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
  212. const QString language_name = QLocale::languageToString(QLocale(locale).language());
  213. const QString lang = QStringLiteral("%1 [%2]").arg(language_name, locale);
  214. ui->language_combobox->addItem(lang, locale);
  215. }
  216. // Unlike other configuration changes, interface language changes need to be reflected on the
  217. // interface immediately. This is done by passing a signal to the main window, and then
  218. // retranslating when passing back.
  219. connect(ui->language_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  220. &ConfigureUi::OnLanguageChanged);
  221. }
  222. void ConfigureUi::InitializeIconSizeComboBox() {
  223. for (size_t i = 0; i < default_game_icon_sizes.size(); i++) {
  224. const auto size = default_game_icon_sizes[i].first;
  225. ui->game_icon_size_combobox->addItem(GetTranslatedGameIconSize(i), size);
  226. }
  227. for (size_t i = 0; i < default_folder_icon_sizes.size(); i++) {
  228. const auto size = default_folder_icon_sizes[i].first;
  229. ui->folder_icon_size_combobox->addItem(GetTranslatedFolderIconSize(i), size);
  230. }
  231. }
  232. void ConfigureUi::InitializeRowComboBoxes() {
  233. UpdateFirstRowComboBox(true);
  234. UpdateSecondRowComboBox(true);
  235. }
  236. void ConfigureUi::UpdateFirstRowComboBox(bool init) {
  237. const int currentIndex =
  238. init ? UISettings::values.row_1_text_id.GetValue()
  239. : ui->row_1_text_combobox->findData(ui->row_1_text_combobox->currentData());
  240. ui->row_1_text_combobox->clear();
  241. for (std::size_t i = 0; i < row_text_names.size(); i++) {
  242. const QString row_text_name = GetTranslatedRowTextName(i);
  243. ui->row_1_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  244. }
  245. ui->row_1_text_combobox->setCurrentIndex(ui->row_1_text_combobox->findData(currentIndex));
  246. ui->row_1_text_combobox->removeItem(4); // None
  247. ui->row_1_text_combobox->removeItem(
  248. ui->row_1_text_combobox->findData(ui->row_2_text_combobox->currentData()));
  249. }
  250. void ConfigureUi::UpdateSecondRowComboBox(bool init) {
  251. const int currentIndex =
  252. init ? UISettings::values.row_2_text_id.GetValue()
  253. : ui->row_2_text_combobox->findData(ui->row_2_text_combobox->currentData());
  254. ui->row_2_text_combobox->clear();
  255. for (std::size_t i = 0; i < row_text_names.size(); ++i) {
  256. const QString row_text_name = GetTranslatedRowTextName(i);
  257. ui->row_2_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  258. }
  259. ui->row_2_text_combobox->setCurrentIndex(ui->row_2_text_combobox->findData(currentIndex));
  260. ui->row_2_text_combobox->removeItem(
  261. ui->row_2_text_combobox->findData(ui->row_1_text_combobox->currentData()));
  262. }
  263. void ConfigureUi::OnLanguageChanged(int index) {
  264. if (index == -1)
  265. return;
  266. emit LanguageChanged(ui->language_combobox->itemData(index).toString());
  267. }