Qt: Make ConfigOption able to handle multiple consumers

This commit is contained in:
Jeffrey Pfau 2015-03-19 22:11:23 -07:00
parent e6377f2e6a
commit ae2b20e476
3 changed files with 53 additions and 19 deletions

View File

@ -23,8 +23,11 @@ ConfigOption::ConfigOption(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 @@ QAction* ConfigOption::addValue(const QString& text, const QVariant& value, QMen
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;
@ -48,6 +54,9 @@ QAction* ConfigOption::addBoolean(const QString& text, QMenu* parent) {
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));
return action;
@ -76,7 +85,10 @@ void ConfigOption::setValue(const QVariant& value) {
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)

View File

@ -32,11 +32,11 @@ Q_OBJECT
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;
};

View File

@ -601,12 +601,16 @@ void Window::setupMenu(QMenuBar* menubar) {
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 @@ void Window::setupMenu(QMenuBar* menubar) {
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 @@ void Window::setupMenu(QMenuBar* menubar) {
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 @@ void Window::setupMenu(QMenuBar* menubar) {
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 @@ void Window::setupMenu(QMenuBar* menubar) {
#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);