From 3f262b8a4a2f39f2bbabd05cea1459468c652899 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Sun, 29 Jun 2014 17:33:40 -0700 Subject: [PATCH] Use signal/slot when creating a source via popup I realized that there's no other way to share the menu if I want to add it as a sub-menu somewhere else. --- obs/window-basic-main.cpp | 56 ++++++++++++++++++++++++++------------- obs/window-basic-main.hpp | 3 +++ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/obs/window-basic-main.cpp b/obs/window-basic-main.cpp index 666d77de5..22532654d 100644 --- a/obs/window-basic-main.cpp +++ b/obs/window-basic-main.cpp @@ -1278,26 +1278,19 @@ void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos) void OBSBasic::AddSource(const char *id) { - OBSBasicSourceSelect sourceSelect(this, id); - sourceSelect.exec(); + if (id && *id) { + OBSBasicSourceSelect sourceSelect(this, id); + sourceSelect.exec(); + } } -void OBSBasic::AddSourcePopupMenu(const QPoint &pos) +QMenu *OBSBasic::CreateAddSourcePopupMenu() { const char *type; bool foundValues = false; size_t idx = 0; - if (!GetCurrentScene()) { - // Tell the user he needs a scene first (help beginners). - QMessageBox::information(this, - QTStr("Basic.Main.AddSourceHelp.Title"), - QTStr("Basic.Main.AddSourceHelp.Text")); - return; - } - - - QMenu popup; + QMenu *popup = new QMenu; while (obs_enum_input_types(idx++, &type)) { const char *name = obs_source_getdisplayname( OBS_SOURCE_TYPE_INPUT, type); @@ -1307,16 +1300,43 @@ void OBSBasic::AddSourcePopupMenu(const QPoint &pos) QAction *popupItem = new QAction(QT_UTF8(name), this); popupItem->setData(QT_UTF8(type)); - popup.addAction(popupItem); + connect(popupItem, SIGNAL(triggered(bool)), + this, SLOT(AddSourceFromAction())); + popup->addAction(popupItem); foundValues = true; } - if (foundValues) { - QAction *ret = popup.exec(pos); - if (ret) - AddSource(ret->data().toString().toUtf8()); + if (!foundValues) { + delete popup; + popup = nullptr; } + + return popup; +} + +void OBSBasic::AddSourceFromAction() +{ + QAction *action = qobject_cast(sender()); + if (!action) + return; + + AddSource(QT_TO_UTF8(action->data().toString())); +} + +void OBSBasic::AddSourcePopupMenu(const QPoint &pos) +{ + if (!GetCurrentScene()) { + // Tell the user he needs a scene first (help beginners). + QMessageBox::information(this, + QTStr("Basic.Main.AddSourceHelp.Title"), + QTStr("Basic.Main.AddSourceHelp.Text")); + return; + } + + QPointer popup = CreateAddSourcePopupMenu(); + if (popup) + popup->exec(pos); } void OBSBasic::on_actionAddSource_triggered() diff --git a/obs/window-basic-main.hpp b/obs/window-basic-main.hpp index 7e2b3c3d6..c6d27d09b 100644 --- a/obs/window-basic-main.hpp +++ b/obs/window-basic-main.hpp @@ -144,6 +144,8 @@ private slots: void ActivateAudioSource(OBSSource source); void DeactivateAudioSource(OBSSource source); + void AddSourceFromAction(); + private: /* OBS Callbacks */ static void SceneItemAdded(void *data, calldata_t params); @@ -163,6 +165,7 @@ private: void ResizePreview(uint32_t cx, uint32_t cy); void AddSource(const char *id); + QMenu *CreateAddSourcePopupMenu(); void AddSourcePopupMenu(const QPoint &pos); public: