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