configure_ui.cpp 7.8 KB

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