1
0

window-basic-source-select.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. obs_transform_info *transform = nullptr;
  23. obs_sceneitem_crop *crop = nullptr;
  24. };
  25. bool OBSBasicSourceSelect::EnumSources(void *data, obs_source_t *source)
  26. {
  27. if (obs_source_is_hidden(source))
  28. return false;
  29. OBSBasicSourceSelect *window =
  30. static_cast<OBSBasicSourceSelect *>(data);
  31. const char *name = obs_source_get_name(source);
  32. const char *id = obs_source_get_unversioned_id(source);
  33. if (strcmp(id, window->id) == 0)
  34. window->ui->sourceList->addItem(QT_UTF8(name));
  35. return true;
  36. }
  37. bool OBSBasicSourceSelect::EnumGroups(void *data, obs_source_t *source)
  38. {
  39. OBSBasicSourceSelect *window =
  40. static_cast<OBSBasicSourceSelect *>(data);
  41. const char *name = obs_source_get_name(source);
  42. const char *id = obs_source_get_unversioned_id(source);
  43. if (strcmp(id, window->id) == 0) {
  44. OBSBasic *main =
  45. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  46. OBSScene scene = main->GetCurrentScene();
  47. obs_sceneitem_t *existing = obs_scene_get_group(scene, name);
  48. if (!existing)
  49. window->ui->sourceList->addItem(QT_UTF8(name));
  50. }
  51. return true;
  52. }
  53. void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t *calldata)
  54. {
  55. OBSBasicSourceSelect *window =
  56. static_cast<OBSBasicSourceSelect *>(data);
  57. obs_source_t *source = (obs_source_t *)calldata_ptr(calldata, "source");
  58. QMetaObject::invokeMethod(window, "SourceAdded",
  59. Q_ARG(OBSSource, source));
  60. }
  61. void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t *calldata)
  62. {
  63. OBSBasicSourceSelect *window =
  64. static_cast<OBSBasicSourceSelect *>(data);
  65. obs_source_t *source = (obs_source_t *)calldata_ptr(calldata, "source");
  66. QMetaObject::invokeMethod(window, "SourceRemoved",
  67. Q_ARG(OBSSource, source));
  68. }
  69. void OBSBasicSourceSelect::SourceAdded(OBSSource source)
  70. {
  71. const char *name = obs_source_get_name(source);
  72. const char *sourceId = obs_source_get_unversioned_id(source);
  73. if (strcmp(sourceId, id) != 0)
  74. return;
  75. ui->sourceList->addItem(name);
  76. }
  77. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  78. {
  79. const char *name = obs_source_get_name(source);
  80. const char *sourceId = obs_source_get_unversioned_id(source);
  81. if (strcmp(sourceId, id) != 0)
  82. return;
  83. QList<QListWidgetItem *> items =
  84. ui->sourceList->findItems(name, Qt::MatchFixedString);
  85. if (!items.count())
  86. return;
  87. delete items[0];
  88. }
  89. static void AddSource(void *_data, obs_scene_t *scene)
  90. {
  91. AddSourceData *data = (AddSourceData *)_data;
  92. obs_sceneitem_t *sceneitem;
  93. sceneitem = obs_scene_add(scene, data->source);
  94. if (data->transform != nullptr)
  95. obs_sceneitem_set_info(sceneitem, data->transform);
  96. if (data->crop != nullptr)
  97. obs_sceneitem_set_crop(sceneitem, data->crop);
  98. obs_sceneitem_set_visible(sceneitem, data->visible);
  99. }
  100. static char *get_new_source_name(const char *name)
  101. {
  102. struct dstr new_name = {0};
  103. int inc = 0;
  104. dstr_copy(&new_name, name);
  105. for (;;) {
  106. obs_source_t *existing_source =
  107. obs_get_source_by_name(new_name.array);
  108. if (!existing_source)
  109. break;
  110. obs_source_release(existing_source);
  111. dstr_printf(&new_name, "%s %d", name, ++inc + 1);
  112. }
  113. return new_name.array;
  114. }
  115. static void AddExisting(OBSSource source, bool visible, bool duplicate,
  116. obs_transform_info *transform, obs_sceneitem_crop *crop)
  117. {
  118. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  119. OBSScene scene = main->GetCurrentScene();
  120. if (!scene)
  121. return;
  122. if (duplicate) {
  123. OBSSource from = source;
  124. char *new_name =
  125. get_new_source_name(obs_source_get_name(source));
  126. source = obs_source_duplicate(from, new_name, false);
  127. obs_source_release(source);
  128. bfree(new_name);
  129. if (!source)
  130. return;
  131. }
  132. AddSourceData data;
  133. data.source = source;
  134. data.visible = visible;
  135. data.transform = transform;
  136. data.crop = crop;
  137. obs_enter_graphics();
  138. obs_scene_atomic_update(scene, AddSource, &data);
  139. obs_leave_graphics();
  140. }
  141. static void AddExisting(const char *name, bool visible, bool duplicate,
  142. obs_transform_info *transform, obs_sceneitem_crop *crop)
  143. {
  144. obs_source_t *source = obs_get_source_by_name(name);
  145. if (source) {
  146. AddExisting(source, visible, duplicate, transform, crop);
  147. obs_source_release(source);
  148. }
  149. }
  150. bool AddNew(QWidget *parent, const char *id, const char *name,
  151. const bool visible, OBSSource &newSource)
  152. {
  153. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  154. OBSScene scene = main->GetCurrentScene();
  155. bool success = false;
  156. if (!scene)
  157. return false;
  158. obs_source_t *source = obs_get_source_by_name(name);
  159. if (source && parent) {
  160. OBSMessageBox::information(parent, QTStr("NameExists.Title"),
  161. QTStr("NameExists.Text"));
  162. } else {
  163. const char *v_id = obs_get_latest_input_type_id(id);
  164. source = obs_source_create(v_id, name, NULL, nullptr);
  165. if (source) {
  166. AddSourceData data;
  167. data.source = source;
  168. data.visible = visible;
  169. obs_enter_graphics();
  170. obs_scene_atomic_update(scene, AddSource, &data);
  171. obs_leave_graphics();
  172. newSource = source;
  173. /* set monitoring if source monitors by default */
  174. uint32_t flags = obs_source_get_output_flags(source);
  175. if ((flags & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  176. obs_source_set_monitoring_type(
  177. source,
  178. OBS_MONITORING_TYPE_MONITOR_ONLY);
  179. }
  180. success = true;
  181. }
  182. }
  183. obs_source_release(source);
  184. return success;
  185. }
  186. void OBSBasicSourceSelect::on_buttonBox_accepted()
  187. {
  188. bool useExisting = ui->selectExisting->isChecked();
  189. bool visible = ui->sourceVisible->isChecked();
  190. if (useExisting) {
  191. QListWidgetItem *item = ui->sourceList->currentItem();
  192. if (!item)
  193. return;
  194. AddExisting(QT_TO_UTF8(item->text()), visible, false, nullptr,
  195. nullptr);
  196. } else {
  197. if (ui->sourceName->text().isEmpty()) {
  198. OBSMessageBox::warning(this,
  199. QTStr("NoNameEntered.Title"),
  200. QTStr("NoNameEntered.Text"));
  201. return;
  202. }
  203. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),
  204. visible, newSource))
  205. return;
  206. OBSBasic *main =
  207. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  208. std::string scene_name =
  209. obs_source_get_name(main->GetCurrentSceneSource());
  210. auto undo = [scene_name, main](const std::string &data) {
  211. obs_source_t *source =
  212. obs_get_source_by_name(data.c_str());
  213. obs_source_release(source);
  214. obs_source_remove(source);
  215. obs_source_t *scene_source =
  216. obs_get_source_by_name(scene_name.c_str());
  217. main->SetCurrentScene(scene_source, true);
  218. obs_source_release(scene_source);
  219. main->RefreshSources(main->GetCurrentScene());
  220. };
  221. obs_data_t *wrapper = obs_data_create();
  222. obs_data_set_string(wrapper, "id", id);
  223. obs_sceneitem_t *item = obs_scene_sceneitem_from_source(
  224. main->GetCurrentScene(), newSource);
  225. obs_data_set_int(wrapper, "item_id",
  226. obs_sceneitem_get_id(item));
  227. obs_data_set_string(
  228. wrapper, "name",
  229. ui->sourceName->text().toUtf8().constData());
  230. obs_data_set_bool(wrapper, "visible", visible);
  231. auto redo = [scene_name, main](const std::string &data) {
  232. obs_source_t *scene_source =
  233. obs_get_source_by_name(scene_name.c_str());
  234. main->SetCurrentScene(scene_source, true);
  235. obs_source_release(scene_source);
  236. obs_data_t *dat =
  237. obs_data_create_from_json(data.c_str());
  238. OBSSource source;
  239. AddNew(NULL, obs_data_get_string(dat, "id"),
  240. obs_data_get_string(dat, "name"),
  241. obs_data_get_bool(dat, "visible"), source);
  242. obs_sceneitem_t *item = obs_scene_sceneitem_from_source(
  243. main->GetCurrentScene(), source);
  244. obs_sceneitem_set_id(item, (int64_t)obs_data_get_int(
  245. dat, "item_id"));
  246. main->RefreshSources(main->GetCurrentScene());
  247. obs_data_release(dat);
  248. obs_sceneitem_release(item);
  249. };
  250. undo_s.add_action(QTStr("Undo.Add").arg(ui->sourceName->text()),
  251. undo, redo,
  252. std::string(obs_source_get_name(newSource)),
  253. std::string(obs_data_get_json(wrapper)));
  254. obs_data_release(wrapper);
  255. obs_sceneitem_release(item);
  256. }
  257. done(DialogCode::Accepted);
  258. }
  259. void OBSBasicSourceSelect::on_buttonBox_rejected()
  260. {
  261. done(DialogCode::Rejected);
  262. }
  263. static inline const char *GetSourceDisplayName(const char *id)
  264. {
  265. if (strcmp(id, "scene") == 0)
  266. return Str("Basic.Scene");
  267. const char *v_id = obs_get_latest_input_type_id(id);
  268. return obs_source_get_display_name(v_id);
  269. }
  270. Q_DECLARE_METATYPE(OBSScene);
  271. template<typename T> static inline T GetOBSRef(QListWidgetItem *item)
  272. {
  273. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  274. }
  275. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_,
  276. undo_stack &undo_s)
  277. : QDialog(parent),
  278. ui(new Ui::OBSBasicSourceSelect),
  279. id(id_),
  280. undo_s(undo_s)
  281. {
  282. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  283. ui->setupUi(this);
  284. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  285. QString placeHolderText{QT_UTF8(GetSourceDisplayName(id))};
  286. QString text{placeHolderText};
  287. int i = 2;
  288. obs_source_t *source = nullptr;
  289. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  290. obs_source_release(source);
  291. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  292. }
  293. ui->sourceName->setText(text);
  294. ui->sourceName->setFocus(); //Fixes deselect of text.
  295. ui->sourceName->selectAll();
  296. installEventFilter(CreateShortcutFilter());
  297. if (strcmp(id_, "scene") == 0) {
  298. OBSBasic *main =
  299. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  300. OBSSource curSceneSource = main->GetCurrentSceneSource();
  301. ui->selectExisting->setChecked(true);
  302. ui->createNew->setChecked(false);
  303. ui->createNew->setEnabled(false);
  304. ui->sourceName->setEnabled(false);
  305. int count = main->ui->scenes->count();
  306. for (int i = 0; i < count; i++) {
  307. QListWidgetItem *item = main->ui->scenes->item(i);
  308. OBSScene scene = GetOBSRef<OBSScene>(item);
  309. OBSSource sceneSource = obs_scene_get_source(scene);
  310. if (curSceneSource == sceneSource)
  311. continue;
  312. const char *name = obs_source_get_name(sceneSource);
  313. ui->sourceList->addItem(QT_UTF8(name));
  314. }
  315. } else if (strcmp(id_, "group") == 0) {
  316. obs_enum_sources(EnumGroups, this);
  317. } else {
  318. obs_enum_sources(EnumSources, this);
  319. }
  320. }
  321. void OBSBasicSourceSelect::SourcePaste(SourceCopyInfo &info, bool dup)
  322. {
  323. OBSSource source = OBSGetStrongRef(info.weak_source);
  324. if (!source)
  325. return;
  326. AddExisting(source, info.visible, dup, info.transform.get(),
  327. info.crop.get());
  328. }