all repos — mgba @ 5167fce4c70711a221c4179ae245aca19b6f394a

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