all repos — mgba @ 11d6ab7f9d3998267ba8750e352f2a4537f73acc

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	connect(m_ui.buttonBox, SIGNAL(accepted()), this, SLOT(updateConfig()));
134	connect(m_ui.buttonBox, &QDialogButtonBox::clicked, [this](QAbstractButton* button) {
135		if (m_ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
136			updateConfig();
137		}
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#ifdef BUILD_SDL
146	inputController->recalibrateAxes();
147	const char* profile = inputController->profileForType(SDL_BINDING_BUTTON);
148	editor = new GBAKeyEditor(inputController, SDL_BINDING_BUTTON, profile);
149	m_ui.stackedWidget->addWidget(editor);
150	m_ui.tabs->addItem("Controllers");
151	connect(m_ui.buttonBox, SIGNAL(accepted()), editor, SLOT(save()));
152#endif
153
154	ShortcutView* shortcutView = new ShortcutView();
155	shortcutView->setController(shortcutController);
156	shortcutView->setInputController(inputController);
157	m_ui.stackedWidget->addWidget(shortcutView);
158	m_ui.tabs->addItem("Shortcuts");
159}
160
161void SettingsView::selectBios() {
162	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
163	if (!filename.isEmpty()) {
164		m_ui.bios->setText(filename);
165	}
166}
167
168void SettingsView::updateConfig() {
169	saveSetting("bios", m_ui.bios);
170	saveSetting("useBios", m_ui.useBios);
171	saveSetting("skipBios", m_ui.skipBios);
172	saveSetting("audioBuffers", m_ui.audioBufferSize);
173	saveSetting("sampleRate", m_ui.sampleRate);
174	saveSetting("videoSync", m_ui.videoSync);
175	saveSetting("audioSync", m_ui.audioSync);
176	saveSetting("frameskip", m_ui.frameskip);
177	saveSetting("fpsTarget", m_ui.fpsTarget);
178	saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
179	saveSetting("volume", m_ui.volume);
180	saveSetting("mute", m_ui.mute);
181	saveSetting("rewindEnable", m_ui.rewind);
182	saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
183	saveSetting("resampleVideo", m_ui.resampleVideo);
184	saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
185	saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
186	saveSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
187	saveSetting("savegamePath", m_ui.savegamePath);
188	saveSetting("savestatePath", m_ui.savestatePath);
189	saveSetting("screenshotPath", m_ui.screenshotPath);
190	saveSetting("patchPath", m_ui.patchPath);
191
192	if (m_ui.fastForwardUnbounded->isChecked()) {
193		saveSetting("fastForwardRatio", "-1");
194	} else {
195		saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
196	}
197
198	switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
199	case IDLE_LOOP_IGNORE:
200		saveSetting("idleOptimization", "ignore");
201		break;
202	case IDLE_LOOP_REMOVE:
203		saveSetting("idleOptimization", "remove");
204		break;
205	case IDLE_LOOP_DETECT:
206		saveSetting("idleOptimization", "detect");
207		break;
208	}
209
210	int loadState = 0;
211	loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
212	loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
213	loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
214	saveSetting("loadStateExtdata", loadState);
215
216	int saveState = 0;
217	saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
218	saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
219	saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
220	saveSetting("saveStateExtdata", saveState);
221
222	QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
223	if (audioDriver != m_controller->getQtOption("audioDriver")) {
224		m_controller->setQtOption("audioDriver", audioDriver);
225		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
226		emit audioDriverChanged();
227	}
228
229	QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
230	if (displayDriver != m_controller->getQtOption("displayDriver")) {
231		m_controller->setQtOption("displayDriver", displayDriver);
232		Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
233		emit displayDriverChanged();
234	}
235
236	m_controller->write();
237
238	emit pathsChanged();
239	emit biosLoaded(m_ui.bios->text());
240}
241
242void SettingsView::reloadConfig() {	
243	loadSetting("bios", m_ui.bios);
244	loadSetting("useBios", m_ui.useBios);
245	loadSetting("skipBios", m_ui.skipBios);
246	loadSetting("audioBuffers", m_ui.audioBufferSize);
247	loadSetting("sampleRate", m_ui.sampleRate);
248	loadSetting("videoSync", m_ui.videoSync);
249	loadSetting("audioSync", m_ui.audioSync);
250	loadSetting("frameskip", m_ui.frameskip);
251	loadSetting("fpsTarget", m_ui.fpsTarget);
252	loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
253	loadSetting("volume", m_ui.volume);
254	loadSetting("mute", m_ui.mute);
255	loadSetting("rewindEnable", m_ui.rewind);
256	loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
257	loadSetting("resampleVideo", m_ui.resampleVideo);
258	loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
259	loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
260	loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
261	loadSetting("savegamePath", m_ui.savegamePath);
262	loadSetting("savestatePath", m_ui.savestatePath);
263	loadSetting("screenshotPath", m_ui.screenshotPath);
264	loadSetting("patchPath", m_ui.patchPath);
265
266	double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
267	if (fastForwardRatio <= 0) {
268		m_ui.fastForwardUnbounded->setChecked(true);
269		m_ui.fastForwardRatio->setEnabled(false);
270	} else {
271		m_ui.fastForwardUnbounded->setChecked(false);
272		m_ui.fastForwardRatio->setEnabled(true);
273		m_ui.fastForwardRatio->setValue(fastForwardRatio);
274	}
275
276	QString idleOptimization = loadSetting("idleOptimization");
277	if (idleOptimization == "ignore") {
278		m_ui.idleOptimization->setCurrentIndex(0);
279	} else if (idleOptimization == "remove") {
280		m_ui.idleOptimization->setCurrentIndex(1);
281	} else if (idleOptimization == "detect") {
282		m_ui.idleOptimization->setCurrentIndex(2);
283	}
284
285	bool ok;
286	int loadState = loadSetting("loadStateExtdata").toInt(&ok);
287	if (!ok) {
288		loadState = SAVESTATE_SCREENSHOT;
289	}
290	m_ui.loadStateScreenshot->setChecked(loadState & SAVESTATE_SCREENSHOT);
291	m_ui.loadStateSave->setChecked(loadState & SAVESTATE_SAVEDATA);
292	m_ui.loadStateCheats->setChecked(loadState & SAVESTATE_CHEATS);
293
294	int saveState = loadSetting("saveStateExtdata").toInt(&ok);
295	if (!ok) {
296		saveState = SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS;
297	}
298	m_ui.saveStateScreenshot->setChecked(saveState & SAVESTATE_SCREENSHOT);
299	m_ui.saveStateSave->setChecked(saveState & SAVESTATE_SAVEDATA);
300	m_ui.saveStateCheats->setChecked(saveState & SAVESTATE_CHEATS);
301}
302
303void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
304	m_controller->setOption(key, field->isChecked());
305	m_controller->updateOption(key);
306}
307
308void SettingsView::saveSetting(const char* key, const QComboBox* field) {
309	saveSetting(key, field->lineEdit());
310}
311
312void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
313	saveSetting(key, field->value());
314}
315
316void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
317	saveSetting(key, field->text());
318}
319
320void SettingsView::saveSetting(const char* key, const QSlider* field) {
321	saveSetting(key, field->value());
322}
323
324void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
325	saveSetting(key, field->value());
326}
327
328void SettingsView::saveSetting(const char* key, const QVariant& field) {
329	m_controller->setOption(key, field);
330	m_controller->updateOption(key);
331}
332
333void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
334	QString option = loadSetting(key);
335	field->setChecked(!option.isNull() && option != "0");
336}
337
338void SettingsView::loadSetting(const char* key, QComboBox* field) {
339	loadSetting(key, field->lineEdit());
340}
341
342void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
343	QString option = loadSetting(key);
344	field->setValue(option.toDouble());
345}
346
347void SettingsView::loadSetting(const char* key, QLineEdit* field) {
348	QString option = loadSetting(key);
349	field->setText(option);
350}
351
352void SettingsView::loadSetting(const char* key, QSlider* field) {
353	QString option = loadSetting(key);
354	field->setValue(option.toInt());
355}
356
357void SettingsView::loadSetting(const char* key, QSpinBox* field) {
358	QString option = loadSetting(key);
359	field->setValue(option.toInt());
360}
361
362QString SettingsView::loadSetting(const char* key) {
363	return m_controller->getOption(key);
364}