all repos — mgba @ 6d23328bdae0698eac57d2ab68c6ba6c02a396b3

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 = 768;
 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
133void ConfigController::setOption(const char* key, bool value) {
134	GBAConfigSetIntValue(&m_config, key, value);
135	ConfigOption* option = m_optionSet[QString(key)];
136	if (option) {
137		option->setValue(value);
138	}
139}
140
141void ConfigController::setOption(const char* key, int value) {
142	GBAConfigSetIntValue(&m_config, key, value);
143	ConfigOption* option = m_optionSet[QString(key)];
144	if (option) {
145		option->setValue(value);
146	}
147}
148
149void ConfigController::setOption(const char* key, unsigned value) {
150	GBAConfigSetUIntValue(&m_config, key, value);
151	ConfigOption* option = m_optionSet[QString(key)];
152	if (option) {
153		option->setValue(value);
154	}
155}
156
157void ConfigController::setOption(const char* key, const char* value) {
158	GBAConfigSetValue(&m_config, key, value);
159	ConfigOption* option = m_optionSet[QString(key)];
160	if (option) {
161		option->setValue(value);
162	}
163}
164
165void ConfigController::setOption(const char* key, const QVariant& value) {
166	if (value.type() == QVariant::Bool) {
167		setOption(key, value.toBool());
168		return;
169	}
170	QString stringValue(value.toString());
171	setOption(key, stringValue.toLocal8Bit().constData());
172}
173
174void ConfigController::write() {
175	GBAConfigSave(&m_config);
176}