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