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