src/platform/qt/ConfigController.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 "ConfigController.h"
7
8#include "GameController.h"
9
10#include <QAction>
11#include <QMenu>
12
13extern "C" {
14#include "platform/commandline.h"
15}
16
17using namespace QGBA;
18
19ConfigOption::ConfigOption(QObject* parent)
20 : QObject(parent)
21{
22}
23
24void ConfigOption::connect(std::function<void(const QVariant&)> slot) {
25 m_slot = slot;
26}
27
28QAction* ConfigOption::addValue(const QString& text, const QVariant& value, QMenu* parent) {
29 QAction* action = new QAction(text, parent);
30 action->setCheckable(true);
31 QObject::connect(action, &QAction::triggered, [this, value]() {
32 emit valueChanged(value);
33 });
34 parent->addAction(action);
35 m_actions.append(qMakePair(action, value));
36 return action;
37}
38
39QAction* ConfigOption::addValue(const QString& text, const char* value, QMenu* parent) {
40 return addValue(text, QString(value), parent);
41}
42
43QAction* ConfigOption::addBoolean(const QString& text, QMenu* parent) {
44 QAction* action = new QAction(text, parent);
45 action->setCheckable(true);
46 QObject::connect(action, &QAction::triggered, [this, action]() {
47 emit valueChanged(action->isChecked());
48 });
49 parent->addAction(action);
50 m_actions.append(qMakePair(action, 1));
51 return action;
52}
53
54void ConfigOption::setValue(bool value) {
55 setValue(QVariant(value));
56}
57
58void ConfigOption::setValue(int value) {
59 setValue(QVariant(value));
60}
61
62void ConfigOption::setValue(unsigned value) {
63 setValue(QVariant(value));
64}
65
66void ConfigOption::setValue(const char* value) {
67 setValue(QVariant(QString(value)));
68}
69
70void ConfigOption::setValue(const QVariant& value) {
71 QPair<QAction*, QVariant> action;
72 foreach(action, m_actions) {
73 bool signalsEnabled = action.first->blockSignals(true);
74 action.first->setChecked(value == action.second);
75 action.first->blockSignals(signalsEnabled);
76 }
77 m_slot(value);
78}
79
80ConfigController::ConfigController(QObject* parent)
81 : QObject(parent)
82 , m_opts()
83{
84 GBAConfigInit(&m_config, PORT);
85
86 m_opts.audioSync = GameController::AUDIO_SYNC;
87 m_opts.videoSync = GameController::VIDEO_SYNC;
88 m_opts.fpsTarget = 60;
89 m_opts.audioBuffers = 2048;
90 m_opts.logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
91 GBAConfigLoadDefaults(&m_config, &m_opts);
92 GBAConfigLoad(&m_config);
93 GBAConfigMap(&m_config, &m_opts);
94}
95
96ConfigController::~ConfigController() {
97 write();
98
99 GBAConfigDeinit(&m_config);
100 GBAConfigFreeOpts(&m_opts);
101}
102
103bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[]) {
104 return ::parseArguments(args, &m_config, argc, argv, 0);
105}
106
107ConfigOption* ConfigController::addOption(const char* key) {
108 QString optionName(key);
109
110 if (m_optionSet.contains(optionName)) {
111 return m_optionSet[optionName];
112 }
113 ConfigOption* newOption = new ConfigOption(this);
114 m_optionSet[optionName] = newOption;
115 connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
116 setOption(key, value);
117 });
118 return newOption;
119}
120
121void ConfigController::updateOption(const char* key) {
122 if (!key) {
123 return;
124 }
125
126 QString optionName(key);
127
128 if (!m_optionSet.contains(optionName)) {
129 return;
130 }
131 m_optionSet[optionName]->setValue(GBAConfigGetValue(&m_config, key));
132}
133
134QString ConfigController::getOption(const char* key) const {
135 return QString(GBAConfigGetValue(&m_config, key));
136}
137
138void ConfigController::setOption(const char* key, bool value) {
139 GBAConfigSetIntValue(&m_config, key, value);
140 QString optionName(key);
141 if (m_optionSet.contains(optionName)) {
142 m_optionSet[optionName]->setValue(value);
143 }
144}
145
146void ConfigController::setOption(const char* key, int value) {
147 GBAConfigSetIntValue(&m_config, key, value);
148 QString optionName(key);
149 if (m_optionSet.contains(optionName)) {
150 m_optionSet[optionName]->setValue(value);
151 }
152}
153
154void ConfigController::setOption(const char* key, unsigned value) {
155 GBAConfigSetUIntValue(&m_config, key, value);
156 QString optionName(key);
157 if (m_optionSet.contains(optionName)) {
158 m_optionSet[optionName]->setValue(value);
159 }
160}
161
162void ConfigController::setOption(const char* key, const char* value) {
163 GBAConfigSetValue(&m_config, key, value);
164 QString optionName(key);
165 if (m_optionSet.contains(optionName)) {
166 m_optionSet[optionName]->setValue(value);
167 }
168}
169
170void ConfigController::setOption(const char* key, const QVariant& value) {
171 if (value.type() == QVariant::Bool) {
172 setOption(key, value.toBool());
173 return;
174 }
175 QString stringValue(value.toString());
176 setOption(key, stringValue.toLocal8Bit().constData());
177}
178
179QList<QString> ConfigController::getMRU() const {
180 QList<QString> mru;
181 for (int i = 0; i < MRU_LIST_SIZE; ++i) {
182 char mruName[7];
183 snprintf(mruName, sizeof(mruName) - 1, "mru.%i", i);
184 mruName[sizeof(mruName) - 1] = '\0';
185 QString item = getOption(mruName);
186 if (item.isNull()) {
187 continue;
188 }
189 mru.append(item);
190 }
191 return mru;
192}
193
194void ConfigController::setMRU(const QList<QString>& mru) {
195 int i = 0;
196 for (const QString& item : mru) {
197 char mruName[7];
198 snprintf(mruName, sizeof(mruName) - 1, "mru.%i", i);
199 mruName[sizeof(mruName) - 1] = '\0';
200 setOption(mruName, item);
201 ++i;
202 if (i >= MRU_LIST_SIZE) {
203 break;
204 }
205 }
206}
207
208void ConfigController::write() {
209 GBAConfigSave(&m_config);
210}