configure_ui.cpp 7.7 KB

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