window-basic-source-select.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <QMessageBox>
  15. #include "window-basic-main.hpp"
  16. #include "window-basic-source-select.hpp"
  17. #include "qt-wrappers.hpp"
  18. #include "obs-app.hpp"
  19. struct AddSourceData {
  20. obs_source_t *source;
  21. bool visible;
  22. };
  23. bool OBSBasicSourceSelect::EnumSources(void *data, obs_source_t *source)
  24. {
  25. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  26. const char *name = obs_source_get_name(source);
  27. const char *id = obs_source_get_id(source);
  28. if (strcmp(id, window->id) == 0)
  29. window->ui->sourceList->addItem(QT_UTF8(name));
  30. return true;
  31. }
  32. bool OBSBasicSourceSelect::EnumGroups(void *data, obs_source_t *source)
  33. {
  34. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  35. const char *name = obs_source_get_name(source);
  36. const char *id = obs_source_get_id(source);
  37. if (strcmp(id, window->id) == 0) {
  38. OBSBasic *main = reinterpret_cast<OBSBasic*>(
  39. App()->GetMainWindow());
  40. OBSScene scene = main->GetCurrentScene();
  41. obs_sceneitem_t *existing = obs_scene_get_group(scene, name);
  42. if (!existing)
  43. window->ui->sourceList->addItem(QT_UTF8(name));
  44. }
  45. return true;
  46. }
  47. void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t *calldata)
  48. {
  49. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  50. obs_source_t *source = (obs_source_t*)calldata_ptr(calldata, "source");
  51. QMetaObject::invokeMethod(window, "SourceAdded",
  52. Q_ARG(OBSSource, source));
  53. }
  54. void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t *calldata)
  55. {
  56. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  57. obs_source_t *source = (obs_source_t*)calldata_ptr(calldata, "source");
  58. QMetaObject::invokeMethod(window, "SourceRemoved",
  59. Q_ARG(OBSSource, source));
  60. }
  61. void OBSBasicSourceSelect::SourceAdded(OBSSource source)
  62. {
  63. const char *name = obs_source_get_name(source);
  64. const char *sourceId = obs_source_get_id(source);
  65. if (strcmp(sourceId, id) != 0)
  66. return;
  67. ui->sourceList->addItem(name);
  68. }
  69. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  70. {
  71. const char *name = obs_source_get_name(source);
  72. const char *sourceId = obs_source_get_id(source);
  73. if (strcmp(sourceId, id) != 0)
  74. return;
  75. QList<QListWidgetItem*> items =
  76. ui->sourceList->findItems(name, Qt::MatchFixedString);
  77. if (!items.count())
  78. return;
  79. delete items[0];
  80. }
  81. static void AddSource(void *_data, obs_scene_t *scene)
  82. {
  83. AddSourceData *data = (AddSourceData *)_data;
  84. obs_sceneitem_t *sceneitem;
  85. sceneitem = obs_scene_add(scene, data->source);
  86. obs_sceneitem_set_visible(sceneitem, data->visible);
  87. }
  88. static char *get_new_source_name(const char *name)
  89. {
  90. struct dstr new_name = {0};
  91. int inc = 0;
  92. dstr_copy(&new_name, name);
  93. for (;;) {
  94. obs_source_t *existing_source = obs_get_source_by_name(
  95. new_name.array);
  96. if (!existing_source)
  97. break;
  98. obs_source_release(existing_source);
  99. dstr_printf(&new_name, "%s %d", name, ++inc + 1);
  100. }
  101. return new_name.array;
  102. }
  103. static void AddExisting(const char *name, bool visible, bool duplicate)
  104. {
  105. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  106. OBSScene scene = main->GetCurrentScene();
  107. if (!scene)
  108. return;
  109. obs_source_t *source = obs_get_source_by_name(name);
  110. if (source) {
  111. if (duplicate) {
  112. obs_source_t *from = source;
  113. char *new_name = get_new_source_name(name);
  114. source = obs_source_duplicate(from, new_name, false);
  115. bfree(new_name);
  116. obs_source_release(from);
  117. if (!source)
  118. return;
  119. }
  120. AddSourceData data;
  121. data.source = source;
  122. data.visible = visible;
  123. obs_enter_graphics();
  124. obs_scene_atomic_update(scene, AddSource, &data);
  125. obs_leave_graphics();
  126. obs_source_release(source);
  127. }
  128. }
  129. bool AddNew(QWidget *parent, const char *id, const char *name,
  130. const bool visible, OBSSource &newSource)
  131. {
  132. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  133. OBSScene scene = main->GetCurrentScene();
  134. bool success = false;
  135. if (!scene)
  136. return false;
  137. obs_source_t *source = obs_get_source_by_name(name);
  138. if (source) {
  139. OBSMessageBox::information(parent,
  140. QTStr("NameExists.Title"),
  141. QTStr("NameExists.Text"));
  142. } else {
  143. source = obs_source_create(id, name, NULL, nullptr);
  144. if (source) {
  145. AddSourceData data;
  146. data.source = source;
  147. data.visible = visible;
  148. obs_enter_graphics();
  149. obs_scene_atomic_update(scene, AddSource, &data);
  150. obs_leave_graphics();
  151. newSource = source;
  152. success = true;
  153. }
  154. }
  155. obs_source_release(source);
  156. return success;
  157. }
  158. void OBSBasicSourceSelect::on_buttonBox_accepted()
  159. {
  160. bool useExisting = ui->selectExisting->isChecked();
  161. bool visible = ui->sourceVisible->isChecked();
  162. if (useExisting) {
  163. QListWidgetItem *item = ui->sourceList->currentItem();
  164. if (!item)
  165. return;
  166. AddExisting(QT_TO_UTF8(item->text()), visible, false);
  167. } else {
  168. if (ui->sourceName->text().isEmpty()) {
  169. OBSMessageBox::warning(this,
  170. QTStr("NoNameEntered.Title"),
  171. QTStr("NoNameEntered.Text"));
  172. return;
  173. }
  174. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),
  175. visible, newSource))
  176. return;
  177. }
  178. done(DialogCode::Accepted);
  179. }
  180. void OBSBasicSourceSelect::on_buttonBox_rejected()
  181. {
  182. done(DialogCode::Rejected);
  183. }
  184. static inline const char *GetSourceDisplayName(const char *id)
  185. {
  186. if (strcmp(id, "scene") == 0)
  187. return Str("Basic.Scene");
  188. return obs_source_get_display_name(id);
  189. }
  190. Q_DECLARE_METATYPE(OBSScene);
  191. template <typename T>
  192. static inline T GetOBSRef(QListWidgetItem *item)
  193. {
  194. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  195. }
  196. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_)
  197. : QDialog (parent),
  198. ui (new Ui::OBSBasicSourceSelect),
  199. id (id_)
  200. {
  201. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  202. ui->setupUi(this);
  203. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  204. QString placeHolderText{QT_UTF8(GetSourceDisplayName(id))};
  205. QString text{placeHolderText};
  206. int i = 2;
  207. obs_source_t *source = nullptr;
  208. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  209. obs_source_release(source);
  210. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  211. }
  212. ui->sourceName->setText(text);
  213. ui->sourceName->setFocus(); //Fixes deselect of text.
  214. ui->sourceName->selectAll();
  215. installEventFilter(CreateShortcutFilter());
  216. if (strcmp(id_, "scene") == 0) {
  217. OBSBasic *main = reinterpret_cast<OBSBasic*>(
  218. App()->GetMainWindow());
  219. OBSSource curSceneSource = main->GetCurrentSceneSource();
  220. ui->selectExisting->setChecked(true);
  221. ui->createNew->setChecked(false);
  222. ui->createNew->setEnabled(false);
  223. ui->sourceName->setEnabled(false);
  224. int count = main->ui->scenes->count();
  225. for (int i = 0; i < count; i++) {
  226. QListWidgetItem *item = main->ui->scenes->item(i);
  227. OBSScene scene = GetOBSRef<OBSScene>(item);
  228. OBSSource sceneSource = obs_scene_get_source(scene);
  229. if (curSceneSource == sceneSource)
  230. continue;
  231. const char *name = obs_source_get_name(sceneSource);
  232. ui->sourceList->addItem(QT_UTF8(name));
  233. }
  234. } else if (strcmp(id_, "group") == 0) {
  235. obs_enum_sources(EnumGroups, this);
  236. } else {
  237. obs_enum_sources(EnumSources, this);
  238. }
  239. }
  240. void OBSBasicSourceSelect::SourcePaste(const char *name, bool visible, bool dup)
  241. {
  242. AddExisting(name, visible, dup);
  243. }