SourceTree.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #include "SourceTree.hpp"
  2. #include "SourceTreeDelegate.hpp"
  3. #include <widgets/OBSBasic.hpp>
  4. #include <QPainter>
  5. #include "moc_SourceTree.cpp"
  6. static inline OBSScene GetCurrentScene()
  7. {
  8. OBSBasic *main = OBSBasic::Get();
  9. return main->GetCurrentScene();
  10. }
  11. /* moves a scene item index (blame linux distros for using older Qt builds) */
  12. static inline void MoveItem(QVector<OBSSceneItem> &items, int oldIdx, int newIdx)
  13. {
  14. OBSSceneItem item = items[oldIdx];
  15. items.remove(oldIdx);
  16. items.insert(newIdx, item);
  17. }
  18. SourceTree::SourceTree(QWidget *parent_) : QListView(parent_)
  19. {
  20. SourceTreeModel *stm_ = new SourceTreeModel(this);
  21. setModel(stm_);
  22. setStyleSheet(QString("*[bgColor=\"1\"]{background-color:rgba(255,68,68,33%);}"
  23. "*[bgColor=\"2\"]{background-color:rgba(255,255,68,33%);}"
  24. "*[bgColor=\"3\"]{background-color:rgba(68,255,68,33%);}"
  25. "*[bgColor=\"4\"]{background-color:rgba(68,255,255,33%);}"
  26. "*[bgColor=\"5\"]{background-color:rgba(68,68,255,33%);}"
  27. "*[bgColor=\"6\"]{background-color:rgba(255,68,255,33%);}"
  28. "*[bgColor=\"7\"]{background-color:rgba(68,68,68,33%);}"
  29. "*[bgColor=\"8\"]{background-color:rgba(255,255,255,33%);}"));
  30. UpdateNoSourcesMessage();
  31. connect(App(), &OBSApp::StyleChanged, this, &SourceTree::UpdateNoSourcesMessage);
  32. connect(App(), &OBSApp::StyleChanged, this, &SourceTree::UpdateIcons);
  33. setItemDelegate(new SourceTreeDelegate(this));
  34. }
  35. void SourceTree::UpdateIcons()
  36. {
  37. SourceTreeModel *stm = GetStm();
  38. stm->SceneChanged();
  39. }
  40. void SourceTree::SetIconsVisible(bool visible)
  41. {
  42. SourceTreeModel *stm = GetStm();
  43. iconsVisible = visible;
  44. stm->SceneChanged();
  45. }
  46. void SourceTree::ResetWidgets()
  47. {
  48. OBSScene scene = GetCurrentScene();
  49. SourceTreeModel *stm = GetStm();
  50. stm->UpdateGroupState(false);
  51. for (int i = 0; i < stm->items.count(); i++) {
  52. QModelIndex index = stm->createIndex(i, 0, nullptr);
  53. setIndexWidget(index, new SourceTreeItem(this, stm->items[i]));
  54. }
  55. }
  56. void SourceTree::UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item)
  57. {
  58. setIndexWidget(idx, new SourceTreeItem(this, item));
  59. }
  60. void SourceTree::UpdateWidgets(bool force)
  61. {
  62. SourceTreeModel *stm = GetStm();
  63. for (int i = 0; i < stm->items.size(); i++) {
  64. obs_sceneitem_t *item = stm->items[i];
  65. SourceTreeItem *widget = GetItemWidget(i);
  66. if (!widget) {
  67. UpdateWidget(stm->createIndex(i, 0), item);
  68. } else {
  69. widget->Update(force);
  70. }
  71. }
  72. }
  73. void SourceTree::SelectItem(obs_sceneitem_t *sceneitem, bool select)
  74. {
  75. SourceTreeModel *stm = GetStm();
  76. int i = 0;
  77. for (; i < stm->items.count(); i++) {
  78. if (stm->items[i] == sceneitem)
  79. break;
  80. }
  81. if (i == stm->items.count())
  82. return;
  83. QModelIndex index = stm->createIndex(i, 0);
  84. if (index.isValid() && select != selectionModel()->isSelected(index))
  85. selectionModel()->select(index, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
  86. }
  87. void SourceTree::mouseDoubleClickEvent(QMouseEvent *event)
  88. {
  89. if (event->button() == Qt::LeftButton)
  90. QListView::mouseDoubleClickEvent(event);
  91. }
  92. void SourceTree::dropEvent(QDropEvent *event)
  93. {
  94. if (event->source() != this) {
  95. QListView::dropEvent(event);
  96. return;
  97. }
  98. OBSBasic *main = OBSBasic::Get();
  99. OBSScene scene = GetCurrentScene();
  100. obs_source_t *scenesource = obs_scene_get_source(scene);
  101. SourceTreeModel *stm = GetStm();
  102. auto &items = stm->items;
  103. QModelIndexList indices = selectedIndexes();
  104. DropIndicatorPosition indicator = dropIndicatorPosition();
  105. int row = indexAt(event->position().toPoint()).row();
  106. bool emptyDrop = row == -1;
  107. if (emptyDrop) {
  108. if (!items.size()) {
  109. QListView::dropEvent(event);
  110. return;
  111. }
  112. row = items.size() - 1;
  113. indicator = QAbstractItemView::BelowItem;
  114. }
  115. /* --------------------------------------- */
  116. /* store destination group if moving to a */
  117. /* group */
  118. obs_sceneitem_t *dropItem = items[row]; /* item being dropped on */
  119. bool itemIsGroup = obs_sceneitem_is_group(dropItem);
  120. obs_sceneitem_t *dropGroup = itemIsGroup ? dropItem : obs_sceneitem_get_group(scene, dropItem);
  121. /* not a group if moving above the group */
  122. if (indicator == QAbstractItemView::AboveItem && itemIsGroup)
  123. dropGroup = nullptr;
  124. if (emptyDrop)
  125. dropGroup = nullptr;
  126. /* --------------------------------------- */
  127. /* remember to remove list items if */
  128. /* dropping on collapsed group */
  129. bool dropOnCollapsed = false;
  130. if (dropGroup) {
  131. obs_data_t *data = obs_sceneitem_get_private_settings(dropGroup);
  132. dropOnCollapsed = obs_data_get_bool(data, "collapsed");
  133. obs_data_release(data);
  134. }
  135. if (indicator == QAbstractItemView::BelowItem || indicator == QAbstractItemView::OnItem ||
  136. indicator == QAbstractItemView::OnViewport)
  137. row++;
  138. if (row < 0 || row > stm->items.count()) {
  139. QListView::dropEvent(event);
  140. return;
  141. }
  142. /* --------------------------------------- */
  143. /* determine if any base group is selected */
  144. bool hasGroups = false;
  145. for (int i = 0; i < indices.size(); i++) {
  146. obs_sceneitem_t *item = items[indices[i].row()];
  147. if (obs_sceneitem_is_group(item)) {
  148. hasGroups = true;
  149. break;
  150. }
  151. }
  152. /* --------------------------------------- */
  153. /* if dropping a group, detect if it's */
  154. /* below another group */
  155. obs_sceneitem_t *itemBelow;
  156. if (row == stm->items.count())
  157. itemBelow = nullptr;
  158. else
  159. itemBelow = stm->items[row];
  160. if (hasGroups) {
  161. if (!itemBelow || obs_sceneitem_get_group(scene, itemBelow) != dropGroup) {
  162. dropGroup = nullptr;
  163. dropOnCollapsed = false;
  164. }
  165. }
  166. /* --------------------------------------- */
  167. /* if dropping groups on other groups, */
  168. /* disregard as invalid drag/drop */
  169. if (dropGroup && hasGroups) {
  170. QListView::dropEvent(event);
  171. return;
  172. }
  173. /* --------------------------------------- */
  174. /* save undo data */
  175. std::vector<obs_source_t *> sources;
  176. for (int i = 0; i < indices.size(); i++) {
  177. obs_sceneitem_t *item = items[indices[i].row()];
  178. if (obs_sceneitem_get_scene(item) != scene)
  179. sources.push_back(obs_scene_get_source(obs_sceneitem_get_scene(item)));
  180. }
  181. if (dropGroup)
  182. sources.push_back(obs_sceneitem_get_source(dropGroup));
  183. OBSData undo_data = main->BackupScene(scene, &sources);
  184. /* --------------------------------------- */
  185. /* if selection includes base group items, */
  186. /* include all group sub-items and treat */
  187. /* them all as one */
  188. if (hasGroups) {
  189. /* remove sub-items if selected */
  190. for (int i = indices.size() - 1; i >= 0; i--) {
  191. obs_sceneitem_t *item = items[indices[i].row()];
  192. obs_scene_t *itemScene = obs_sceneitem_get_scene(item);
  193. if (itemScene != scene) {
  194. indices.removeAt(i);
  195. }
  196. }
  197. /* add all sub-items of selected groups */
  198. for (int i = indices.size() - 1; i >= 0; i--) {
  199. obs_sceneitem_t *item = items[indices[i].row()];
  200. if (obs_sceneitem_is_group(item)) {
  201. for (int j = items.size() - 1; j >= 0; j--) {
  202. obs_sceneitem_t *subitem = items[j];
  203. obs_sceneitem_t *subitemGroup = obs_sceneitem_get_group(scene, subitem);
  204. if (subitemGroup == item) {
  205. QModelIndex idx = stm->createIndex(j, 0);
  206. indices.insert(i + 1, idx);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. /* --------------------------------------- */
  213. /* build persistent indices */
  214. QList<QPersistentModelIndex> persistentIndices;
  215. persistentIndices.reserve(indices.count());
  216. for (QModelIndex &index : indices)
  217. persistentIndices.append(index);
  218. std::sort(persistentIndices.begin(), persistentIndices.end());
  219. /* --------------------------------------- */
  220. /* move all items to destination index */
  221. int r = row;
  222. for (auto &persistentIdx : persistentIndices) {
  223. int from = persistentIdx.row();
  224. int to = r;
  225. int itemTo = to;
  226. if (itemTo > from)
  227. itemTo--;
  228. if (itemTo != from) {
  229. stm->beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
  230. MoveItem(items, from, itemTo);
  231. stm->endMoveRows();
  232. }
  233. r = persistentIdx.row() + 1;
  234. }
  235. std::sort(persistentIndices.begin(), persistentIndices.end());
  236. int firstIdx = persistentIndices.front().row();
  237. int lastIdx = persistentIndices.back().row();
  238. /* --------------------------------------- */
  239. /* reorder scene items in back-end */
  240. QVector<struct obs_sceneitem_order_info> orderList;
  241. obs_sceneitem_t *lastGroup = nullptr;
  242. int insertCollapsedIdx = 0;
  243. auto insertCollapsed = [&](obs_sceneitem_t *item) {
  244. struct obs_sceneitem_order_info info;
  245. info.group = lastGroup;
  246. info.item = item;
  247. orderList.insert(insertCollapsedIdx++, info);
  248. };
  249. using insertCollapsed_t = decltype(insertCollapsed);
  250. auto preInsertCollapsed = [](obs_scene_t *, obs_sceneitem_t *item, void *param) {
  251. (*static_cast<insertCollapsed_t *>(param))(item);
  252. return true;
  253. };
  254. auto insertLastGroup = [&]() {
  255. OBSDataAutoRelease data = obs_sceneitem_get_private_settings(lastGroup);
  256. bool collapsed = obs_data_get_bool(data, "collapsed");
  257. if (collapsed) {
  258. insertCollapsedIdx = 0;
  259. obs_sceneitem_group_enum_items(lastGroup, preInsertCollapsed, &insertCollapsed);
  260. }
  261. struct obs_sceneitem_order_info info;
  262. info.group = nullptr;
  263. info.item = lastGroup;
  264. orderList.insert(0, info);
  265. };
  266. auto updateScene = [&]() {
  267. struct obs_sceneitem_order_info info;
  268. for (int i = 0; i < items.size(); i++) {
  269. obs_sceneitem_t *item = items[i];
  270. obs_sceneitem_t *group;
  271. if (obs_sceneitem_is_group(item)) {
  272. if (lastGroup) {
  273. insertLastGroup();
  274. }
  275. lastGroup = item;
  276. continue;
  277. }
  278. if (!hasGroups && i >= firstIdx && i <= lastIdx)
  279. group = dropGroup;
  280. else
  281. group = obs_sceneitem_get_group(scene, item);
  282. if (lastGroup && lastGroup != group) {
  283. insertLastGroup();
  284. }
  285. lastGroup = group;
  286. info.group = group;
  287. info.item = item;
  288. orderList.insert(0, info);
  289. }
  290. if (lastGroup) {
  291. insertLastGroup();
  292. }
  293. obs_scene_reorder_items2(scene, orderList.data(), orderList.size());
  294. };
  295. using updateScene_t = decltype(updateScene);
  296. auto preUpdateScene = [](void *data, obs_scene_t *) {
  297. (*static_cast<updateScene_t *>(data))();
  298. };
  299. ignoreReorder = true;
  300. obs_scene_atomic_update(scene, preUpdateScene, &updateScene);
  301. ignoreReorder = false;
  302. /* --------------------------------------- */
  303. /* save redo data */
  304. OBSData redo_data = main->BackupScene(scene, &sources);
  305. /* --------------------------------------- */
  306. /* add undo/redo action */
  307. const char *scene_name = obs_source_get_name(scenesource);
  308. QString action_name = QTStr("Undo.ReorderSources").arg(scene_name);
  309. main->CreateSceneUndoRedoAction(action_name, undo_data, redo_data);
  310. /* --------------------------------------- */
  311. /* remove items if dropped in to collapsed */
  312. /* group */
  313. if (dropOnCollapsed) {
  314. stm->beginRemoveRows(QModelIndex(), firstIdx, lastIdx);
  315. items.remove(firstIdx, lastIdx - firstIdx + 1);
  316. stm->endRemoveRows();
  317. }
  318. /* --------------------------------------- */
  319. /* update widgets and accept event */
  320. UpdateWidgets(true);
  321. event->accept();
  322. event->setDropAction(Qt::CopyAction);
  323. QListView::dropEvent(event);
  324. }
  325. void SourceTree::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
  326. {
  327. {
  328. QSignalBlocker sourcesSignalBlocker(this);
  329. SourceTreeModel *stm = GetStm();
  330. QModelIndexList selectedIdxs = selected.indexes();
  331. QModelIndexList deselectedIdxs = deselected.indexes();
  332. for (int i = 0; i < selectedIdxs.count(); i++) {
  333. int idx = selectedIdxs[i].row();
  334. obs_sceneitem_select(stm->items[idx], true);
  335. }
  336. for (int i = 0; i < deselectedIdxs.count(); i++) {
  337. int idx = deselectedIdxs[i].row();
  338. obs_sceneitem_select(stm->items[idx], false);
  339. }
  340. }
  341. QListView::selectionChanged(selected, deselected);
  342. }
  343. void SourceTree::NewGroupEdit(int row)
  344. {
  345. if (!Edit(row)) {
  346. OBSBasic *main = OBSBasic::Get();
  347. main->undo_s.pop_disabled();
  348. blog(LOG_WARNING, "Uh, somehow the edit didn't process, this "
  349. "code should never be reached.\nAnd by "
  350. "\"never be reached\", I mean that "
  351. "theoretically, it should be\nimpossible "
  352. "for this code to be reached. But if this "
  353. "code is reached,\nfeel free to laugh at "
  354. "Lain, because apparently it is, in fact, "
  355. "actually\npossible for this code to be "
  356. "reached. But I mean, again, theoretically\n"
  357. "it should be impossible. So if you see "
  358. "this in your log, just know that\nit's "
  359. "really dumb, and depressing. But at least "
  360. "the undo/redo action is\nstill covered, so "
  361. "in theory things *should* be fine. But "
  362. "it's entirely\npossible that they might "
  363. "not be exactly. But again, yea. This "
  364. "really\nshould not be possible.");
  365. OBSData redoSceneData = main->BackupScene(GetCurrentScene());
  366. QString text = QTStr("Undo.GroupItems").arg("Unknown");
  367. main->CreateSceneUndoRedoAction(text, undoSceneData, redoSceneData);
  368. undoSceneData = nullptr;
  369. }
  370. }
  371. bool SourceTree::Edit(int row)
  372. {
  373. SourceTreeModel *stm = GetStm();
  374. if (row < 0 || row >= stm->items.count())
  375. return false;
  376. QModelIndex index = stm->createIndex(row, 0);
  377. QWidget *widget = indexWidget(index);
  378. SourceTreeItem *itemWidget = reinterpret_cast<SourceTreeItem *>(widget);
  379. if (itemWidget->IsEditing()) {
  380. #ifdef __APPLE__
  381. itemWidget->ExitEditMode(true);
  382. #endif
  383. return false;
  384. }
  385. itemWidget->EnterEditMode();
  386. edit(index);
  387. return true;
  388. }
  389. bool SourceTree::MultipleBaseSelected() const
  390. {
  391. SourceTreeModel *stm = GetStm();
  392. QModelIndexList selectedIndices = selectedIndexes();
  393. OBSScene scene = GetCurrentScene();
  394. if (selectedIndices.size() < 1) {
  395. return false;
  396. }
  397. for (auto &idx : selectedIndices) {
  398. obs_sceneitem_t *item = stm->items[idx.row()];
  399. if (obs_sceneitem_is_group(item)) {
  400. return false;
  401. }
  402. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  403. if (itemScene != scene) {
  404. return false;
  405. }
  406. }
  407. return true;
  408. }
  409. bool SourceTree::GroupsSelected() const
  410. {
  411. SourceTreeModel *stm = GetStm();
  412. QModelIndexList selectedIndices = selectedIndexes();
  413. OBSScene scene = GetCurrentScene();
  414. if (selectedIndices.size() < 1) {
  415. return false;
  416. }
  417. for (auto &idx : selectedIndices) {
  418. obs_sceneitem_t *item = stm->items[idx.row()];
  419. if (!obs_sceneitem_is_group(item)) {
  420. return false;
  421. }
  422. }
  423. return true;
  424. }
  425. bool SourceTree::GroupedItemsSelected() const
  426. {
  427. SourceTreeModel *stm = GetStm();
  428. QModelIndexList selectedIndices = selectedIndexes();
  429. OBSScene scene = GetCurrentScene();
  430. if (!selectedIndices.size()) {
  431. return false;
  432. }
  433. for (auto &idx : selectedIndices) {
  434. obs_sceneitem_t *item = stm->items[idx.row()];
  435. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  436. if (itemScene != scene) {
  437. return true;
  438. }
  439. }
  440. return false;
  441. }
  442. void SourceTree::Remove(OBSSceneItem item, OBSScene scene)
  443. {
  444. OBSBasic *main = OBSBasic::Get();
  445. GetStm()->Remove(item);
  446. main->SaveProject();
  447. if (!main->SavingDisabled()) {
  448. obs_source_t *sceneSource = obs_scene_get_source(scene);
  449. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  450. blog(LOG_INFO, "User Removed source '%s' (%s) from scene '%s'", obs_source_get_name(itemSource),
  451. obs_source_get_id(itemSource), obs_source_get_name(sceneSource));
  452. }
  453. }
  454. void SourceTree::GroupSelectedItems()
  455. {
  456. QModelIndexList indices = selectedIndexes();
  457. std::sort(indices.begin(), indices.end());
  458. GetStm()->GroupSelectedItems(indices);
  459. }
  460. void SourceTree::UngroupSelectedGroups()
  461. {
  462. QModelIndexList indices = selectedIndexes();
  463. GetStm()->UngroupSelectedGroups(indices);
  464. }
  465. void SourceTree::AddGroup()
  466. {
  467. GetStm()->AddGroup();
  468. }
  469. void SourceTree::UpdateNoSourcesMessage()
  470. {
  471. QString file = !App()->IsThemeDark() ? ":res/images/no_sources.svg" : "theme:Dark/no_sources.svg";
  472. iconNoSources.load(file);
  473. QTextOption opt(Qt::AlignHCenter);
  474. opt.setWrapMode(QTextOption::WordWrap);
  475. textNoSources.setTextOption(opt);
  476. textNoSources.setText(QTStr("NoSources.Label").replace("\n", "<br/>"));
  477. textPrepared = false;
  478. }
  479. void SourceTree::paintEvent(QPaintEvent *event)
  480. {
  481. SourceTreeModel *stm = GetStm();
  482. if (stm && !stm->items.count()) {
  483. QPainter p(viewport());
  484. if (!textPrepared) {
  485. textNoSources.prepare(QTransform(), p.font());
  486. textPrepared = true;
  487. }
  488. QRectF iconRect = iconNoSources.viewBoxF();
  489. iconRect.setSize(QSizeF(32.0, 32.0));
  490. QSizeF iconSize = iconRect.size();
  491. QSizeF textSize = textNoSources.size();
  492. QSizeF thisSize = size();
  493. const qreal spacing = 16.0;
  494. qreal totalHeight = iconSize.height() + spacing + textSize.height();
  495. qreal x = thisSize.width() / 2.0 - iconSize.width() / 2.0;
  496. qreal y = thisSize.height() / 2.0 - totalHeight / 2.0;
  497. iconRect.moveTo(std::round(x), std::round(y));
  498. iconNoSources.render(&p, iconRect);
  499. x = thisSize.width() / 2.0 - textSize.width() / 2.0;
  500. y += spacing + iconSize.height();
  501. p.drawStaticText(x, y, textNoSources);
  502. } else {
  503. QListView::paintEvent(event);
  504. }
  505. }