all repos — mgba @ b14aafe9cb5499bbeb54478c74b56e781e5936ca

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