all repos — mgba @ 6e03d88818e414f25c100155a0163f65a2afd885

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 defined(BUILD_GL) || defined(BUILD_GLES)
191	if (m_shader) {
192		m_ui.stackedWidget->removeWidget(m_shader);
193		m_shader->setParent(nullptr);
194	}
195#endif
196}
197
198void SettingsView::setShaderSelector(ShaderSelector* shaderSelector) {
199#if defined(BUILD_GL) || defined(BUILD_GLES)
200	m_shader = shaderSelector;
201	m_ui.stackedWidget->addWidget(m_shader);
202	m_ui.tabs->addItem(tr("Shaders"));
203	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, m_shader, &ShaderSelector::saved);
204#endif
205}
206
207void SettingsView::selectBios(QLineEdit* bios) {
208	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
209	if (!filename.isEmpty()) {
210		bios->setText(filename);
211	}
212}
213
214void SettingsView::updateConfig() {
215	saveSetting("gba.bios", m_ui.gbaBios);
216	saveSetting("gb.bios", m_ui.gbBios);
217	saveSetting("gbc.bios", m_ui.gbcBios);
218	saveSetting("useBios", m_ui.useBios);
219	saveSetting("skipBios", m_ui.skipBios);
220	saveSetting("audioBuffers", m_ui.audioBufferSize);
221	saveSetting("sampleRate", m_ui.sampleRate);
222	saveSetting("videoSync", m_ui.videoSync);
223	saveSetting("audioSync", m_ui.audioSync);
224	saveSetting("frameskip", m_ui.frameskip);
225	saveSetting("fpsTarget", m_ui.fpsTarget);
226	saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
227	saveSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
228	saveSetting("volume", m_ui.volume);
229	saveSetting("mute", m_ui.mute);
230	saveSetting("rewindEnable", m_ui.rewind);
231	saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
232	saveSetting("rewindSave", m_ui.rewindSave);
233	saveSetting("resampleVideo", m_ui.resampleVideo);
234	saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
235	saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
236	saveSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
237	saveSetting("savegamePath", m_ui.savegamePath);
238	saveSetting("savestatePath", m_ui.savestatePath);
239	saveSetting("screenshotPath", m_ui.screenshotPath);
240	saveSetting("patchPath", m_ui.patchPath);
241	saveSetting("libraryStyle", m_ui.libraryStyle->currentIndex());
242	saveSetting("showLibrary", m_ui.showLibrary);
243	saveSetting("preload", m_ui.preload);
244
245	if (m_ui.fastForwardUnbounded->isChecked()) {
246		saveSetting("fastForwardRatio", "-1");
247	} else {
248		saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
249	}
250
251	switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
252	case IDLE_LOOP_IGNORE:
253		saveSetting("idleOptimization", "ignore");
254		break;
255	case IDLE_LOOP_REMOVE:
256		saveSetting("idleOptimization", "remove");
257		break;
258	case IDLE_LOOP_DETECT:
259		saveSetting("idleOptimization", "detect");
260		break;
261	}
262
263	int loadState = SAVESTATE_RTC;
264	loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
265	loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
266	loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
267	saveSetting("loadStateExtdata", loadState);
268
269	int saveState = SAVESTATE_RTC | SAVESTATE_METADATA;
270	saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
271	saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
272	saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
273	saveSetting("saveStateExtdata", saveState);
274
275	QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
276	if (audioDriver != m_controller->getQtOption("audioDriver")) {
277		m_controller->setQtOption("audioDriver", audioDriver);
278		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
279		emit audioDriverChanged();
280	}
281
282	QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
283	if (displayDriver != m_controller->getQtOption("displayDriver")) {
284		m_controller->setQtOption("displayDriver", displayDriver);
285		Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
286		emit displayDriverChanged();
287	}
288
289	QLocale language = m_ui.languages->itemData(m_ui.languages->currentIndex()).toLocale();
290	if (language != m_controller->getQtOption("language").toLocale() && !(language.bcp47Name() == QLocale::system().bcp47Name() && m_controller->getQtOption("language").isNull())) {
291		m_controller->setQtOption("language", language.bcp47Name());
292		emit languageChanged();
293	}
294
295	m_controller->write();
296
297	emit pathsChanged();
298	emit biosLoaded(PLATFORM_GBA, m_ui.gbaBios->text());
299}
300
301void SettingsView::reloadConfig() {	
302	loadSetting("bios", m_ui.gbaBios);
303	loadSetting("gba.bios", m_ui.gbaBios);
304	loadSetting("gb.bios", m_ui.gbBios);
305	loadSetting("gbc.bios", m_ui.gbcBios);
306	loadSetting("useBios", m_ui.useBios);
307	loadSetting("skipBios", m_ui.skipBios);
308	loadSetting("audioBuffers", m_ui.audioBufferSize);
309	loadSetting("sampleRate", m_ui.sampleRate);
310	loadSetting("videoSync", m_ui.videoSync);
311	loadSetting("audioSync", m_ui.audioSync);
312	loadSetting("frameskip", m_ui.frameskip);
313	loadSetting("fpsTarget", m_ui.fpsTarget);
314	loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
315	loadSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
316	loadSetting("volume", m_ui.volume);
317	loadSetting("mute", m_ui.mute);
318	loadSetting("rewindEnable", m_ui.rewind);
319	loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
320	loadSetting("rewindSave", m_ui.rewindSave);
321	loadSetting("resampleVideo", m_ui.resampleVideo);
322	loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
323	loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
324	loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
325	loadSetting("savegamePath", m_ui.savegamePath);
326	loadSetting("savestatePath", m_ui.savestatePath);
327	loadSetting("screenshotPath", m_ui.screenshotPath);
328	loadSetting("patchPath", m_ui.patchPath);
329	loadSetting("showLibrary", m_ui.showLibrary);
330	loadSetting("preload", m_ui.preload);
331
332	m_ui.libraryStyle->setCurrentIndex(loadSetting("libraryStyle").toInt());
333
334	double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
335	if (fastForwardRatio <= 0) {
336		m_ui.fastForwardUnbounded->setChecked(true);
337		m_ui.fastForwardRatio->setEnabled(false);
338	} else {
339		m_ui.fastForwardUnbounded->setChecked(false);
340		m_ui.fastForwardRatio->setEnabled(true);
341		m_ui.fastForwardRatio->setValue(fastForwardRatio);
342	}
343
344	QString idleOptimization = loadSetting("idleOptimization");
345	if (idleOptimization == "ignore") {
346		m_ui.idleOptimization->setCurrentIndex(0);
347	} else if (idleOptimization == "remove") {
348		m_ui.idleOptimization->setCurrentIndex(1);
349	} else if (idleOptimization == "detect") {
350		m_ui.idleOptimization->setCurrentIndex(2);
351	}
352
353	bool ok;
354	int loadState = loadSetting("loadStateExtdata").toInt(&ok);
355	if (!ok) {
356		loadState = SAVESTATE_SCREENSHOT | SAVESTATE_RTC;
357	}
358	m_ui.loadStateScreenshot->setChecked(loadState & SAVESTATE_SCREENSHOT);
359	m_ui.loadStateSave->setChecked(loadState & SAVESTATE_SAVEDATA);
360	m_ui.loadStateCheats->setChecked(loadState & SAVESTATE_CHEATS);
361
362	int saveState = loadSetting("saveStateExtdata").toInt(&ok);
363	if (!ok) {
364		saveState = SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS | SAVESTATE_RTC | SAVESTATE_METADATA;
365	}
366	m_ui.saveStateScreenshot->setChecked(saveState & SAVESTATE_SCREENSHOT);
367	m_ui.saveStateSave->setChecked(saveState & SAVESTATE_SAVEDATA);
368	m_ui.saveStateCheats->setChecked(saveState & SAVESTATE_CHEATS);
369}
370
371void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
372	m_controller->setOption(key, field->isChecked());
373	m_controller->updateOption(key);
374}
375
376void SettingsView::saveSetting(const char* key, const QComboBox* field) {
377	saveSetting(key, field->lineEdit());
378}
379
380void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
381	saveSetting(key, field->value());
382}
383
384void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
385	saveSetting(key, field->text());
386}
387
388void SettingsView::saveSetting(const char* key, const QSlider* field) {
389	saveSetting(key, field->value());
390}
391
392void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
393	saveSetting(key, field->value());
394}
395
396void SettingsView::saveSetting(const char* key, const QVariant& field) {
397	m_controller->setOption(key, field);
398	m_controller->updateOption(key);
399}
400
401void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
402	QString option = loadSetting(key);
403	field->setChecked(!option.isNull() && option != "0");
404}
405
406void SettingsView::loadSetting(const char* key, QComboBox* field) {
407	loadSetting(key, field->lineEdit());
408}
409
410void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
411	QString option = loadSetting(key);
412	field->setValue(option.toDouble());
413}
414
415void SettingsView::loadSetting(const char* key, QLineEdit* field) {
416	QString option = loadSetting(key);
417	field->setText(option);
418}
419
420void SettingsView::loadSetting(const char* key, QSlider* field) {
421	QString option = loadSetting(key);
422	field->setValue(option.toInt());
423}
424
425void SettingsView::loadSetting(const char* key, QSpinBox* field) {
426	QString option = loadSetting(key);
427	field->setValue(option.toInt());
428}
429
430QString SettingsView::loadSetting(const char* key) {
431	return m_controller->getOption(key);
432}