all repos — mgba @ 7c8f1d9726eb246aac895fe90297a0ecd1ebe13a

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 <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	GBAConfigLoadDefaults(&m_config, &m_opts);
 91	GBAConfigLoad(&m_config);
 92	GBAConfigMap(&m_config, &m_opts);
 93}
 94
 95ConfigController::~ConfigController() {
 96	write();
 97
 98	GBAConfigDeinit(&m_config);
 99	GBAConfigFreeOpts(&m_opts);
100}
101
102bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[]) {
103	return ::parseArguments(args, &m_config, argc, argv, 0);
104}
105
106ConfigOption* ConfigController::addOption(const char* key) {
107	QString optionName(key);
108
109	if (m_optionSet.contains(optionName)) {
110		return m_optionSet[optionName];
111	}
112	ConfigOption* newOption = new ConfigOption(this);
113	m_optionSet[optionName] = newOption;
114	connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
115		setOption(key, value);
116	});
117	return newOption;
118}
119
120void ConfigController::updateOption(const char* key) {
121	if (!key) {
122		return;
123	}
124
125	QString optionName(key);
126
127	if (!m_optionSet.contains(optionName)) {
128		return;
129	}
130	m_optionSet[optionName]->setValue(GBAConfigGetValue(&m_config, key));
131}
132
133QString ConfigController::getOption(const char* key) {
134	return QString(GBAConfigGetValue(&m_config, key));
135}
136
137void ConfigController::setOption(const char* key, bool value) {
138	GBAConfigSetIntValue(&m_config, key, value);
139	QString optionName(key);
140	if (m_optionSet.contains(optionName)) {
141		m_optionSet[optionName]->setValue(value);
142	}
143}
144
145void ConfigController::setOption(const char* key, int value) {
146	GBAConfigSetIntValue(&m_config, key, value);
147	QString optionName(key);
148	if (m_optionSet.contains(optionName)) {
149		m_optionSet[optionName]->setValue(value);
150	}
151}
152
153void ConfigController::setOption(const char* key, unsigned value) {
154	GBAConfigSetUIntValue(&m_config, key, value);
155	QString optionName(key);
156	if (m_optionSet.contains(optionName)) {
157		m_optionSet[optionName]->setValue(value);
158	}
159}
160
161void ConfigController::setOption(const char* key, const char* value) {
162	GBAConfigSetValue(&m_config, key, value);
163	QString optionName(key);
164	if (m_optionSet.contains(optionName)) {
165		m_optionSet[optionName]->setValue(value);
166	}
167}
168
169void ConfigController::setOption(const char* key, const QVariant& value) {
170	if (value.type() == QVariant::Bool) {
171		setOption(key, value.toBool());
172		return;
173	}
174	QString stringValue(value.toString());
175	setOption(key, stringValue.toLocal8Bit().constData());
176}
177
178void ConfigController::write() {
179	GBAConfigSave(&m_config);
180}