source-tree.cpp 43 KB

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