all repos — mgba @ c83833bfd47f7dc31455d53f92e1dc83fa506746

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