Browse Source

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.
jp9000 11 years ago
parent
commit
3f262b8a4a
2 changed files with 41 additions and 18 deletions
  1. 38 18
      obs/window-basic-main.cpp
  2. 3 0
      obs/window-basic-main.hpp

+ 38 - 18
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<QAction*>(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<QMenu> popup = CreateAddSourcePopupMenu();
+	if (popup)
+		popup->exec(pos);
 }
 
 void OBSBasic::on_actionAddSource_triggered()

+ 3 - 0
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: