all repos — mgba @ 78e4083a562a1ce34a29dee8ecdb315401c9fb1a

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