1
0

source-tree.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. hasGroups = true;
  779. st->UpdateWidgets(true);
  780. obs_sceneitem_select(item, true);
  781. /* ----------------------------------------------------------------- */
  782. /* obs_scene_insert_group triggers a full refresh of scene items via */
  783. /* the item_add signal. No need to insert a row, just edit the one */
  784. /* that's created automatically. */
  785. int newIdx = indices[0].row();
  786. QMetaObject::invokeMethod(st, "NewGroupEdit", Qt::QueuedConnection,
  787. Q_ARG(int, newIdx));
  788. }
  789. void SourceTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
  790. {
  791. OBSBasic *main = OBSBasic::Get();
  792. if (indices.count() == 0)
  793. return;
  794. OBSScene scene = main->GetCurrentScene();
  795. OBSData undoData = main->BackupScene(scene);
  796. for (int i = indices.count() - 1; i >= 0; i--) {
  797. obs_sceneitem_t *item = items[indices[i].row()];
  798. obs_sceneitem_group_ungroup(item);
  799. }
  800. SceneChanged();
  801. OBSData redoData = main->BackupScene(scene);
  802. main->CreateSceneUndoRedoAction(QTStr("Basic.Main.Ungroup"), undoData,
  803. redoData);
  804. }
  805. void SourceTreeModel::ExpandGroup(obs_sceneitem_t *item)
  806. {
  807. int itemIdx = items.indexOf(item);
  808. if (itemIdx == -1)
  809. return;
  810. itemIdx++;
  811. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  812. QVector<OBSSceneItem> subItems;
  813. obs_scene_enum_items(scene, enumItem, &subItems);
  814. if (!subItems.size())
  815. return;
  816. beginInsertRows(QModelIndex(), itemIdx, itemIdx + subItems.size() - 1);
  817. for (int i = 0; i < subItems.size(); i++)
  818. items.insert(i + itemIdx, subItems[i]);
  819. endInsertRows();
  820. st->UpdateWidgets();
  821. }
  822. void SourceTreeModel::CollapseGroup(obs_sceneitem_t *item)
  823. {
  824. int startIdx = -1;
  825. int endIdx = -1;
  826. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  827. for (int i = 0; i < items.size(); i++) {
  828. obs_scene_t *itemScene = obs_sceneitem_get_scene(items[i]);
  829. if (itemScene == scene) {
  830. if (startIdx == -1)
  831. startIdx = i;
  832. endIdx = i;
  833. }
  834. }
  835. if (startIdx == -1)
  836. return;
  837. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  838. items.remove(startIdx, endIdx - startIdx + 1);
  839. endRemoveRows();
  840. }
  841. void SourceTreeModel::UpdateGroupState(bool update)
  842. {
  843. bool nowHasGroups = false;
  844. for (auto &item : items) {
  845. if (obs_sceneitem_is_group(item)) {
  846. nowHasGroups = true;
  847. break;
  848. }
  849. }
  850. if (nowHasGroups != hasGroups) {
  851. hasGroups = nowHasGroups;
  852. if (update) {
  853. st->UpdateWidgets(true);
  854. }
  855. }
  856. }
  857. /* ========================================================================= */
  858. SourceTree::SourceTree(QWidget *parent_) : QListView(parent_)
  859. {
  860. SourceTreeModel *stm_ = new SourceTreeModel(this);
  861. setModel(stm_);
  862. setStyleSheet(QString(
  863. "*[bgColor=\"1\"]{background-color:rgba(255,68,68,33%);}"
  864. "*[bgColor=\"2\"]{background-color:rgba(255,255,68,33%);}"
  865. "*[bgColor=\"3\"]{background-color:rgba(68,255,68,33%);}"
  866. "*[bgColor=\"4\"]{background-color:rgba(68,255,255,33%);}"
  867. "*[bgColor=\"5\"]{background-color:rgba(68,68,255,33%);}"
  868. "*[bgColor=\"6\"]{background-color:rgba(255,68,255,33%);}"
  869. "*[bgColor=\"7\"]{background-color:rgba(68,68,68,33%);}"
  870. "*[bgColor=\"8\"]{background-color:rgba(255,255,255,33%);}"));
  871. setMouseTracking(true);
  872. UpdateNoSourcesMessage();
  873. connect(App(), &OBSApp::StyleChanged, this,
  874. &SourceTree::UpdateNoSourcesMessage);
  875. connect(App(), &OBSApp::StyleChanged, this, &SourceTree::UpdateIcons);
  876. }
  877. void SourceTree::UpdateIcons()
  878. {
  879. SourceTreeModel *stm = GetStm();
  880. stm->SceneChanged();
  881. }
  882. void SourceTree::SetIconsVisible(bool visible)
  883. {
  884. SourceTreeModel *stm = GetStm();
  885. iconsVisible = visible;
  886. stm->SceneChanged();
  887. }
  888. void SourceTree::ResetWidgets()
  889. {
  890. OBSScene scene = GetCurrentScene();
  891. SourceTreeModel *stm = GetStm();
  892. stm->UpdateGroupState(false);
  893. for (int i = 0; i < stm->items.count(); i++) {
  894. QModelIndex index = stm->createIndex(i, 0, nullptr);
  895. setIndexWidget(index, new SourceTreeItem(this, stm->items[i]));
  896. }
  897. }
  898. void SourceTree::UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item)
  899. {
  900. setIndexWidget(idx, new SourceTreeItem(this, item));
  901. }
  902. void SourceTree::UpdateWidgets(bool force)
  903. {
  904. SourceTreeModel *stm = GetStm();
  905. for (int i = 0; i < stm->items.size(); i++) {
  906. obs_sceneitem_t *item = stm->items[i];
  907. SourceTreeItem *widget = GetItemWidget(i);
  908. if (!widget) {
  909. UpdateWidget(stm->createIndex(i, 0), item);
  910. } else {
  911. widget->Update(force);
  912. }
  913. }
  914. }
  915. void SourceTree::SelectItem(obs_sceneitem_t *sceneitem, bool select)
  916. {
  917. SourceTreeModel *stm = GetStm();
  918. int i = 0;
  919. for (; i < stm->items.count(); i++) {
  920. if (stm->items[i] == sceneitem)
  921. break;
  922. }
  923. if (i == stm->items.count())
  924. return;
  925. QModelIndex index = stm->createIndex(i, 0);
  926. if (index.isValid())
  927. selectionModel()->select(
  928. index, select ? QItemSelectionModel::Select
  929. : QItemSelectionModel::Deselect);
  930. }
  931. Q_DECLARE_METATYPE(OBSSceneItem);
  932. void SourceTree::mouseDoubleClickEvent(QMouseEvent *event)
  933. {
  934. if (event->button() == Qt::LeftButton)
  935. QListView::mouseDoubleClickEvent(event);
  936. }
  937. void SourceTree::dropEvent(QDropEvent *event)
  938. {
  939. if (event->source() != this) {
  940. QListView::dropEvent(event);
  941. return;
  942. }
  943. OBSBasic *main = OBSBasic::Get();
  944. OBSScene scene = GetCurrentScene();
  945. obs_source_t *scenesource = obs_scene_get_source(scene);
  946. SourceTreeModel *stm = GetStm();
  947. auto &items = stm->items;
  948. QModelIndexList indices = selectedIndexes();
  949. DropIndicatorPosition indicator = dropIndicatorPosition();
  950. int row = indexAt(
  951. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  952. event->position().toPoint()
  953. #else
  954. event->pos()
  955. #endif
  956. )
  957. .row();
  958. bool emptyDrop = row == -1;
  959. if (emptyDrop) {
  960. if (!items.size()) {
  961. QListView::dropEvent(event);
  962. return;
  963. }
  964. row = items.size() - 1;
  965. indicator = QAbstractItemView::BelowItem;
  966. }
  967. /* --------------------------------------- */
  968. /* store destination group if moving to a */
  969. /* group */
  970. obs_sceneitem_t *dropItem = items[row]; /* item being dropped on */
  971. bool itemIsGroup = obs_sceneitem_is_group(dropItem);
  972. obs_sceneitem_t *dropGroup =
  973. itemIsGroup ? dropItem
  974. : obs_sceneitem_get_group(scene, dropItem);
  975. /* not a group if moving above the group */
  976. if (indicator == QAbstractItemView::AboveItem && itemIsGroup)
  977. dropGroup = nullptr;
  978. if (emptyDrop)
  979. dropGroup = nullptr;
  980. /* --------------------------------------- */
  981. /* remember to remove list items if */
  982. /* dropping on collapsed group */
  983. bool dropOnCollapsed = false;
  984. if (dropGroup) {
  985. obs_data_t *data =
  986. obs_sceneitem_get_private_settings(dropGroup);
  987. dropOnCollapsed = obs_data_get_bool(data, "collapsed");
  988. obs_data_release(data);
  989. }
  990. if (indicator == QAbstractItemView::BelowItem ||
  991. indicator == QAbstractItemView::OnItem ||
  992. indicator == QAbstractItemView::OnViewport)
  993. row++;
  994. if (row < 0 || row > stm->items.count()) {
  995. QListView::dropEvent(event);
  996. return;
  997. }
  998. /* --------------------------------------- */
  999. /* determine if any base group is selected */
  1000. bool hasGroups = false;
  1001. for (int i = 0; i < indices.size(); i++) {
  1002. obs_sceneitem_t *item = items[indices[i].row()];
  1003. if (obs_sceneitem_is_group(item)) {
  1004. hasGroups = true;
  1005. break;
  1006. }
  1007. }
  1008. /* --------------------------------------- */
  1009. /* if dropping a group, detect if it's */
  1010. /* below another group */
  1011. obs_sceneitem_t *itemBelow;
  1012. if (row == stm->items.count())
  1013. itemBelow = nullptr;
  1014. else
  1015. itemBelow = stm->items[row];
  1016. if (hasGroups) {
  1017. if (!itemBelow ||
  1018. obs_sceneitem_get_group(scene, itemBelow) != dropGroup) {
  1019. indicator = QAbstractItemView::BelowItem;
  1020. dropGroup = nullptr;
  1021. dropOnCollapsed = false;
  1022. }
  1023. }
  1024. /* --------------------------------------- */
  1025. /* if dropping groups on other groups, */
  1026. /* disregard as invalid drag/drop */
  1027. if (dropGroup && hasGroups) {
  1028. QListView::dropEvent(event);
  1029. return;
  1030. }
  1031. /* --------------------------------------- */
  1032. /* save undo data */
  1033. std::vector<obs_source_t *> sources;
  1034. for (int i = 0; i < indices.size(); i++) {
  1035. obs_sceneitem_t *item = items[indices[i].row()];
  1036. if (obs_sceneitem_get_scene(item) != scene)
  1037. sources.push_back(obs_scene_get_source(
  1038. obs_sceneitem_get_scene(item)));
  1039. }
  1040. if (dropGroup)
  1041. sources.push_back(obs_sceneitem_get_source(dropGroup));
  1042. OBSData undo_data = main->BackupScene(scene, &sources);
  1043. /* --------------------------------------- */
  1044. /* if selection includes base group items, */
  1045. /* include all group sub-items and treat */
  1046. /* them all as one */
  1047. if (hasGroups) {
  1048. /* remove sub-items if selected */
  1049. for (int i = indices.size() - 1; i >= 0; i--) {
  1050. obs_sceneitem_t *item = items[indices[i].row()];
  1051. obs_scene_t *itemScene = obs_sceneitem_get_scene(item);
  1052. if (itemScene != scene) {
  1053. indices.removeAt(i);
  1054. }
  1055. }
  1056. /* add all sub-items of selected groups */
  1057. for (int i = indices.size() - 1; i >= 0; i--) {
  1058. obs_sceneitem_t *item = items[indices[i].row()];
  1059. if (obs_sceneitem_is_group(item)) {
  1060. for (int j = items.size() - 1; j >= 0; j--) {
  1061. obs_sceneitem_t *subitem = items[j];
  1062. obs_sceneitem_t *subitemGroup =
  1063. obs_sceneitem_get_group(
  1064. scene, subitem);
  1065. if (subitemGroup == item) {
  1066. QModelIndex idx =
  1067. stm->createIndex(j, 0);
  1068. indices.insert(i + 1, idx);
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. /* --------------------------------------- */
  1075. /* build persistent indices */
  1076. QList<QPersistentModelIndex> persistentIndices;
  1077. persistentIndices.reserve(indices.count());
  1078. for (QModelIndex &index : indices)
  1079. persistentIndices.append(index);
  1080. std::sort(persistentIndices.begin(), persistentIndices.end());
  1081. /* --------------------------------------- */
  1082. /* move all items to destination index */
  1083. int r = row;
  1084. for (auto &persistentIdx : persistentIndices) {
  1085. int from = persistentIdx.row();
  1086. int to = r;
  1087. int itemTo = to;
  1088. if (itemTo > from)
  1089. itemTo--;
  1090. if (itemTo != from) {
  1091. stm->beginMoveRows(QModelIndex(), from, from,
  1092. QModelIndex(), to);
  1093. MoveItem(items, from, itemTo);
  1094. stm->endMoveRows();
  1095. }
  1096. r = persistentIdx.row() + 1;
  1097. }
  1098. std::sort(persistentIndices.begin(), persistentIndices.end());
  1099. int firstIdx = persistentIndices.front().row();
  1100. int lastIdx = persistentIndices.back().row();
  1101. /* --------------------------------------- */
  1102. /* reorder scene items in back-end */
  1103. QVector<struct obs_sceneitem_order_info> orderList;
  1104. obs_sceneitem_t *lastGroup = nullptr;
  1105. int insertCollapsedIdx = 0;
  1106. auto insertCollapsed = [&](obs_sceneitem_t *item) {
  1107. struct obs_sceneitem_order_info info;
  1108. info.group = lastGroup;
  1109. info.item = item;
  1110. orderList.insert(insertCollapsedIdx++, info);
  1111. };
  1112. using insertCollapsed_t = decltype(insertCollapsed);
  1113. auto preInsertCollapsed = [](obs_scene_t *, obs_sceneitem_t *item,
  1114. void *param) {
  1115. (*reinterpret_cast<insertCollapsed_t *>(param))(item);
  1116. return true;
  1117. };
  1118. auto insertLastGroup = [&]() {
  1119. OBSDataAutoRelease data =
  1120. obs_sceneitem_get_private_settings(lastGroup);
  1121. bool collapsed = obs_data_get_bool(data, "collapsed");
  1122. if (collapsed) {
  1123. insertCollapsedIdx = 0;
  1124. obs_sceneitem_group_enum_items(lastGroup,
  1125. preInsertCollapsed,
  1126. &insertCollapsed);
  1127. }
  1128. struct obs_sceneitem_order_info info;
  1129. info.group = nullptr;
  1130. info.item = lastGroup;
  1131. orderList.insert(0, info);
  1132. };
  1133. auto updateScene = [&]() {
  1134. struct obs_sceneitem_order_info info;
  1135. for (int i = 0; i < items.size(); i++) {
  1136. obs_sceneitem_t *item = items[i];
  1137. obs_sceneitem_t *group;
  1138. if (obs_sceneitem_is_group(item)) {
  1139. if (lastGroup) {
  1140. insertLastGroup();
  1141. }
  1142. lastGroup = item;
  1143. continue;
  1144. }
  1145. if (!hasGroups && i >= firstIdx && i <= lastIdx)
  1146. group = dropGroup;
  1147. else
  1148. group = obs_sceneitem_get_group(scene, item);
  1149. if (lastGroup && lastGroup != group) {
  1150. insertLastGroup();
  1151. }
  1152. lastGroup = group;
  1153. info.group = group;
  1154. info.item = item;
  1155. orderList.insert(0, info);
  1156. }
  1157. if (lastGroup) {
  1158. insertLastGroup();
  1159. }
  1160. obs_scene_reorder_items2(scene, orderList.data(),
  1161. orderList.size());
  1162. };
  1163. using updateScene_t = decltype(updateScene);
  1164. auto preUpdateScene = [](void *data, obs_scene_t *) {
  1165. (*reinterpret_cast<updateScene_t *>(data))();
  1166. };
  1167. ignoreReorder = true;
  1168. obs_scene_atomic_update(scene, preUpdateScene, &updateScene);
  1169. ignoreReorder = false;
  1170. /* --------------------------------------- */
  1171. /* save redo data */
  1172. OBSData redo_data = main->BackupScene(scene, &sources);
  1173. /* --------------------------------------- */
  1174. /* add undo/redo action */
  1175. const char *scene_name = obs_source_get_name(scenesource);
  1176. QString action_name = QTStr("Undo.ReorderSources").arg(scene_name);
  1177. main->CreateSceneUndoRedoAction(action_name, undo_data, redo_data);
  1178. /* --------------------------------------- */
  1179. /* remove items if dropped in to collapsed */
  1180. /* group */
  1181. if (dropOnCollapsed) {
  1182. stm->beginRemoveRows(QModelIndex(), firstIdx, lastIdx);
  1183. items.remove(firstIdx, lastIdx - firstIdx + 1);
  1184. stm->endRemoveRows();
  1185. }
  1186. /* --------------------------------------- */
  1187. /* update widgets and accept event */
  1188. UpdateWidgets(true);
  1189. event->accept();
  1190. event->setDropAction(Qt::CopyAction);
  1191. QListView::dropEvent(event);
  1192. }
  1193. void SourceTree::mouseMoveEvent(QMouseEvent *event)
  1194. {
  1195. QPoint pos = event->pos();
  1196. SourceTreeItem *item = qobject_cast<SourceTreeItem *>(childAt(pos));
  1197. OBSBasicPreview *preview = OBSBasicPreview::Get();
  1198. QListView::mouseMoveEvent(event);
  1199. std::lock_guard<std::mutex> lock(preview->selectMutex);
  1200. preview->hoveredPreviewItems.clear();
  1201. if (item)
  1202. preview->hoveredPreviewItems.push_back(item->sceneitem);
  1203. }
  1204. void SourceTree::leaveEvent(QEvent *event)
  1205. {
  1206. OBSBasicPreview *preview = OBSBasicPreview::Get();
  1207. QListView::leaveEvent(event);
  1208. std::lock_guard<std::mutex> lock(preview->selectMutex);
  1209. preview->hoveredPreviewItems.clear();
  1210. }
  1211. void SourceTree::selectionChanged(const QItemSelection &selected,
  1212. const QItemSelection &deselected)
  1213. {
  1214. {
  1215. SignalBlocker sourcesSignalBlocker(this);
  1216. SourceTreeModel *stm = GetStm();
  1217. QModelIndexList selectedIdxs = selected.indexes();
  1218. QModelIndexList deselectedIdxs = deselected.indexes();
  1219. for (int i = 0; i < selectedIdxs.count(); i++) {
  1220. int idx = selectedIdxs[i].row();
  1221. obs_sceneitem_select(stm->items[idx], true);
  1222. }
  1223. for (int i = 0; i < deselectedIdxs.count(); i++) {
  1224. int idx = deselectedIdxs[i].row();
  1225. obs_sceneitem_select(stm->items[idx], false);
  1226. }
  1227. }
  1228. QListView::selectionChanged(selected, deselected);
  1229. }
  1230. void SourceTree::NewGroupEdit(int row)
  1231. {
  1232. if (!Edit(row)) {
  1233. OBSBasic *main = OBSBasic::Get();
  1234. main->undo_s.pop_disabled();
  1235. blog(LOG_WARNING, "Uh, somehow the edit didn't process, this "
  1236. "code should never be reached.\nAnd by "
  1237. "\"never be reached\", I mean that "
  1238. "theoretically, it should be\nimpossible "
  1239. "for this code to be reached. But if this "
  1240. "code is reached,\nfeel free to laugh at "
  1241. "Jim, because apparently it is, in fact, "
  1242. "actually\npossible for this code to be "
  1243. "reached. But I mean, again, theoretically\n"
  1244. "it should be impossible. So if you see "
  1245. "this in your log, just know that\nit's "
  1246. "really dumb, and depressing. But at least "
  1247. "the undo/redo action is\nstill covered, so "
  1248. "in theory things *should* be fine. But "
  1249. "it's entirely\npossible that they might "
  1250. "not be exactly. But again, yea. This "
  1251. "really\nshould not be possible.");
  1252. OBSData redoSceneData = main->BackupScene(GetCurrentScene());
  1253. QString text = QTStr("Undo.GroupItems").arg("Unknown");
  1254. main->CreateSceneUndoRedoAction(text, undoSceneData,
  1255. redoSceneData);
  1256. undoSceneData = nullptr;
  1257. }
  1258. }
  1259. bool SourceTree::Edit(int row)
  1260. {
  1261. SourceTreeModel *stm = GetStm();
  1262. if (row < 0 || row >= stm->items.count())
  1263. return false;
  1264. QModelIndex index = stm->createIndex(row, 0);
  1265. QWidget *widget = indexWidget(index);
  1266. SourceTreeItem *itemWidget = reinterpret_cast<SourceTreeItem *>(widget);
  1267. if (itemWidget->IsEditing())
  1268. return false;
  1269. itemWidget->EnterEditMode();
  1270. edit(index);
  1271. return true;
  1272. }
  1273. bool SourceTree::MultipleBaseSelected() const
  1274. {
  1275. SourceTreeModel *stm = GetStm();
  1276. QModelIndexList selectedIndices = selectedIndexes();
  1277. OBSScene scene = GetCurrentScene();
  1278. if (selectedIndices.size() < 1) {
  1279. return false;
  1280. }
  1281. for (auto &idx : selectedIndices) {
  1282. obs_sceneitem_t *item = stm->items[idx.row()];
  1283. if (obs_sceneitem_is_group(item)) {
  1284. return false;
  1285. }
  1286. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1287. if (itemScene != scene) {
  1288. return false;
  1289. }
  1290. }
  1291. return true;
  1292. }
  1293. bool SourceTree::GroupsSelected() const
  1294. {
  1295. SourceTreeModel *stm = GetStm();
  1296. QModelIndexList selectedIndices = selectedIndexes();
  1297. OBSScene scene = GetCurrentScene();
  1298. if (selectedIndices.size() < 1) {
  1299. return false;
  1300. }
  1301. for (auto &idx : selectedIndices) {
  1302. obs_sceneitem_t *item = stm->items[idx.row()];
  1303. if (!obs_sceneitem_is_group(item)) {
  1304. return false;
  1305. }
  1306. }
  1307. return true;
  1308. }
  1309. bool SourceTree::GroupedItemsSelected() const
  1310. {
  1311. SourceTreeModel *stm = GetStm();
  1312. QModelIndexList selectedIndices = selectedIndexes();
  1313. OBSScene scene = GetCurrentScene();
  1314. if (!selectedIndices.size()) {
  1315. return false;
  1316. }
  1317. for (auto &idx : selectedIndices) {
  1318. obs_sceneitem_t *item = stm->items[idx.row()];
  1319. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1320. if (itemScene != scene) {
  1321. return true;
  1322. }
  1323. }
  1324. return false;
  1325. }
  1326. void SourceTree::Remove(OBSSceneItem item)
  1327. {
  1328. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1329. GetStm()->Remove(item);
  1330. main->SaveProject();
  1331. if (!main->SavingDisabled()) {
  1332. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1333. obs_source_t *sceneSource = obs_scene_get_source(scene);
  1334. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  1335. blog(LOG_INFO, "User Removed source '%s' (%s) from scene '%s'",
  1336. obs_source_get_name(itemSource),
  1337. obs_source_get_id(itemSource),
  1338. obs_source_get_name(sceneSource));
  1339. }
  1340. }
  1341. void SourceTree::GroupSelectedItems()
  1342. {
  1343. QModelIndexList indices = selectedIndexes();
  1344. std::sort(indices.begin(), indices.end());
  1345. GetStm()->GroupSelectedItems(indices);
  1346. }
  1347. void SourceTree::UngroupSelectedGroups()
  1348. {
  1349. QModelIndexList indices = selectedIndexes();
  1350. GetStm()->UngroupSelectedGroups(indices);
  1351. }
  1352. void SourceTree::AddGroup()
  1353. {
  1354. GetStm()->AddGroup();
  1355. }
  1356. void SourceTree::UpdateNoSourcesMessage()
  1357. {
  1358. std::string darkPath;
  1359. GetDataFilePath("themes/Dark/no_sources.svg", darkPath);
  1360. QString file = !App()->IsThemeDark() ? ":res/images/no_sources.svg"
  1361. : darkPath.c_str();
  1362. iconNoSources.load(file);
  1363. QTextOption opt(Qt::AlignHCenter);
  1364. opt.setWrapMode(QTextOption::WordWrap);
  1365. textNoSources.setTextOption(opt);
  1366. textNoSources.setText(QTStr("NoSources.Label").replace("\n", "<br/>"));
  1367. textPrepared = false;
  1368. }
  1369. void SourceTree::paintEvent(QPaintEvent *event)
  1370. {
  1371. SourceTreeModel *stm = GetStm();
  1372. if (stm && !stm->items.count()) {
  1373. QPainter p(viewport());
  1374. if (!textPrepared) {
  1375. textNoSources.prepare(QTransform(), p.font());
  1376. textPrepared = true;
  1377. }
  1378. QRectF iconRect = iconNoSources.viewBoxF();
  1379. QSizeF iconSize = iconRect.size();
  1380. QSizeF textSize = textNoSources.size();
  1381. QSizeF thisSize = size();
  1382. qreal totalHeight = textSize.height() + iconSize.height();
  1383. qreal x = thisSize.width() / 2.0 - textSize.width() / 2.0;
  1384. qreal y = thisSize.height() / 2.0 - totalHeight / 2.0;
  1385. p.drawStaticText(x, y, textNoSources);
  1386. x = thisSize.width() / 2.0 - iconSize.width() / 2.0;
  1387. y += textSize.height();
  1388. iconRect.moveTo(x, y);
  1389. iconNoSources.render(&p, iconRect);
  1390. } else {
  1391. QListView::paintEvent(event);
  1392. }
  1393. }