configure_ui.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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(ui->language_combobox->findData(
  163. QString::fromStdString(UISettings::values.language.GetValue())));
  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. ui->language_combobox->addItem(tr("<System>"), QString{});
  208. ui->language_combobox->addItem(tr("English"), QStringLiteral("en"));
  209. QDirIterator it(QStringLiteral(":/languages"), QDirIterator::NoIteratorFlags);
  210. while (it.hasNext()) {
  211. QString locale = it.next();
  212. locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
  213. locale.remove(0, locale.lastIndexOf(QLatin1Char{'/'}) + 1);
  214. const QString lang = QLocale::languageToString(QLocale(locale).language());
  215. const QString country = QLocale::countryToString(QLocale(locale).country());
  216. ui->language_combobox->addItem(QStringLiteral("%1 (%2)").arg(lang, country), locale);
  217. }
  218. // Unlike other configuration changes, interface language changes need to be reflected on the
  219. // interface immediately. This is done by passing a signal to the main window, and then
  220. // retranslating when passing back.
  221. connect(ui->language_combobox, qOverload<int>(&QComboBox::currentIndexChanged), this,
  222. &ConfigureUi::OnLanguageChanged);
  223. }
  224. void ConfigureUi::InitializeIconSizeComboBox() {
  225. for (size_t i = 0; i < default_game_icon_sizes.size(); i++) {
  226. const auto size = default_game_icon_sizes[i].first;
  227. ui->game_icon_size_combobox->addItem(GetTranslatedGameIconSize(i), size);
  228. }
  229. for (size_t i = 0; i < default_folder_icon_sizes.size(); i++) {
  230. const auto size = default_folder_icon_sizes[i].first;
  231. ui->folder_icon_size_combobox->addItem(GetTranslatedFolderIconSize(i), size);
  232. }
  233. }
  234. void ConfigureUi::InitializeRowComboBoxes() {
  235. UpdateFirstRowComboBox(true);
  236. UpdateSecondRowComboBox(true);
  237. }
  238. void ConfigureUi::UpdateFirstRowComboBox(bool init) {
  239. const int currentIndex =
  240. init ? UISettings::values.row_1_text_id.GetValue()
  241. : ui->row_1_text_combobox->findData(ui->row_1_text_combobox->currentData());
  242. ui->row_1_text_combobox->clear();
  243. for (std::size_t i = 0; i < row_text_names.size(); i++) {
  244. const QString row_text_name = GetTranslatedRowTextName(i);
  245. ui->row_1_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  246. }
  247. ui->row_1_text_combobox->setCurrentIndex(ui->row_1_text_combobox->findData(currentIndex));
  248. ui->row_1_text_combobox->removeItem(4); // None
  249. ui->row_1_text_combobox->removeItem(
  250. ui->row_1_text_combobox->findData(ui->row_2_text_combobox->currentData()));
  251. }
  252. void ConfigureUi::UpdateSecondRowComboBox(bool init) {
  253. const int currentIndex =
  254. init ? UISettings::values.row_2_text_id.GetValue()
  255. : ui->row_2_text_combobox->findData(ui->row_2_text_combobox->currentData());
  256. ui->row_2_text_combobox->clear();
  257. for (std::size_t i = 0; i < row_text_names.size(); ++i) {
  258. const QString row_text_name = GetTranslatedRowTextName(i);
  259. ui->row_2_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  260. }
  261. ui->row_2_text_combobox->setCurrentIndex(ui->row_2_text_combobox->findData(currentIndex));
  262. ui->row_2_text_combobox->removeItem(
  263. ui->row_2_text_combobox->findData(ui->row_1_text_combobox->currentData()));
  264. }
  265. void ConfigureUi::OnLanguageChanged(int index) {
  266. if (index == -1)
  267. return;
  268. emit LanguageChanged(ui->language_combobox->itemData(index).toString());
  269. }
  270. void ConfigureUi::UpdateWidthText() {
  271. const u32 height = ScreenshotDimensionToInt(ui->screenshot_height->currentText());
  272. const u32 width = UISettings::CalculateWidth(height, ratio);
  273. if (height == 0) {
  274. const auto up_factor = GetUpFactor(resolution_setting);
  275. const u32 height_docked = Layout::ScreenDocked::Height * up_factor;
  276. const u32 width_docked = UISettings::CalculateWidth(height_docked, ratio);
  277. const u32 height_undocked = Layout::ScreenUndocked::Height * up_factor;
  278. const u32 width_undocked = UISettings::CalculateWidth(height_undocked, ratio);
  279. ui->screenshot_width->setText(tr("Auto (%1 x %2, %3 x %4)", "Screenshot width value")
  280. .arg(width_undocked)
  281. .arg(height_undocked)
  282. .arg(width_docked)
  283. .arg(height_docked));
  284. } else {
  285. ui->screenshot_width->setText(QStringLiteral("%1 x").arg(width));
  286. }
  287. }
  288. void ConfigureUi::UpdateScreenshotInfo(Settings::AspectRatio ratio_,
  289. Settings::ResolutionSetup resolution_setting_) {
  290. ratio = ratio_;
  291. resolution_setting = resolution_setting_;
  292. UpdateWidthText();
  293. }