configure_ui.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "yuzu/configuration/configure_ui.h"
  4. #include <array>
  5. #include <cstdlib>
  6. #include <set>
  7. #include <stdexcept>
  8. #include <string>
  9. #include <utility>
  10. #include <QCheckBox>
  11. #include <QComboBox>
  12. #include <QCoreApplication>
  13. #include <QDirIterator>
  14. #include <QFileDialog>
  15. #include <QString>
  16. #include <QToolButton>
  17. #include <QVariant>
  18. #include "common/common_types.h"
  19. #include "common/fs/path_util.h"
  20. #include "common/logging/log.h"
  21. #include "common/settings.h"
  22. #include "common/settings_enums.h"
  23. #include "core/core.h"
  24. #include "core/frontend/framebuffer_layout.h"
  25. #include "ui_configure_ui.h"
  26. #include "yuzu/uisettings.h"
  27. namespace {
  28. constexpr std::array default_game_icon_sizes{
  29. std::make_pair(0, QT_TRANSLATE_NOOP("ConfigureUI", "None")),
  30. std::make_pair(32, QT_TRANSLATE_NOOP("ConfigureUI", "Small (32x32)")),
  31. std::make_pair(64, QT_TRANSLATE_NOOP("ConfigureUI", "Standard (64x64)")),
  32. std::make_pair(128, QT_TRANSLATE_NOOP("ConfigureUI", "Large (128x128)")),
  33. std::make_pair(256, QT_TRANSLATE_NOOP("ConfigureUI", "Full Size (256x256)")),
  34. };
  35. constexpr std::array default_folder_icon_sizes{
  36. std::make_pair(0, QT_TRANSLATE_NOOP("ConfigureUI", "None")),
  37. std::make_pair(24, QT_TRANSLATE_NOOP("ConfigureUI", "Small (24x24)")),
  38. std::make_pair(48, QT_TRANSLATE_NOOP("ConfigureUI", "Standard (48x48)")),
  39. std::make_pair(72, QT_TRANSLATE_NOOP("ConfigureUI", "Large (72x72)")),
  40. };
  41. // clang-format off
  42. constexpr std::array row_text_names{
  43. QT_TRANSLATE_NOOP("ConfigureUI", "Filename"),
  44. QT_TRANSLATE_NOOP("ConfigureUI", "Filetype"),
  45. QT_TRANSLATE_NOOP("ConfigureUI", "Title ID"),
  46. QT_TRANSLATE_NOOP("ConfigureUI", "Title Name"),
  47. QT_TRANSLATE_NOOP("ConfigureUI", "None"),
  48. };
  49. // clang-format on
  50. QString GetTranslatedGameIconSize(size_t index) {
  51. return QCoreApplication::translate("ConfigureUI", default_game_icon_sizes[index].second);
  52. }
  53. QString GetTranslatedFolderIconSize(size_t index) {
  54. return QCoreApplication::translate("ConfigureUI", default_folder_icon_sizes[index].second);
  55. }
  56. QString GetTranslatedRowTextName(size_t index) {
  57. return QCoreApplication::translate("ConfigureUI", row_text_names[index]);
  58. }
  59. } // Anonymous namespace
  60. static float GetUpFactor(Settings::ResolutionSetup res_setup) {
  61. Settings::ResolutionScalingInfo info{};
  62. Settings::TranslateResolutionInfo(res_setup, info);
  63. return info.up_factor;
  64. }
  65. static void PopulateResolutionComboBox(QComboBox* screenshot_height, QWidget* parent) {
  66. screenshot_height->clear();
  67. const auto& enumeration =
  68. Settings::EnumMetadata<Settings::ResolutionSetup>::Canonicalizations();
  69. std::set<u32> resolutions{};
  70. for (const auto& [name, value] : enumeration) {
  71. const float up_factor = GetUpFactor(value);
  72. u32 height_undocked = Layout::ScreenUndocked::Height * up_factor;
  73. u32 height_docked = Layout::ScreenDocked::Height * up_factor;
  74. resolutions.emplace(height_undocked);
  75. resolutions.emplace(height_docked);
  76. }
  77. screenshot_height->addItem(parent->tr("Auto", "Screenshot height option"));
  78. for (const auto res : resolutions) {
  79. screenshot_height->addItem(QString::fromStdString(std::to_string(res)));
  80. }
  81. }
  82. static u32 ScreenshotDimensionToInt(const QString& height) {
  83. return std::strtoul(height.toUtf8(), nullptr, 0);
  84. }
  85. ConfigureUi::ConfigureUi(Core::System& system_, QWidget* parent)
  86. : QWidget(parent), ui{std::make_unique<Ui::ConfigureUi>()},
  87. ratio{Settings::values.aspect_ratio.GetValue()},
  88. resolution_setting{Settings::values.resolution_setup.GetValue()}, system{system_} {
  89. ui->setupUi(this);
  90. InitializeLanguageComboBox();
  91. for (const auto& theme : UISettings::themes) {
  92. ui->theme_combobox->addItem(QString::fromUtf8(theme.first),
  93. QString::fromUtf8(theme.second));
  94. }
  95. InitializeIconSizeComboBox();
  96. InitializeRowComboBoxes();
  97. PopulateResolutionComboBox(ui->screenshot_height, this);
  98. SetConfiguration();
  99. // Force game list reload if any of the relevant settings are changed.
  100. connect(ui->show_add_ons, &QCheckBox::stateChanged, this, &ConfigureUi::RequestGameListUpdate);
  101. connect(ui->show_compat, &QCheckBox::stateChanged, this, &ConfigureUi::RequestGameListUpdate);
  102. connect(ui->show_size, &QCheckBox::stateChanged, this, &ConfigureUi::RequestGameListUpdate);
  103. connect(ui->show_types, &QCheckBox::stateChanged, this, &ConfigureUi::RequestGameListUpdate);
  104. connect(ui->show_play_time, &QCheckBox::stateChanged, this,
  105. &ConfigureUi::RequestGameListUpdate);
  106. connect(ui->game_icon_size_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  107. &ConfigureUi::RequestGameListUpdate);
  108. connect(ui->folder_icon_size_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  109. this, &ConfigureUi::RequestGameListUpdate);
  110. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  111. &ConfigureUi::RequestGameListUpdate);
  112. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  113. &ConfigureUi::RequestGameListUpdate);
  114. // Update text ComboBoxes after user interaction.
  115. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::activated),
  116. [this] { ConfigureUi::UpdateSecondRowComboBox(); });
  117. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::activated),
  118. [this] { ConfigureUi::UpdateFirstRowComboBox(); });
  119. // Set screenshot path to user specification.
  120. connect(ui->screenshot_path_button, &QToolButton::pressed, this, [this] {
  121. auto dir =
  122. QFileDialog::getExistingDirectory(this, tr("Select Screenshots Path..."),
  123. QString::fromStdString(Common::FS::GetYuzuPathString(
  124. Common::FS::YuzuPath::ScreenshotsDir)));
  125. if (!dir.isEmpty()) {
  126. if (dir.back() != QChar::fromLatin1('/')) {
  127. dir.append(QChar::fromLatin1('/'));
  128. }
  129. ui->screenshot_path_edit->setText(dir);
  130. }
  131. });
  132. connect(ui->screenshot_height, &QComboBox::currentTextChanged, [this]() { UpdateWidthText(); });
  133. UpdateWidthText();
  134. }
  135. ConfigureUi::~ConfigureUi() = default;
  136. void ConfigureUi::ApplyConfiguration() {
  137. UISettings::values.theme =
  138. ui->theme_combobox->itemData(ui->theme_combobox->currentIndex()).toString().toStdString();
  139. UISettings::values.show_add_ons = ui->show_add_ons->isChecked();
  140. UISettings::values.show_compat = ui->show_compat->isChecked();
  141. UISettings::values.show_size = ui->show_size->isChecked();
  142. UISettings::values.show_types = ui->show_types->isChecked();
  143. UISettings::values.show_play_time = ui->show_play_time->isChecked();
  144. UISettings::values.game_icon_size = ui->game_icon_size_combobox->currentData().toUInt();
  145. UISettings::values.folder_icon_size = ui->folder_icon_size_combobox->currentData().toUInt();
  146. UISettings::values.row_1_text_id = ui->row_1_text_combobox->currentData().toUInt();
  147. UISettings::values.row_2_text_id = ui->row_2_text_combobox->currentData().toUInt();
  148. UISettings::values.enable_screenshot_save_as = ui->enable_screenshot_save_as->isChecked();
  149. Common::FS::SetYuzuPath(Common::FS::YuzuPath::ScreenshotsDir,
  150. ui->screenshot_path_edit->text().toStdString());
  151. const u32 height = ScreenshotDimensionToInt(ui->screenshot_height->currentText());
  152. UISettings::values.screenshot_height.SetValue(height);
  153. RequestGameListUpdate();
  154. system.ApplySettings();
  155. }
  156. void ConfigureUi::RequestGameListUpdate() {
  157. UISettings::values.is_game_list_reload_pending.exchange(true);
  158. }
  159. void ConfigureUi::SetConfiguration() {
  160. ui->theme_combobox->setCurrentIndex(
  161. ui->theme_combobox->findData(QString::fromStdString(UISettings::values.theme)));
  162. ui->language_combobox->setCurrentIndex(
  163. ui->language_combobox->findData(QString::fromStdString(UISettings::values.language)));
  164. ui->show_add_ons->setChecked(UISettings::values.show_add_ons.GetValue());
  165. ui->show_compat->setChecked(UISettings::values.show_compat.GetValue());
  166. ui->show_size->setChecked(UISettings::values.show_size.GetValue());
  167. ui->show_types->setChecked(UISettings::values.show_types.GetValue());
  168. ui->show_play_time->setChecked(UISettings::values.show_play_time.GetValue());
  169. ui->game_icon_size_combobox->setCurrentIndex(
  170. ui->game_icon_size_combobox->findData(UISettings::values.game_icon_size.GetValue()));
  171. ui->folder_icon_size_combobox->setCurrentIndex(
  172. ui->folder_icon_size_combobox->findData(UISettings::values.folder_icon_size.GetValue()));
  173. ui->enable_screenshot_save_as->setChecked(
  174. UISettings::values.enable_screenshot_save_as.GetValue());
  175. ui->screenshot_path_edit->setText(QString::fromStdString(
  176. Common::FS::GetYuzuPathString(Common::FS::YuzuPath::ScreenshotsDir)));
  177. const auto height = UISettings::values.screenshot_height.GetValue();
  178. if (height == 0) {
  179. ui->screenshot_height->setCurrentIndex(0);
  180. } else {
  181. ui->screenshot_height->setCurrentText(QStringLiteral("%1").arg(height));
  182. }
  183. }
  184. void ConfigureUi::changeEvent(QEvent* event) {
  185. if (event->type() == QEvent::LanguageChange) {
  186. RetranslateUI();
  187. }
  188. QWidget::changeEvent(event);
  189. }
  190. void ConfigureUi::RetranslateUI() {
  191. ui->retranslateUi(this);
  192. for (int i = 0; i < ui->game_icon_size_combobox->count(); i++) {
  193. ui->game_icon_size_combobox->setItemText(i,
  194. GetTranslatedGameIconSize(static_cast<size_t>(i)));
  195. }
  196. for (int i = 0; i < ui->folder_icon_size_combobox->count(); i++) {
  197. ui->folder_icon_size_combobox->setItemText(
  198. i, GetTranslatedFolderIconSize(static_cast<size_t>(i)));
  199. }
  200. for (int i = 0; i < ui->row_1_text_combobox->count(); i++) {
  201. const QString name = GetTranslatedRowTextName(static_cast<size_t>(i));
  202. ui->row_1_text_combobox->setItemText(i, name);
  203. ui->row_2_text_combobox->setItemText(i, name);
  204. }
  205. }
  206. void ConfigureUi::InitializeLanguageComboBox() {
  207. // This is a list of lexicographically sorted languages, only the available translations are
  208. // shown to the user.
  209. static const struct {
  210. const QString name;
  211. const char* id;
  212. } languages[] = {
  213. // clang-format off
  214. {QStringLiteral(u"Bahasa Indonesia"), "id"}, // Indonesian
  215. {QStringLiteral(u"Bahasa Melayu"), "ms"}, // Malay
  216. {QStringLiteral(u"Catal\u00E0"), "ca"}, // Catalan
  217. {QStringLiteral(u"\u010Ce\u0161tina"), "cs"}, // Czech
  218. {QStringLiteral(u"Dansk"), "da"}, // Danish
  219. {QStringLiteral(u"Deutsch"), "de"}, // German
  220. {QStringLiteral(u"English"), "en"}, // English
  221. {QStringLiteral(u"Espa\u00F1ol"), "es"}, // Spanish
  222. {QStringLiteral(u"Fran\u00E7ais"), "fr"}, // French
  223. {QStringLiteral(u"Hrvatski"), "hr"}, // Croatian
  224. {QStringLiteral(u"Italiano"), "it"}, // Italian
  225. {QStringLiteral(u"Magyar"), "hu"}, // Hungarian
  226. {QStringLiteral(u"Nederlands"), "nl"}, // Dutch
  227. {QStringLiteral(u"Norsk bokm\u00E5l"), "nb"}, // Norwegian
  228. {QStringLiteral(u"Polski"), "pl"}, // Polish
  229. {QStringLiteral(u"Portugu\u00EAs"), "pt_PT"}, // Portuguese
  230. {QStringLiteral(u"Portugu\u00EAs (Brasil)"), "pt_BR"}, // Portuguese (Brazil)
  231. {QStringLiteral(u"Rom\u00E2n\u0103"), "ro"}, // Romanian
  232. {QStringLiteral(u"Srpski"), "sr"}, // Serbian
  233. {QStringLiteral(u"Suomi"), "fi"}, // Finnish
  234. {QStringLiteral(u"Svenska"), "sv"}, // Swedish
  235. {QStringLiteral(u"Ti\u1EBFng Vi\u1EC7t"), "vi"}, // Vietnamese
  236. {QStringLiteral(u"Ti\u1EBFng Vi\u1EC7t (Vi\u1EC7t Nam)"), "vi_VN"}, // Vietnamese
  237. {QStringLiteral(u"T\u00FCrk\u00E7e"), "tr_TR"}, // Turkish
  238. {QStringLiteral(u"\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC"), "el"}, // Greek
  239. {QStringLiteral(u"\u0420\u0443\u0441\u0441\u043A\u0438\u0439"), "ru_RU"}, // Russian
  240. {QStringLiteral(u"\u0423\u043A\u0440\u0430\u0457\u043D\u0441\u044C\u043A\u0430"),
  241. "uk"}, // Ukrainian
  242. {QStringLiteral(u"\u0627\u0644\u0639\u0631\u0628\u064A\u0629"), "ar"}, // Arabic
  243. {QStringLiteral(u"\u0641\u0627\u0631\u0633\u06CC"), "fa"}, // Farsi
  244. {QStringLiteral(u"\uD55C\uAD6D\uC5B4"), "ko_KR"}, // Korean
  245. {QStringLiteral(u"\u65E5\u672C\u8A9E"), "ja_JP"}, // Japanese
  246. {QStringLiteral(u"\u7B80\u4F53\u4E2D\u6587"), "zh_CN"}, // Simplified Chinese
  247. {QStringLiteral(u"\u7E41\u9AD4\u4E2D\u6587"), "zh_TW"}, // Traditional Chinese
  248. // clang-format on
  249. };
  250. ui->language_combobox->addItem(tr("<System>"), QString{});
  251. QDir languages_dir{QStringLiteral(":/languages")};
  252. QStringList language_files = languages_dir.entryList();
  253. for (const auto& lang : languages) {
  254. if (QString::fromLatin1(lang.id) == QStringLiteral("en")) {
  255. ui->language_combobox->addItem(lang.name, QStringLiteral("en"));
  256. language_files.removeOne(QStringLiteral("en.qm"));
  257. continue;
  258. }
  259. for (int i = 0; i < language_files.size(); ++i) {
  260. QString locale = language_files[i];
  261. locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
  262. if (QString::fromLatin1(lang.id) == locale) {
  263. ui->language_combobox->addItem(lang.name, locale);
  264. language_files.removeAt(i);
  265. break;
  266. }
  267. }
  268. }
  269. // Anything remaining will be at the bottom
  270. for (const QString& file : language_files) {
  271. LOG_CRITICAL(Frontend, "Unexpected Language File: {}", file.toStdString());
  272. QString locale = file;
  273. locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
  274. const QString language_name = QLocale::languageToString(QLocale(locale).language());
  275. const QString lang = QStringLiteral("%1 [%2]").arg(language_name, locale);
  276. ui->language_combobox->addItem(lang, locale);
  277. }
  278. // Unlike other configuration changes, interface language changes need to be reflected on the
  279. // interface immediately. This is done by passing a signal to the main window, and then
  280. // retranslating when passing back.
  281. connect(ui->language_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  282. &ConfigureUi::OnLanguageChanged);
  283. }
  284. void ConfigureUi::InitializeIconSizeComboBox() {
  285. for (size_t i = 0; i < default_game_icon_sizes.size(); i++) {
  286. const auto size = default_game_icon_sizes[i].first;
  287. ui->game_icon_size_combobox->addItem(GetTranslatedGameIconSize(i), size);
  288. }
  289. for (size_t i = 0; i < default_folder_icon_sizes.size(); i++) {
  290. const auto size = default_folder_icon_sizes[i].first;
  291. ui->folder_icon_size_combobox->addItem(GetTranslatedFolderIconSize(i), size);
  292. }
  293. }
  294. void ConfigureUi::InitializeRowComboBoxes() {
  295. UpdateFirstRowComboBox(true);
  296. UpdateSecondRowComboBox(true);
  297. }
  298. void ConfigureUi::UpdateFirstRowComboBox(bool init) {
  299. const int currentIndex =
  300. init ? UISettings::values.row_1_text_id.GetValue()
  301. : ui->row_1_text_combobox->findData(ui->row_1_text_combobox->currentData());
  302. ui->row_1_text_combobox->clear();
  303. for (std::size_t i = 0; i < row_text_names.size(); i++) {
  304. const QString row_text_name = GetTranslatedRowTextName(i);
  305. ui->row_1_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  306. }
  307. ui->row_1_text_combobox->setCurrentIndex(ui->row_1_text_combobox->findData(currentIndex));
  308. ui->row_1_text_combobox->removeItem(4); // None
  309. ui->row_1_text_combobox->removeItem(
  310. ui->row_1_text_combobox->findData(ui->row_2_text_combobox->currentData()));
  311. }
  312. void ConfigureUi::UpdateSecondRowComboBox(bool init) {
  313. const int currentIndex =
  314. init ? UISettings::values.row_2_text_id.GetValue()
  315. : ui->row_2_text_combobox->findData(ui->row_2_text_combobox->currentData());
  316. ui->row_2_text_combobox->clear();
  317. for (std::size_t i = 0; i < row_text_names.size(); ++i) {
  318. const QString row_text_name = GetTranslatedRowTextName(i);
  319. ui->row_2_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  320. }
  321. ui->row_2_text_combobox->setCurrentIndex(ui->row_2_text_combobox->findData(currentIndex));
  322. ui->row_2_text_combobox->removeItem(
  323. ui->row_2_text_combobox->findData(ui->row_1_text_combobox->currentData()));
  324. }
  325. void ConfigureUi::OnLanguageChanged(int index) {
  326. if (index == -1)
  327. return;
  328. emit LanguageChanged(ui->language_combobox->itemData(index).toString());
  329. }
  330. void ConfigureUi::UpdateWidthText() {
  331. const u32 height = ScreenshotDimensionToInt(ui->screenshot_height->currentText());
  332. const u32 width = UISettings::CalculateWidth(height, ratio);
  333. if (height == 0) {
  334. const auto up_factor = GetUpFactor(resolution_setting);
  335. const u32 height_docked = Layout::ScreenDocked::Height * up_factor;
  336. const u32 width_docked = UISettings::CalculateWidth(height_docked, ratio);
  337. const u32 height_undocked = Layout::ScreenUndocked::Height * up_factor;
  338. const u32 width_undocked = UISettings::CalculateWidth(height_undocked, ratio);
  339. ui->screenshot_width->setText(tr("Auto (%1 x %2, %3 x %4)", "Screenshot width value")
  340. .arg(width_undocked)
  341. .arg(height_undocked)
  342. .arg(width_docked)
  343. .arg(height_docked));
  344. } else {
  345. ui->screenshot_width->setText(QStringLiteral("%1 x").arg(width));
  346. }
  347. }
  348. void ConfigureUi::UpdateScreenshotInfo(Settings::AspectRatio ratio_,
  349. Settings::ResolutionSetup resolution_setting_) {
  350. ratio = ratio_;
  351. resolution_setting = resolution_setting_;
  352. UpdateWidthText();
  353. }