src/platform/qt/SettingsView.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "SettingsView.h"
7
8#include "AudioProcessor.h"
9#include "ConfigController.h"
10#include "Display.h"
11#include "GBAApp.h"
12#include "GBAKeyEditor.h"
13#include "InputController.h"
14#include "ShortcutView.h"
15
16#include <mgba/core/serialize.h>
17#include <mgba/internal/gba/gba.h>
18
19using namespace QGBA;
20
21SettingsView::SettingsView(ConfigController* controller, InputController* inputController, ShortcutController* shortcutController, QWidget* parent)
22 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
23 , m_controller(controller)
24{
25 m_ui.setupUi(this);
26
27 reloadConfig();
28
29 if (m_ui.savegamePath->text().isEmpty()) {
30 m_ui.savegameSameDir->setChecked(true);
31 }
32 connect(m_ui.savegameSameDir, &QAbstractButton::toggled, [this] (bool e) {
33 if (e) {
34 m_ui.savegamePath->clear();
35 }
36 });
37 connect(m_ui.savegameBrowse, &QAbstractButton::pressed, [this] () {
38 QString path = GBAApp::app()->getOpenDirectoryName(this, "Select directory");
39 if (!path.isNull()) {
40 m_ui.savegameSameDir->setChecked(false);
41 m_ui.savegamePath->setText(path);
42 }
43 });
44
45 if (m_ui.savestatePath->text().isEmpty()) {
46 m_ui.savestateSameDir->setChecked(true);
47 }
48 connect(m_ui.savestateSameDir, &QAbstractButton::toggled, [this] (bool e) {
49 if (e) {
50 m_ui.savestatePath->clear();
51 }
52 });
53 connect(m_ui.savestateBrowse, &QAbstractButton::pressed, [this] () {
54 QString path = GBAApp::app()->getOpenDirectoryName(this, "Select directory");
55 if (!path.isNull()) {
56 m_ui.savestateSameDir->setChecked(false);
57 m_ui.savestatePath->setText(path);
58 }
59 });
60
61 if (m_ui.screenshotPath->text().isEmpty()) {
62 m_ui.screenshotSameDir->setChecked(true);
63 }
64 connect(m_ui.screenshotSameDir, &QAbstractButton::toggled, [this] (bool e) {
65 if (e) {
66 m_ui.screenshotPath->clear();
67 }
68 });
69 connect(m_ui.screenshotBrowse, &QAbstractButton::pressed, [this] () {
70 QString path = GBAApp::app()->getOpenDirectoryName(this, "Select directory");
71 if (!path.isNull()) {
72 m_ui.screenshotSameDir->setChecked(false);
73 m_ui.screenshotPath->setText(path);
74 }
75 });
76
77 if (m_ui.patchPath->text().isEmpty()) {
78 m_ui.patchSameDir->setChecked(true);
79 }
80 connect(m_ui.patchSameDir, &QAbstractButton::toggled, [this] (bool e) {
81 if (e) {
82 m_ui.patchPath->clear();
83 }
84 });
85 connect(m_ui.patchBrowse, &QAbstractButton::pressed, [this] () {
86 QString path = GBAApp::app()->getOpenDirectoryName(this, "Select directory");
87 if (!path.isNull()) {
88 m_ui.patchSameDir->setChecked(false);
89 m_ui.patchPath->setText(path);
90 }
91 });
92
93 // TODO: Move to reloadConfig()
94 QVariant audioDriver = m_controller->getQtOption("audioDriver");
95#ifdef BUILD_QT_MULTIMEDIA
96 m_ui.audioDriver->addItem(tr("Qt Multimedia"), static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA));
97 if (!audioDriver.isNull() && audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA)) {
98 m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
99 }
100#endif
101
102#ifdef BUILD_SDL
103 m_ui.audioDriver->addItem(tr("SDL"), static_cast<int>(AudioProcessor::Driver::SDL));
104 if (audioDriver.isNull() || audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::SDL)) {
105 m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
106 }
107#endif
108
109 // TODO: Move to reloadConfig()
110 QVariant displayDriver = m_controller->getQtOption("displayDriver");
111 m_ui.displayDriver->addItem(tr("Software (Qt)"), static_cast<int>(Display::Driver::QT));
112 if (!displayDriver.isNull() && displayDriver.toInt() == static_cast<int>(Display::Driver::QT)) {
113 m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
114 }
115
116#if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
117 m_ui.displayDriver->addItem(tr("OpenGL"), static_cast<int>(Display::Driver::OPENGL));
118 if (displayDriver.isNull() || displayDriver.toInt() == static_cast<int>(Display::Driver::OPENGL)) {
119 m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
120 }
121#endif
122
123#ifdef BUILD_GL
124 m_ui.displayDriver->addItem(tr("OpenGL (force version 1.x)"), static_cast<int>(Display::Driver::OPENGL1));
125 if (!displayDriver.isNull() && displayDriver.toInt() == static_cast<int>(Display::Driver::OPENGL1)) {
126 m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
127 }
128#endif
129
130 connect(m_ui.gbaBiosBrowse, &QPushButton::clicked, [this]() {
131 selectBios(m_ui.gbaBios);
132 });
133 connect(m_ui.gbBiosBrowse, &QPushButton::clicked, [this]() {
134 selectBios(m_ui.gbBios);
135 });
136 connect(m_ui.gbcBiosBrowse, &QPushButton::clicked, [this]() {
137 selectBios(m_ui.gbcBios);
138 });
139
140 GBAKeyEditor* editor = new GBAKeyEditor(inputController, InputController::KEYBOARD, QString(), this);
141 m_ui.stackedWidget->addWidget(editor);
142 m_ui.tabs->addItem("Keyboard");
143 connect(m_ui.buttonBox, SIGNAL(accepted()), editor, SLOT(save()));
144
145 GBAKeyEditor* buttonEditor = nullptr;
146#ifdef BUILD_SDL
147 inputController->recalibrateAxes();
148 const char* profile = inputController->profileForType(SDL_BINDING_BUTTON);
149 buttonEditor = new GBAKeyEditor(inputController, SDL_BINDING_BUTTON, profile);
150 m_ui.stackedWidget->addWidget(buttonEditor);
151 m_ui.tabs->addItem("Controllers");
152 connect(m_ui.buttonBox, SIGNAL(accepted()), buttonEditor, SLOT(save()));
153#endif
154
155 connect(m_ui.buttonBox, SIGNAL(accepted()), this, SLOT(updateConfig()));
156 connect(m_ui.buttonBox, &QDialogButtonBox::clicked, [this, editor, buttonEditor](QAbstractButton* button) {
157 if (m_ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
158 updateConfig();
159 editor->save();
160 if (buttonEditor) {
161 buttonEditor->save();
162 }
163 }
164 });
165
166 ShortcutView* shortcutView = new ShortcutView();
167 shortcutView->setController(shortcutController);
168 shortcutView->setInputController(inputController);
169 m_ui.stackedWidget->addWidget(shortcutView);
170 m_ui.tabs->addItem("Shortcuts");
171}
172
173void SettingsView::selectBios(QLineEdit* bios) {
174 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
175 if (!filename.isEmpty()) {
176 bios->setText(filename);
177 }
178}
179
180void SettingsView::updateConfig() {
181 saveSetting("gba.bios", m_ui.gbaBios);
182 saveSetting("gb.bios", m_ui.gbBios);
183 saveSetting("gbc.bios", m_ui.gbcBios);
184 saveSetting("useBios", m_ui.useBios);
185 saveSetting("skipBios", m_ui.skipBios);
186 saveSetting("audioBuffers", m_ui.audioBufferSize);
187 saveSetting("sampleRate", m_ui.sampleRate);
188 saveSetting("videoSync", m_ui.videoSync);
189 saveSetting("audioSync", m_ui.audioSync);
190 saveSetting("frameskip", m_ui.frameskip);
191 saveSetting("fpsTarget", m_ui.fpsTarget);
192 saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
193 saveSetting("volume", m_ui.volume);
194 saveSetting("mute", m_ui.mute);
195 saveSetting("rewindEnable", m_ui.rewind);
196 saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
197 saveSetting("resampleVideo", m_ui.resampleVideo);
198 saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
199 saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
200 saveSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
201 saveSetting("savegamePath", m_ui.savegamePath);
202 saveSetting("savestatePath", m_ui.savestatePath);
203 saveSetting("screenshotPath", m_ui.screenshotPath);
204 saveSetting("patchPath", m_ui.patchPath);
205
206 if (m_ui.fastForwardUnbounded->isChecked()) {
207 saveSetting("fastForwardRatio", "-1");
208 } else {
209 saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
210 }
211
212 switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
213 case IDLE_LOOP_IGNORE:
214 saveSetting("idleOptimization", "ignore");
215 break;
216 case IDLE_LOOP_REMOVE:
217 saveSetting("idleOptimization", "remove");
218 break;
219 case IDLE_LOOP_DETECT:
220 saveSetting("idleOptimization", "detect");
221 break;
222 }
223
224 int loadState = 0;
225 loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
226 loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
227 loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
228 saveSetting("loadStateExtdata", loadState);
229
230 int saveState = 0;
231 saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
232 saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
233 saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
234 saveSetting("saveStateExtdata", saveState);
235
236 QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
237 if (audioDriver != m_controller->getQtOption("audioDriver")) {
238 m_controller->setQtOption("audioDriver", audioDriver);
239 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
240 emit audioDriverChanged();
241 }
242
243 QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
244 if (displayDriver != m_controller->getQtOption("displayDriver")) {
245 m_controller->setQtOption("displayDriver", displayDriver);
246 Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
247 emit displayDriverChanged();
248 }
249
250 m_controller->write();
251
252 emit pathsChanged();
253 emit biosLoaded(PLATFORM_GBA, m_ui.gbaBios->text());
254}
255
256void SettingsView::reloadConfig() {
257 loadSetting("bios", m_ui.gbaBios);
258 loadSetting("gba.bios", m_ui.gbaBios);
259 loadSetting("gb.bios", m_ui.gbBios);
260 loadSetting("gbc.bios", m_ui.gbcBios);
261 loadSetting("useBios", m_ui.useBios);
262 loadSetting("skipBios", m_ui.skipBios);
263 loadSetting("audioBuffers", m_ui.audioBufferSize);
264 loadSetting("sampleRate", m_ui.sampleRate);
265 loadSetting("videoSync", m_ui.videoSync);
266 loadSetting("audioSync", m_ui.audioSync);
267 loadSetting("frameskip", m_ui.frameskip);
268 loadSetting("fpsTarget", m_ui.fpsTarget);
269 loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
270 loadSetting("volume", m_ui.volume);
271 loadSetting("mute", m_ui.mute);
272 loadSetting("rewindEnable", m_ui.rewind);
273 loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
274 loadSetting("resampleVideo", m_ui.resampleVideo);
275 loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
276 loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
277 loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
278 loadSetting("savegamePath", m_ui.savegamePath);
279 loadSetting("savestatePath", m_ui.savestatePath);
280 loadSetting("screenshotPath", m_ui.screenshotPath);
281 loadSetting("patchPath", m_ui.patchPath);
282
283 double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
284 if (fastForwardRatio <= 0) {
285 m_ui.fastForwardUnbounded->setChecked(true);
286 m_ui.fastForwardRatio->setEnabled(false);
287 } else {
288 m_ui.fastForwardUnbounded->setChecked(false);
289 m_ui.fastForwardRatio->setEnabled(true);
290 m_ui.fastForwardRatio->setValue(fastForwardRatio);
291 }
292
293 QString idleOptimization = loadSetting("idleOptimization");
294 if (idleOptimization == "ignore") {
295 m_ui.idleOptimization->setCurrentIndex(0);
296 } else if (idleOptimization == "remove") {
297 m_ui.idleOptimization->setCurrentIndex(1);
298 } else if (idleOptimization == "detect") {
299 m_ui.idleOptimization->setCurrentIndex(2);
300 }
301
302 bool ok;
303 int loadState = loadSetting("loadStateExtdata").toInt(&ok);
304 if (!ok) {
305 loadState = SAVESTATE_SCREENSHOT;
306 }
307 m_ui.loadStateScreenshot->setChecked(loadState & SAVESTATE_SCREENSHOT);
308 m_ui.loadStateSave->setChecked(loadState & SAVESTATE_SAVEDATA);
309 m_ui.loadStateCheats->setChecked(loadState & SAVESTATE_CHEATS);
310
311 int saveState = loadSetting("saveStateExtdata").toInt(&ok);
312 if (!ok) {
313 saveState = SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS;
314 }
315 m_ui.saveStateScreenshot->setChecked(saveState & SAVESTATE_SCREENSHOT);
316 m_ui.saveStateSave->setChecked(saveState & SAVESTATE_SAVEDATA);
317 m_ui.saveStateCheats->setChecked(saveState & SAVESTATE_CHEATS);
318}
319
320void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
321 m_controller->setOption(key, field->isChecked());
322 m_controller->updateOption(key);
323}
324
325void SettingsView::saveSetting(const char* key, const QComboBox* field) {
326 saveSetting(key, field->lineEdit());
327}
328
329void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
330 saveSetting(key, field->value());
331}
332
333void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
334 saveSetting(key, field->text());
335}
336
337void SettingsView::saveSetting(const char* key, const QSlider* field) {
338 saveSetting(key, field->value());
339}
340
341void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
342 saveSetting(key, field->value());
343}
344
345void SettingsView::saveSetting(const char* key, const QVariant& field) {
346 m_controller->setOption(key, field);
347 m_controller->updateOption(key);
348}
349
350void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
351 QString option = loadSetting(key);
352 field->setChecked(!option.isNull() && option != "0");
353}
354
355void SettingsView::loadSetting(const char* key, QComboBox* field) {
356 loadSetting(key, field->lineEdit());
357}
358
359void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
360 QString option = loadSetting(key);
361 field->setValue(option.toDouble());
362}
363
364void SettingsView::loadSetting(const char* key, QLineEdit* field) {
365 QString option = loadSetting(key);
366 field->setText(option);
367}
368
369void SettingsView::loadSetting(const char* key, QSlider* field) {
370 QString option = loadSetting(key);
371 field->setValue(option.toInt());
372}
373
374void SettingsView::loadSetting(const char* key, QSpinBox* field) {
375 QString option = loadSetting(key);
376 field->setValue(option.toInt());
377}
378
379QString SettingsView::loadSetting(const char* key) {
380 return m_controller->getOption(key);
381}