all repos — mgba @ 6066c6e3739ff46ace5f49eddcf2024180c9f74c

mGBA Game Boy Advance Emulator

src/platform/qt/ConfigController.cpp (view raw)

  1#include "ConfigController.h"
  2
  3#include "GameController.h"
  4
  5#include <QAction>
  6#include <QMenu>
  7
  8extern "C" {
  9#include "platform/commandline.h"
 10}
 11
 12using namespace QGBA;
 13
 14ConfigOption::ConfigOption(QObject* parent)
 15	: QObject(parent)
 16{
 17}
 18
 19void ConfigOption::connect(std::function<void(const QVariant&)> slot) {
 20	m_slot = slot;
 21}
 22
 23QAction* ConfigOption::addValue(const QString& text, const QVariant& value, QMenu* parent) {
 24	QAction* action = new QAction(text, parent);
 25	action->setCheckable(true);
 26	QObject::connect(action, &QAction::triggered, [this, value]() {
 27		emit valueChanged(value);
 28	});
 29	parent->addAction(action);
 30	m_actions.append(qMakePair(action, value));
 31	return action;
 32}
 33
 34QAction* ConfigOption::addValue(const QString& text, const char* value, QMenu* parent) {
 35	return addValue(text, QString(value), parent);
 36}
 37
 38QAction* ConfigOption::addBoolean(const QString& text, QMenu* parent) {
 39	QAction* action = new QAction(text, parent);
 40	action->setCheckable(true);
 41	QObject::connect(action, &QAction::triggered, [this, action]() {
 42		emit valueChanged(action->isChecked());
 43	});
 44	parent->addAction(action);
 45	m_actions.append(qMakePair(action, 1));
 46	return action;
 47}
 48
 49void ConfigOption::setValue(bool value) {
 50	setValue(QVariant(value));
 51}
 52
 53void ConfigOption::setValue(int value) {
 54	setValue(QVariant(value));
 55}
 56
 57void ConfigOption::setValue(unsigned value) {
 58	setValue(QVariant(value));
 59}
 60
 61void ConfigOption::setValue(const char* value) {
 62	setValue(QVariant(QString(value)));
 63}
 64
 65void ConfigOption::setValue(const QVariant& value) {
 66	QPair<QAction*, QVariant> action;
 67	foreach(action, m_actions) {
 68		bool signalsEnabled = action.first->blockSignals(true);
 69		action.first->setChecked(value == action.second);
 70		action.first->blockSignals(signalsEnabled);
 71	}
 72	m_slot(value);
 73}
 74
 75ConfigController::ConfigController(QObject* parent)
 76	: QObject(parent)
 77	, m_opts()
 78{
 79	GBAConfigInit(&m_config, PORT);
 80
 81	m_opts.audioSync = GameController::AUDIO_SYNC;
 82	m_opts.videoSync = GameController::VIDEO_SYNC;
 83	m_opts.fpsTarget = 60;
 84	m_opts.audioBuffers = 768;
 85	GBAConfigLoadDefaults(&m_config, &m_opts);
 86	GBAConfigLoad(&m_config);
 87	GBAConfigMap(&m_config, &m_opts);
 88}
 89
 90ConfigController::~ConfigController() {
 91	write();
 92
 93	GBAConfigDeinit(&m_config);
 94	GBAConfigFreeOpts(&m_opts);
 95}
 96
 97bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[]) {
 98	return ::parseArguments(args, &m_config, argc, argv, 0);
 99}
100
101ConfigOption* ConfigController::addOption(const char* key) {
102	QString optionName(key);
103
104	if (m_optionSet.contains(optionName)) {
105		return m_optionSet[optionName];
106	}
107	ConfigOption* newOption = new ConfigOption(this);
108	m_optionSet[optionName] = newOption;
109	connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
110		setOption(key, value);
111	});
112	return newOption;
113}
114
115void ConfigController::updateOption(const char* key) {
116	if (!key) {
117		return;
118	}
119
120	QString optionName(key);
121
122	if (!m_optionSet.contains(optionName)) {
123		return;
124	}
125	m_optionSet[optionName]->setValue(GBAConfigGetValue(&m_config, key));
126}
127
128void ConfigController::setOption(const char* key, bool value) {
129	GBAConfigSetIntValue(&m_config, key, value);
130	ConfigOption* option = m_optionSet[QString(key)];
131	if (option) {
132		option->setValue(value);
133	}
134}
135
136void ConfigController::setOption(const char* key, int value) {
137	GBAConfigSetIntValue(&m_config, key, value);
138	ConfigOption* option = m_optionSet[QString(key)];
139	if (option) {
140		option->setValue(value);
141	}
142}
143
144void ConfigController::setOption(const char* key, unsigned value) {
145	GBAConfigSetUIntValue(&m_config, key, value);
146	ConfigOption* option = m_optionSet[QString(key)];
147	if (option) {
148		option->setValue(value);
149	}
150}
151
152void ConfigController::setOption(const char* key, const char* value) {
153	GBAConfigSetValue(&m_config, key, value);
154	ConfigOption* option = m_optionSet[QString(key)];
155	if (option) {
156		option->setValue(value);
157	}
158}
159
160void ConfigController::setOption(const char* key, const QVariant& value) {
161	if (value.type() == QVariant::Bool) {
162		setOption(key, value.toBool());
163		return;
164	}
165	QString stringValue(value.toString());
166	setOption(key, stringValue.toLocal8Bit().constData());
167}
168
169void ConfigController::write() {
170	GBAConfigSave(&m_config);
171}