all repos — mgba @ 4f1788b2e05744fbb6e5ee24af3d02e2ba2304ba

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#ifdef BUILD_GL
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	connect(m_ui.biosBrowse, SIGNAL(clicked()), this, SLOT(selectBios()));
125	connect(m_ui.buttonBox, SIGNAL(accepted()), this, SLOT(updateConfig()));
126	connect(m_ui.buttonBox, &QDialogButtonBox::clicked, [this](QAbstractButton* button) {
127		if (m_ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
128			updateConfig();
129		}
130	});
131
132	GBAKeyEditor* editor = new GBAKeyEditor(inputController, InputController::KEYBOARD, QString(), this);
133	m_ui.stackedWidget->addWidget(editor);
134	m_ui.tabs->addItem("Keyboard");
135	connect(m_ui.buttonBox, SIGNAL(accepted()), editor, SLOT(save()));
136
137#ifdef BUILD_SDL
138	inputController->recalibrateAxes();
139	const char* profile = inputController->profileForType(SDL_BINDING_BUTTON);
140	editor = new GBAKeyEditor(inputController, SDL_BINDING_BUTTON, profile);
141	m_ui.stackedWidget->addWidget(editor);
142	m_ui.tabs->addItem("Controllers");
143	connect(m_ui.buttonBox, SIGNAL(accepted()), editor, SLOT(save()));
144#endif
145
146	ShortcutView* shortcutView = new ShortcutView();
147	shortcutView->setController(shortcutController);
148	shortcutView->setInputController(inputController);
149	m_ui.stackedWidget->addWidget(shortcutView);
150	m_ui.tabs->addItem("Shortcuts");
151}
152
153void SettingsView::selectBios() {
154	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
155	if (!filename.isEmpty()) {
156		m_ui.bios->setText(filename);
157	}
158}
159
160void SettingsView::recalculateRewind() {
161	int interval = m_ui.rewindInterval->value();
162	int capacity = m_ui.rewindCapacity->value();
163	double duration = m_ui.fpsTarget->value();
164	m_ui.rewindDuration->setValue(interval * capacity / duration);
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("rewindBufferInterval", m_ui.rewindInterval);
183	saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
184	saveSetting("resampleVideo", m_ui.resampleVideo);
185	saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
186	saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
187	saveSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
188	saveSetting("savegamePath", m_ui.savegamePath);
189	saveSetting("savestatePath", m_ui.savestatePath);
190	saveSetting("screenshotPath", m_ui.screenshotPath);
191	saveSetting("patchPath", m_ui.patchPath);
192
193	if (m_ui.fastForwardUnbounded->isChecked()) {
194		saveSetting("fastForwardRatio", "-1");
195	} else {
196		saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
197	}
198
199	switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
200	case IDLE_LOOP_IGNORE:
201		saveSetting("idleOptimization", "ignore");
202		break;
203	case IDLE_LOOP_REMOVE:
204		saveSetting("idleOptimization", "remove");
205		break;
206	case IDLE_LOOP_DETECT:
207		saveSetting("idleOptimization", "detect");
208		break;
209	}
210
211	int loadState = 0;
212	loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
213	loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
214	loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
215	saveSetting("loadStateExtdata", loadState);
216
217	int saveState = 0;
218	saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
219	saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
220	saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
221	saveSetting("saveStateExtdata", saveState);
222
223	QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
224	if (audioDriver != m_controller->getQtOption("audioDriver")) {
225		m_controller->setQtOption("audioDriver", audioDriver);
226		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
227		emit audioDriverChanged();
228	}
229
230	QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
231	if (displayDriver != m_controller->getQtOption("displayDriver")) {
232		m_controller->setQtOption("displayDriver", displayDriver);
233		Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
234		emit displayDriverChanged();
235	}
236
237	m_controller->write();
238
239	emit pathsChanged();
240	emit biosLoaded(m_ui.bios->text());
241}
242
243void SettingsView::reloadConfig() {	
244	loadSetting("bios", m_ui.bios);
245	loadSetting("useBios", m_ui.useBios);
246	loadSetting("skipBios", m_ui.skipBios);
247	loadSetting("audioBuffers", m_ui.audioBufferSize);
248	loadSetting("sampleRate", m_ui.sampleRate);
249	loadSetting("videoSync", m_ui.videoSync);
250	loadSetting("audioSync", m_ui.audioSync);
251	loadSetting("frameskip", m_ui.frameskip);
252	loadSetting("fpsTarget", m_ui.fpsTarget);
253	loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
254	loadSetting("volume", m_ui.volume);
255	loadSetting("mute", m_ui.mute);
256	loadSetting("rewindEnable", m_ui.rewind);
257	loadSetting("rewindBufferInterval", m_ui.rewindInterval);
258	loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
259	loadSetting("resampleVideo", m_ui.resampleVideo);
260	loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
261	loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
262	loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
263	loadSetting("savegamePath", m_ui.savegamePath);
264	loadSetting("savestatePath", m_ui.savestatePath);
265	loadSetting("screenshotPath", m_ui.screenshotPath);
266	loadSetting("patchPath", m_ui.patchPath);
267
268	double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
269	if (fastForwardRatio <= 0) {
270		m_ui.fastForwardUnbounded->setChecked(true);
271		m_ui.fastForwardRatio->setEnabled(false);
272	} else {
273		m_ui.fastForwardUnbounded->setChecked(false);
274		m_ui.fastForwardRatio->setEnabled(true);
275		m_ui.fastForwardRatio->setValue(fastForwardRatio);
276	}
277
278	connect(m_ui.rewindInterval, SIGNAL(valueChanged(int)), this, SLOT(recalculateRewind()));
279	connect(m_ui.rewindCapacity, SIGNAL(valueChanged(int)), this, SLOT(recalculateRewind()));
280	connect(m_ui.fpsTarget, SIGNAL(valueChanged(double)), this, SLOT(recalculateRewind()));
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}