all repos — mgba @ b2cceffdae98d4b74590b19303dc68f3758f325b

mGBA Game Boy Advance Emulator

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	m_opts.rewindEnable = false;
100	m_opts.rewindBufferInterval = 0;
101	m_opts.rewindBufferCapacity = 0;
102	GBAConfigLoadDefaults(&m_config, &m_opts);
103	GBAConfigLoad(&m_config);
104	GBAConfigMap(&m_config, &m_opts);
105}
106
107ConfigController::~ConfigController() {
108	write();
109
110	GBAConfigDeinit(&m_config);
111	GBAConfigFreeOpts(&m_opts);
112}
113
114bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[]) {
115	return ::parseArguments(args, &m_config, argc, argv, 0);
116}
117
118ConfigOption* ConfigController::addOption(const char* key) {
119	QString optionName(key);
120
121	if (m_optionSet.contains(optionName)) {
122		return m_optionSet[optionName];
123	}
124	ConfigOption* newOption = new ConfigOption(this);
125	m_optionSet[optionName] = newOption;
126	connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
127		setOption(key, value);
128	});
129	return newOption;
130}
131
132void ConfigController::updateOption(const char* key) {
133	if (!key) {
134		return;
135	}
136
137	QString optionName(key);
138
139	if (!m_optionSet.contains(optionName)) {
140		return;
141	}
142	m_optionSet[optionName]->setValue(GBAConfigGetValue(&m_config, key));
143}
144
145QString ConfigController::getOption(const char* key) const {
146	return QString(GBAConfigGetValue(&m_config, key));
147}
148
149QVariant ConfigController::getQtOption(const QString& key, const QString& group) const {
150	if (!group.isNull()) {
151		m_settings->beginGroup(group);
152	}
153	QVariant value = m_settings->value(key);
154	if (!group.isNull()) {
155		m_settings->endGroup();
156	}
157	return value;
158}
159
160void ConfigController::setOption(const char* key, bool value) {
161	GBAConfigSetIntValue(&m_config, key, value);
162	QString optionName(key);
163	if (m_optionSet.contains(optionName)) {
164		m_optionSet[optionName]->setValue(value);
165	}
166}
167
168void ConfigController::setOption(const char* key, int value) {
169	GBAConfigSetIntValue(&m_config, key, value);
170	QString optionName(key);
171	if (m_optionSet.contains(optionName)) {
172		m_optionSet[optionName]->setValue(value);
173	}
174}
175
176void ConfigController::setOption(const char* key, unsigned value) {
177	GBAConfigSetUIntValue(&m_config, key, value);
178	QString optionName(key);
179	if (m_optionSet.contains(optionName)) {
180		m_optionSet[optionName]->setValue(value);
181	}
182}
183
184void ConfigController::setOption(const char* key, const char* value) {
185	GBAConfigSetValue(&m_config, key, value);
186	QString optionName(key);
187	if (m_optionSet.contains(optionName)) {
188		m_optionSet[optionName]->setValue(value);
189	}
190}
191
192void ConfigController::setOption(const char* key, const QVariant& value) {
193	if (value.type() == QVariant::Bool) {
194		setOption(key, value.toBool());
195		return;
196	}
197	QString stringValue(value.toString());
198	setOption(key, stringValue.toLocal8Bit().constData());
199}
200
201void ConfigController::setQtOption(const QString& key, const QVariant& value, const QString& group) {
202	if (!group.isNull()) {
203		m_settings->beginGroup(group);
204	}
205	m_settings->setValue(key, value);
206	if (!group.isNull()) {
207		m_settings->endGroup();
208	}
209}
210
211QList<QString> ConfigController::getMRU() const {
212	QList<QString> mru;
213	m_settings->beginGroup("mru");
214	for (int i = 0; i < MRU_LIST_SIZE; ++i) {
215		QString item = m_settings->value(QString::number(i)).toString();
216		if (item.isNull()) {
217			continue;
218		}
219		mru.append(item);
220	}
221	m_settings->endGroup();
222	return mru;
223}
224
225void ConfigController::setMRU(const QList<QString>& mru) {
226	int i = 0;
227	m_settings->beginGroup("mru");
228	for (const QString& item : mru) {
229		m_settings->setValue(QString::number(i), item);
230		++i;
231		if (i >= MRU_LIST_SIZE) {
232			break;
233		}
234	}
235	m_settings->endGroup();
236}
237
238void ConfigController::write() {
239	GBAConfigSave(&m_config);
240	m_settings->sync();
241}