all repos — mgba @ 50402c830729f2ba5a6fc3e6facfd8b258f7f97d

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
 13using namespace QGBA;
 14
 15SettingsView::SettingsView(ConfigController* controller, QWidget* parent)
 16	: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
 17	, m_controller(controller)
 18{
 19	m_ui.setupUi(this);
 20
 21	loadSetting("bios", m_ui.bios);
 22	loadSetting("useBios", m_ui.useBios);
 23	loadSetting("skipBios", m_ui.skipBios);
 24	loadSetting("audioBuffers", m_ui.audioBufferSize);
 25	loadSetting("sampleRate", m_ui.sampleRate);
 26	loadSetting("videoSync", m_ui.videoSync);
 27	loadSetting("audioSync", m_ui.audioSync);
 28	loadSetting("frameskip", m_ui.frameskip);
 29	loadSetting("fpsTarget", m_ui.fpsTarget);
 30	loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
 31	loadSetting("volume", m_ui.volume);
 32	loadSetting("mute", m_ui.mute);
 33	loadSetting("rewindEnable", m_ui.rewind);
 34	loadSetting("rewindBufferInterval", m_ui.rewindInterval);
 35	loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
 36	loadSetting("resampleVideo", m_ui.resampleVideo);
 37	loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
 38	loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
 39
 40	double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
 41	if (fastForwardRatio <= 0) {
 42		m_ui.fastForwardUnbounded->setChecked(true);
 43		m_ui.fastForwardRatio->setEnabled(false);
 44	} else {
 45		m_ui.fastForwardUnbounded->setChecked(false);
 46		m_ui.fastForwardRatio->setEnabled(true);
 47		m_ui.fastForwardRatio->setValue(fastForwardRatio);
 48	}
 49	connect(m_ui.fastForwardUnbounded, &QAbstractButton::toggled, [this](bool checked) {
 50		m_ui.fastForwardRatio->setEnabled(!checked);
 51	});
 52
 53	QString idleOptimization = loadSetting("idleOptimization");
 54	if (idleOptimization == "ignore") {
 55		m_ui.idleOptimization->setCurrentIndex(0);
 56	} else if (idleOptimization == "remove") {
 57		m_ui.idleOptimization->setCurrentIndex(1);
 58	} else if (idleOptimization == "detect") {
 59		m_ui.idleOptimization->setCurrentIndex(2);
 60	}
 61
 62	QVariant audioDriver = m_controller->getQtOption("audioDriver");
 63#ifdef BUILD_QT_MULTIMEDIA
 64	m_ui.audioDriver->addItem(tr("Qt Multimedia"), static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA));
 65	if (!audioDriver.isNull() && audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA)) {
 66		m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
 67	}
 68#endif
 69
 70#ifdef BUILD_SDL
 71	m_ui.audioDriver->addItem(tr("SDL"), static_cast<int>(AudioProcessor::Driver::SDL));
 72	if (audioDriver.isNull() || audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::SDL)) {
 73		m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
 74	}
 75#endif
 76
 77	QVariant displayDriver = m_controller->getQtOption("displayDriver");
 78	m_ui.displayDriver->addItem(tr("Software (Qt)"), static_cast<int>(Display::Driver::QT));
 79	if (!displayDriver.isNull() && displayDriver.toInt() == static_cast<int>(Display::Driver::QT)) {
 80		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
 81	}
 82
 83#ifdef BUILD_GL
 84	m_ui.displayDriver->addItem(tr("OpenGL"), static_cast<int>(Display::Driver::OPENGL));
 85	if (displayDriver.isNull() || displayDriver.toInt() == static_cast<int>(Display::Driver::OPENGL)) {
 86		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
 87	}
 88#endif
 89
 90	connect(m_ui.biosBrowse, SIGNAL(clicked()), this, SLOT(selectBios()));
 91	connect(m_ui.buttonBox, SIGNAL(accepted()), this, SLOT(updateConfig()));
 92	connect(m_ui.buttonBox, &QDialogButtonBox::clicked, [this](QAbstractButton* button) {
 93		if (m_ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
 94			updateConfig();
 95		}
 96	});
 97}
 98
 99void SettingsView::selectBios() {
100	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
101	if (!filename.isEmpty()) {
102		m_ui.bios->setText(filename);
103	}
104}
105
106void SettingsView::updateConfig() {
107	saveSetting("bios", m_ui.bios);
108	saveSetting("useBios", m_ui.useBios);
109	saveSetting("skipBios", m_ui.skipBios);
110	saveSetting("audioBuffers", m_ui.audioBufferSize);
111	saveSetting("sampleRate", m_ui.sampleRate);
112	saveSetting("videoSync", m_ui.videoSync);
113	saveSetting("audioSync", m_ui.audioSync);
114	saveSetting("frameskip", m_ui.frameskip);
115	saveSetting("fpsTarget", m_ui.fpsTarget);
116	saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
117	saveSetting("volume", m_ui.volume);
118	saveSetting("mute", m_ui.mute);
119	saveSetting("rewindEnable", m_ui.rewind);
120	saveSetting("rewindBufferInterval", m_ui.rewindInterval);
121	saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
122	saveSetting("resampleVideo", m_ui.resampleVideo);
123	saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
124	saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
125
126	if (m_ui.fastForwardUnbounded->isChecked()) {
127		saveSetting("fastForwardRatio", "-1");
128	} else {
129		saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
130	}
131
132	switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
133	case IDLE_LOOP_IGNORE:
134		saveSetting("idleOptimization", "ignore");
135		break;
136	case IDLE_LOOP_REMOVE:
137		saveSetting("idleOptimization", "remove");
138		break;
139	case IDLE_LOOP_DETECT:
140		saveSetting("idleOptimization", "detect");
141		break;
142	}
143
144	QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
145	if (audioDriver != m_controller->getQtOption("audioDriver")) {
146		m_controller->setQtOption("audioDriver", audioDriver);
147		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
148		emit audioDriverChanged();
149	}
150
151	QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
152	if (displayDriver != m_controller->getQtOption("displayDriver")) {
153		m_controller->setQtOption("displayDriver", displayDriver);
154		Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
155		emit displayDriverChanged();
156	}
157
158	m_controller->write();
159
160	emit biosLoaded(m_ui.bios->text());
161}
162
163void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
164	m_controller->setOption(key, field->isChecked());
165	m_controller->updateOption(key);
166}
167
168void SettingsView::saveSetting(const char* key, const QComboBox* field) {
169	saveSetting(key, field->lineEdit());
170}
171
172void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
173	saveSetting(key, field->value());
174}
175
176void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
177	saveSetting(key, field->text());
178}
179
180void SettingsView::saveSetting(const char* key, const QSlider* field) {
181	saveSetting(key, field->value());
182}
183
184void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
185	saveSetting(key, field->value());
186}
187
188void SettingsView::saveSetting(const char* key, const QVariant& field) {
189	m_controller->setOption(key, field);
190	m_controller->updateOption(key);
191}
192
193void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
194	QString option = loadSetting(key);
195	field->setChecked(!option.isNull() && option != "0");
196}
197
198void SettingsView::loadSetting(const char* key, QComboBox* field) {
199	loadSetting(key, field->lineEdit());
200}
201
202void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
203	QString option = loadSetting(key);
204	field->setValue(option.toDouble());
205}
206
207void SettingsView::loadSetting(const char* key, QLineEdit* field) {
208	QString option = loadSetting(key);
209	field->setText(option);
210}
211
212void SettingsView::loadSetting(const char* key, QSlider* field) {
213	QString option = loadSetting(key);
214	field->setValue(option.toInt());
215}
216
217void SettingsView::loadSetting(const char* key, QSpinBox* field) {
218	QString option = loadSetting(key);
219	field->setValue(option.toInt());
220}
221
222QString SettingsView::loadSetting(const char* key) {
223	return m_controller->getOption(key);
224}