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