all repos — mgba @ ae2b20e47648380a5e9e502fa4614785c1556659

mGBA Game Boy Advance Emulator

Qt: Make ConfigOption able to handle multiple consumers
Jeffrey Pfau jeffrey@endrift.com
Thu, 19 Mar 2015 22:11:23 -0700
commit

ae2b20e47648380a5e9e502fa4614785c1556659

parent

e6377f2e6a8902a780f8caa4d40e1768963eb12d

M src/platform/qt/ConfigController.cppsrc/platform/qt/ConfigController.cpp

@@ -23,8 +23,11 @@ : QObject(parent)

{ } -void ConfigOption::connect(std::function<void(const QVariant&)> slot) { - m_slot = slot; +void ConfigOption::connect(std::function<void(const QVariant&)> slot, QObject* parent) { + m_slots[parent] = slot; + QObject::connect(parent, &QAction::destroyed, [this, slot, parent]() { + m_slots.remove(parent); + }); } QAction* ConfigOption::addValue(const QString& text, const QVariant& value, QMenu* parent) {

@@ -33,6 +36,9 @@ action->setCheckable(true);

QObject::connect(action, &QAction::triggered, [this, value]() { emit valueChanged(value); }); + QObject::connect(parent, &QAction::destroyed, [this, action, value]() { + m_actions.removeAll(qMakePair(action, value)); + }); parent->addAction(action); m_actions.append(qMakePair(action, value)); return action;

@@ -47,6 +53,9 @@ QAction* action = new QAction(text, parent);

action->setCheckable(true); QObject::connect(action, &QAction::triggered, [this, action]() { emit valueChanged(action->isChecked()); + }); + QObject::connect(parent, &QAction::destroyed, [this, action]() { + m_actions.removeAll(qMakePair(action, 1)); }); parent->addAction(action); m_actions.append(qMakePair(action, 1));

@@ -76,7 +85,10 @@ bool signalsEnabled = action.first->blockSignals(true);

action.first->setChecked(value == action.second); action.first->blockSignals(signalsEnabled); } - m_slot(value); + std::function<void(const QVariant&)> slot; + foreach(slot, m_slots.values()) { + slot(value); + } } ConfigController::ConfigController(QObject* parent)
M src/platform/qt/ConfigController.hsrc/platform/qt/ConfigController.h

@@ -32,11 +32,11 @@

public: ConfigOption(QObject* parent = nullptr); - void connect(std::function<void(const QVariant&)>); + void connect(std::function<void(const QVariant&)>, QObject* parent = nullptr); - QAction* addValue(const QString& text, const QVariant& value, QMenu* parent = 0); - QAction* addValue(const QString& text, const char* value, QMenu* parent = 0); - QAction* addBoolean(const QString& text, QMenu* parent = 0); + QAction* addValue(const QString& text, const QVariant& value, QMenu* parent = nullptr); + QAction* addValue(const QString& text, const char* value, QMenu* parent = nullptr); + QAction* addBoolean(const QString& text, QMenu* parent = nullptr); public slots: void setValue(bool value);

@@ -49,7 +49,7 @@ signals:

void valueChanged(const QVariant& value); private: - std::function<void(const QVariant&)> m_slot; + QMap<QObject*, std::function<void(const QVariant&)>> m_slots; QList<QPair<QAction*, QVariant>> m_actions; };
M src/platform/qt/Window.cppsrc/platform/qt/Window.cpp

@@ -601,12 +601,16 @@ addControlledAction(emulationMenu, rewind, "rewind");

ConfigOption* videoSync = m_config->addOption("videoSync"); videoSync->addBoolean(tr("Sync to &video"), emulationMenu); - videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); }); + videoSync->connect([this](const QVariant& value) { + m_controller->setVideoSync(value.toBool()); + }, this); m_config->updateOption("videoSync"); ConfigOption* audioSync = m_config->addOption("audioSync"); audioSync->addBoolean(tr("Sync to &audio"), emulationMenu); - audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); }); + audioSync->connect([this](const QVariant& value) { + m_controller->setAudioSync(value.toBool()); + }, this); m_config->updateOption("audioSync"); QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));

@@ -625,17 +629,23 @@ addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");

ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio"); lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu); - lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); }); + lockAspectRatio->connect([this](const QVariant& value) { + m_display->lockAspectRatio(value.toBool()); + }, this); m_config->updateOption("lockAspectRatio"); ConfigOption* resampleVideo = m_config->addOption("resampleVideo"); resampleVideo->addBoolean(tr("Resample video"), avMenu); - resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); }); + resampleVideo->connect([this](const QVariant& value) { + m_display->filter(value.toBool()); + }, this); m_config->updateOption("resampleVideo"); QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip")); ConfigOption* skip = m_config->addOption("frameskip"); - skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); }); + skip->connect([this](const QVariant& value) { + m_controller->setFrameskip(value.toInt()); + }, this); for (int i = 0; i <= 10; ++i) { skip->addValue(QString::number(i), i, skipMenu); }

@@ -645,7 +655,9 @@ avMenu->addSeparator();

QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size")); ConfigOption* buffers = m_config->addOption("audioBuffers"); - buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); }); + buffers->connect([this](const QVariant& value) { + emit audioBufferSamplesChanged(value.toInt()); + }, this); buffers->addValue(tr("512"), 512, buffersMenu); buffers->addValue(tr("768"), 768, buffersMenu); buffers->addValue(tr("1024"), 1024, buffersMenu);

@@ -657,7 +669,9 @@ avMenu->addSeparator();

QMenu* target = avMenu->addMenu("FPS target"); ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget"); - fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); }); + fpsTargetOption->connect([this](const QVariant& value) { + emit fpsTargetChanged(value.toInt()); + }, this); fpsTargetOption->addValue(tr("15"), 15, target); fpsTargetOption->addValue(tr("30"), 30, target); fpsTargetOption->addValue(tr("45"), 45, target);

@@ -749,16 +763,24 @@ addControlledAction(toolsMenu, gamepad, "remapGamepad");

#endif ConfigOption* skipBios = m_config->addOption("skipBios"); - skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); }); + skipBios->connect([this](const QVariant& value) { + m_controller->setSkipBIOS(value.toBool()); + }, this); ConfigOption* rewindEnable = m_config->addOption("rewindEnable"); - rewindEnable->connect([this](const QVariant& value) { m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt()); }); + rewindEnable->connect([this](const QVariant& value) { + m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt()); + }, this); ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity"); - rewindBufferCapacity->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt()); }); + rewindBufferCapacity->connect([this](const QVariant& value) { + m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt()); + }, this); ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval"); - rewindBufferInterval->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt()); }); + rewindBufferInterval->connect([this](const QVariant& value) { + m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt()); + }, this); QMenu* other = new QMenu(tr("Other"), this); m_shortcutController->addMenu(other);