all repos — mgba @ 5c699e9d6cbfbcd5768a876a8fdbb42f6cfaf9d6

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	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
146QVariant ConfigController::getQtOption(const QString& key, const QString& group) const {
147	if (!group.isNull()) {
148		m_settings->beginGroup(group);
149	}
150	QVariant value = m_settings->value(key);
151	if (!group.isNull()) {
152		m_settings->endGroup();
153	}
154	return value;
155}
156
157void ConfigController::setOption(const char* key, bool value) {
158	GBAConfigSetIntValue(&m_config, key, value);
159	QString optionName(key);
160	if (m_optionSet.contains(optionName)) {
161		m_optionSet[optionName]->setValue(value);
162	}
163}
164
165void ConfigController::setOption(const char* key, int value) {
166	GBAConfigSetIntValue(&m_config, key, value);
167	QString optionName(key);
168	if (m_optionSet.contains(optionName)) {
169		m_optionSet[optionName]->setValue(value);
170	}
171}
172
173void ConfigController::setOption(const char* key, unsigned value) {
174	GBAConfigSetUIntValue(&m_config, key, value);
175	QString optionName(key);
176	if (m_optionSet.contains(optionName)) {
177		m_optionSet[optionName]->setValue(value);
178	}
179}
180
181void ConfigController::setOption(const char* key, const char* value) {
182	GBAConfigSetValue(&m_config, key, value);
183	QString optionName(key);
184	if (m_optionSet.contains(optionName)) {
185		m_optionSet[optionName]->setValue(value);
186	}
187}
188
189void ConfigController::setOption(const char* key, const QVariant& value) {
190	if (value.type() == QVariant::Bool) {
191		setOption(key, value.toBool());
192		return;
193	}
194	QString stringValue(value.toString());
195	setOption(key, stringValue.toLocal8Bit().constData());
196}
197
198void ConfigController::setQtOption(const QString& key, const QVariant& value, const QString& group) {
199	if (!group.isNull()) {
200		m_settings->beginGroup(group);
201	}
202	m_settings->setValue(key, value);
203	if (!group.isNull()) {
204		m_settings->endGroup();
205	}
206}
207
208QList<QString> ConfigController::getMRU() const {
209	QList<QString> mru;
210	m_settings->beginGroup("mru");
211	for (int i = 0; i < MRU_LIST_SIZE; ++i) {
212		QString item = m_settings->value(QString::number(i)).toString();
213		if (item.isNull()) {
214			continue;
215		}
216		mru.append(item);
217	}
218	m_settings->endGroup();
219	return mru;
220}
221
222void ConfigController::setMRU(const QList<QString>& mru) {
223	int i = 0;
224	m_settings->beginGroup("mru");
225	for (const QString& item : mru) {
226		m_settings->setValue(QString::number(i), item);
227		++i;
228		if (i >= MRU_LIST_SIZE) {
229			break;
230		}
231	}
232	m_settings->endGroup();
233}
234
235void ConfigController::write() {
236	GBAConfigSave(&m_config);
237	m_settings->sync();
238}