all repos — mgba @ 3851a8230bdf641ba9df41691bd76c6366b9a0df

mGBA Game Boy Advance Emulator

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