all repos — mgba @ 959f66a1a0faff37607a53c4da1ea7b508d6a43a

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