source-tree.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. #include "window-basic-main.hpp"
  2. #include "obs-app.hpp"
  3. #include "source-tree.hpp"
  4. #include "qt-wrappers.hpp"
  5. #include "visibility-checkbox.hpp"
  6. #include "locked-checkbox.hpp"
  7. #include "expand-checkbox.hpp"
  8. #include "platform.hpp"
  9. #include <obs-frontend-api.h>
  10. #include <obs.h>
  11. #include <string>
  12. #include <QLabel>
  13. #include <QLineEdit>
  14. #include <QSpacerItem>
  15. #include <QPushButton>
  16. #include <QVBoxLayout>
  17. #include <QHBoxLayout>
  18. #include <QMouseEvent>
  19. #include <QAccessible>
  20. #include <QStylePainter>
  21. #include <QStyleOptionFocusRect>
  22. static inline OBSScene GetCurrentScene()
  23. {
  24. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  25. return main->GetCurrentScene();
  26. }
  27. /* ========================================================================= */
  28. SourceTreeItem::SourceTreeItem(SourceTree *tree_, OBSSceneItem sceneitem_)
  29. : tree(tree_), sceneitem(sceneitem_)
  30. {
  31. setAttribute(Qt::WA_TranslucentBackground);
  32. setMouseTracking(true);
  33. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  34. const char *name = obs_source_get_name(source);
  35. obs_data_t *privData = obs_sceneitem_get_private_settings(sceneitem);
  36. int preset = obs_data_get_int(privData, "color-preset");
  37. if (preset == 1) {
  38. const char *color = obs_data_get_string(privData, "color");
  39. std::string col = "background: ";
  40. col += color;
  41. setStyleSheet(col.c_str());
  42. } else if (preset > 1) {
  43. setStyleSheet("");
  44. setProperty("bgColor", preset - 1);
  45. } else {
  46. setStyleSheet("background: none");
  47. }
  48. obs_data_release(privData);
  49. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  50. const char *id = obs_source_get_id(source);
  51. QLabel *iconLabel = nullptr;
  52. if (tree->iconsVisible) {
  53. QIcon icon;
  54. if (strcmp(id, "scene") == 0)
  55. icon = main->GetSceneIcon();
  56. else if (strcmp(id, "group") == 0)
  57. icon = main->GetGroupIcon();
  58. else
  59. icon = main->GetSourceIcon(id);
  60. QPixmap pixmap = icon.pixmap(QSize(16, 16));
  61. iconLabel = new QLabel();
  62. iconLabel->setPixmap(pixmap);
  63. iconLabel->setFixedSize(16, 16);
  64. iconLabel->setStyleSheet("background: none");
  65. }
  66. vis = new VisibilityCheckBox();
  67. vis->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  68. vis->setFixedSize(16, 16);
  69. vis->setChecked(obs_sceneitem_visible(sceneitem));
  70. vis->setStyleSheet("background: none");
  71. vis->setAccessibleName(QTStr("Basic.Main.Sources.Visibility"));
  72. vis->setAccessibleDescription(
  73. QTStr("Basic.Main.Sources.VisibilityDescription").arg(name));
  74. lock = new LockedCheckBox();
  75. lock->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  76. lock->setFixedSize(16, 16);
  77. lock->setChecked(obs_sceneitem_locked(sceneitem));
  78. lock->setStyleSheet("background: none");
  79. lock->setAccessibleName(QTStr("Basic.Main.Sources.Lock"));
  80. lock->setAccessibleDescription(
  81. QTStr("Basic.Main.Sources.LockDescription").arg(name));
  82. label = new QLabel(QT_UTF8(name));
  83. label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  84. label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  85. label->setAttribute(Qt::WA_TranslucentBackground);
  86. #ifdef __APPLE__
  87. vis->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  88. lock->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  89. #endif
  90. boxLayout = new QHBoxLayout();
  91. boxLayout->setContentsMargins(0, 0, 0, 0);
  92. if (iconLabel) {
  93. boxLayout->addWidget(iconLabel);
  94. boxLayout->addSpacing(2);
  95. }
  96. boxLayout->addWidget(label);
  97. boxLayout->addWidget(vis);
  98. boxLayout->addSpacing(1);
  99. boxLayout->addWidget(lock);
  100. #ifdef __APPLE__
  101. /* Hack: Fixes a bug where scrollbars would be above the lock icon */
  102. boxLayout->addSpacing(16);
  103. #endif
  104. Update(false);
  105. setLayout(boxLayout);
  106. /* --------------------------------------------------------- */
  107. auto setItemVisible = [this](bool checked) {
  108. obs_scene_t *scene = obs_sceneitem_get_scene(sceneitem);
  109. obs_source_t *scenesource = obs_scene_get_source(scene);
  110. int64_t id = obs_sceneitem_get_id(sceneitem);
  111. std::string name = obs_source_get_name(scenesource);
  112. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  113. auto undo_redo = [id, name](const std::string &val) {
  114. bool vis = val[0] == '1';
  115. obs_scene_t *s = obs_get_scene_by_name(name.c_str());
  116. obs_sceneitem_t *si =
  117. obs_scene_find_sceneitem_by_id(s, id);
  118. if (si)
  119. obs_sceneitem_set_visible(si, vis);
  120. obs_scene_release(s);
  121. };
  122. QString str = QTStr(checked ? "Undo.ShowSceneItem"
  123. : "Undo.HideSceneItem");
  124. str = str.arg(obs_source_get_name(source), name.c_str());
  125. OBSBasic *main = OBSBasic::Get();
  126. main->undo_s.add_action(str, undo_redo, undo_redo,
  127. checked ? "0" : "1",
  128. checked ? "1" : "0", nullptr);
  129. SignalBlocker sourcesSignalBlocker(this);
  130. obs_sceneitem_set_visible(sceneitem, checked);
  131. };
  132. auto setItemLocked = [this](bool checked) {
  133. SignalBlocker sourcesSignalBlocker(this);
  134. obs_sceneitem_set_locked(sceneitem, checked);
  135. };
  136. connect(vis, &QAbstractButton::clicked, setItemVisible);
  137. connect(lock, &QAbstractButton::clicked, setItemLocked);
  138. }
  139. void SourceTreeItem::paintEvent(QPaintEvent *event)
  140. {
  141. QStyleOption opt;
  142. opt.initFrom(this);
  143. QPainter p(this);
  144. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  145. QWidget::paintEvent(event);
  146. }
  147. void SourceTreeItem::DisconnectSignals()
  148. {
  149. sceneRemoveSignal.Disconnect();
  150. itemRemoveSignal.Disconnect();
  151. selectSignal.Disconnect();
  152. deselectSignal.Disconnect();
  153. visibleSignal.Disconnect();
  154. lockedSignal.Disconnect();
  155. renameSignal.Disconnect();
  156. removeSignal.Disconnect();
  157. }
  158. void SourceTreeItem::Clear()
  159. {
  160. DisconnectSignals();
  161. sceneitem = nullptr;
  162. }
  163. void SourceTreeItem::ReconnectSignals()
  164. {
  165. if (!sceneitem)
  166. return;
  167. DisconnectSignals();
  168. /* --------------------------------------------------------- */
  169. auto removeItem = [](void *data, calldata_t *cd) {
  170. SourceTreeItem *this_ =
  171. reinterpret_cast<SourceTreeItem *>(data);
  172. obs_sceneitem_t *curItem =
  173. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  174. if (curItem == this_->sceneitem) {
  175. QMetaObject::invokeMethod(this_->tree, "Remove",
  176. Q_ARG(OBSSceneItem, curItem));
  177. curItem = nullptr;
  178. }
  179. if (!curItem)
  180. QMetaObject::invokeMethod(this_, "Clear");
  181. };
  182. auto itemVisible = [](void *data, calldata_t *cd) {
  183. SourceTreeItem *this_ =
  184. reinterpret_cast<SourceTreeItem *>(data);
  185. obs_sceneitem_t *curItem =
  186. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  187. bool visible = calldata_bool(cd, "visible");
  188. if (curItem == this_->sceneitem)
  189. QMetaObject::invokeMethod(this_, "VisibilityChanged",
  190. Q_ARG(bool, visible));
  191. };
  192. auto itemLocked = [](void *data, calldata_t *cd) {
  193. SourceTreeItem *this_ =
  194. reinterpret_cast<SourceTreeItem *>(data);
  195. obs_sceneitem_t *curItem =
  196. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  197. bool locked = calldata_bool(cd, "locked");
  198. if (curItem == this_->sceneitem)
  199. QMetaObject::invokeMethod(this_, "LockedChanged",
  200. Q_ARG(bool, locked));
  201. };
  202. auto itemSelect = [](void *data, calldata_t *cd) {
  203. SourceTreeItem *this_ =
  204. reinterpret_cast<SourceTreeItem *>(data);
  205. obs_sceneitem_t *curItem =
  206. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  207. if (curItem == this_->sceneitem)
  208. QMetaObject::invokeMethod(this_, "Select");
  209. };
  210. auto itemDeselect = [](void *data, calldata_t *cd) {
  211. SourceTreeItem *this_ =
  212. reinterpret_cast<SourceTreeItem *>(data);
  213. obs_sceneitem_t *curItem =
  214. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  215. if (curItem == this_->sceneitem)
  216. QMetaObject::invokeMethod(this_, "Deselect");
  217. };
  218. auto reorderGroup = [](void *data, calldata_t *) {
  219. SourceTreeItem *this_ =
  220. reinterpret_cast<SourceTreeItem *>(data);
  221. QMetaObject::invokeMethod(this_->tree, "ReorderItems");
  222. };
  223. obs_scene_t *scene = obs_sceneitem_get_scene(sceneitem);
  224. obs_source_t *sceneSource = obs_scene_get_source(scene);
  225. signal_handler_t *signal = obs_source_get_signal_handler(sceneSource);
  226. sceneRemoveSignal.Connect(signal, "remove", removeItem, this);
  227. itemRemoveSignal.Connect(signal, "item_remove", removeItem, this);
  228. visibleSignal.Connect(signal, "item_visible", itemVisible, this);
  229. lockedSignal.Connect(signal, "item_locked", itemLocked, this);
  230. selectSignal.Connect(signal, "item_select", itemSelect, this);
  231. deselectSignal.Connect(signal, "item_deselect", itemDeselect, this);
  232. if (obs_sceneitem_is_group(sceneitem)) {
  233. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  234. signal = obs_source_get_signal_handler(source);
  235. groupReorderSignal.Connect(signal, "reorder", reorderGroup,
  236. this);
  237. }
  238. /* --------------------------------------------------------- */
  239. auto renamed = [](void *data, calldata_t *cd) {
  240. SourceTreeItem *this_ =
  241. reinterpret_cast<SourceTreeItem *>(data);
  242. const char *name = calldata_string(cd, "new_name");
  243. QMetaObject::invokeMethod(this_, "Renamed",
  244. Q_ARG(QString, QT_UTF8(name)));
  245. };
  246. auto removeSource = [](void *data, calldata_t *) {
  247. SourceTreeItem *this_ =
  248. reinterpret_cast<SourceTreeItem *>(data);
  249. this_->DisconnectSignals();
  250. this_->sceneitem = nullptr;
  251. };
  252. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  253. signal = obs_source_get_signal_handler(source);
  254. renameSignal.Connect(signal, "rename", renamed, this);
  255. removeSignal.Connect(signal, "remove", removeSource, this);
  256. }
  257. void SourceTreeItem::mouseDoubleClickEvent(QMouseEvent *event)
  258. {
  259. QWidget::mouseDoubleClickEvent(event);
  260. if (expand) {
  261. expand->setChecked(!expand->isChecked());
  262. } else {
  263. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  264. OBSBasic *main =
  265. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  266. if (source) {
  267. main->CreatePropertiesWindow(source);
  268. }
  269. }
  270. }
  271. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  272. void SourceTreeItem::enterEvent(QEnterEvent *event)
  273. #else
  274. void SourceTreeItem::enterEvent(QEvent *event)
  275. #endif
  276. {
  277. QWidget::enterEvent(event);
  278. OBSBasicPreview *preview = OBSBasicPreview::Get();
  279. std::lock_guard<std::mutex> lock(preview->selectMutex);
  280. preview->hoveredPreviewItems.clear();
  281. preview->hoveredPreviewItems.push_back(sceneitem);
  282. }
  283. void SourceTreeItem::leaveEvent(QEvent *event)
  284. {
  285. QWidget::leaveEvent(event);
  286. OBSBasicPreview *preview = OBSBasicPreview::Get();
  287. std::lock_guard<std::mutex> lock(preview->selectMutex);
  288. preview->hoveredPreviewItems.clear();
  289. }
  290. bool SourceTreeItem::IsEditing()
  291. {
  292. return editor != nullptr;
  293. }
  294. void SourceTreeItem::EnterEditMode()
  295. {
  296. setFocusPolicy(Qt::StrongFocus);
  297. int index = boxLayout->indexOf(label);
  298. boxLayout->removeWidget(label);
  299. editor = new QLineEdit(label->text());
  300. editor->setStyleSheet("background: none");
  301. editor->selectAll();
  302. editor->installEventFilter(this);
  303. boxLayout->insertWidget(index, editor);
  304. setFocusProxy(editor);
  305. }
  306. void SourceTreeItem::ExitEditMode(bool save)
  307. {
  308. if (!editor)
  309. return;
  310. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  311. OBSScene scene = main->GetCurrentScene();
  312. std::string newName = QT_TO_UTF8(editor->text());
  313. setFocusProxy(nullptr);
  314. int index = boxLayout->indexOf(editor);
  315. boxLayout->removeWidget(editor);
  316. delete editor;
  317. editor = nullptr;
  318. setFocusPolicy(Qt::NoFocus);
  319. boxLayout->insertWidget(index, label);
  320. label->setFocus();
  321. /* ----------------------------------------- */
  322. /* check for empty string */
  323. if (!save)
  324. return;
  325. if (newName.empty()) {
  326. OBSMessageBox::information(main, QTStr("NoNameEntered.Title"),
  327. QTStr("NoNameEntered.Text"));
  328. return;
  329. }
  330. /* ----------------------------------------- */
  331. /* Check for same name */
  332. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  333. if (newName == obs_source_get_name(source))
  334. return;
  335. /* ----------------------------------------- */
  336. /* check for existing source */
  337. obs_source_t *existingSource = obs_get_source_by_name(newName.c_str());
  338. obs_source_release(existingSource);
  339. bool exists = !!existingSource;
  340. if (exists) {
  341. OBSMessageBox::information(main, QTStr("NameExists.Title"),
  342. QTStr("NameExists.Text"));
  343. return;
  344. }
  345. /* ----------------------------------------- */
  346. /* rename */
  347. SignalBlocker sourcesSignalBlocker(this);
  348. std::string prevName(obs_source_get_name(source));
  349. std::string scene_name =
  350. obs_source_get_name(main->GetCurrentSceneSource());
  351. auto undo = [scene_name, prevName, main](const std::string &data) {
  352. obs_source_t *source = obs_get_source_by_name(data.c_str());
  353. obs_source_set_name(source, prevName.c_str());
  354. obs_source_release(source);
  355. obs_source_t *scene_source =
  356. obs_get_source_by_name(scene_name.c_str());
  357. main->SetCurrentScene(scene_source, true);
  358. obs_source_release(scene_source);
  359. };
  360. auto redo = [scene_name, main, newName](const std::string &data) {
  361. obs_source_t *source = obs_get_source_by_name(data.c_str());
  362. obs_source_set_name(source, newName.c_str());
  363. obs_source_release(source);
  364. obs_source_t *scene_source =
  365. obs_get_source_by_name(scene_name.c_str());
  366. main->SetCurrentScene(scene_source, true);
  367. obs_source_release(scene_source);
  368. };
  369. main->undo_s.add_action(QTStr("Undo.Rename").arg(newName.c_str()), undo,
  370. redo, newName, prevName, NULL);
  371. obs_source_set_name(source, newName.c_str());
  372. label->setText(QT_UTF8(newName.c_str()));
  373. }
  374. bool SourceTreeItem::eventFilter(QObject *object, QEvent *event)
  375. {
  376. if (editor != object)
  377. return false;
  378. if (LineEditCanceled(event)) {
  379. QMetaObject::invokeMethod(this, "ExitEditMode",
  380. Qt::QueuedConnection,
  381. Q_ARG(bool, false));
  382. return true;
  383. }
  384. if (LineEditChanged(event)) {
  385. QMetaObject::invokeMethod(this, "ExitEditMode",
  386. Qt::QueuedConnection,
  387. Q_ARG(bool, true));
  388. return true;
  389. }
  390. return false;
  391. }
  392. void SourceTreeItem::VisibilityChanged(bool visible)
  393. {
  394. vis->setChecked(visible);
  395. }
  396. void SourceTreeItem::LockedChanged(bool locked)
  397. {
  398. lock->setChecked(locked);
  399. }
  400. void SourceTreeItem::Renamed(const QString &name)
  401. {
  402. label->setText(name);
  403. }
  404. void SourceTreeItem::Update(bool force)
  405. {
  406. OBSScene scene = GetCurrentScene();
  407. obs_scene_t *itemScene = obs_sceneitem_get_scene(sceneitem);
  408. Type newType;
  409. /* ------------------------------------------------- */
  410. /* if it's a group item, insert group checkbox */
  411. if (obs_sceneitem_is_group(sceneitem)) {
  412. newType = Type::Group;
  413. /* ------------------------------------------------- */
  414. /* if it's a group sub-item */
  415. } else if (itemScene != scene) {
  416. newType = Type::SubItem;
  417. /* ------------------------------------------------- */
  418. /* if it's a regular item */
  419. } else {
  420. newType = Type::Item;
  421. }
  422. /* ------------------------------------------------- */
  423. if (!force && newType == type) {
  424. return;
  425. }
  426. /* ------------------------------------------------- */
  427. ReconnectSignals();
  428. if (spacer) {
  429. boxLayout->removeItem(spacer);
  430. delete spacer;
  431. spacer = nullptr;
  432. }
  433. if (type == Type::Group) {
  434. boxLayout->removeWidget(expand);
  435. expand->deleteLater();
  436. expand = nullptr;
  437. }
  438. type = newType;
  439. if (type == Type::SubItem) {
  440. spacer = new QSpacerItem(16, 1);
  441. boxLayout->insertItem(0, spacer);
  442. } else if (type == Type::Group) {
  443. expand = new SourceTreeSubItemCheckBox();
  444. expand->setSizePolicy(QSizePolicy::Maximum,
  445. QSizePolicy::Maximum);
  446. expand->setMaximumSize(10, 16);
  447. expand->setMinimumSize(10, 0);
  448. #ifdef __APPLE__
  449. expand->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  450. #endif
  451. boxLayout->insertWidget(0, expand);
  452. obs_data_t *data =
  453. obs_sceneitem_get_private_settings(sceneitem);
  454. expand->blockSignals(true);
  455. expand->setChecked(obs_data_get_bool(data, "collapsed"));
  456. expand->blockSignals(false);
  457. obs_data_release(data);
  458. connect(expand, &QPushButton::toggled, this,
  459. &SourceTreeItem::ExpandClicked);
  460. } else {
  461. spacer = new QSpacerItem(3, 1);
  462. boxLayout->insertItem(0, spacer);
  463. }
  464. }
  465. void SourceTreeItem::ExpandClicked(bool checked)
  466. {
  467. OBSData data = obs_sceneitem_get_private_settings(sceneitem);
  468. obs_data_release(data);
  469. obs_data_set_bool(data, "collapsed", checked);
  470. if (!checked)
  471. tree->GetStm()->ExpandGroup(sceneitem);
  472. else
  473. tree->GetStm()->CollapseGroup(sceneitem);
  474. }
  475. void SourceTreeItem::Select()
  476. {
  477. tree->SelectItem(sceneitem, true);
  478. OBSBasic::Get()->UpdateContextBarDeferred();
  479. }
  480. void SourceTreeItem::Deselect()
  481. {
  482. tree->SelectItem(sceneitem, false);
  483. OBSBasic::Get()->UpdateContextBarDeferred();
  484. }
  485. /* ========================================================================= */
  486. void SourceTreeModel::OBSFrontendEvent(enum obs_frontend_event event, void *ptr)
  487. {
  488. SourceTreeModel *stm = reinterpret_cast<SourceTreeModel *>(ptr);
  489. switch ((int)event) {
  490. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  491. stm->SceneChanged();
  492. break;
  493. case OBS_FRONTEND_EVENT_EXIT:
  494. case OBS_FRONTEND_EVENT_SCENE_COLLECTION_CLEANUP:
  495. stm->Clear();
  496. break;
  497. }
  498. }
  499. void SourceTreeModel::Clear()
  500. {
  501. beginResetModel();
  502. items.clear();
  503. endResetModel();
  504. hasGroups = false;
  505. }
  506. static bool enumItem(obs_scene_t *, obs_sceneitem_t *item, void *ptr)
  507. {
  508. QVector<OBSSceneItem> &items =
  509. *reinterpret_cast<QVector<OBSSceneItem> *>(ptr);
  510. obs_source_t *src = obs_sceneitem_get_source(item);
  511. if (obs_source_removed(src)) {
  512. return true;
  513. }
  514. if (obs_sceneitem_is_group(item)) {
  515. obs_data_t *data = obs_sceneitem_get_private_settings(item);
  516. bool collapse = obs_data_get_bool(data, "collapsed");
  517. if (!collapse) {
  518. obs_scene_t *scene =
  519. obs_sceneitem_group_get_scene(item);
  520. obs_scene_enum_items(scene, enumItem, &items);
  521. }
  522. obs_data_release(data);
  523. }
  524. items.insert(0, item);
  525. return true;
  526. }
  527. void SourceTreeModel::SceneChanged()
  528. {
  529. OBSScene scene = GetCurrentScene();
  530. beginResetModel();
  531. items.clear();
  532. obs_scene_enum_items(scene, enumItem, &items);
  533. endResetModel();
  534. UpdateGroupState(false);
  535. st->ResetWidgets();
  536. for (int i = 0; i < items.count(); i++) {
  537. bool select = obs_sceneitem_selected(items[i]);
  538. QModelIndex index = createIndex(i, 0);
  539. st->selectionModel()->select(
  540. index, select ? QItemSelectionModel::Select
  541. : QItemSelectionModel::Deselect);
  542. }
  543. }
  544. /* moves a scene item index (blame linux distros for using older Qt builds) */
  545. static inline void MoveItem(QVector<OBSSceneItem> &items, int oldIdx,
  546. int newIdx)
  547. {
  548. OBSSceneItem item = items[oldIdx];
  549. items.remove(oldIdx);
  550. items.insert(newIdx, item);
  551. }
  552. /* reorders list optimally with model reorder funcs */
  553. void SourceTreeModel::ReorderItems()
  554. {
  555. OBSScene scene = GetCurrentScene();
  556. QVector<OBSSceneItem> newitems;
  557. obs_scene_enum_items(scene, enumItem, &newitems);
  558. /* if item list has changed size, do full reset */
  559. if (newitems.count() != items.count()) {
  560. SceneChanged();
  561. return;
  562. }
  563. for (;;) {
  564. int idx1Old = 0;
  565. int idx1New = 0;
  566. int count;
  567. int i;
  568. /* find first starting changed item index */
  569. for (i = 0; i < newitems.count(); i++) {
  570. obs_sceneitem_t *oldItem = items[i];
  571. obs_sceneitem_t *newItem = newitems[i];
  572. if (oldItem != newItem) {
  573. idx1Old = i;
  574. break;
  575. }
  576. }
  577. /* if everything is the same, break */
  578. if (i == newitems.count()) {
  579. break;
  580. }
  581. /* find new starting index */
  582. for (i = idx1Old + 1; i < newitems.count(); i++) {
  583. obs_sceneitem_t *oldItem = items[idx1Old];
  584. obs_sceneitem_t *newItem = newitems[i];
  585. if (oldItem == newItem) {
  586. idx1New = i;
  587. break;
  588. }
  589. }
  590. /* if item could not be found, do full reset */
  591. if (i == newitems.count()) {
  592. SceneChanged();
  593. return;
  594. }
  595. /* get move count */
  596. for (count = 1; (idx1New + count) < newitems.count(); count++) {
  597. int oldIdx = idx1Old + count;
  598. int newIdx = idx1New + count;
  599. obs_sceneitem_t *oldItem = items[oldIdx];
  600. obs_sceneitem_t *newItem = newitems[newIdx];
  601. if (oldItem != newItem) {
  602. break;
  603. }
  604. }
  605. /* move items */
  606. beginMoveRows(QModelIndex(), idx1Old, idx1Old + count - 1,
  607. QModelIndex(), idx1New + count);
  608. for (i = 0; i < count; i++) {
  609. int to = idx1New + count;
  610. if (to > idx1Old)
  611. to--;
  612. MoveItem(items, idx1Old, to);
  613. }
  614. endMoveRows();
  615. }
  616. }
  617. void SourceTreeModel::Add(obs_sceneitem_t *item)
  618. {
  619. if (obs_sceneitem_is_group(item)) {
  620. SceneChanged();
  621. } else {
  622. beginInsertRows(QModelIndex(), 0, 0);
  623. items.insert(0, item);
  624. endInsertRows();
  625. st->UpdateWidget(createIndex(0, 0, nullptr), item);
  626. }
  627. }
  628. void SourceTreeModel::Remove(obs_sceneitem_t *item)
  629. {
  630. int idx = -1;
  631. for (int i = 0; i < items.count(); i++) {
  632. if (items[i] == item) {
  633. idx = i;
  634. break;
  635. }
  636. }
  637. if (idx == -1)
  638. return;
  639. int startIdx = idx;
  640. int endIdx = idx;
  641. bool is_group = obs_sceneitem_is_group(item);
  642. if (is_group) {
  643. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  644. for (int i = endIdx + 1; i < items.count(); i++) {
  645. obs_sceneitem_t *subitem = items[i];
  646. obs_scene_t *subscene =
  647. obs_sceneitem_get_scene(subitem);
  648. if (subscene == scene)
  649. endIdx = i;
  650. else
  651. break;
  652. }
  653. }
  654. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  655. items.remove(idx, endIdx - startIdx + 1);
  656. endRemoveRows();
  657. if (is_group)
  658. UpdateGroupState(true);
  659. }
  660. OBSSceneItem SourceTreeModel::Get(int idx)
  661. {
  662. if (idx == -1 || idx >= items.count())
  663. return OBSSceneItem();
  664. return items[idx];
  665. }
  666. SourceTreeModel::SourceTreeModel(SourceTree *st_)
  667. : QAbstractListModel(st_), st(st_)
  668. {
  669. obs_frontend_add_event_callback(OBSFrontendEvent, this);
  670. }
  671. SourceTreeModel::~SourceTreeModel()
  672. {
  673. obs_frontend_remove_event_callback(OBSFrontendEvent, this);
  674. }
  675. int SourceTreeModel::rowCount(const QModelIndex &parent) const
  676. {
  677. return parent.isValid() ? 0 : items.count();
  678. }
  679. QVariant SourceTreeModel::data(const QModelIndex &index, int role) const
  680. {
  681. if (role == Qt::AccessibleTextRole) {
  682. OBSSceneItem item = items[index.row()];
  683. obs_source_t *source = obs_sceneitem_get_source(item);
  684. return QVariant(QT_UTF8(obs_source_get_name(source)));
  685. }
  686. return QVariant();
  687. }
  688. Qt::ItemFlags SourceTreeModel::flags(const QModelIndex &index) const
  689. {
  690. if (!index.isValid())
  691. return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled;
  692. obs_sceneitem_t *item = items[index.row()];
  693. bool is_group = obs_sceneitem_is_group(item);
  694. return QAbstractListModel::flags(index) | Qt::ItemIsEditable |
  695. Qt::ItemIsDragEnabled |
  696. (is_group ? Qt::ItemIsDropEnabled : Qt::NoItemFlags);
  697. }
  698. Qt::DropActions SourceTreeModel::supportedDropActions() const
  699. {
  700. return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
  701. }
  702. QString SourceTreeModel::GetNewGroupName()
  703. {
  704. OBSScene scene = GetCurrentScene();
  705. QString name = QTStr("Group");
  706. int i = 2;
  707. for (;;) {
  708. obs_source_t *group = obs_get_source_by_name(QT_TO_UTF8(name));
  709. obs_source_release(group);
  710. if (!group)
  711. break;
  712. name = QTStr("Basic.Main.Group").arg(QString::number(i++));
  713. }
  714. return name;
  715. }
  716. void SourceTreeModel::AddGroup()
  717. {
  718. QString name = GetNewGroupName();
  719. obs_sceneitem_t *group =
  720. obs_scene_add_group(GetCurrentScene(), QT_TO_UTF8(name));
  721. if (!group)
  722. return;
  723. beginInsertRows(QModelIndex(), 0, 0);
  724. items.insert(0, group);
  725. endInsertRows();
  726. st->UpdateWidget(createIndex(0, 0, nullptr), group);
  727. UpdateGroupState(true);
  728. QMetaObject::invokeMethod(st, "Edit", Qt::QueuedConnection,
  729. Q_ARG(int, 0));
  730. }
  731. void SourceTreeModel::GroupSelectedItems(QModelIndexList &indices)
  732. {
  733. if (indices.count() == 0)
  734. return;
  735. OBSScene scene = GetCurrentScene();
  736. QString name = GetNewGroupName();
  737. QVector<obs_sceneitem_t *> item_order;
  738. for (int i = indices.count() - 1; i >= 0; i--) {
  739. obs_sceneitem_t *item = items[indices[i].row()];
  740. item_order << item;
  741. }
  742. obs_sceneitem_t *item = obs_scene_insert_group(
  743. scene, QT_TO_UTF8(name), item_order.data(), item_order.size());
  744. if (!item) {
  745. return;
  746. }
  747. for (obs_sceneitem_t *item : item_order)
  748. obs_sceneitem_select(item, false);
  749. int newIdx = indices[0].row();
  750. beginInsertRows(QModelIndex(), newIdx, newIdx);
  751. items.insert(newIdx, item);
  752. endInsertRows();
  753. for (int i = 0; i < indices.size(); i++) {
  754. int fromIdx = indices[i].row() + 1;
  755. int toIdx = newIdx + i + 1;
  756. if (fromIdx != toIdx) {
  757. beginMoveRows(QModelIndex(), fromIdx, fromIdx,
  758. QModelIndex(), toIdx);
  759. MoveItem(items, fromIdx, toIdx);
  760. endMoveRows();
  761. }
  762. }
  763. hasGroups = true;
  764. st->UpdateWidgets(true);
  765. obs_sceneitem_select(item, true);
  766. QMetaObject::invokeMethod(st, "Edit", Qt::QueuedConnection,
  767. Q_ARG(int, newIdx));
  768. }
  769. void SourceTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
  770. {
  771. if (indices.count() == 0)
  772. return;
  773. for (int i = indices.count() - 1; i >= 0; i--) {
  774. obs_sceneitem_t *item = items[indices[i].row()];
  775. obs_sceneitem_group_ungroup(item);
  776. }
  777. SceneChanged();
  778. }
  779. void SourceTreeModel::ExpandGroup(obs_sceneitem_t *item)
  780. {
  781. int itemIdx = items.indexOf(item);
  782. if (itemIdx == -1)
  783. return;
  784. itemIdx++;
  785. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  786. QVector<OBSSceneItem> subItems;
  787. obs_scene_enum_items(scene, enumItem, &subItems);
  788. if (!subItems.size())
  789. return;
  790. beginInsertRows(QModelIndex(), itemIdx, itemIdx + subItems.size() - 1);
  791. for (int i = 0; i < subItems.size(); i++)
  792. items.insert(i + itemIdx, subItems[i]);
  793. endInsertRows();
  794. st->UpdateWidgets();
  795. }
  796. void SourceTreeModel::CollapseGroup(obs_sceneitem_t *item)
  797. {
  798. int startIdx = -1;
  799. int endIdx = -1;
  800. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  801. for (int i = 0; i < items.size(); i++) {
  802. obs_scene_t *itemScene = obs_sceneitem_get_scene(items[i]);
  803. if (itemScene == scene) {
  804. if (startIdx == -1)
  805. startIdx = i;
  806. endIdx = i;
  807. }
  808. }
  809. if (startIdx == -1)
  810. return;
  811. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  812. items.remove(startIdx, endIdx - startIdx + 1);
  813. endRemoveRows();
  814. }
  815. void SourceTreeModel::UpdateGroupState(bool update)
  816. {
  817. bool nowHasGroups = false;
  818. for (auto &item : items) {
  819. if (obs_sceneitem_is_group(item)) {
  820. nowHasGroups = true;
  821. break;
  822. }
  823. }
  824. if (nowHasGroups != hasGroups) {
  825. hasGroups = nowHasGroups;
  826. if (update) {
  827. st->UpdateWidgets(true);
  828. }
  829. }
  830. }
  831. /* ========================================================================= */
  832. SourceTree::SourceTree(QWidget *parent_) : QListView(parent_)
  833. {
  834. SourceTreeModel *stm_ = new SourceTreeModel(this);
  835. setModel(stm_);
  836. setStyleSheet(QString(
  837. "*[bgColor=\"1\"]{background-color:rgba(255,68,68,33%);}"
  838. "*[bgColor=\"2\"]{background-color:rgba(255,255,68,33%);}"
  839. "*[bgColor=\"3\"]{background-color:rgba(68,255,68,33%);}"
  840. "*[bgColor=\"4\"]{background-color:rgba(68,255,255,33%);}"
  841. "*[bgColor=\"5\"]{background-color:rgba(68,68,255,33%);}"
  842. "*[bgColor=\"6\"]{background-color:rgba(255,68,255,33%);}"
  843. "*[bgColor=\"7\"]{background-color:rgba(68,68,68,33%);}"
  844. "*[bgColor=\"8\"]{background-color:rgba(255,255,255,33%);}"));
  845. setMouseTracking(true);
  846. UpdateNoSourcesMessage();
  847. connect(App(), &OBSApp::StyleChanged, this,
  848. &SourceTree::UpdateNoSourcesMessage);
  849. connect(App(), &OBSApp::StyleChanged, this, &SourceTree::UpdateIcons);
  850. }
  851. void SourceTree::UpdateIcons()
  852. {
  853. SourceTreeModel *stm = GetStm();
  854. stm->SceneChanged();
  855. }
  856. void SourceTree::SetIconsVisible(bool visible)
  857. {
  858. SourceTreeModel *stm = GetStm();
  859. iconsVisible = visible;
  860. stm->SceneChanged();
  861. }
  862. void SourceTree::ResetWidgets()
  863. {
  864. OBSScene scene = GetCurrentScene();
  865. SourceTreeModel *stm = GetStm();
  866. stm->UpdateGroupState(false);
  867. for (int i = 0; i < stm->items.count(); i++) {
  868. QModelIndex index = stm->createIndex(i, 0, nullptr);
  869. setIndexWidget(index, new SourceTreeItem(this, stm->items[i]));
  870. }
  871. }
  872. void SourceTree::UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item)
  873. {
  874. setIndexWidget(idx, new SourceTreeItem(this, item));
  875. }
  876. void SourceTree::UpdateWidgets(bool force)
  877. {
  878. SourceTreeModel *stm = GetStm();
  879. for (int i = 0; i < stm->items.size(); i++) {
  880. obs_sceneitem_t *item = stm->items[i];
  881. SourceTreeItem *widget = GetItemWidget(i);
  882. if (!widget) {
  883. UpdateWidget(stm->createIndex(i, 0), item);
  884. } else {
  885. widget->Update(force);
  886. }
  887. }
  888. }
  889. void SourceTree::SelectItem(obs_sceneitem_t *sceneitem, bool select)
  890. {
  891. SourceTreeModel *stm = GetStm();
  892. int i = 0;
  893. for (; i < stm->items.count(); i++) {
  894. if (stm->items[i] == sceneitem)
  895. break;
  896. }
  897. if (i == stm->items.count())
  898. return;
  899. QModelIndex index = stm->createIndex(i, 0);
  900. if (index.isValid())
  901. selectionModel()->select(
  902. index, select ? QItemSelectionModel::Select
  903. : QItemSelectionModel::Deselect);
  904. }
  905. Q_DECLARE_METATYPE(OBSSceneItem);
  906. void SourceTree::mouseDoubleClickEvent(QMouseEvent *event)
  907. {
  908. if (event->button() == Qt::LeftButton)
  909. QListView::mouseDoubleClickEvent(event);
  910. }
  911. void SourceTree::dropEvent(QDropEvent *event)
  912. {
  913. if (event->source() != this) {
  914. QListView::dropEvent(event);
  915. return;
  916. }
  917. OBSBasic *main = OBSBasic::Get();
  918. OBSScene scene = GetCurrentScene();
  919. obs_source_t *scenesource = obs_scene_get_source(scene);
  920. SourceTreeModel *stm = GetStm();
  921. auto &items = stm->items;
  922. QModelIndexList indices = selectedIndexes();
  923. DropIndicatorPosition indicator = dropIndicatorPosition();
  924. int row = indexAt(event->pos()).row();
  925. bool emptyDrop = row == -1;
  926. if (emptyDrop) {
  927. if (!items.size()) {
  928. QListView::dropEvent(event);
  929. return;
  930. }
  931. row = items.size() - 1;
  932. indicator = QAbstractItemView::BelowItem;
  933. }
  934. /* --------------------------------------- */
  935. /* store destination group if moving to a */
  936. /* group */
  937. obs_sceneitem_t *dropItem = items[row]; /* item being dropped on */
  938. bool itemIsGroup = obs_sceneitem_is_group(dropItem);
  939. obs_sceneitem_t *dropGroup =
  940. itemIsGroup ? dropItem
  941. : obs_sceneitem_get_group(scene, dropItem);
  942. /* not a group if moving above the group */
  943. if (indicator == QAbstractItemView::AboveItem && itemIsGroup)
  944. dropGroup = nullptr;
  945. if (emptyDrop)
  946. dropGroup = nullptr;
  947. /* --------------------------------------- */
  948. /* remember to remove list items if */
  949. /* dropping on collapsed group */
  950. bool dropOnCollapsed = false;
  951. if (dropGroup) {
  952. obs_data_t *data =
  953. obs_sceneitem_get_private_settings(dropGroup);
  954. dropOnCollapsed = obs_data_get_bool(data, "collapsed");
  955. obs_data_release(data);
  956. }
  957. if (indicator == QAbstractItemView::BelowItem ||
  958. indicator == QAbstractItemView::OnItem ||
  959. indicator == QAbstractItemView::OnViewport)
  960. row++;
  961. if (row < 0 || row > stm->items.count()) {
  962. QListView::dropEvent(event);
  963. return;
  964. }
  965. /* --------------------------------------- */
  966. /* determine if any base group is selected */
  967. bool hasGroups = false;
  968. for (int i = 0; i < indices.size(); i++) {
  969. obs_sceneitem_t *item = items[indices[i].row()];
  970. if (obs_sceneitem_is_group(item)) {
  971. hasGroups = true;
  972. break;
  973. }
  974. }
  975. /* --------------------------------------- */
  976. /* if dropping a group, detect if it's */
  977. /* below another group */
  978. obs_sceneitem_t *itemBelow;
  979. if (row == stm->items.count())
  980. itemBelow = nullptr;
  981. else
  982. itemBelow = stm->items[row];
  983. if (hasGroups) {
  984. if (!itemBelow ||
  985. obs_sceneitem_get_group(scene, itemBelow) != dropGroup) {
  986. indicator = QAbstractItemView::BelowItem;
  987. dropGroup = nullptr;
  988. dropOnCollapsed = false;
  989. }
  990. }
  991. /* --------------------------------------- */
  992. /* if dropping groups on other groups, */
  993. /* disregard as invalid drag/drop */
  994. if (dropGroup && hasGroups) {
  995. QListView::dropEvent(event);
  996. return;
  997. }
  998. /* --------------------------------------- */
  999. /* save undo data */
  1000. OBSData undo_data = main->BackupScene(scenesource);
  1001. /* --------------------------------------- */
  1002. /* if selection includes base group items, */
  1003. /* include all group sub-items and treat */
  1004. /* them all as one */
  1005. if (hasGroups) {
  1006. /* remove sub-items if selected */
  1007. for (int i = indices.size() - 1; i >= 0; i--) {
  1008. obs_sceneitem_t *item = items[indices[i].row()];
  1009. obs_scene_t *itemScene = obs_sceneitem_get_scene(item);
  1010. if (itemScene != scene) {
  1011. indices.removeAt(i);
  1012. }
  1013. }
  1014. /* add all sub-items of selected groups */
  1015. for (int i = indices.size() - 1; i >= 0; i--) {
  1016. obs_sceneitem_t *item = items[indices[i].row()];
  1017. if (obs_sceneitem_is_group(item)) {
  1018. for (int j = items.size() - 1; j >= 0; j--) {
  1019. obs_sceneitem_t *subitem = items[j];
  1020. obs_sceneitem_t *subitemGroup =
  1021. obs_sceneitem_get_group(
  1022. scene, subitem);
  1023. if (subitemGroup == item) {
  1024. QModelIndex idx =
  1025. stm->createIndex(j, 0);
  1026. indices.insert(i + 1, idx);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. /* --------------------------------------- */
  1033. /* build persistent indices */
  1034. QList<QPersistentModelIndex> persistentIndices;
  1035. persistentIndices.reserve(indices.count());
  1036. for (QModelIndex &index : indices)
  1037. persistentIndices.append(index);
  1038. std::sort(persistentIndices.begin(), persistentIndices.end());
  1039. /* --------------------------------------- */
  1040. /* move all items to destination index */
  1041. int r = row;
  1042. for (auto &persistentIdx : persistentIndices) {
  1043. int from = persistentIdx.row();
  1044. int to = r;
  1045. int itemTo = to;
  1046. if (itemTo > from)
  1047. itemTo--;
  1048. if (itemTo != from) {
  1049. stm->beginMoveRows(QModelIndex(), from, from,
  1050. QModelIndex(), to);
  1051. MoveItem(items, from, itemTo);
  1052. stm->endMoveRows();
  1053. }
  1054. r = persistentIdx.row() + 1;
  1055. }
  1056. std::sort(persistentIndices.begin(), persistentIndices.end());
  1057. int firstIdx = persistentIndices.front().row();
  1058. int lastIdx = persistentIndices.back().row();
  1059. /* --------------------------------------- */
  1060. /* reorder scene items in back-end */
  1061. QVector<struct obs_sceneitem_order_info> orderList;
  1062. obs_sceneitem_t *lastGroup = nullptr;
  1063. int insertCollapsedIdx = 0;
  1064. auto insertCollapsed = [&](obs_sceneitem_t *item) {
  1065. struct obs_sceneitem_order_info info;
  1066. info.group = lastGroup;
  1067. info.item = item;
  1068. orderList.insert(insertCollapsedIdx++, info);
  1069. };
  1070. using insertCollapsed_t = decltype(insertCollapsed);
  1071. auto preInsertCollapsed = [](obs_scene_t *, obs_sceneitem_t *item,
  1072. void *param) {
  1073. (*reinterpret_cast<insertCollapsed_t *>(param))(item);
  1074. return true;
  1075. };
  1076. auto insertLastGroup = [&]() {
  1077. obs_data_t *data =
  1078. obs_sceneitem_get_private_settings(lastGroup);
  1079. bool collapsed = obs_data_get_bool(data, "collapsed");
  1080. obs_data_release(data);
  1081. if (collapsed) {
  1082. insertCollapsedIdx = 0;
  1083. obs_sceneitem_group_enum_items(lastGroup,
  1084. preInsertCollapsed,
  1085. &insertCollapsed);
  1086. }
  1087. struct obs_sceneitem_order_info info;
  1088. info.group = nullptr;
  1089. info.item = lastGroup;
  1090. orderList.insert(0, info);
  1091. };
  1092. auto updateScene = [&]() {
  1093. struct obs_sceneitem_order_info info;
  1094. for (int i = 0; i < items.size(); i++) {
  1095. obs_sceneitem_t *item = items[i];
  1096. obs_sceneitem_t *group;
  1097. if (obs_sceneitem_is_group(item)) {
  1098. if (lastGroup) {
  1099. insertLastGroup();
  1100. }
  1101. lastGroup = item;
  1102. continue;
  1103. }
  1104. if (!hasGroups && i >= firstIdx && i <= lastIdx)
  1105. group = dropGroup;
  1106. else
  1107. group = obs_sceneitem_get_group(scene, item);
  1108. if (lastGroup && lastGroup != group) {
  1109. insertLastGroup();
  1110. }
  1111. lastGroup = group;
  1112. info.group = group;
  1113. info.item = item;
  1114. orderList.insert(0, info);
  1115. }
  1116. if (lastGroup) {
  1117. insertLastGroup();
  1118. }
  1119. obs_scene_reorder_items2(scene, orderList.data(),
  1120. orderList.size());
  1121. };
  1122. using updateScene_t = decltype(updateScene);
  1123. auto preUpdateScene = [](void *data, obs_scene_t *) {
  1124. (*reinterpret_cast<updateScene_t *>(data))();
  1125. };
  1126. ignoreReorder = true;
  1127. obs_scene_atomic_update(scene, preUpdateScene, &updateScene);
  1128. ignoreReorder = false;
  1129. /* --------------------------------------- */
  1130. /* save redo data */
  1131. OBSData redo_data = main->BackupScene(scenesource);
  1132. /* --------------------------------------- */
  1133. /* add undo/redo action */
  1134. const char *scene_name = obs_source_get_name(scenesource);
  1135. QString action_name = QTStr("Undo.ReorderSources").arg(scene_name);
  1136. main->CreateSceneUndoRedoAction(action_name, undo_data, redo_data);
  1137. /* --------------------------------------- */
  1138. /* remove items if dropped in to collapsed */
  1139. /* group */
  1140. if (dropOnCollapsed) {
  1141. stm->beginRemoveRows(QModelIndex(), firstIdx, lastIdx);
  1142. items.remove(firstIdx, lastIdx - firstIdx + 1);
  1143. stm->endRemoveRows();
  1144. }
  1145. /* --------------------------------------- */
  1146. /* update widgets and accept event */
  1147. UpdateWidgets(true);
  1148. event->accept();
  1149. event->setDropAction(Qt::CopyAction);
  1150. QListView::dropEvent(event);
  1151. }
  1152. void SourceTree::mouseMoveEvent(QMouseEvent *event)
  1153. {
  1154. QPoint pos = event->pos();
  1155. SourceTreeItem *item = qobject_cast<SourceTreeItem *>(childAt(pos));
  1156. OBSBasicPreview *preview = OBSBasicPreview::Get();
  1157. QListView::mouseMoveEvent(event);
  1158. std::lock_guard<std::mutex> lock(preview->selectMutex);
  1159. preview->hoveredPreviewItems.clear();
  1160. if (item)
  1161. preview->hoveredPreviewItems.push_back(item->sceneitem);
  1162. }
  1163. void SourceTree::leaveEvent(QEvent *event)
  1164. {
  1165. OBSBasicPreview *preview = OBSBasicPreview::Get();
  1166. QListView::leaveEvent(event);
  1167. std::lock_guard<std::mutex> lock(preview->selectMutex);
  1168. preview->hoveredPreviewItems.clear();
  1169. }
  1170. void SourceTree::selectionChanged(const QItemSelection &selected,
  1171. const QItemSelection &deselected)
  1172. {
  1173. {
  1174. SignalBlocker sourcesSignalBlocker(this);
  1175. SourceTreeModel *stm = GetStm();
  1176. QModelIndexList selectedIdxs = selected.indexes();
  1177. QModelIndexList deselectedIdxs = deselected.indexes();
  1178. for (int i = 0; i < selectedIdxs.count(); i++) {
  1179. int idx = selectedIdxs[i].row();
  1180. obs_sceneitem_select(stm->items[idx], true);
  1181. }
  1182. for (int i = 0; i < deselectedIdxs.count(); i++) {
  1183. int idx = deselectedIdxs[i].row();
  1184. obs_sceneitem_select(stm->items[idx], false);
  1185. }
  1186. }
  1187. QListView::selectionChanged(selected, deselected);
  1188. }
  1189. void SourceTree::Edit(int row)
  1190. {
  1191. SourceTreeModel *stm = GetStm();
  1192. if (row < 0 || row >= stm->items.count())
  1193. return;
  1194. QModelIndex index = stm->createIndex(row, 0);
  1195. QWidget *widget = indexWidget(index);
  1196. SourceTreeItem *itemWidget = reinterpret_cast<SourceTreeItem *>(widget);
  1197. if (itemWidget->IsEditing())
  1198. return;
  1199. itemWidget->EnterEditMode();
  1200. edit(index);
  1201. }
  1202. bool SourceTree::MultipleBaseSelected() const
  1203. {
  1204. SourceTreeModel *stm = GetStm();
  1205. QModelIndexList selectedIndices = selectedIndexes();
  1206. OBSScene scene = GetCurrentScene();
  1207. if (selectedIndices.size() < 1) {
  1208. return false;
  1209. }
  1210. for (auto &idx : selectedIndices) {
  1211. obs_sceneitem_t *item = stm->items[idx.row()];
  1212. if (obs_sceneitem_is_group(item)) {
  1213. return false;
  1214. }
  1215. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1216. if (itemScene != scene) {
  1217. return false;
  1218. }
  1219. }
  1220. return true;
  1221. }
  1222. bool SourceTree::GroupsSelected() const
  1223. {
  1224. SourceTreeModel *stm = GetStm();
  1225. QModelIndexList selectedIndices = selectedIndexes();
  1226. OBSScene scene = GetCurrentScene();
  1227. if (selectedIndices.size() < 1) {
  1228. return false;
  1229. }
  1230. for (auto &idx : selectedIndices) {
  1231. obs_sceneitem_t *item = stm->items[idx.row()];
  1232. if (!obs_sceneitem_is_group(item)) {
  1233. return false;
  1234. }
  1235. }
  1236. return true;
  1237. }
  1238. bool SourceTree::GroupedItemsSelected() const
  1239. {
  1240. SourceTreeModel *stm = GetStm();
  1241. QModelIndexList selectedIndices = selectedIndexes();
  1242. OBSScene scene = GetCurrentScene();
  1243. if (!selectedIndices.size()) {
  1244. return false;
  1245. }
  1246. for (auto &idx : selectedIndices) {
  1247. obs_sceneitem_t *item = stm->items[idx.row()];
  1248. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1249. if (itemScene != scene) {
  1250. return true;
  1251. }
  1252. }
  1253. return false;
  1254. }
  1255. void SourceTree::Remove(OBSSceneItem item)
  1256. {
  1257. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1258. GetStm()->Remove(item);
  1259. main->SaveProject();
  1260. if (!main->SavingDisabled()) {
  1261. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1262. obs_source_t *sceneSource = obs_scene_get_source(scene);
  1263. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  1264. blog(LOG_INFO, "User Removed source '%s' (%s) from scene '%s'",
  1265. obs_source_get_name(itemSource),
  1266. obs_source_get_id(itemSource),
  1267. obs_source_get_name(sceneSource));
  1268. }
  1269. }
  1270. void SourceTree::GroupSelectedItems()
  1271. {
  1272. QModelIndexList indices = selectedIndexes();
  1273. std::sort(indices.begin(), indices.end());
  1274. GetStm()->GroupSelectedItems(indices);
  1275. }
  1276. void SourceTree::UngroupSelectedGroups()
  1277. {
  1278. QModelIndexList indices = selectedIndexes();
  1279. GetStm()->UngroupSelectedGroups(indices);
  1280. }
  1281. void SourceTree::AddGroup()
  1282. {
  1283. GetStm()->AddGroup();
  1284. }
  1285. void SourceTree::UpdateNoSourcesMessage()
  1286. {
  1287. std::string darkPath;
  1288. GetDataFilePath("themes/Dark/no_sources.svg", darkPath);
  1289. QColor color = palette().text().color();
  1290. bool lightTheme = (color.redF() < 0.5);
  1291. QString file = lightTheme ? ":res/images/no_sources.svg"
  1292. : darkPath.c_str();
  1293. iconNoSources.load(file);
  1294. QTextOption opt(Qt::AlignHCenter);
  1295. opt.setWrapMode(QTextOption::WordWrap);
  1296. textNoSources.setTextOption(opt);
  1297. textNoSources.setText(QTStr("NoSources.Label").replace("\n", "<br/>"));
  1298. textPrepared = false;
  1299. }
  1300. void SourceTree::paintEvent(QPaintEvent *event)
  1301. {
  1302. SourceTreeModel *stm = GetStm();
  1303. if (stm && !stm->items.count()) {
  1304. QPainter p(viewport());
  1305. if (!textPrepared) {
  1306. textNoSources.prepare(QTransform(), p.font());
  1307. textPrepared = true;
  1308. }
  1309. QRectF iconRect = iconNoSources.viewBoxF();
  1310. QSizeF iconSize = iconRect.size();
  1311. QSizeF textSize = textNoSources.size();
  1312. QSizeF thisSize = size();
  1313. qreal totalHeight = textSize.height() + iconSize.height();
  1314. qreal x = thisSize.width() / 2.0 - textSize.width() / 2.0;
  1315. qreal y = thisSize.height() / 2.0 - totalHeight / 2.0;
  1316. p.drawStaticText(x, y, textNoSources);
  1317. x = thisSize.width() / 2.0 - iconSize.width() / 2.0;
  1318. y += textSize.height();
  1319. iconRect.moveTo(x, y);
  1320. iconNoSources.render(&p, iconRect);
  1321. } else {
  1322. QListView::paintEvent(event);
  1323. }
  1324. }