all repos — mgba @ e3b5711295b5ddb5ccc1bb8c0b29f37fe81636c0

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/core/version.h>
 18#include <mgba/internal/gba/gba.h>
 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	connect(m_ui.clearCache, &QAbstractButton::pressed, this, &SettingsView::libraryCleared);
 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.gbaBiosBrowse, &QPushButton::clicked, [this]() {
133		selectBios(m_ui.gbaBios);
134	});
135	connect(m_ui.gbBiosBrowse, &QPushButton::clicked, [this]() {
136		selectBios(m_ui.gbBios);
137	});
138	connect(m_ui.gbcBiosBrowse, &QPushButton::clicked, [this]() {
139		selectBios(m_ui.gbcBios);
140	});
141
142	GBAKeyEditor* editor = new GBAKeyEditor(inputController, InputController::KEYBOARD, QString(), this);
143	m_ui.stackedWidget->addWidget(editor);
144	m_ui.tabs->addItem(tr("Keyboard"));
145	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, editor, &GBAKeyEditor::save);
146
147	GBAKeyEditor* buttonEditor = nullptr;
148#ifdef BUILD_SDL
149	inputController->recalibrateAxes();
150	const char* profile = inputController->profileForType(SDL_BINDING_BUTTON);
151	buttonEditor = new GBAKeyEditor(inputController, SDL_BINDING_BUTTON, profile);
152	m_ui.stackedWidget->addWidget(buttonEditor);
153	m_ui.tabs->addItem(tr("Controllers"));
154	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, buttonEditor, &GBAKeyEditor::save);
155#endif
156
157	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &SettingsView::updateConfig);
158	connect(m_ui.buttonBox, &QDialogButtonBox::clicked, [this, editor, buttonEditor](QAbstractButton* button) {
159		if (m_ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
160			updateConfig();
161			editor->save();
162			if (buttonEditor) {
163				buttonEditor->save();
164			}
165		}
166	});
167
168	m_ui.languages->setItemData(0, QLocale("en"));
169	QDir ts(":/translations/");
170	for (auto name : ts.entryList()) {
171		if (!name.endsWith(".qm")) {
172			continue;
173		}
174		QLocale locale(name.remove(QString("%0-").arg(binaryName)).remove(".qm"));
175		m_ui.languages->addItem(locale.nativeLanguageName(), locale);
176		if (locale == QLocale()) {
177			m_ui.languages->setCurrentIndex(m_ui.languages->count() - 1);
178		}
179	}
180
181	ShortcutView* shortcutView = new ShortcutView();
182	shortcutView->setController(shortcutController);
183	shortcutView->setInputController(inputController);
184	m_ui.stackedWidget->addWidget(shortcutView);
185	m_ui.tabs->addItem(tr("Shortcuts"));
186}
187
188void SettingsView::selectBios(QLineEdit* bios) {
189	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
190	if (!filename.isEmpty()) {
191		bios->setText(filename);
192	}
193}
194
195void SettingsView::updateConfig() {
196	saveSetting("gba.bios", m_ui.gbaBios);
197	saveSetting("gb.bios", m_ui.gbBios);
198	saveSetting("gbc.bios", m_ui.gbcBios);
199	saveSetting("useBios", m_ui.useBios);
200	saveSetting("skipBios", m_ui.skipBios);
201	saveSetting("audioBuffers", m_ui.audioBufferSize);
202	saveSetting("sampleRate", m_ui.sampleRate);
203	saveSetting("videoSync", m_ui.videoSync);
204	saveSetting("audioSync", m_ui.audioSync);
205	saveSetting("frameskip", m_ui.frameskip);
206	saveSetting("fpsTarget", m_ui.fpsTarget);
207	saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
208	saveSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
209	saveSetting("volume", m_ui.volume);
210	saveSetting("mute", m_ui.mute);
211	saveSetting("rewindEnable", m_ui.rewind);
212	saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
213	saveSetting("rewindSave", m_ui.rewindSave);
214	saveSetting("resampleVideo", m_ui.resampleVideo);
215	saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
216	saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
217	saveSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
218	saveSetting("savegamePath", m_ui.savegamePath);
219	saveSetting("savestatePath", m_ui.savestatePath);
220	saveSetting("screenshotPath", m_ui.screenshotPath);
221	saveSetting("patchPath", m_ui.patchPath);
222	saveSetting("libraryStyle", m_ui.libraryStyle->currentIndex());
223	saveSetting("showLibrary", m_ui.showLibrary);
224	saveSetting("preload", m_ui.preload);
225
226	if (m_ui.fastForwardUnbounded->isChecked()) {
227		saveSetting("fastForwardRatio", "-1");
228	} else {
229		saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
230	}
231
232	switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
233	case IDLE_LOOP_IGNORE:
234		saveSetting("idleOptimization", "ignore");
235		break;
236	case IDLE_LOOP_REMOVE:
237		saveSetting("idleOptimization", "remove");
238		break;
239	case IDLE_LOOP_DETECT:
240		saveSetting("idleOptimization", "detect");
241		break;
242	}
243
244	int loadState = SAVESTATE_RTC;
245	loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
246	loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
247	loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
248	saveSetting("loadStateExtdata", loadState);
249
250	int saveState = SAVESTATE_RTC | SAVESTATE_METADATA;
251	saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
252	saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
253	saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
254	saveSetting("saveStateExtdata", saveState);
255
256	QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
257	if (audioDriver != m_controller->getQtOption("audioDriver")) {
258		m_controller->setQtOption("audioDriver", audioDriver);
259		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
260		emit audioDriverChanged();
261	}
262
263	QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
264	if (displayDriver != m_controller->getQtOption("displayDriver")) {
265		m_controller->setQtOption("displayDriver", displayDriver);
266		Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
267		emit displayDriverChanged();
268	}
269
270	QLocale language = m_ui.languages->itemData(m_ui.languages->currentIndex()).toLocale();
271	if (language != m_controller->getQtOption("language").toLocale() && !(language.bcp47Name() == QLocale::system().bcp47Name() && m_controller->getQtOption("language").isNull())) {
272		m_controller->setQtOption("language", language.bcp47Name());
273		emit languageChanged();
274	}
275
276	m_controller->write();
277
278	emit pathsChanged();
279	emit biosLoaded(PLATFORM_GBA, m_ui.gbaBios->text());
280}
281
282void SettingsView::reloadConfig() {	
283	loadSetting("bios", m_ui.gbaBios);
284	loadSetting("gba.bios", m_ui.gbaBios);
285	loadSetting("gb.bios", m_ui.gbBios);
286	loadSetting("gbc.bios", m_ui.gbcBios);
287	loadSetting("useBios", m_ui.useBios);
288	loadSetting("skipBios", m_ui.skipBios);
289	loadSetting("audioBuffers", m_ui.audioBufferSize);
290	loadSetting("sampleRate", m_ui.sampleRate);
291	loadSetting("videoSync", m_ui.videoSync);
292	loadSetting("audioSync", m_ui.audioSync);
293	loadSetting("frameskip", m_ui.frameskip);
294	loadSetting("fpsTarget", m_ui.fpsTarget);
295	loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
296	loadSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
297	loadSetting("volume", m_ui.volume);
298	loadSetting("mute", m_ui.mute);
299	loadSetting("rewindEnable", m_ui.rewind);
300	loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
301	loadSetting("rewindSave", m_ui.rewindSave);
302	loadSetting("resampleVideo", m_ui.resampleVideo);
303	loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
304	loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
305	loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
306	loadSetting("savegamePath", m_ui.savegamePath);
307	loadSetting("savestatePath", m_ui.savestatePath);
308	loadSetting("screenshotPath", m_ui.screenshotPath);
309	loadSetting("patchPath", m_ui.patchPath);
310	loadSetting("showLibrary", m_ui.showLibrary);
311	loadSetting("preload", m_ui.preload);
312
313	double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
314	if (fastForwardRatio <= 0) {
315		m_ui.fastForwardUnbounded->setChecked(true);
316		m_ui.fastForwardRatio->setEnabled(false);
317	} else {
318		m_ui.fastForwardUnbounded->setChecked(false);
319		m_ui.fastForwardRatio->setEnabled(true);
320		m_ui.fastForwardRatio->setValue(fastForwardRatio);
321	}
322
323	QString idleOptimization = loadSetting("idleOptimization");
324	if (idleOptimization == "ignore") {
325		m_ui.idleOptimization->setCurrentIndex(0);
326	} else if (idleOptimization == "remove") {
327		m_ui.idleOptimization->setCurrentIndex(1);
328	} else if (idleOptimization == "detect") {
329		m_ui.idleOptimization->setCurrentIndex(2);
330	}
331
332	bool ok;
333	int loadState = loadSetting("loadStateExtdata").toInt(&ok);
334	if (!ok) {
335		loadState = SAVESTATE_SCREENSHOT | SAVESTATE_RTC;
336	}
337	m_ui.loadStateScreenshot->setChecked(loadState & SAVESTATE_SCREENSHOT);
338	m_ui.loadStateSave->setChecked(loadState & SAVESTATE_SAVEDATA);
339	m_ui.loadStateCheats->setChecked(loadState & SAVESTATE_CHEATS);
340
341	int saveState = loadSetting("saveStateExtdata").toInt(&ok);
342	if (!ok) {
343		saveState = SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS | SAVESTATE_RTC | SAVESTATE_METADATA;
344	}
345	m_ui.saveStateScreenshot->setChecked(saveState & SAVESTATE_SCREENSHOT);
346	m_ui.saveStateSave->setChecked(saveState & SAVESTATE_SAVEDATA);
347	m_ui.saveStateCheats->setChecked(saveState & SAVESTATE_CHEATS);
348}
349
350void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
351	m_controller->setOption(key, field->isChecked());
352	m_controller->updateOption(key);
353}
354
355void SettingsView::saveSetting(const char* key, const QComboBox* field) {
356	saveSetting(key, field->lineEdit());
357}
358
359void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
360	saveSetting(key, field->value());
361}
362
363void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
364	saveSetting(key, field->text());
365}
366
367void SettingsView::saveSetting(const char* key, const QSlider* field) {
368	saveSetting(key, field->value());
369}
370
371void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
372	saveSetting(key, field->value());
373}
374
375void SettingsView::saveSetting(const char* key, const QVariant& field) {
376	m_controller->setOption(key, field);
377	m_controller->updateOption(key);
378}
379
380void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
381	QString option = loadSetting(key);
382	field->setChecked(!option.isNull() && option != "0");
383}
384
385void SettingsView::loadSetting(const char* key, QComboBox* field) {
386	loadSetting(key, field->lineEdit());
387}
388
389void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
390	QString option = loadSetting(key);
391	field->setValue(option.toDouble());
392}
393
394void SettingsView::loadSetting(const char* key, QLineEdit* field) {
395	QString option = loadSetting(key);
396	field->setText(option);
397}
398
399void SettingsView::loadSetting(const char* key, QSlider* field) {
400	QString option = loadSetting(key);
401	field->setValue(option.toInt());
402}
403
404void SettingsView::loadSetting(const char* key, QSpinBox* field) {
405	QString option = loadSetting(key);
406	field->setValue(option.toInt());
407}
408
409QString SettingsView::loadSetting(const char* key) {
410	return m_controller->getOption(key);
411}