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
11#include <QFileDialog>
12
13using namespace QGBA;
14
15SettingsView::SettingsView(ConfigController* controller, QWidget* parent)
16 : QWidget(parent)
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("videoSync", m_ui.videoSync);
26 loadSetting("audioSync", m_ui.audioSync);
27 loadSetting("frameskip", m_ui.frameskip);
28 loadSetting("fpsTarget", m_ui.fpsTarget);
29 loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
30 loadSetting("volume", m_ui.volume);
31 loadSetting("mute", m_ui.mute);
32 loadSetting("rewindEnable", m_ui.rewind);
33 loadSetting("rewindBufferInterval", m_ui.rewindInterval);
34 loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
35 loadSetting("resampleVideo", m_ui.resampleVideo);
36 loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
37 loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
38
39 QString idleOptimization = loadSetting("idleOptimization");
40 if (idleOptimization == "ignore") {
41 m_ui.idleOptimization->setCurrentIndex(0);
42 } else if (idleOptimization == "remove") {
43 m_ui.idleOptimization->setCurrentIndex(1);
44 } else if (idleOptimization == "detect") {
45 m_ui.idleOptimization->setCurrentIndex(2);
46 }
47
48 QVariant audioDriver = m_controller->getQtOption("audioDriver");
49#ifdef BUILD_QT_MULTIMEDIA
50 m_ui.audioDriver->addItem(tr("Qt Multimedia"), static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA));
51 if (!audioDriver.isNull() && audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA)) {
52 m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
53 }
54#endif
55
56#ifdef BUILD_SDL
57 m_ui.audioDriver->addItem(tr("SDL"), static_cast<int>(AudioProcessor::Driver::SDL));
58 if (audioDriver.isNull() || audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::SDL)) {
59 m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
60 }
61#endif
62
63 connect(m_ui.biosBrowse, SIGNAL(clicked()), this, SLOT(selectBios()));
64 connect(m_ui.buttonBox, SIGNAL(accepted()), this, SLOT(updateConfig()));
65}
66
67void SettingsView::selectBios() {
68 QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"));
69 if (!filename.isEmpty()) {
70 m_ui.bios->setText(filename);
71 }
72}
73
74void SettingsView::updateConfig() {
75 saveSetting("bios", m_ui.bios);
76 saveSetting("useBios", m_ui.useBios);
77 saveSetting("skipBios", m_ui.skipBios);
78 saveSetting("audioBuffers", m_ui.audioBufferSize);
79 saveSetting("videoSync", m_ui.videoSync);
80 saveSetting("audioSync", m_ui.audioSync);
81 saveSetting("frameskip", m_ui.frameskip);
82 saveSetting("fpsTarget", m_ui.fpsTarget);
83 saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
84 saveSetting("volume", m_ui.volume);
85 saveSetting("mute", m_ui.mute);
86 saveSetting("rewindEnable", m_ui.rewind);
87 saveSetting("rewindBufferInterval", m_ui.rewindInterval);
88 saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
89 saveSetting("resampleVideo", m_ui.resampleVideo);
90 saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
91 saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
92
93 switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
94 case IDLE_LOOP_IGNORE:
95 saveSetting("idleOptimization", "ignore");
96 break;
97 case IDLE_LOOP_REMOVE:
98 saveSetting("idleOptimization", "remove");
99 break;
100 case IDLE_LOOP_DETECT:
101 saveSetting("idleOptimization", "detect");
102 break;
103 }
104
105 QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
106 if (audioDriver != m_controller->getQtOption("audioDriver")) {
107 m_controller->setQtOption("audioDriver", audioDriver);
108 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
109 emit audioDriverChanged();
110 }
111
112 m_controller->write();
113
114 emit biosLoaded(m_ui.bios->text());
115}
116
117void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
118 m_controller->setOption(key, field->isChecked());
119 m_controller->updateOption(key);
120}
121
122void SettingsView::saveSetting(const char* key, const QComboBox* field) {
123 saveSetting(key, field->lineEdit());
124}
125
126void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
127 saveSetting(key, field->text());
128}
129
130void SettingsView::saveSetting(const char* key, const QSlider* field) {
131 saveSetting(key, QString::number(field->value()));
132}
133
134void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
135 saveSetting(key, field->cleanText());
136}
137
138void SettingsView::saveSetting(const char* key, const QString& field) {
139 m_controller->setOption(key, field);
140 m_controller->updateOption(key);
141}
142
143void SettingsView::loadSetting(const char* key, QAbstractButton* field) {
144 QString option = loadSetting(key);
145 field->setChecked(!option.isNull() && option != "0");
146}
147
148void SettingsView::loadSetting(const char* key, QComboBox* field) {
149 loadSetting(key, field->lineEdit());
150}
151
152void SettingsView::loadSetting(const char* key, QLineEdit* field) {
153 QString option = loadSetting(key);
154 field->setText(option);
155}
156
157void SettingsView::loadSetting(const char* key, QSlider* field) {
158 QString option = loadSetting(key);
159 field->setValue(option.toInt());
160}
161
162void SettingsView::loadSetting(const char* key, QSpinBox* field) {
163 QString option = loadSetting(key);
164 field->setValue(option.toInt());
165}
166
167QString SettingsView::loadSetting(const char* key) {
168 return m_controller->getOption(key);
169}