all repos — mgba @ 0c0fab5402726e988a1335539008a03012dea844

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 "ShaderSelector.h"
 15#include "ShortcutView.h"
 16
 17#include <mgba/core/serialize.h>
 18#include <mgba/core/version.h>
 19#include <mgba/internal/gba/gba.h>
 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	connect(m_ui.clearCache, &QAbstractButton::pressed, this, &SettingsView::libraryCleared);
 95
 96	// TODO: Move to reloadConfig()
 97	QVariant audioDriver = m_controller->getQtOption("audioDriver");
 98#ifdef BUILD_QT_MULTIMEDIA
 99	m_ui.audioDriver->addItem(tr("Qt Multimedia"), static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA));
100	if (!audioDriver.isNull() && audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA)) {
101		m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
102	}
103#endif
104
105#ifdef BUILD_SDL
106	m_ui.audioDriver->addItem(tr("SDL"), static_cast<int>(AudioProcessor::Driver::SDL));
107	if (audioDriver.isNull() || audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::SDL)) {
108		m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
109	}
110#endif
111
112	// TODO: Move to reloadConfig()
113	QVariant displayDriver = m_controller->getQtOption("displayDriver");
114	m_ui.displayDriver->addItem(tr("Software (Qt)"), static_cast<int>(Display::Driver::QT));
115	if (!displayDriver.isNull() && displayDriver.toInt() == static_cast<int>(Display::Driver::QT)) {
116		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
117	}
118
119#if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
120	m_ui.displayDriver->addItem(tr("OpenGL"), static_cast<int>(Display::Driver::OPENGL));
121	if (displayDriver.isNull() || displayDriver.toInt() == static_cast<int>(Display::Driver::OPENGL)) {
122		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
123	}
124#endif
125
126#ifdef BUILD_GL
127	m_ui.displayDriver->addItem(tr("OpenGL (force version 1.x)"), static_cast<int>(Display::Driver::OPENGL1));
128	if (!displayDriver.isNull() && displayDriver.toInt() == static_cast<int>(Display::Driver::OPENGL1)) {
129		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
130	}
131#endif
132
133	// TODO: Move to reloadConfig()
134	QVariant cameraDriver = m_controller->getQtOption("cameraDriver");
135	m_ui.cameraDriver->addItem(tr("None (Still Image)"), static_cast<int>(InputController::CameraDriver::NONE));
136	if (cameraDriver.isNull() || cameraDriver.toInt() == static_cast<int>(InputController::CameraDriver::NONE)) {
137		m_ui.cameraDriver->setCurrentIndex(m_ui.cameraDriver->count() - 1);
138	}
139
140#ifdef BUILD_QT_MULTIMEDIA
141	m_ui.cameraDriver->addItem(tr("Qt Multimedia"), static_cast<int>(InputController::CameraDriver::QT_MULTIMEDIA));
142	if (!cameraDriver.isNull() && cameraDriver.toInt() == static_cast<int>(InputController::CameraDriver::QT_MULTIMEDIA)) {
143		m_ui.cameraDriver->setCurrentIndex(m_ui.cameraDriver->count() - 1);
144	}
145#endif
146
147#ifdef M_CORE_GBA
148	connect(m_ui.gbaBiosBrowse, &QPushButton::clicked, [this]() {
149		selectBios(m_ui.gbaBios);
150	});
151#else
152	m_ui.gbaBiosBrowse->hide();
153#endif
154
155#ifdef M_CORE_GB
156	connect(m_ui.gbBiosBrowse, &QPushButton::clicked, [this]() {
157		selectBios(m_ui.gbBios);
158	});
159	connect(m_ui.gbcBiosBrowse, &QPushButton::clicked, [this]() {
160		selectBios(m_ui.gbcBios);
161	});
162#else
163	m_ui.gbBiosBrowse->hide();
164	m_ui.gbcBiosBrowse->hide();
165	m_ui.gb->hide();
166#endif
167
168	GBAKeyEditor* editor = new GBAKeyEditor(inputController, InputController::KEYBOARD, QString(), this);
169	m_ui.stackedWidget->addWidget(editor);
170	m_ui.tabs->addItem(tr("Keyboard"));
171	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, editor, &GBAKeyEditor::save);
172
173	GBAKeyEditor* buttonEditor = nullptr;
174#ifdef BUILD_SDL
175	inputController->recalibrateAxes();
176	const char* profile = inputController->profileForType(SDL_BINDING_BUTTON);
177	buttonEditor = new GBAKeyEditor(inputController, SDL_BINDING_BUTTON, profile);
178	m_ui.stackedWidget->addWidget(buttonEditor);
179	m_ui.tabs->addItem(tr("Controllers"));
180	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, buttonEditor, &GBAKeyEditor::save);
181#endif
182
183	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &SettingsView::updateConfig);
184	connect(m_ui.buttonBox, &QDialogButtonBox::clicked, [this, editor, buttonEditor](QAbstractButton* button) {
185		if (m_ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
186			updateConfig();
187			editor->save();
188			if (buttonEditor) {
189				buttonEditor->save();
190			}
191		}
192	});
193
194	m_ui.languages->setItemData(0, QLocale("en"));
195	QDir ts(":/translations/");
196	for (auto name : ts.entryList()) {
197		if (!name.endsWith(".qm") || !name.startsWith(binaryName)) {
198			continue;
199		}
200		QLocale locale(name.remove(QString("%0-").arg(binaryName)).remove(".qm"));
201		m_ui.languages->addItem(locale.nativeLanguageName(), locale);
202		if (locale == QLocale()) {
203			m_ui.languages->setCurrentIndex(m_ui.languages->count() - 1);
204		}
205	}
206
207	ShortcutView* shortcutView = new ShortcutView();
208	shortcutView->setController(shortcutController);
209	shortcutView->setInputController(inputController);
210	m_ui.stackedWidget->addWidget(shortcutView);
211	m_ui.tabs->addItem(tr("Shortcuts"));
212}
213
214SettingsView::~SettingsView() {
215#if defined(BUILD_GL) || defined(BUILD_GLES)
216	setShaderSelector(nullptr);
217#endif
218}
219
220void SettingsView::setShaderSelector(ShaderSelector* shaderSelector) {
221#if defined(BUILD_GL) || defined(BUILD_GLES)
222	if (m_shader) {
223		auto items = m_ui.tabs->findItems(tr("Shaders"), Qt::MatchFixedString);
224		for (const auto& item : items) {
225			m_ui.tabs->removeItemWidget(item);
226		}
227		m_ui.stackedWidget->removeWidget(m_shader);
228		m_shader->setParent(nullptr);
229	}
230	m_shader = shaderSelector;
231	if (shaderSelector) {
232		m_ui.stackedWidget->addWidget(m_shader);
233		m_ui.tabs->addItem(tr("Shaders"));
234		connect(m_ui.buttonBox, &QDialogButtonBox::accepted, m_shader, &ShaderSelector::saved);
235	}
236#endif
237}
238
239void SettingsView::selectBios(QLineEdit* bios) {
240	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
241	if (!filename.isEmpty()) {
242		bios->setText(filename);
243	}
244}
245
246void SettingsView::updateConfig() {
247	saveSetting("gba.bios", m_ui.gbaBios);
248	saveSetting("gb.bios", m_ui.gbBios);
249	saveSetting("gbc.bios", m_ui.gbcBios);
250	saveSetting("useBios", m_ui.useBios);
251	saveSetting("skipBios", m_ui.skipBios);
252	saveSetting("audioBuffers", m_ui.audioBufferSize);
253	saveSetting("sampleRate", m_ui.sampleRate);
254	saveSetting("videoSync", m_ui.videoSync);
255	saveSetting("audioSync", m_ui.audioSync);
256	saveSetting("frameskip", m_ui.frameskip);
257	saveSetting("fpsTarget", m_ui.fpsTarget);
258	saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
259	saveSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
260	saveSetting("volume", m_ui.volume);
261	saveSetting("mute", m_ui.mute);
262	saveSetting("rewindEnable", m_ui.rewind);
263	saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
264	saveSetting("rewindSave", m_ui.rewindSave);
265	saveSetting("resampleVideo", m_ui.resampleVideo);
266	saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
267	saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
268	saveSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
269	saveSetting("savegamePath", m_ui.savegamePath);
270	saveSetting("savestatePath", m_ui.savestatePath);
271	saveSetting("screenshotPath", m_ui.screenshotPath);
272	saveSetting("patchPath", m_ui.patchPath);
273	saveSetting("libraryStyle", m_ui.libraryStyle->currentIndex());
274	saveSetting("showLibrary", m_ui.showLibrary);
275	saveSetting("preload", m_ui.preload);
276
277	if (m_ui.fastForwardUnbounded->isChecked()) {
278		saveSetting("fastForwardRatio", "-1");
279	} else {
280		saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
281	}
282
283	switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
284	case IDLE_LOOP_IGNORE:
285		saveSetting("idleOptimization", "ignore");
286		break;
287	case IDLE_LOOP_REMOVE:
288		saveSetting("idleOptimization", "remove");
289		break;
290	case IDLE_LOOP_DETECT:
291		saveSetting("idleOptimization", "detect");
292		break;
293	}
294
295	int loadState = SAVESTATE_RTC;
296	loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
297	loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
298	loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
299	saveSetting("loadStateExtdata", loadState);
300
301	int saveState = SAVESTATE_RTC | SAVESTATE_METADATA;
302	saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
303	saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
304	saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
305	saveSetting("saveStateExtdata", saveState);
306
307	QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
308	if (audioDriver != m_controller->getQtOption("audioDriver")) {
309		m_controller->setQtOption("audioDriver", audioDriver);
310		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
311		emit audioDriverChanged();
312	}
313
314	QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
315	if (displayDriver != m_controller->getQtOption("displayDriver")) {
316		m_controller->setQtOption("displayDriver", displayDriver);
317		Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
318		setShaderSelector(nullptr);
319		emit displayDriverChanged();
320	}
321
322	QVariant cameraDriver = m_ui.cameraDriver->itemData(m_ui.cameraDriver->currentIndex());
323	if (cameraDriver != m_controller->getQtOption("cameraDriver")) {
324		m_controller->setQtOption("cameraDriver", cameraDriver);
325		emit cameraDriverChanged();
326	}
327
328	QLocale language = m_ui.languages->itemData(m_ui.languages->currentIndex()).toLocale();
329	if (language != m_controller->getQtOption("language").toLocale() && !(language.bcp47Name() == QLocale::system().bcp47Name() && m_controller->getQtOption("language").isNull())) {
330		m_controller->setQtOption("language", language.bcp47Name());
331		emit languageChanged();
332	}
333
334	m_controller->write();
335
336	emit pathsChanged();
337	emit biosLoaded(PLATFORM_GBA, m_ui.gbaBios->text());
338}
339
340void SettingsView::reloadConfig() {	
341	loadSetting("bios", m_ui.gbaBios);
342	loadSetting("gba.bios", m_ui.gbaBios);
343	loadSetting("gb.bios", m_ui.gbBios);
344	loadSetting("gbc.bios", m_ui.gbcBios);
345	loadSetting("useBios", m_ui.useBios);
346	loadSetting("skipBios", m_ui.skipBios);
347	loadSetting("audioBuffers", m_ui.audioBufferSize);
348	loadSetting("sampleRate", m_ui.sampleRate);
349	loadSetting("videoSync", m_ui.videoSync);
350	loadSetting("audioSync", m_ui.audioSync);
351	loadSetting("frameskip", m_ui.frameskip);
352	loadSetting("fpsTarget", m_ui.fpsTarget);
353	loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
354	loadSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
355	loadSetting("volume", m_ui.volume);
356	loadSetting("mute", m_ui.mute);
357	loadSetting("rewindEnable", m_ui.rewind);
358	loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
359	loadSetting("rewindSave", m_ui.rewindSave);
360	loadSetting("resampleVideo", m_ui.resampleVideo);
361	loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
362	loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
363	loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
364	loadSetting("savegamePath", m_ui.savegamePath);
365	loadSetting("savestatePath", m_ui.savestatePath);
366	loadSetting("screenshotPath", m_ui.screenshotPath);
367	loadSetting("patchPath", m_ui.patchPath);
368	loadSetting("showLibrary", m_ui.showLibrary);
369	loadSetting("preload", m_ui.preload);
370
371	m_ui.libraryStyle->setCurrentIndex(loadSetting("libraryStyle").toInt());
372
373	double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
374	if (fastForwardRatio <= 0) {
375		m_ui.fastForwardUnbounded->setChecked(true);
376		m_ui.fastForwardRatio->setEnabled(false);
377	} else {
378		m_ui.fastForwardUnbounded->setChecked(false);
379		m_ui.fastForwardRatio->setEnabled(true);
380		m_ui.fastForwardRatio->setValue(fastForwardRatio);
381	}
382
383	QString idleOptimization = loadSetting("idleOptimization");
384	if (idleOptimization == "ignore") {
385		m_ui.idleOptimization->setCurrentIndex(0);
386	} else if (idleOptimization == "remove") {
387		m_ui.idleOptimization->setCurrentIndex(1);
388	} else if (idleOptimization == "detect") {
389		m_ui.idleOptimization->setCurrentIndex(2);
390	}
391
392	bool ok;
393	int loadState = loadSetting("loadStateExtdata").toInt(&ok);
394	if (!ok) {
395		loadState = SAVESTATE_SCREENSHOT | SAVESTATE_RTC;
396	}
397	m_ui.loadStateScreenshot->setChecked(loadState & SAVESTATE_SCREENSHOT);
398	m_ui.loadStateSave->setChecked(loadState & SAVESTATE_SAVEDATA);
399	m_ui.loadStateCheats->setChecked(loadState & SAVESTATE_CHEATS);
400
401	int saveState = loadSetting("saveStateExtdata").toInt(&ok);
402	if (!ok) {
403		saveState = SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS | SAVESTATE_RTC | SAVESTATE_METADATA;
404	}
405	m_ui.saveStateScreenshot->setChecked(saveState & SAVESTATE_SCREENSHOT);
406	m_ui.saveStateSave->setChecked(saveState & SAVESTATE_SAVEDATA);
407	m_ui.saveStateCheats->setChecked(saveState & SAVESTATE_CHEATS);
408}
409
410void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
411	m_controller->setOption(key, field->isChecked());
412	m_controller->updateOption(key);
413}
414
415void SettingsView::saveSetting(const char* key, const QComboBox* field) {
416	saveSetting(key, field->lineEdit());
417}
418
419void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
420	saveSetting(key, field->value());
421}
422
423void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
424	saveSetting(key, field->text());
425}
426
427void SettingsView::saveSetting(const char* key, const QSlider* field) {
428	saveSetting(key, field->value());
429}
430
431void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
432	saveSetting(key, field->value());
433}
434
435void SettingsView::saveSetting(const char* key, const QVariant& field) {
436	m_controller->setOption(key, field);
437	m_controller->updateOption(key);
438}
439
440void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
441	QString option = loadSetting(key);
442	field->setChecked(!option.isNull() && option != "0");
443}
444
445void SettingsView::loadSetting(const char* key, QComboBox* field) {
446	loadSetting(key, field->lineEdit());
447}
448
449void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
450	QString option = loadSetting(key);
451	field->setValue(option.toDouble());
452}
453
454void SettingsView::loadSetting(const char* key, QLineEdit* field) {
455	QString option = loadSetting(key);
456	field->setText(option);
457}
458
459void SettingsView::loadSetting(const char* key, QSlider* field) {
460	QString option = loadSetting(key);
461	field->setValue(option.toInt());
462}
463
464void SettingsView::loadSetting(const char* key, QSpinBox* field) {
465	QString option = loadSetting(key);
466	field->setValue(option.toInt());
467}
468
469QString SettingsView::loadSetting(const char* key) {
470	return m_controller->getOption(key);
471}