window-basic-source-select.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t *calldata)
  33. {
  34. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  35. obs_source_t *source = (obs_source_t*)calldata_ptr(calldata, "source");
  36. QMetaObject::invokeMethod(window, "SourceAdded",
  37. Q_ARG(OBSSource, source));
  38. }
  39. void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t *calldata)
  40. {
  41. OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
  42. obs_source_t *source = (obs_source_t*)calldata_ptr(calldata, "source");
  43. QMetaObject::invokeMethod(window, "SourceRemoved",
  44. Q_ARG(OBSSource, source));
  45. }
  46. void OBSBasicSourceSelect::SourceAdded(OBSSource source)
  47. {
  48. const char *name = obs_source_get_name(source);
  49. const char *sourceId = obs_source_get_id(source);
  50. if (strcmp(sourceId, id) != 0)
  51. return;
  52. ui->sourceList->addItem(name);
  53. }
  54. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  55. {
  56. const char *name = obs_source_get_name(source);
  57. const char *sourceId = obs_source_get_id(source);
  58. if (strcmp(sourceId, id) != 0)
  59. return;
  60. QList<QListWidgetItem*> items =
  61. ui->sourceList->findItems(name, Qt::MatchFixedString);
  62. if (!items.count())
  63. return;
  64. delete items[0];
  65. }
  66. static void AddSource(void *_data, obs_scene_t *scene)
  67. {
  68. AddSourceData *data = (AddSourceData *)_data;
  69. obs_sceneitem_t *sceneitem;
  70. sceneitem = obs_scene_add(scene, data->source);
  71. obs_sceneitem_set_visible(sceneitem, data->visible);
  72. }
  73. static char *get_new_source_name(const char *name)
  74. {
  75. struct dstr new_name = {0};
  76. int inc = 0;
  77. dstr_copy(&new_name, name);
  78. for (;;) {
  79. obs_source_t *existing_source = obs_get_source_by_name(
  80. new_name.array);
  81. if (!existing_source)
  82. break;
  83. obs_source_release(existing_source);
  84. dstr_printf(&new_name, "%s %d", name, ++inc + 1);
  85. }
  86. return new_name.array;
  87. }
  88. static void AddExisting(const char *name, bool visible, bool duplicate)
  89. {
  90. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  91. OBSScene scene = main->GetCurrentScene();
  92. if (!scene)
  93. return;
  94. obs_source_t *source = obs_get_source_by_name(name);
  95. if (source) {
  96. if (duplicate) {
  97. obs_source_t *from = source;
  98. char *new_name = get_new_source_name(name);
  99. source = obs_source_duplicate(from, new_name, false);
  100. bfree(new_name);
  101. obs_source_release(from);
  102. if (!source)
  103. return;
  104. }
  105. AddSourceData data;
  106. data.source = source;
  107. data.visible = visible;
  108. obs_scene_atomic_update(scene, AddSource, &data);
  109. obs_source_release(source);
  110. }
  111. }
  112. bool AddNew(QWidget *parent, const char *id, const char *name,
  113. const bool visible, OBSSource &newSource)
  114. {
  115. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  116. OBSScene scene = main->GetCurrentScene();
  117. bool success = false;
  118. if (!scene)
  119. return false;
  120. obs_source_t *source = obs_get_source_by_name(name);
  121. if (source) {
  122. OBSMessageBox::information(parent,
  123. QTStr("NameExists.Title"),
  124. QTStr("NameExists.Text"));
  125. } else {
  126. source = obs_source_create(id, name, NULL, nullptr);
  127. if (source) {
  128. AddSourceData data;
  129. data.source = source;
  130. data.visible = visible;
  131. obs_scene_atomic_update(scene, AddSource, &data);
  132. newSource = source;
  133. success = true;
  134. }
  135. }
  136. obs_source_release(source);
  137. return success;
  138. }
  139. void OBSBasicSourceSelect::on_buttonBox_accepted()
  140. {
  141. bool useExisting = ui->selectExisting->isChecked();
  142. bool visible = ui->sourceVisible->isChecked();
  143. if (useExisting) {
  144. QListWidgetItem *item = ui->sourceList->currentItem();
  145. if (!item)
  146. return;
  147. AddExisting(QT_TO_UTF8(item->text()), visible, false);
  148. } else {
  149. if (ui->sourceName->text().isEmpty()) {
  150. OBSMessageBox::information(this,
  151. QTStr("NoNameEntered.Title"),
  152. QTStr("NoNameEntered.Text"));
  153. return;
  154. }
  155. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),
  156. visible, newSource))
  157. return;
  158. }
  159. done(DialogCode::Accepted);
  160. }
  161. void OBSBasicSourceSelect::on_buttonBox_rejected()
  162. {
  163. done(DialogCode::Rejected);
  164. }
  165. static inline const char *GetSourceDisplayName(const char *id)
  166. {
  167. if (strcmp(id, "scene") == 0)
  168. return Str("Basic.Scene");
  169. return obs_source_get_display_name(id);
  170. }
  171. Q_DECLARE_METATYPE(OBSScene);
  172. template <typename T>
  173. static inline T GetOBSRef(QListWidgetItem *item)
  174. {
  175. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  176. }
  177. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_)
  178. : QDialog (parent),
  179. ui (new Ui::OBSBasicSourceSelect),
  180. id (id_)
  181. {
  182. ui->setupUi(this);
  183. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  184. QString placeHolderText{QT_UTF8(GetSourceDisplayName(id))};
  185. QString text{placeHolderText};
  186. int i = 2;
  187. obs_source_t *source = nullptr;
  188. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  189. obs_source_release(source);
  190. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  191. }
  192. ui->sourceName->setText(text);
  193. ui->sourceName->setFocus(); //Fixes deselect of text.
  194. ui->sourceName->selectAll();
  195. installEventFilter(CreateShortcutFilter());
  196. if (strcmp(id_, "scene") == 0) {
  197. OBSBasic *main = reinterpret_cast<OBSBasic*>(
  198. App()->GetMainWindow());
  199. OBSSource curSceneSource = main->GetCurrentSceneSource();
  200. ui->selectExisting->setChecked(true);
  201. ui->createNew->setChecked(false);
  202. ui->createNew->setEnabled(false);
  203. ui->sourceName->setEnabled(false);
  204. int count = main->ui->scenes->count();
  205. for (int i = 0; i < count; i++) {
  206. QListWidgetItem *item = main->ui->scenes->item(i);
  207. OBSScene scene = GetOBSRef<OBSScene>(item);
  208. OBSSource sceneSource = obs_scene_get_source(scene);
  209. if (curSceneSource == sceneSource)
  210. continue;
  211. const char *name = obs_source_get_name(sceneSource);
  212. ui->sourceList->addItem(QT_UTF8(name));
  213. }
  214. } else {
  215. obs_enum_sources(EnumSources, this);
  216. }
  217. }
  218. void OBSBasicSourceSelect::SourcePaste(const char *name, bool visible, bool dup)
  219. {
  220. AddExisting(name, visible, dup);
  221. }