configure_ui.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_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->game_icon_size_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  62. &ConfigureUi::RequestGameListUpdate);
  63. connect(ui->folder_icon_size_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  64. this, &ConfigureUi::RequestGameListUpdate);
  65. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  66. &ConfigureUi::RequestGameListUpdate);
  67. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  68. &ConfigureUi::RequestGameListUpdate);
  69. // Update text ComboBoxes after user interaction.
  70. connect(ui->row_1_text_combobox, QOverload<int>::of(&QComboBox::activated),
  71. [this] { ConfigureUi::UpdateSecondRowComboBox(); });
  72. connect(ui->row_2_text_combobox, QOverload<int>::of(&QComboBox::activated),
  73. [this] { ConfigureUi::UpdateFirstRowComboBox(); });
  74. // Set screenshot path to user specification.
  75. connect(ui->screenshot_path_button, &QToolButton::pressed, this, [this] {
  76. auto dir =
  77. QFileDialog::getExistingDirectory(this, tr("Select Screenshots Path..."),
  78. QString::fromStdString(Common::FS::GetYuzuPathString(
  79. Common::FS::YuzuPath::ScreenshotsDir)));
  80. if (!dir.isEmpty()) {
  81. if (dir.back() != QChar::fromLatin1('/')) {
  82. dir.append(QChar::fromLatin1('/'));
  83. }
  84. ui->screenshot_path_edit->setText(dir);
  85. }
  86. });
  87. }
  88. ConfigureUi::~ConfigureUi() = default;
  89. void ConfigureUi::ApplyConfiguration() {
  90. UISettings::values.theme =
  91. ui->theme_combobox->itemData(ui->theme_combobox->currentIndex()).toString();
  92. UISettings::values.show_add_ons = ui->show_add_ons->isChecked();
  93. UISettings::values.game_icon_size = ui->game_icon_size_combobox->currentData().toUInt();
  94. UISettings::values.folder_icon_size = ui->folder_icon_size_combobox->currentData().toUInt();
  95. UISettings::values.row_1_text_id = ui->row_1_text_combobox->currentData().toUInt();
  96. UISettings::values.row_2_text_id = ui->row_2_text_combobox->currentData().toUInt();
  97. UISettings::values.enable_screenshot_save_as = ui->enable_screenshot_save_as->isChecked();
  98. Common::FS::SetYuzuPath(Common::FS::YuzuPath::ScreenshotsDir,
  99. ui->screenshot_path_edit->text().toStdString());
  100. system.ApplySettings();
  101. }
  102. void ConfigureUi::RequestGameListUpdate() {
  103. UISettings::values.is_game_list_reload_pending.exchange(true);
  104. }
  105. void ConfigureUi::SetConfiguration() {
  106. ui->theme_combobox->setCurrentIndex(ui->theme_combobox->findData(UISettings::values.theme));
  107. ui->language_combobox->setCurrentIndex(
  108. ui->language_combobox->findData(UISettings::values.language));
  109. ui->show_add_ons->setChecked(UISettings::values.show_add_ons.GetValue());
  110. ui->game_icon_size_combobox->setCurrentIndex(
  111. ui->game_icon_size_combobox->findData(UISettings::values.game_icon_size.GetValue()));
  112. ui->folder_icon_size_combobox->setCurrentIndex(
  113. ui->folder_icon_size_combobox->findData(UISettings::values.folder_icon_size.GetValue()));
  114. ui->enable_screenshot_save_as->setChecked(
  115. UISettings::values.enable_screenshot_save_as.GetValue());
  116. ui->screenshot_path_edit->setText(QString::fromStdString(
  117. Common::FS::GetYuzuPathString(Common::FS::YuzuPath::ScreenshotsDir)));
  118. }
  119. void ConfigureUi::changeEvent(QEvent* event) {
  120. if (event->type() == QEvent::LanguageChange) {
  121. RetranslateUI();
  122. }
  123. QWidget::changeEvent(event);
  124. }
  125. void ConfigureUi::RetranslateUI() {
  126. ui->retranslateUi(this);
  127. for (int i = 0; i < ui->game_icon_size_combobox->count(); i++) {
  128. ui->game_icon_size_combobox->setItemText(i,
  129. GetTranslatedGameIconSize(static_cast<size_t>(i)));
  130. }
  131. for (int i = 0; i < ui->folder_icon_size_combobox->count(); i++) {
  132. ui->folder_icon_size_combobox->setItemText(
  133. i, GetTranslatedFolderIconSize(static_cast<size_t>(i)));
  134. }
  135. for (int i = 0; i < ui->row_1_text_combobox->count(); i++) {
  136. const QString name = GetTranslatedRowTextName(static_cast<size_t>(i));
  137. ui->row_1_text_combobox->setItemText(i, name);
  138. ui->row_2_text_combobox->setItemText(i, name);
  139. }
  140. }
  141. void ConfigureUi::InitializeLanguageComboBox() {
  142. ui->language_combobox->addItem(tr("<System>"), QString{});
  143. ui->language_combobox->addItem(tr("English"), QStringLiteral("en"));
  144. QDirIterator it(QStringLiteral(":/languages"), QDirIterator::NoIteratorFlags);
  145. while (it.hasNext()) {
  146. QString locale = it.next();
  147. locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
  148. locale.remove(0, locale.lastIndexOf(QLatin1Char{'/'}) + 1);
  149. const QString lang = QLocale::languageToString(QLocale(locale).language());
  150. ui->language_combobox->addItem(lang, locale);
  151. }
  152. // Unlike other configuration changes, interface language changes need to be reflected on the
  153. // interface immediately. This is done by passing a signal to the main window, and then
  154. // retranslating when passing back.
  155. connect(ui->language_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  156. &ConfigureUi::OnLanguageChanged);
  157. }
  158. void ConfigureUi::InitializeIconSizeComboBox() {
  159. for (size_t i = 0; i < default_game_icon_sizes.size(); i++) {
  160. const auto size = default_game_icon_sizes[i].first;
  161. ui->game_icon_size_combobox->addItem(GetTranslatedGameIconSize(i), size);
  162. }
  163. for (size_t i = 0; i < default_folder_icon_sizes.size(); i++) {
  164. const auto size = default_folder_icon_sizes[i].first;
  165. ui->folder_icon_size_combobox->addItem(GetTranslatedFolderIconSize(i), size);
  166. }
  167. }
  168. void ConfigureUi::InitializeRowComboBoxes() {
  169. UpdateFirstRowComboBox(true);
  170. UpdateSecondRowComboBox(true);
  171. }
  172. void ConfigureUi::UpdateFirstRowComboBox(bool init) {
  173. const int currentIndex =
  174. init ? UISettings::values.row_1_text_id.GetValue()
  175. : ui->row_1_text_combobox->findData(ui->row_1_text_combobox->currentData());
  176. ui->row_1_text_combobox->clear();
  177. for (std::size_t i = 0; i < row_text_names.size(); i++) {
  178. const QString row_text_name = GetTranslatedRowTextName(i);
  179. ui->row_1_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  180. }
  181. ui->row_1_text_combobox->setCurrentIndex(ui->row_1_text_combobox->findData(currentIndex));
  182. ui->row_1_text_combobox->removeItem(4); // None
  183. ui->row_1_text_combobox->removeItem(
  184. ui->row_1_text_combobox->findData(ui->row_2_text_combobox->currentData()));
  185. }
  186. void ConfigureUi::UpdateSecondRowComboBox(bool init) {
  187. const int currentIndex =
  188. init ? UISettings::values.row_2_text_id.GetValue()
  189. : ui->row_2_text_combobox->findData(ui->row_2_text_combobox->currentData());
  190. ui->row_2_text_combobox->clear();
  191. for (std::size_t i = 0; i < row_text_names.size(); ++i) {
  192. const QString row_text_name = GetTranslatedRowTextName(i);
  193. ui->row_2_text_combobox->addItem(row_text_name, QVariant::fromValue(i));
  194. }
  195. ui->row_2_text_combobox->setCurrentIndex(ui->row_2_text_combobox->findData(currentIndex));
  196. ui->row_2_text_combobox->removeItem(
  197. ui->row_2_text_combobox->findData(ui->row_1_text_combobox->currentData()));
  198. }
  199. void ConfigureUi::OnLanguageChanged(int index) {
  200. if (index == -1)
  201. return;
  202. emit LanguageChanged(ui->language_combobox->itemData(index).toString());
  203. }