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 saveSetting("showLibrary", m_ui.showLibrary);
206
207 if (m_ui.fastForwardUnbounded->isChecked()) {
208 saveSetting("fastForwardRatio", "-1");
209 } else {
210 saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
211 }
212
213 switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
214 case IDLE_LOOP_IGNORE:
215 saveSetting("idleOptimization", "ignore");
216 break;
217 case IDLE_LOOP_REMOVE:
218 saveSetting("idleOptimization", "remove");
219 break;
220 case IDLE_LOOP_DETECT:
221 saveSetting("idleOptimization", "detect");
222 break;
223 }
224
225 int loadState = 0;
226 loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
227 loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
228 loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
229 saveSetting("loadStateExtdata", loadState);
230
231 int saveState = 0;
232 saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
233 saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
234 saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
235 saveSetting("saveStateExtdata", saveState);
236
237 QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
238 if (audioDriver != m_controller->getQtOption("audioDriver")) {
239 m_controller->setQtOption("audioDriver", audioDriver);
240 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
241 emit audioDriverChanged();
242 }
243
244 QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
245 if (displayDriver != m_controller->getQtOption("displayDriver")) {
246 m_controller->setQtOption("displayDriver", displayDriver);
247 Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
248 emit displayDriverChanged();
249 }
250
251 m_controller->write();
252
253 emit pathsChanged();
254 emit biosLoaded(PLATFORM_GBA, m_ui.gbaBios->text());
255}
256
257void SettingsView::reloadConfig() {
258 loadSetting("bios", m_ui.gbaBios);
259 loadSetting("gba.bios", m_ui.gbaBios);
260 loadSetting("gb.bios", m_ui.gbBios);
261 loadSetting("gbc.bios", m_ui.gbcBios);
262 loadSetting("useBios", m_ui.useBios);
263 loadSetting("skipBios", m_ui.skipBios);
264 loadSetting("audioBuffers", m_ui.audioBufferSize);
265 loadSetting("sampleRate", m_ui.sampleRate);
266 loadSetting("videoSync", m_ui.videoSync);
267 loadSetting("audioSync", m_ui.audioSync);
268 loadSetting("frameskip", m_ui.frameskip);
269 loadSetting("fpsTarget", m_ui.fpsTarget);
270 loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
271 loadSetting("volume", m_ui.volume);
272 loadSetting("mute", m_ui.mute);
273 loadSetting("rewindEnable", m_ui.rewind);
274 loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
275 loadSetting("resampleVideo", m_ui.resampleVideo);
276 loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
277 loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
278 loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
279 loadSetting("savegamePath", m_ui.savegamePath);
280 loadSetting("savestatePath", m_ui.savestatePath);
281 loadSetting("screenshotPath", m_ui.screenshotPath);
282 loadSetting("patchPath", m_ui.patchPath);
283 loadSetting("showLibrary", m_ui.showLibrary);
284
285 double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
286 if (fastForwardRatio <= 0) {
287 m_ui.fastForwardUnbounded->setChecked(true);
288 m_ui.fastForwardRatio->setEnabled(false);
289 } else {
290 m_ui.fastForwardUnbounded->setChecked(false);
291 m_ui.fastForwardRatio->setEnabled(true);
292 m_ui.fastForwardRatio->setValue(fastForwardRatio);
293 }
294
295 QString idleOptimization = loadSetting("idleOptimization");
296 if (idleOptimization == "ignore") {
297 m_ui.idleOptimization->setCurrentIndex(0);
298 } else if (idleOptimization == "remove") {
299 m_ui.idleOptimization->setCurrentIndex(1);
300 } else if (idleOptimization == "detect") {
301 m_ui.idleOptimization->setCurrentIndex(2);
302 }
303
304 bool ok;
305 int loadState = loadSetting("loadStateExtdata").toInt(&ok);
306 if (!ok) {
307 loadState = SAVESTATE_SCREENSHOT;
308 }
309 m_ui.loadStateScreenshot->setChecked(loadState & SAVESTATE_SCREENSHOT);
310 m_ui.loadStateSave->setChecked(loadState & SAVESTATE_SAVEDATA);
311 m_ui.loadStateCheats->setChecked(loadState & SAVESTATE_CHEATS);
312
313 int saveState = loadSetting("saveStateExtdata").toInt(&ok);
314 if (!ok) {
315 saveState = SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS;
316 }
317 m_ui.saveStateScreenshot->setChecked(saveState & SAVESTATE_SCREENSHOT);
318 m_ui.saveStateSave->setChecked(saveState & SAVESTATE_SAVEDATA);
319 m_ui.saveStateCheats->setChecked(saveState & SAVESTATE_CHEATS);
320}
321
322void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
323 m_controller->setOption(key, field->isChecked());
324 m_controller->updateOption(key);
325}
326
327void SettingsView::saveSetting(const char* key, const QComboBox* field) {
328 saveSetting(key, field->lineEdit());
329}
330
331void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
332 saveSetting(key, field->value());
333}
334
335void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
336 saveSetting(key, field->text());
337}
338
339void SettingsView::saveSetting(const char* key, const QSlider* field) {
340 saveSetting(key, field->value());
341}
342
343void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
344 saveSetting(key, field->value());
345}
346
347void SettingsView::saveSetting(const char* key, const QVariant& field) {
348 m_controller->setOption(key, field);
349 m_controller->updateOption(key);
350}
351
352void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
353 QString option = loadSetting(key);
354 field->setChecked(!option.isNull() && option != "0");
355}
356
357void SettingsView::loadSetting(const char* key, QComboBox* field) {
358 loadSetting(key, field->lineEdit());
359}
360
361void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
362 QString option = loadSetting(key);
363 field->setValue(option.toDouble());
364}
365
366void SettingsView::loadSetting(const char* key, QLineEdit* field) {
367 QString option = loadSetting(key);
368 field->setText(option);
369}
370
371void SettingsView::loadSetting(const char* key, QSlider* field) {
372 QString option = loadSetting(key);
373 field->setValue(option.toInt());
374}
375
376void SettingsView::loadSetting(const char* key, QSpinBox* field) {
377 QString option = loadSetting(key);
378 field->setValue(option.toInt());
379}
380
381QString SettingsView::loadSetting(const char* key) {
382 return m_controller->getOption(key);
383}