source-tree.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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->setEnabled(sourceVisible);
  64. iconLabel->setStyleSheet("background: none");
  65. }
  66. vis = new VisibilityCheckBox();
  67. vis->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  68. vis->setChecked(sourceVisible);
  69. vis->setStyleSheet("background: none");
  70. vis->setAccessibleName(QTStr("Basic.Main.Sources.Visibility"));
  71. vis->setAccessibleDescription(
  72. QTStr("Basic.Main.Sources.VisibilityDescription").arg(name));
  73. lock = new LockedCheckBox();
  74. lock->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  75. lock->setChecked(obs_sceneitem_locked(sceneitem));
  76. lock->setStyleSheet("background: none");
  77. lock->setAccessibleName(QTStr("Basic.Main.Sources.Lock"));
  78. lock->setAccessibleDescription(
  79. QTStr("Basic.Main.Sources.LockDescription").arg(name));
  80. label = new QLabel(QT_UTF8(name));
  81. label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  82. label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  83. label->setAttribute(Qt::WA_TranslucentBackground);
  84. label->setEnabled(sourceVisible);
  85. #ifdef __APPLE__
  86. vis->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  87. lock->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  88. #endif
  89. boxLayout = new QHBoxLayout();
  90. boxLayout->setContentsMargins(0, 0, 0, 0);
  91. if (iconLabel) {
  92. boxLayout->addWidget(iconLabel);
  93. boxLayout->addSpacing(2);
  94. }
  95. boxLayout->addWidget(label);
  96. boxLayout->addWidget(vis);
  97. boxLayout->addSpacing(1);
  98. boxLayout->addWidget(lock);
  99. #ifdef __APPLE__
  100. /* Hack: Fixes a bug where scrollbars would be above the lock icon */
  101. boxLayout->addSpacing(16);
  102. #endif
  103. Update(false);
  104. setLayout(boxLayout);
  105. /* --------------------------------------------------------- */
  106. auto setItemVisible = [this](bool val) {
  107. obs_scene_t *scene = obs_sceneitem_get_scene(sceneitem);
  108. obs_source_t *scenesource = obs_scene_get_source(scene);
  109. int64_t id = obs_sceneitem_get_id(sceneitem);
  110. const char *name = obs_source_get_name(scenesource);
  111. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  112. auto undo_redo = [](const std::string &name, int64_t id,
  113. bool val) {
  114. OBSSourceAutoRelease s =
  115. obs_get_source_by_name(name.c_str());
  116. obs_scene_t *sc = obs_group_or_scene_from_source(s);
  117. obs_sceneitem_t *si =
  118. obs_scene_find_sceneitem_by_id(sc, id);
  119. if (si)
  120. obs_sceneitem_set_visible(si, val);
  121. };
  122. QString str = QTStr(val ? "Undo.ShowSceneItem"
  123. : "Undo.HideSceneItem");
  124. OBSBasic *main = OBSBasic::Get();
  125. main->undo_s.add_action(
  126. str.arg(obs_source_get_name(source), name),
  127. std::bind(undo_redo, std::placeholders::_1, id, !val),
  128. std::bind(undo_redo, std::placeholders::_1, id, val),
  129. name, name);
  130. SignalBlocker sourcesSignalBlocker(this);
  131. obs_sceneitem_set_visible(sceneitem, val);
  132. };
  133. auto setItemLocked = [this](bool checked) {
  134. SignalBlocker sourcesSignalBlocker(this);
  135. obs_sceneitem_set_locked(sceneitem, checked);
  136. };
  137. connect(vis, &QAbstractButton::clicked, setItemVisible);
  138. connect(lock, &QAbstractButton::clicked, setItemLocked);
  139. }
  140. void SourceTreeItem::paintEvent(QPaintEvent *event)
  141. {
  142. QStyleOption opt;
  143. opt.initFrom(this);
  144. QPainter p(this);
  145. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  146. QWidget::paintEvent(event);
  147. }
  148. void SourceTreeItem::DisconnectSignals()
  149. {
  150. sceneRemoveSignal.Disconnect();
  151. itemRemoveSignal.Disconnect();
  152. selectSignal.Disconnect();
  153. deselectSignal.Disconnect();
  154. visibleSignal.Disconnect();
  155. lockedSignal.Disconnect();
  156. renameSignal.Disconnect();
  157. removeSignal.Disconnect();
  158. if (obs_sceneitem_is_group(sceneitem))
  159. groupReorderSignal.Disconnect();
  160. }
  161. void SourceTreeItem::Clear()
  162. {
  163. DisconnectSignals();
  164. sceneitem = nullptr;
  165. }
  166. void SourceTreeItem::ReconnectSignals()
  167. {
  168. if (!sceneitem)
  169. return;
  170. DisconnectSignals();
  171. /* --------------------------------------------------------- */
  172. auto removeItem = [](void *data, calldata_t *cd) {
  173. SourceTreeItem *this_ =
  174. reinterpret_cast<SourceTreeItem *>(data);
  175. obs_sceneitem_t *curItem =
  176. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  177. if (curItem == this_->sceneitem) {
  178. QMetaObject::invokeMethod(this_->tree, "Remove",
  179. Q_ARG(OBSSceneItem, curItem));
  180. curItem = nullptr;
  181. }
  182. if (!curItem)
  183. QMetaObject::invokeMethod(this_, "Clear");
  184. };
  185. auto itemVisible = [](void *data, calldata_t *cd) {
  186. SourceTreeItem *this_ =
  187. reinterpret_cast<SourceTreeItem *>(data);
  188. obs_sceneitem_t *curItem =
  189. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  190. bool visible = calldata_bool(cd, "visible");
  191. if (curItem == this_->sceneitem)
  192. QMetaObject::invokeMethod(this_, "VisibilityChanged",
  193. Q_ARG(bool, visible));
  194. };
  195. auto itemLocked = [](void *data, calldata_t *cd) {
  196. SourceTreeItem *this_ =
  197. reinterpret_cast<SourceTreeItem *>(data);
  198. obs_sceneitem_t *curItem =
  199. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  200. bool locked = calldata_bool(cd, "locked");
  201. if (curItem == this_->sceneitem)
  202. QMetaObject::invokeMethod(this_, "LockedChanged",
  203. Q_ARG(bool, locked));
  204. };
  205. auto itemSelect = [](void *data, calldata_t *cd) {
  206. SourceTreeItem *this_ =
  207. reinterpret_cast<SourceTreeItem *>(data);
  208. obs_sceneitem_t *curItem =
  209. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  210. if (curItem == this_->sceneitem)
  211. QMetaObject::invokeMethod(this_, "Select");
  212. };
  213. auto itemDeselect = [](void *data, calldata_t *cd) {
  214. SourceTreeItem *this_ =
  215. reinterpret_cast<SourceTreeItem *>(data);
  216. obs_sceneitem_t *curItem =
  217. (obs_sceneitem_t *)calldata_ptr(cd, "item");
  218. if (curItem == this_->sceneitem)
  219. QMetaObject::invokeMethod(this_, "Deselect");
  220. };
  221. auto reorderGroup = [](void *data, calldata_t *) {
  222. SourceTreeItem *this_ =
  223. reinterpret_cast<SourceTreeItem *>(data);
  224. QMetaObject::invokeMethod(this_->tree, "ReorderItems");
  225. };
  226. obs_scene_t *scene = obs_sceneitem_get_scene(sceneitem);
  227. obs_source_t *sceneSource = obs_scene_get_source(scene);
  228. signal_handler_t *signal = obs_source_get_signal_handler(sceneSource);
  229. sceneRemoveSignal.Connect(signal, "remove", removeItem, this);
  230. itemRemoveSignal.Connect(signal, "item_remove", removeItem, this);
  231. visibleSignal.Connect(signal, "item_visible", itemVisible, this);
  232. lockedSignal.Connect(signal, "item_locked", itemLocked, this);
  233. selectSignal.Connect(signal, "item_select", itemSelect, this);
  234. deselectSignal.Connect(signal, "item_deselect", itemDeselect, this);
  235. if (obs_sceneitem_is_group(sceneitem)) {
  236. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  237. signal = obs_source_get_signal_handler(source);
  238. groupReorderSignal.Connect(signal, "reorder", reorderGroup,
  239. this);
  240. }
  241. /* --------------------------------------------------------- */
  242. auto renamed = [](void *data, calldata_t *cd) {
  243. SourceTreeItem *this_ =
  244. reinterpret_cast<SourceTreeItem *>(data);
  245. const char *name = calldata_string(cd, "new_name");
  246. QMetaObject::invokeMethod(this_, "Renamed",
  247. Q_ARG(QString, QT_UTF8(name)));
  248. };
  249. auto removeSource = [](void *data, calldata_t *) {
  250. SourceTreeItem *this_ =
  251. reinterpret_cast<SourceTreeItem *>(data);
  252. this_->DisconnectSignals();
  253. this_->sceneitem = nullptr;
  254. QMetaObject::invokeMethod(this_->tree, "RefreshItems");
  255. };
  256. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  257. signal = obs_source_get_signal_handler(source);
  258. renameSignal.Connect(signal, "rename", renamed, this);
  259. removeSignal.Connect(signal, "remove", removeSource, this);
  260. }
  261. void SourceTreeItem::mouseDoubleClickEvent(QMouseEvent *event)
  262. {
  263. QWidget::mouseDoubleClickEvent(event);
  264. if (expand) {
  265. expand->setChecked(!expand->isChecked());
  266. } else {
  267. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  268. OBSBasic *main =
  269. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  270. if (source) {
  271. main->CreatePropertiesWindow(source);
  272. }
  273. }
  274. }
  275. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  276. void SourceTreeItem::enterEvent(QEnterEvent *event)
  277. #else
  278. void SourceTreeItem::enterEvent(QEvent *event)
  279. #endif
  280. {
  281. QWidget::enterEvent(event);
  282. OBSBasicPreview *preview = OBSBasicPreview::Get();
  283. std::lock_guard<std::mutex> lock(preview->selectMutex);
  284. preview->hoveredPreviewItems.clear();
  285. preview->hoveredPreviewItems.push_back(sceneitem);
  286. }
  287. void SourceTreeItem::leaveEvent(QEvent *event)
  288. {
  289. QWidget::leaveEvent(event);
  290. OBSBasicPreview *preview = OBSBasicPreview::Get();
  291. std::lock_guard<std::mutex> lock(preview->selectMutex);
  292. preview->hoveredPreviewItems.clear();
  293. }
  294. bool SourceTreeItem::IsEditing()
  295. {
  296. return editor != nullptr;
  297. }
  298. void SourceTreeItem::EnterEditMode()
  299. {
  300. setFocusPolicy(Qt::StrongFocus);
  301. int index = boxLayout->indexOf(label);
  302. boxLayout->removeWidget(label);
  303. editor = new QLineEdit(label->text());
  304. editor->setStyleSheet("background: none");
  305. editor->selectAll();
  306. editor->installEventFilter(this);
  307. boxLayout->insertWidget(index, editor);
  308. setFocusProxy(editor);
  309. }
  310. void SourceTreeItem::ExitEditMode(bool save)
  311. {
  312. ExitEditModeInternal(save);
  313. if (tree->undoSceneData) {
  314. OBSBasic *main = OBSBasic::Get();
  315. main->undo_s.pop_disabled();
  316. OBSData redoSceneData = main->BackupScene(GetCurrentScene());
  317. QString text = QTStr("Undo.GroupItems").arg(newName.c_str());
  318. main->CreateSceneUndoRedoAction(text, tree->undoSceneData,
  319. redoSceneData);
  320. tree->undoSceneData = nullptr;
  321. }
  322. }
  323. void SourceTreeItem::ExitEditModeInternal(bool save)
  324. {
  325. if (!editor) {
  326. return;
  327. }
  328. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  329. OBSScene scene = main->GetCurrentScene();
  330. newName = QT_TO_UTF8(editor->text());
  331. setFocusProxy(nullptr);
  332. int index = boxLayout->indexOf(editor);
  333. boxLayout->removeWidget(editor);
  334. delete editor;
  335. editor = nullptr;
  336. setFocusPolicy(Qt::NoFocus);
  337. boxLayout->insertWidget(index, label);
  338. label->setFocus();
  339. /* ----------------------------------------- */
  340. /* check for empty string */
  341. if (!save)
  342. return;
  343. if (newName.empty()) {
  344. OBSMessageBox::information(main, QTStr("NoNameEntered.Title"),
  345. QTStr("NoNameEntered.Text"));
  346. return;
  347. }
  348. /* ----------------------------------------- */
  349. /* Check for same name */
  350. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  351. if (newName == obs_source_get_name(source))
  352. return;
  353. /* ----------------------------------------- */
  354. /* check for existing source */
  355. OBSSourceAutoRelease existingSource =
  356. obs_get_source_by_name(newName.c_str());
  357. bool exists = !!existingSource;
  358. if (exists) {
  359. OBSMessageBox::information(main, QTStr("NameExists.Title"),
  360. QTStr("NameExists.Text"));
  361. return;
  362. }
  363. /* ----------------------------------------- */
  364. /* rename */
  365. SignalBlocker sourcesSignalBlocker(this);
  366. std::string prevName(obs_source_get_name(source));
  367. std::string scene_name =
  368. obs_source_get_name(main->GetCurrentSceneSource());
  369. auto undo = [scene_name, prevName, main](const std::string &data) {
  370. OBSSourceAutoRelease source =
  371. obs_get_source_by_name(data.c_str());
  372. obs_source_set_name(source, prevName.c_str());
  373. OBSSourceAutoRelease scene_source =
  374. obs_get_source_by_name(scene_name.c_str());
  375. main->SetCurrentScene(scene_source.Get(), true);
  376. };
  377. std::string editedName = newName;
  378. auto redo = [scene_name, main, editedName](const std::string &data) {
  379. OBSSourceAutoRelease source =
  380. obs_get_source_by_name(data.c_str());
  381. obs_source_set_name(source, editedName.c_str());
  382. OBSSourceAutoRelease scene_source =
  383. obs_get_source_by_name(scene_name.c_str());
  384. main->SetCurrentScene(scene_source.Get(), true);
  385. };
  386. main->undo_s.add_action(QTStr("Undo.Rename").arg(newName.c_str()), undo,
  387. redo, newName, prevName);
  388. obs_source_set_name(source, newName.c_str());
  389. label->setText(QT_UTF8(newName.c_str()));
  390. }
  391. bool SourceTreeItem::eventFilter(QObject *object, QEvent *event)
  392. {
  393. if (editor != object)
  394. return false;
  395. if (LineEditCanceled(event)) {
  396. QMetaObject::invokeMethod(this, "ExitEditMode",
  397. Qt::QueuedConnection,
  398. Q_ARG(bool, false));
  399. return true;
  400. }
  401. if (LineEditChanged(event)) {
  402. QMetaObject::invokeMethod(this, "ExitEditMode",
  403. Qt::QueuedConnection,
  404. Q_ARG(bool, true));
  405. return true;
  406. }
  407. return false;
  408. }
  409. void SourceTreeItem::VisibilityChanged(bool visible)
  410. {
  411. if (iconLabel) {
  412. iconLabel->setEnabled(visible);
  413. }
  414. label->setEnabled(visible);
  415. vis->setChecked(visible);
  416. }
  417. void SourceTreeItem::LockedChanged(bool locked)
  418. {
  419. lock->setChecked(locked);
  420. OBSBasic::Get()->UpdateEditMenu();
  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. OBSBasic::Get()->UpdateEditMenu();
  500. }
  501. void SourceTreeItem::Deselect()
  502. {
  503. tree->SelectItem(sceneitem, false);
  504. OBSBasic::Get()->UpdateContextBarDeferred();
  505. OBSBasic::Get()->UpdateEditMenu();
  506. }
  507. /* ========================================================================= */
  508. void SourceTreeModel::OBSFrontendEvent(enum obs_frontend_event event, void *ptr)
  509. {
  510. SourceTreeModel *stm = reinterpret_cast<SourceTreeModel *>(ptr);
  511. switch ((int)event) {
  512. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  513. stm->SceneChanged();
  514. break;
  515. case OBS_FRONTEND_EVENT_EXIT:
  516. case OBS_FRONTEND_EVENT_SCENE_COLLECTION_CLEANUP:
  517. stm->Clear();
  518. break;
  519. }
  520. }
  521. void SourceTreeModel::Clear()
  522. {
  523. beginResetModel();
  524. items.clear();
  525. endResetModel();
  526. hasGroups = false;
  527. }
  528. static bool enumItem(obs_scene_t *, obs_sceneitem_t *item, void *ptr)
  529. {
  530. QVector<OBSSceneItem> &items =
  531. *reinterpret_cast<QVector<OBSSceneItem> *>(ptr);
  532. obs_source_t *src = obs_sceneitem_get_source(item);
  533. if (obs_source_removed(src)) {
  534. return true;
  535. }
  536. if (obs_sceneitem_is_group(item)) {
  537. OBSDataAutoRelease data =
  538. obs_sceneitem_get_private_settings(item);
  539. bool collapse = obs_data_get_bool(data, "collapsed");
  540. if (!collapse) {
  541. obs_scene_t *scene =
  542. obs_sceneitem_group_get_scene(item);
  543. obs_scene_enum_items(scene, enumItem, &items);
  544. }
  545. }
  546. items.insert(0, item);
  547. return true;
  548. }
  549. void SourceTreeModel::SceneChanged()
  550. {
  551. OBSScene scene = GetCurrentScene();
  552. beginResetModel();
  553. items.clear();
  554. obs_scene_enum_items(scene, enumItem, &items);
  555. endResetModel();
  556. UpdateGroupState(false);
  557. st->ResetWidgets();
  558. for (int i = 0; i < items.count(); i++) {
  559. bool select = obs_sceneitem_selected(items[i]);
  560. QModelIndex index = createIndex(i, 0);
  561. st->selectionModel()->select(
  562. index, select ? QItemSelectionModel::Select
  563. : QItemSelectionModel::Deselect);
  564. }
  565. }
  566. /* moves a scene item index (blame linux distros for using older Qt builds) */
  567. static inline void MoveItem(QVector<OBSSceneItem> &items, int oldIdx,
  568. int newIdx)
  569. {
  570. OBSSceneItem item = items[oldIdx];
  571. items.remove(oldIdx);
  572. items.insert(newIdx, item);
  573. }
  574. /* reorders list optimally with model reorder funcs */
  575. void SourceTreeModel::ReorderItems()
  576. {
  577. OBSScene scene = GetCurrentScene();
  578. QVector<OBSSceneItem> newitems;
  579. obs_scene_enum_items(scene, enumItem, &newitems);
  580. /* if item list has changed size, do full reset */
  581. if (newitems.count() != items.count()) {
  582. SceneChanged();
  583. return;
  584. }
  585. for (;;) {
  586. int idx1Old = 0;
  587. int idx1New = 0;
  588. int count;
  589. int i;
  590. /* find first starting changed item index */
  591. for (i = 0; i < newitems.count(); i++) {
  592. obs_sceneitem_t *oldItem = items[i];
  593. obs_sceneitem_t *newItem = newitems[i];
  594. if (oldItem != newItem) {
  595. idx1Old = i;
  596. break;
  597. }
  598. }
  599. /* if everything is the same, break */
  600. if (i == newitems.count()) {
  601. break;
  602. }
  603. /* find new starting index */
  604. for (i = idx1Old + 1; i < newitems.count(); i++) {
  605. obs_sceneitem_t *oldItem = items[idx1Old];
  606. obs_sceneitem_t *newItem = newitems[i];
  607. if (oldItem == newItem) {
  608. idx1New = i;
  609. break;
  610. }
  611. }
  612. /* if item could not be found, do full reset */
  613. if (i == newitems.count()) {
  614. SceneChanged();
  615. return;
  616. }
  617. /* get move count */
  618. for (count = 1; (idx1New + count) < newitems.count(); count++) {
  619. int oldIdx = idx1Old + count;
  620. int newIdx = idx1New + count;
  621. obs_sceneitem_t *oldItem = items[oldIdx];
  622. obs_sceneitem_t *newItem = newitems[newIdx];
  623. if (oldItem != newItem) {
  624. break;
  625. }
  626. }
  627. /* move items */
  628. beginMoveRows(QModelIndex(), idx1Old, idx1Old + count - 1,
  629. QModelIndex(), idx1New + count);
  630. for (i = 0; i < count; i++) {
  631. int to = idx1New + count;
  632. if (to > idx1Old)
  633. to--;
  634. MoveItem(items, idx1Old, to);
  635. }
  636. endMoveRows();
  637. }
  638. }
  639. void SourceTreeModel::Add(obs_sceneitem_t *item)
  640. {
  641. if (obs_sceneitem_is_group(item)) {
  642. SceneChanged();
  643. } else {
  644. beginInsertRows(QModelIndex(), 0, 0);
  645. items.insert(0, item);
  646. endInsertRows();
  647. st->UpdateWidget(createIndex(0, 0, nullptr), item);
  648. }
  649. }
  650. void SourceTreeModel::Remove(obs_sceneitem_t *item)
  651. {
  652. int idx = -1;
  653. for (int i = 0; i < items.count(); i++) {
  654. if (items[i] == item) {
  655. idx = i;
  656. break;
  657. }
  658. }
  659. if (idx == -1)
  660. return;
  661. int startIdx = idx;
  662. int endIdx = idx;
  663. bool is_group = obs_sceneitem_is_group(item);
  664. if (is_group) {
  665. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  666. for (int i = endIdx + 1; i < items.count(); i++) {
  667. obs_sceneitem_t *subitem = items[i];
  668. obs_scene_t *subscene =
  669. obs_sceneitem_get_scene(subitem);
  670. if (subscene == scene)
  671. endIdx = i;
  672. else
  673. break;
  674. }
  675. }
  676. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  677. items.remove(idx, endIdx - startIdx + 1);
  678. endRemoveRows();
  679. if (is_group)
  680. UpdateGroupState(true);
  681. }
  682. OBSSceneItem SourceTreeModel::Get(int idx)
  683. {
  684. if (idx == -1 || idx >= items.count())
  685. return OBSSceneItem();
  686. return items[idx];
  687. }
  688. SourceTreeModel::SourceTreeModel(SourceTree *st_)
  689. : QAbstractListModel(st_), st(st_)
  690. {
  691. obs_frontend_add_event_callback(OBSFrontendEvent, this);
  692. }
  693. SourceTreeModel::~SourceTreeModel()
  694. {
  695. obs_frontend_remove_event_callback(OBSFrontendEvent, this);
  696. }
  697. int SourceTreeModel::rowCount(const QModelIndex &parent) const
  698. {
  699. return parent.isValid() ? 0 : items.count();
  700. }
  701. QVariant SourceTreeModel::data(const QModelIndex &index, int role) const
  702. {
  703. if (role == Qt::AccessibleTextRole) {
  704. OBSSceneItem item = items[index.row()];
  705. obs_source_t *source = obs_sceneitem_get_source(item);
  706. return QVariant(QT_UTF8(obs_source_get_name(source)));
  707. }
  708. return QVariant();
  709. }
  710. Qt::ItemFlags SourceTreeModel::flags(const QModelIndex &index) const
  711. {
  712. if (!index.isValid())
  713. return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled;
  714. obs_sceneitem_t *item = items[index.row()];
  715. bool is_group = obs_sceneitem_is_group(item);
  716. return QAbstractListModel::flags(index) | Qt::ItemIsEditable |
  717. Qt::ItemIsDragEnabled |
  718. (is_group ? Qt::ItemIsDropEnabled : Qt::NoItemFlags);
  719. }
  720. Qt::DropActions SourceTreeModel::supportedDropActions() const
  721. {
  722. return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
  723. }
  724. QString SourceTreeModel::GetNewGroupName()
  725. {
  726. OBSScene scene = GetCurrentScene();
  727. QString name = QTStr("Group");
  728. int i = 2;
  729. for (;;) {
  730. OBSSourceAutoRelease group =
  731. obs_get_source_by_name(QT_TO_UTF8(name));
  732. if (!group)
  733. break;
  734. name = QTStr("Basic.Main.Group").arg(QString::number(i++));
  735. }
  736. return name;
  737. }
  738. void SourceTreeModel::AddGroup()
  739. {
  740. QString name = GetNewGroupName();
  741. obs_sceneitem_t *group =
  742. obs_scene_add_group(GetCurrentScene(), QT_TO_UTF8(name));
  743. if (!group)
  744. return;
  745. beginInsertRows(QModelIndex(), 0, 0);
  746. items.insert(0, group);
  747. endInsertRows();
  748. st->UpdateWidget(createIndex(0, 0, nullptr), group);
  749. UpdateGroupState(true);
  750. QMetaObject::invokeMethod(st, "Edit", Qt::QueuedConnection,
  751. Q_ARG(int, 0));
  752. }
  753. void SourceTreeModel::GroupSelectedItems(QModelIndexList &indices)
  754. {
  755. if (indices.count() == 0)
  756. return;
  757. OBSBasic *main = OBSBasic::Get();
  758. OBSScene scene = GetCurrentScene();
  759. QString name = GetNewGroupName();
  760. QVector<obs_sceneitem_t *> item_order;
  761. for (int i = indices.count() - 1; i >= 0; i--) {
  762. obs_sceneitem_t *item = items[indices[i].row()];
  763. item_order << item;
  764. }
  765. st->undoSceneData = main->BackupScene(scene);
  766. obs_sceneitem_t *item = obs_scene_insert_group(
  767. scene, QT_TO_UTF8(name), item_order.data(), item_order.size());
  768. if (!item) {
  769. st->undoSceneData = nullptr;
  770. return;
  771. }
  772. main->undo_s.push_disabled();
  773. for (obs_sceneitem_t *item : item_order)
  774. obs_sceneitem_select(item, false);
  775. hasGroups = true;
  776. st->UpdateWidgets(true);
  777. obs_sceneitem_select(item, true);
  778. /* ----------------------------------------------------------------- */
  779. /* obs_scene_insert_group triggers a full refresh of scene items via */
  780. /* the item_add signal. No need to insert a row, just edit the one */
  781. /* that's created automatically. */
  782. int newIdx = indices[0].row();
  783. QMetaObject::invokeMethod(st, "NewGroupEdit", Qt::QueuedConnection,
  784. Q_ARG(int, newIdx));
  785. }
  786. void SourceTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
  787. {
  788. OBSBasic *main = OBSBasic::Get();
  789. if (indices.count() == 0)
  790. return;
  791. OBSScene scene = main->GetCurrentScene();
  792. OBSData undoData = main->BackupScene(scene);
  793. for (int i = indices.count() - 1; i >= 0; i--) {
  794. obs_sceneitem_t *item = items[indices[i].row()];
  795. obs_sceneitem_group_ungroup(item);
  796. }
  797. SceneChanged();
  798. OBSData redoData = main->BackupScene(scene);
  799. main->CreateSceneUndoRedoAction(QTStr("Basic.Main.Ungroup"), undoData,
  800. redoData);
  801. }
  802. void SourceTreeModel::ExpandGroup(obs_sceneitem_t *item)
  803. {
  804. int itemIdx = items.indexOf(item);
  805. if (itemIdx == -1)
  806. return;
  807. itemIdx++;
  808. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  809. QVector<OBSSceneItem> subItems;
  810. obs_scene_enum_items(scene, enumItem, &subItems);
  811. if (!subItems.size())
  812. return;
  813. beginInsertRows(QModelIndex(), itemIdx, itemIdx + subItems.size() - 1);
  814. for (int i = 0; i < subItems.size(); i++)
  815. items.insert(i + itemIdx, subItems[i]);
  816. endInsertRows();
  817. st->UpdateWidgets();
  818. }
  819. void SourceTreeModel::CollapseGroup(obs_sceneitem_t *item)
  820. {
  821. int startIdx = -1;
  822. int endIdx = -1;
  823. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  824. for (int i = 0; i < items.size(); i++) {
  825. obs_scene_t *itemScene = obs_sceneitem_get_scene(items[i]);
  826. if (itemScene == scene) {
  827. if (startIdx == -1)
  828. startIdx = i;
  829. endIdx = i;
  830. }
  831. }
  832. if (startIdx == -1)
  833. return;
  834. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  835. items.remove(startIdx, endIdx - startIdx + 1);
  836. endRemoveRows();
  837. }
  838. void SourceTreeModel::UpdateGroupState(bool update)
  839. {
  840. bool nowHasGroups = false;
  841. for (auto &item : items) {
  842. if (obs_sceneitem_is_group(item)) {
  843. nowHasGroups = true;
  844. break;
  845. }
  846. }
  847. if (nowHasGroups != hasGroups) {
  848. hasGroups = nowHasGroups;
  849. if (update) {
  850. st->UpdateWidgets(true);
  851. }
  852. }
  853. }
  854. /* ========================================================================= */
  855. SourceTree::SourceTree(QWidget *parent_) : QListView(parent_)
  856. {
  857. SourceTreeModel *stm_ = new SourceTreeModel(this);
  858. setModel(stm_);
  859. setStyleSheet(QString(
  860. "*[bgColor=\"1\"]{background-color:rgba(255,68,68,33%);}"
  861. "*[bgColor=\"2\"]{background-color:rgba(255,255,68,33%);}"
  862. "*[bgColor=\"3\"]{background-color:rgba(68,255,68,33%);}"
  863. "*[bgColor=\"4\"]{background-color:rgba(68,255,255,33%);}"
  864. "*[bgColor=\"5\"]{background-color:rgba(68,68,255,33%);}"
  865. "*[bgColor=\"6\"]{background-color:rgba(255,68,255,33%);}"
  866. "*[bgColor=\"7\"]{background-color:rgba(68,68,68,33%);}"
  867. "*[bgColor=\"8\"]{background-color:rgba(255,255,255,33%);}"));
  868. setMouseTracking(true);
  869. UpdateNoSourcesMessage();
  870. connect(App(), &OBSApp::StyleChanged, this,
  871. &SourceTree::UpdateNoSourcesMessage);
  872. connect(App(), &OBSApp::StyleChanged, this, &SourceTree::UpdateIcons);
  873. }
  874. void SourceTree::UpdateIcons()
  875. {
  876. SourceTreeModel *stm = GetStm();
  877. stm->SceneChanged();
  878. }
  879. void SourceTree::SetIconsVisible(bool visible)
  880. {
  881. SourceTreeModel *stm = GetStm();
  882. iconsVisible = visible;
  883. stm->SceneChanged();
  884. }
  885. void SourceTree::ResetWidgets()
  886. {
  887. OBSScene scene = GetCurrentScene();
  888. SourceTreeModel *stm = GetStm();
  889. stm->UpdateGroupState(false);
  890. for (int i = 0; i < stm->items.count(); i++) {
  891. QModelIndex index = stm->createIndex(i, 0, nullptr);
  892. setIndexWidget(index, new SourceTreeItem(this, stm->items[i]));
  893. }
  894. }
  895. void SourceTree::UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item)
  896. {
  897. setIndexWidget(idx, new SourceTreeItem(this, item));
  898. }
  899. void SourceTree::UpdateWidgets(bool force)
  900. {
  901. SourceTreeModel *stm = GetStm();
  902. for (int i = 0; i < stm->items.size(); i++) {
  903. obs_sceneitem_t *item = stm->items[i];
  904. SourceTreeItem *widget = GetItemWidget(i);
  905. if (!widget) {
  906. UpdateWidget(stm->createIndex(i, 0), item);
  907. } else {
  908. widget->Update(force);
  909. }
  910. }
  911. }
  912. void SourceTree::SelectItem(obs_sceneitem_t *sceneitem, bool select)
  913. {
  914. SourceTreeModel *stm = GetStm();
  915. int i = 0;
  916. for (; i < stm->items.count(); i++) {
  917. if (stm->items[i] == sceneitem)
  918. break;
  919. }
  920. if (i == stm->items.count())
  921. return;
  922. QModelIndex index = stm->createIndex(i, 0);
  923. if (index.isValid())
  924. selectionModel()->select(
  925. index, select ? QItemSelectionModel::Select
  926. : QItemSelectionModel::Deselect);
  927. }
  928. Q_DECLARE_METATYPE(OBSSceneItem);
  929. void SourceTree::mouseDoubleClickEvent(QMouseEvent *event)
  930. {
  931. if (event->button() == Qt::LeftButton)
  932. QListView::mouseDoubleClickEvent(event);
  933. }
  934. void SourceTree::dropEvent(QDropEvent *event)
  935. {
  936. if (event->source() != this) {
  937. QListView::dropEvent(event);
  938. return;
  939. }
  940. OBSBasic *main = OBSBasic::Get();
  941. OBSScene scene = GetCurrentScene();
  942. obs_source_t *scenesource = obs_scene_get_source(scene);
  943. SourceTreeModel *stm = GetStm();
  944. auto &items = stm->items;
  945. QModelIndexList indices = selectedIndexes();
  946. DropIndicatorPosition indicator = dropIndicatorPosition();
  947. int row = indexAt(
  948. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  949. event->position().toPoint()
  950. #else
  951. event->pos()
  952. #endif
  953. )
  954. .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. QString file = !App()->IsThemeDark() ? ":res/images/no_sources.svg"
  1358. : darkPath.c_str();
  1359. iconNoSources.load(file);
  1360. QTextOption opt(Qt::AlignHCenter);
  1361. opt.setWrapMode(QTextOption::WordWrap);
  1362. textNoSources.setTextOption(opt);
  1363. textNoSources.setText(QTStr("NoSources.Label").replace("\n", "<br/>"));
  1364. textPrepared = false;
  1365. }
  1366. void SourceTree::paintEvent(QPaintEvent *event)
  1367. {
  1368. SourceTreeModel *stm = GetStm();
  1369. if (stm && !stm->items.count()) {
  1370. QPainter p(viewport());
  1371. if (!textPrepared) {
  1372. textNoSources.prepare(QTransform(), p.font());
  1373. textPrepared = true;
  1374. }
  1375. QRectF iconRect = iconNoSources.viewBoxF();
  1376. QSizeF iconSize = iconRect.size();
  1377. QSizeF textSize = textNoSources.size();
  1378. QSizeF thisSize = size();
  1379. qreal totalHeight = textSize.height() + iconSize.height();
  1380. qreal x = thisSize.width() / 2.0 - textSize.width() / 2.0;
  1381. qreal y = thisSize.height() / 2.0 - totalHeight / 2.0;
  1382. p.drawStaticText(x, y, textNoSources);
  1383. x = thisSize.width() / 2.0 - iconSize.width() / 2.0;
  1384. y += textSize.height();
  1385. iconRect.moveTo(x, y);
  1386. iconNoSources.render(&p, iconRect);
  1387. } else {
  1388. QListView::paintEvent(event);
  1389. }
  1390. }