source-tree.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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_),
  30. sceneitem(sceneitem_)
  31. {
  32. setAttribute(Qt::WA_TranslucentBackground);
  33. setMouseTracking(true);
  34. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  35. const char *name = obs_source_get_name(source);
  36. OBSDataAutoRelease privData =
  37. obs_sceneitem_get_private_settings(sceneitem);
  38. int preset = obs_data_get_int(privData, "color-preset");
  39. if (preset == 1) {
  40. const char *color = obs_data_get_string(privData, "color");
  41. std::string col = "background: ";
  42. col += color;
  43. setStyleSheet(col.c_str());
  44. } else if (preset > 1) {
  45. setStyleSheet("");
  46. setProperty("bgColor", preset - 1);
  47. } else {
  48. setStyleSheet("background: none");
  49. }
  50. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  51. const char *id = obs_source_get_id(source);
  52. bool sourceVisible = obs_sceneitem_visible(sceneitem);
  53. if (tree->iconsVisible) {
  54. QIcon icon;
  55. if (strcmp(id, "scene") == 0)
  56. icon = main->GetSceneIcon();
  57. else if (strcmp(id, "group") == 0)
  58. icon = main->GetGroupIcon();
  59. else
  60. icon = main->GetSourceIcon(id);
  61. QPixmap pixmap = icon.pixmap(QSize(16, 16));
  62. iconLabel = new QLabel();
  63. iconLabel->setPixmap(pixmap);
  64. iconLabel->setEnabled(sourceVisible);
  65. iconLabel->setStyleSheet("background: none");
  66. }
  67. vis = new VisibilityCheckBox();
  68. vis->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  69. vis->setChecked(sourceVisible);
  70. vis->setStyleSheet("background: none");
  71. vis->setAccessibleName(QTStr("Basic.Main.Sources.Visibility"));
  72. vis->setAccessibleDescription(
  73. QTStr("Basic.Main.Sources.VisibilityDescription").arg(name));
  74. lock = new LockedCheckBox();
  75. lock->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  76. lock->setChecked(obs_sceneitem_locked(sceneitem));
  77. lock->setStyleSheet("background: none");
  78. lock->setAccessibleName(QTStr("Basic.Main.Sources.Lock"));
  79. lock->setAccessibleDescription(
  80. QTStr("Basic.Main.Sources.LockDescription").arg(name));
  81. label = new QLabel(QT_UTF8(name));
  82. label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  83. label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  84. label->setAttribute(Qt::WA_TranslucentBackground);
  85. label->setEnabled(sourceVisible);
  86. #ifdef __APPLE__
  87. vis->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  88. lock->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  89. #endif
  90. boxLayout = new QHBoxLayout();
  91. boxLayout->setContentsMargins(0, 0, 0, 0);
  92. if (iconLabel) {
  93. boxLayout->addWidget(iconLabel);
  94. boxLayout->addSpacing(2);
  95. }
  96. boxLayout->addWidget(label);
  97. boxLayout->addWidget(vis);
  98. boxLayout->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. const char *uuid = obs_source_get_uuid(scenesource);
  112. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  113. auto undo_redo = [](const std::string &uuid, int64_t id,
  114. bool val) {
  115. OBSSourceAutoRelease s =
  116. obs_get_source_by_uuid(uuid.c_str());
  117. obs_scene_t *sc = obs_group_or_scene_from_source(s);
  118. obs_sceneitem_t *si =
  119. obs_scene_find_sceneitem_by_id(sc, id);
  120. if (si)
  121. obs_sceneitem_set_visible(si, val);
  122. };
  123. QString str = QTStr(val ? "Undo.ShowSceneItem"
  124. : "Undo.HideSceneItem");
  125. OBSBasic *main = OBSBasic::Get();
  126. main->undo_s.add_action(
  127. str.arg(obs_source_get_name(source), name),
  128. std::bind(undo_redo, std::placeholders::_1, id, !val),
  129. std::bind(undo_redo, std::placeholders::_1, id, val),
  130. uuid, uuid);
  131. SignalBlocker sourcesSignalBlocker(this);
  132. obs_sceneitem_set_visible(sceneitem, val);
  133. };
  134. auto setItemLocked = [this](bool checked) {
  135. SignalBlocker sourcesSignalBlocker(this);
  136. obs_sceneitem_set_locked(sceneitem, checked);
  137. };
  138. connect(vis, &QAbstractButton::clicked, setItemVisible);
  139. connect(lock, &QAbstractButton::clicked, setItemLocked);
  140. }
  141. void SourceTreeItem::paintEvent(QPaintEvent *event)
  142. {
  143. QStyleOption opt;
  144. opt.initFrom(this);
  145. QPainter p(this);
  146. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  147. QWidget::paintEvent(event);
  148. }
  149. void SourceTreeItem::DisconnectSignals()
  150. {
  151. sceneRemoveSignal.Disconnect();
  152. itemRemoveSignal.Disconnect();
  153. selectSignal.Disconnect();
  154. deselectSignal.Disconnect();
  155. visibleSignal.Disconnect();
  156. lockedSignal.Disconnect();
  157. renameSignal.Disconnect();
  158. removeSignal.Disconnect();
  159. if (obs_sceneitem_is_group(sceneitem))
  160. groupReorderSignal.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 (obs_source_configurable(source)) {
  272. main->CreatePropertiesWindow(source);
  273. }
  274. }
  275. }
  276. void SourceTreeItem::enterEvent(QEnterEvent *event)
  277. {
  278. QWidget::enterEvent(event);
  279. OBSBasicPreview *preview = OBSBasicPreview::Get();
  280. std::lock_guard<std::mutex> lock(preview->selectMutex);
  281. preview->hoveredPreviewItems.clear();
  282. preview->hoveredPreviewItems.push_back(sceneitem);
  283. }
  284. void SourceTreeItem::leaveEvent(QEvent *event)
  285. {
  286. QWidget::leaveEvent(event);
  287. OBSBasicPreview *preview = OBSBasicPreview::Get();
  288. std::lock_guard<std::mutex> lock(preview->selectMutex);
  289. preview->hoveredPreviewItems.clear();
  290. }
  291. bool SourceTreeItem::IsEditing()
  292. {
  293. return editor != nullptr;
  294. }
  295. void SourceTreeItem::EnterEditMode()
  296. {
  297. setFocusPolicy(Qt::StrongFocus);
  298. int index = boxLayout->indexOf(label);
  299. boxLayout->removeWidget(label);
  300. editor = new QLineEdit(label->text());
  301. editor->setStyleSheet("background: none");
  302. editor->selectAll();
  303. editor->installEventFilter(this);
  304. boxLayout->insertWidget(index, editor);
  305. setFocusProxy(editor);
  306. }
  307. void SourceTreeItem::ExitEditMode(bool save)
  308. {
  309. ExitEditModeInternal(save);
  310. if (tree->undoSceneData) {
  311. OBSBasic *main = OBSBasic::Get();
  312. main->undo_s.pop_disabled();
  313. OBSData redoSceneData = main->BackupScene(GetCurrentScene());
  314. QString text = QTStr("Undo.GroupItems").arg(newName.c_str());
  315. main->CreateSceneUndoRedoAction(text, tree->undoSceneData,
  316. redoSceneData);
  317. tree->undoSceneData = nullptr;
  318. }
  319. }
  320. void SourceTreeItem::ExitEditModeInternal(bool save)
  321. {
  322. if (!editor) {
  323. return;
  324. }
  325. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  326. OBSScene scene = main->GetCurrentScene();
  327. newName = QT_TO_UTF8(editor->text());
  328. setFocusProxy(nullptr);
  329. int index = boxLayout->indexOf(editor);
  330. boxLayout->removeWidget(editor);
  331. delete editor;
  332. editor = nullptr;
  333. setFocusPolicy(Qt::NoFocus);
  334. boxLayout->insertWidget(index, label);
  335. setFocus();
  336. /* ----------------------------------------- */
  337. /* check for empty string */
  338. if (!save)
  339. return;
  340. if (newName.empty()) {
  341. OBSMessageBox::information(main, QTStr("NoNameEntered.Title"),
  342. QTStr("NoNameEntered.Text"));
  343. return;
  344. }
  345. /* ----------------------------------------- */
  346. /* Check for same name */
  347. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  348. if (newName == obs_source_get_name(source))
  349. return;
  350. /* ----------------------------------------- */
  351. /* check for existing source */
  352. OBSSourceAutoRelease existingSource =
  353. obs_get_source_by_name(newName.c_str());
  354. bool exists = !!existingSource;
  355. if (exists) {
  356. OBSMessageBox::information(main, QTStr("NameExists.Title"),
  357. QTStr("NameExists.Text"));
  358. return;
  359. }
  360. /* ----------------------------------------- */
  361. /* rename */
  362. SignalBlocker sourcesSignalBlocker(this);
  363. std::string prevName(obs_source_get_name(source));
  364. std::string scene_uuid =
  365. obs_source_get_uuid(main->GetCurrentSceneSource());
  366. auto undo = [scene_uuid, prevName, main](const std::string &data) {
  367. OBSSourceAutoRelease source =
  368. obs_get_source_by_uuid(data.c_str());
  369. obs_source_set_name(source, prevName.c_str());
  370. OBSSourceAutoRelease scene_source =
  371. obs_get_source_by_uuid(scene_uuid.c_str());
  372. main->SetCurrentScene(scene_source.Get(), true);
  373. };
  374. std::string editedName = newName;
  375. auto redo = [scene_uuid, main, editedName](const std::string &data) {
  376. OBSSourceAutoRelease source =
  377. obs_get_source_by_uuid(data.c_str());
  378. obs_source_set_name(source, editedName.c_str());
  379. OBSSourceAutoRelease scene_source =
  380. obs_get_source_by_uuid(scene_uuid.c_str());
  381. main->SetCurrentScene(scene_source.Get(), true);
  382. };
  383. const char *uuid = obs_source_get_uuid(source);
  384. main->undo_s.add_action(QTStr("Undo.Rename").arg(newName.c_str()), undo,
  385. redo, uuid, uuid);
  386. obs_source_set_name(source, newName.c_str());
  387. label->setText(QT_UTF8(newName.c_str()));
  388. }
  389. bool SourceTreeItem::eventFilter(QObject *object, QEvent *event)
  390. {
  391. if (editor != object)
  392. return false;
  393. if (LineEditCanceled(event)) {
  394. QMetaObject::invokeMethod(this, "ExitEditMode",
  395. Qt::QueuedConnection,
  396. Q_ARG(bool, false));
  397. return true;
  398. }
  399. if (LineEditChanged(event)) {
  400. QMetaObject::invokeMethod(this, "ExitEditMode",
  401. Qt::QueuedConnection,
  402. Q_ARG(bool, true));
  403. return true;
  404. }
  405. return false;
  406. }
  407. void SourceTreeItem::VisibilityChanged(bool visible)
  408. {
  409. if (iconLabel) {
  410. iconLabel->setEnabled(visible);
  411. }
  412. label->setEnabled(visible);
  413. vis->setChecked(visible);
  414. }
  415. void SourceTreeItem::LockedChanged(bool locked)
  416. {
  417. lock->setChecked(locked);
  418. OBSBasic::Get()->UpdateEditMenu();
  419. }
  420. void SourceTreeItem::Renamed(const QString &name)
  421. {
  422. label->setText(name);
  423. }
  424. void SourceTreeItem::Update(bool force)
  425. {
  426. OBSScene scene = GetCurrentScene();
  427. obs_scene_t *itemScene = obs_sceneitem_get_scene(sceneitem);
  428. Type newType;
  429. /* ------------------------------------------------- */
  430. /* if it's a group item, insert group checkbox */
  431. if (obs_sceneitem_is_group(sceneitem)) {
  432. newType = Type::Group;
  433. /* ------------------------------------------------- */
  434. /* if it's a group sub-item */
  435. } else if (itemScene != scene) {
  436. newType = Type::SubItem;
  437. /* ------------------------------------------------- */
  438. /* if it's a regular item */
  439. } else {
  440. newType = Type::Item;
  441. }
  442. /* ------------------------------------------------- */
  443. if (!force && newType == type) {
  444. return;
  445. }
  446. /* ------------------------------------------------- */
  447. ReconnectSignals();
  448. if (spacer) {
  449. boxLayout->removeItem(spacer);
  450. delete spacer;
  451. spacer = nullptr;
  452. }
  453. if (type == Type::Group) {
  454. boxLayout->removeWidget(expand);
  455. expand->deleteLater();
  456. expand = nullptr;
  457. }
  458. type = newType;
  459. if (type == Type::SubItem) {
  460. spacer = new QSpacerItem(16, 1);
  461. boxLayout->insertItem(0, spacer);
  462. } else if (type == Type::Group) {
  463. expand = new SourceTreeSubItemCheckBox();
  464. expand->setSizePolicy(QSizePolicy::Maximum,
  465. QSizePolicy::Maximum);
  466. expand->setMaximumSize(10, 16);
  467. expand->setMinimumSize(10, 0);
  468. #ifdef __APPLE__
  469. expand->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  470. #endif
  471. boxLayout->insertWidget(0, expand);
  472. OBSDataAutoRelease data =
  473. obs_sceneitem_get_private_settings(sceneitem);
  474. expand->blockSignals(true);
  475. expand->setChecked(obs_data_get_bool(data, "collapsed"));
  476. expand->blockSignals(false);
  477. connect(expand, &QPushButton::toggled, this,
  478. &SourceTreeItem::ExpandClicked);
  479. } else {
  480. spacer = new QSpacerItem(3, 1);
  481. boxLayout->insertItem(0, spacer);
  482. }
  483. }
  484. void SourceTreeItem::ExpandClicked(bool checked)
  485. {
  486. OBSDataAutoRelease data = obs_sceneitem_get_private_settings(sceneitem);
  487. obs_data_set_bool(data, "collapsed", checked);
  488. if (!checked)
  489. tree->GetStm()->ExpandGroup(sceneitem);
  490. else
  491. tree->GetStm()->CollapseGroup(sceneitem);
  492. }
  493. void SourceTreeItem::Select()
  494. {
  495. tree->SelectItem(sceneitem, true);
  496. OBSBasic::Get()->UpdateContextBarDeferred();
  497. OBSBasic::Get()->UpdateEditMenu();
  498. }
  499. void SourceTreeItem::Deselect()
  500. {
  501. tree->SelectItem(sceneitem, false);
  502. OBSBasic::Get()->UpdateContextBarDeferred();
  503. OBSBasic::Get()->UpdateEditMenu();
  504. }
  505. /* ========================================================================= */
  506. void SourceTreeModel::OBSFrontendEvent(enum obs_frontend_event event, void *ptr)
  507. {
  508. SourceTreeModel *stm = reinterpret_cast<SourceTreeModel *>(ptr);
  509. switch (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. default:
  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_),
  690. st(st_)
  691. {
  692. obs_frontend_add_event_callback(OBSFrontendEvent, this);
  693. }
  694. SourceTreeModel::~SourceTreeModel()
  695. {
  696. obs_frontend_remove_event_callback(OBSFrontendEvent, this);
  697. }
  698. int SourceTreeModel::rowCount(const QModelIndex &parent) const
  699. {
  700. return parent.isValid() ? 0 : items.count();
  701. }
  702. QVariant SourceTreeModel::data(const QModelIndex &index, int role) const
  703. {
  704. if (role == Qt::AccessibleTextRole) {
  705. OBSSceneItem item = items[index.row()];
  706. obs_source_t *source = obs_sceneitem_get_source(item);
  707. return QVariant(QT_UTF8(obs_source_get_name(source)));
  708. }
  709. return QVariant();
  710. }
  711. Qt::ItemFlags SourceTreeModel::flags(const QModelIndex &index) const
  712. {
  713. if (!index.isValid())
  714. return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled;
  715. obs_sceneitem_t *item = items[index.row()];
  716. bool is_group = obs_sceneitem_is_group(item);
  717. return QAbstractListModel::flags(index) | Qt::ItemIsEditable |
  718. Qt::ItemIsDragEnabled |
  719. (is_group ? Qt::ItemIsDropEnabled : Qt::NoItemFlags);
  720. }
  721. Qt::DropActions SourceTreeModel::supportedDropActions() const
  722. {
  723. return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
  724. }
  725. QString SourceTreeModel::GetNewGroupName()
  726. {
  727. OBSScene scene = GetCurrentScene();
  728. QString name = QTStr("Group");
  729. int i = 2;
  730. for (;;) {
  731. OBSSourceAutoRelease group =
  732. obs_get_source_by_name(QT_TO_UTF8(name));
  733. if (!group)
  734. break;
  735. name = QTStr("Basic.Main.Group").arg(QString::number(i++));
  736. }
  737. return name;
  738. }
  739. void SourceTreeModel::AddGroup()
  740. {
  741. QString name = GetNewGroupName();
  742. obs_sceneitem_t *group =
  743. obs_scene_add_group(GetCurrentScene(), QT_TO_UTF8(name));
  744. if (!group)
  745. return;
  746. beginInsertRows(QModelIndex(), 0, 0);
  747. items.insert(0, group);
  748. endInsertRows();
  749. st->UpdateWidget(createIndex(0, 0, nullptr), group);
  750. UpdateGroupState(true);
  751. QMetaObject::invokeMethod(st, "Edit", Qt::QueuedConnection,
  752. Q_ARG(int, 0));
  753. }
  754. void SourceTreeModel::GroupSelectedItems(QModelIndexList &indices)
  755. {
  756. if (indices.count() == 0)
  757. return;
  758. OBSBasic *main = OBSBasic::Get();
  759. OBSScene scene = GetCurrentScene();
  760. QString name = GetNewGroupName();
  761. QVector<obs_sceneitem_t *> item_order;
  762. for (int i = indices.count() - 1; i >= 0; i--) {
  763. obs_sceneitem_t *item = items[indices[i].row()];
  764. item_order << item;
  765. }
  766. st->undoSceneData = main->BackupScene(scene);
  767. obs_sceneitem_t *item = obs_scene_insert_group(
  768. scene, QT_TO_UTF8(name), item_order.data(), item_order.size());
  769. if (!item) {
  770. st->undoSceneData = nullptr;
  771. return;
  772. }
  773. main->undo_s.push_disabled();
  774. for (obs_sceneitem_t *item : item_order)
  775. obs_sceneitem_select(item, false);
  776. hasGroups = true;
  777. st->UpdateWidgets(true);
  778. obs_sceneitem_select(item, true);
  779. /* ----------------------------------------------------------------- */
  780. /* obs_scene_insert_group triggers a full refresh of scene items via */
  781. /* the item_add signal. No need to insert a row, just edit the one */
  782. /* that's created automatically. */
  783. int newIdx = indices[0].row();
  784. QMetaObject::invokeMethod(st, "NewGroupEdit", Qt::QueuedConnection,
  785. Q_ARG(int, newIdx));
  786. }
  787. void SourceTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
  788. {
  789. OBSBasic *main = OBSBasic::Get();
  790. if (indices.count() == 0)
  791. return;
  792. OBSScene scene = main->GetCurrentScene();
  793. OBSData undoData = main->BackupScene(scene);
  794. for (int i = indices.count() - 1; i >= 0; i--) {
  795. obs_sceneitem_t *item = items[indices[i].row()];
  796. obs_sceneitem_group_ungroup(item);
  797. }
  798. SceneChanged();
  799. OBSData redoData = main->BackupScene(scene);
  800. main->CreateSceneUndoRedoAction(QTStr("Basic.Main.Ungroup"), undoData,
  801. redoData);
  802. }
  803. void SourceTreeModel::ExpandGroup(obs_sceneitem_t *item)
  804. {
  805. int itemIdx = items.indexOf(item);
  806. if (itemIdx == -1)
  807. return;
  808. itemIdx++;
  809. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  810. QVector<OBSSceneItem> subItems;
  811. obs_scene_enum_items(scene, enumItem, &subItems);
  812. if (!subItems.size())
  813. return;
  814. beginInsertRows(QModelIndex(), itemIdx, itemIdx + subItems.size() - 1);
  815. for (int i = 0; i < subItems.size(); i++)
  816. items.insert(i + itemIdx, subItems[i]);
  817. endInsertRows();
  818. st->UpdateWidgets();
  819. }
  820. void SourceTreeModel::CollapseGroup(obs_sceneitem_t *item)
  821. {
  822. int startIdx = -1;
  823. int endIdx = -1;
  824. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  825. for (int i = 0; i < items.size(); i++) {
  826. obs_scene_t *itemScene = obs_sceneitem_get_scene(items[i]);
  827. if (itemScene == scene) {
  828. if (startIdx == -1)
  829. startIdx = i;
  830. endIdx = i;
  831. }
  832. }
  833. if (startIdx == -1)
  834. return;
  835. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  836. items.remove(startIdx, endIdx - startIdx + 1);
  837. endRemoveRows();
  838. }
  839. void SourceTreeModel::UpdateGroupState(bool update)
  840. {
  841. bool nowHasGroups = false;
  842. for (auto &item : items) {
  843. if (obs_sceneitem_is_group(item)) {
  844. nowHasGroups = true;
  845. break;
  846. }
  847. }
  848. if (nowHasGroups != hasGroups) {
  849. hasGroups = nowHasGroups;
  850. if (update) {
  851. st->UpdateWidgets(true);
  852. }
  853. }
  854. }
  855. /* ========================================================================= */
  856. SourceTree::SourceTree(QWidget *parent_) : QListView(parent_)
  857. {
  858. SourceTreeModel *stm_ = new SourceTreeModel(this);
  859. setModel(stm_);
  860. setStyleSheet(QString(
  861. "*[bgColor=\"1\"]{background-color:rgba(255,68,68,33%);}"
  862. "*[bgColor=\"2\"]{background-color:rgba(255,255,68,33%);}"
  863. "*[bgColor=\"3\"]{background-color:rgba(68,255,68,33%);}"
  864. "*[bgColor=\"4\"]{background-color:rgba(68,255,255,33%);}"
  865. "*[bgColor=\"5\"]{background-color:rgba(68,68,255,33%);}"
  866. "*[bgColor=\"6\"]{background-color:rgba(255,68,255,33%);}"
  867. "*[bgColor=\"7\"]{background-color:rgba(68,68,68,33%);}"
  868. "*[bgColor=\"8\"]{background-color:rgba(255,255,255,33%);}"));
  869. UpdateNoSourcesMessage();
  870. connect(App(), &OBSApp::StyleChanged, this,
  871. &SourceTree::UpdateNoSourcesMessage);
  872. connect(App(), &OBSApp::StyleChanged, this, &SourceTree::UpdateIcons);
  873. setItemDelegate(new SourceTreeDelegate(this));
  874. }
  875. void SourceTree::UpdateIcons()
  876. {
  877. SourceTreeModel *stm = GetStm();
  878. stm->SceneChanged();
  879. }
  880. void SourceTree::SetIconsVisible(bool visible)
  881. {
  882. SourceTreeModel *stm = GetStm();
  883. iconsVisible = visible;
  884. stm->SceneChanged();
  885. }
  886. void SourceTree::ResetWidgets()
  887. {
  888. OBSScene scene = GetCurrentScene();
  889. SourceTreeModel *stm = GetStm();
  890. stm->UpdateGroupState(false);
  891. for (int i = 0; i < stm->items.count(); i++) {
  892. QModelIndex index = stm->createIndex(i, 0, nullptr);
  893. setIndexWidget(index, new SourceTreeItem(this, stm->items[i]));
  894. }
  895. }
  896. void SourceTree::UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item)
  897. {
  898. setIndexWidget(idx, new SourceTreeItem(this, item));
  899. }
  900. void SourceTree::UpdateWidgets(bool force)
  901. {
  902. SourceTreeModel *stm = GetStm();
  903. for (int i = 0; i < stm->items.size(); i++) {
  904. obs_sceneitem_t *item = stm->items[i];
  905. SourceTreeItem *widget = GetItemWidget(i);
  906. if (!widget) {
  907. UpdateWidget(stm->createIndex(i, 0), item);
  908. } else {
  909. widget->Update(force);
  910. }
  911. }
  912. }
  913. void SourceTree::SelectItem(obs_sceneitem_t *sceneitem, bool select)
  914. {
  915. SourceTreeModel *stm = GetStm();
  916. int i = 0;
  917. for (; i < stm->items.count(); i++) {
  918. if (stm->items[i] == sceneitem)
  919. break;
  920. }
  921. if (i == stm->items.count())
  922. return;
  923. QModelIndex index = stm->createIndex(i, 0);
  924. if (index.isValid() && select != selectionModel()->isSelected(index))
  925. selectionModel()->select(
  926. index, select ? QItemSelectionModel::Select
  927. : QItemSelectionModel::Deselect);
  928. }
  929. Q_DECLARE_METATYPE(OBSSceneItem);
  930. void SourceTree::mouseDoubleClickEvent(QMouseEvent *event)
  931. {
  932. if (event->button() == Qt::LeftButton)
  933. QListView::mouseDoubleClickEvent(event);
  934. }
  935. void SourceTree::dropEvent(QDropEvent *event)
  936. {
  937. if (event->source() != this) {
  938. QListView::dropEvent(event);
  939. return;
  940. }
  941. OBSBasic *main = OBSBasic::Get();
  942. OBSScene scene = GetCurrentScene();
  943. obs_source_t *scenesource = obs_scene_get_source(scene);
  944. SourceTreeModel *stm = GetStm();
  945. auto &items = stm->items;
  946. QModelIndexList indices = selectedIndexes();
  947. DropIndicatorPosition indicator = dropIndicatorPosition();
  948. int row = indexAt(event->position().toPoint()).row();
  949. bool emptyDrop = row == -1;
  950. if (emptyDrop) {
  951. if (!items.size()) {
  952. QListView::dropEvent(event);
  953. return;
  954. }
  955. row = items.size() - 1;
  956. indicator = QAbstractItemView::BelowItem;
  957. }
  958. /* --------------------------------------- */
  959. /* store destination group if moving to a */
  960. /* group */
  961. obs_sceneitem_t *dropItem = items[row]; /* item being dropped on */
  962. bool itemIsGroup = obs_sceneitem_is_group(dropItem);
  963. obs_sceneitem_t *dropGroup =
  964. itemIsGroup ? dropItem
  965. : obs_sceneitem_get_group(scene, dropItem);
  966. /* not a group if moving above the group */
  967. if (indicator == QAbstractItemView::AboveItem && itemIsGroup)
  968. dropGroup = nullptr;
  969. if (emptyDrop)
  970. dropGroup = nullptr;
  971. /* --------------------------------------- */
  972. /* remember to remove list items if */
  973. /* dropping on collapsed group */
  974. bool dropOnCollapsed = false;
  975. if (dropGroup) {
  976. obs_data_t *data =
  977. obs_sceneitem_get_private_settings(dropGroup);
  978. dropOnCollapsed = obs_data_get_bool(data, "collapsed");
  979. obs_data_release(data);
  980. }
  981. if (indicator == QAbstractItemView::BelowItem ||
  982. indicator == QAbstractItemView::OnItem ||
  983. indicator == QAbstractItemView::OnViewport)
  984. row++;
  985. if (row < 0 || row > stm->items.count()) {
  986. QListView::dropEvent(event);
  987. return;
  988. }
  989. /* --------------------------------------- */
  990. /* determine if any base group is selected */
  991. bool hasGroups = false;
  992. for (int i = 0; i < indices.size(); i++) {
  993. obs_sceneitem_t *item = items[indices[i].row()];
  994. if (obs_sceneitem_is_group(item)) {
  995. hasGroups = true;
  996. break;
  997. }
  998. }
  999. /* --------------------------------------- */
  1000. /* if dropping a group, detect if it's */
  1001. /* below another group */
  1002. obs_sceneitem_t *itemBelow;
  1003. if (row == stm->items.count())
  1004. itemBelow = nullptr;
  1005. else
  1006. itemBelow = stm->items[row];
  1007. if (hasGroups) {
  1008. if (!itemBelow ||
  1009. obs_sceneitem_get_group(scene, itemBelow) != dropGroup) {
  1010. dropGroup = nullptr;
  1011. dropOnCollapsed = false;
  1012. }
  1013. }
  1014. /* --------------------------------------- */
  1015. /* if dropping groups on other groups, */
  1016. /* disregard as invalid drag/drop */
  1017. if (dropGroup && hasGroups) {
  1018. QListView::dropEvent(event);
  1019. return;
  1020. }
  1021. /* --------------------------------------- */
  1022. /* save undo data */
  1023. std::vector<obs_source_t *> sources;
  1024. for (int i = 0; i < indices.size(); i++) {
  1025. obs_sceneitem_t *item = items[indices[i].row()];
  1026. if (obs_sceneitem_get_scene(item) != scene)
  1027. sources.push_back(obs_scene_get_source(
  1028. obs_sceneitem_get_scene(item)));
  1029. }
  1030. if (dropGroup)
  1031. sources.push_back(obs_sceneitem_get_source(dropGroup));
  1032. OBSData undo_data = main->BackupScene(scene, &sources);
  1033. /* --------------------------------------- */
  1034. /* if selection includes base group items, */
  1035. /* include all group sub-items and treat */
  1036. /* them all as one */
  1037. if (hasGroups) {
  1038. /* remove sub-items if selected */
  1039. for (int i = indices.size() - 1; i >= 0; i--) {
  1040. obs_sceneitem_t *item = items[indices[i].row()];
  1041. obs_scene_t *itemScene = obs_sceneitem_get_scene(item);
  1042. if (itemScene != scene) {
  1043. indices.removeAt(i);
  1044. }
  1045. }
  1046. /* add all sub-items of selected groups */
  1047. for (int i = indices.size() - 1; i >= 0; i--) {
  1048. obs_sceneitem_t *item = items[indices[i].row()];
  1049. if (obs_sceneitem_is_group(item)) {
  1050. for (int j = items.size() - 1; j >= 0; j--) {
  1051. obs_sceneitem_t *subitem = items[j];
  1052. obs_sceneitem_t *subitemGroup =
  1053. obs_sceneitem_get_group(
  1054. scene, subitem);
  1055. if (subitemGroup == item) {
  1056. QModelIndex idx =
  1057. stm->createIndex(j, 0);
  1058. indices.insert(i + 1, idx);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. /* --------------------------------------- */
  1065. /* build persistent indices */
  1066. QList<QPersistentModelIndex> persistentIndices;
  1067. persistentIndices.reserve(indices.count());
  1068. for (QModelIndex &index : indices)
  1069. persistentIndices.append(index);
  1070. std::sort(persistentIndices.begin(), persistentIndices.end());
  1071. /* --------------------------------------- */
  1072. /* move all items to destination index */
  1073. int r = row;
  1074. for (auto &persistentIdx : persistentIndices) {
  1075. int from = persistentIdx.row();
  1076. int to = r;
  1077. int itemTo = to;
  1078. if (itemTo > from)
  1079. itemTo--;
  1080. if (itemTo != from) {
  1081. stm->beginMoveRows(QModelIndex(), from, from,
  1082. QModelIndex(), to);
  1083. MoveItem(items, from, itemTo);
  1084. stm->endMoveRows();
  1085. }
  1086. r = persistentIdx.row() + 1;
  1087. }
  1088. std::sort(persistentIndices.begin(), persistentIndices.end());
  1089. int firstIdx = persistentIndices.front().row();
  1090. int lastIdx = persistentIndices.back().row();
  1091. /* --------------------------------------- */
  1092. /* reorder scene items in back-end */
  1093. QVector<struct obs_sceneitem_order_info> orderList;
  1094. obs_sceneitem_t *lastGroup = nullptr;
  1095. int insertCollapsedIdx = 0;
  1096. auto insertCollapsed = [&](obs_sceneitem_t *item) {
  1097. struct obs_sceneitem_order_info info;
  1098. info.group = lastGroup;
  1099. info.item = item;
  1100. orderList.insert(insertCollapsedIdx++, info);
  1101. };
  1102. using insertCollapsed_t = decltype(insertCollapsed);
  1103. auto preInsertCollapsed = [](obs_scene_t *, obs_sceneitem_t *item,
  1104. void *param) {
  1105. (*reinterpret_cast<insertCollapsed_t *>(param))(item);
  1106. return true;
  1107. };
  1108. auto insertLastGroup = [&]() {
  1109. OBSDataAutoRelease data =
  1110. obs_sceneitem_get_private_settings(lastGroup);
  1111. bool collapsed = obs_data_get_bool(data, "collapsed");
  1112. if (collapsed) {
  1113. insertCollapsedIdx = 0;
  1114. obs_sceneitem_group_enum_items(lastGroup,
  1115. preInsertCollapsed,
  1116. &insertCollapsed);
  1117. }
  1118. struct obs_sceneitem_order_info info;
  1119. info.group = nullptr;
  1120. info.item = lastGroup;
  1121. orderList.insert(0, info);
  1122. };
  1123. auto updateScene = [&]() {
  1124. struct obs_sceneitem_order_info info;
  1125. for (int i = 0; i < items.size(); i++) {
  1126. obs_sceneitem_t *item = items[i];
  1127. obs_sceneitem_t *group;
  1128. if (obs_sceneitem_is_group(item)) {
  1129. if (lastGroup) {
  1130. insertLastGroup();
  1131. }
  1132. lastGroup = item;
  1133. continue;
  1134. }
  1135. if (!hasGroups && i >= firstIdx && i <= lastIdx)
  1136. group = dropGroup;
  1137. else
  1138. group = obs_sceneitem_get_group(scene, item);
  1139. if (lastGroup && lastGroup != group) {
  1140. insertLastGroup();
  1141. }
  1142. lastGroup = group;
  1143. info.group = group;
  1144. info.item = item;
  1145. orderList.insert(0, info);
  1146. }
  1147. if (lastGroup) {
  1148. insertLastGroup();
  1149. }
  1150. obs_scene_reorder_items2(scene, orderList.data(),
  1151. orderList.size());
  1152. };
  1153. using updateScene_t = decltype(updateScene);
  1154. auto preUpdateScene = [](void *data, obs_scene_t *) {
  1155. (*reinterpret_cast<updateScene_t *>(data))();
  1156. };
  1157. ignoreReorder = true;
  1158. obs_scene_atomic_update(scene, preUpdateScene, &updateScene);
  1159. ignoreReorder = false;
  1160. /* --------------------------------------- */
  1161. /* save redo data */
  1162. OBSData redo_data = main->BackupScene(scene, &sources);
  1163. /* --------------------------------------- */
  1164. /* add undo/redo action */
  1165. const char *scene_name = obs_source_get_name(scenesource);
  1166. QString action_name = QTStr("Undo.ReorderSources").arg(scene_name);
  1167. main->CreateSceneUndoRedoAction(action_name, undo_data, redo_data);
  1168. /* --------------------------------------- */
  1169. /* remove items if dropped in to collapsed */
  1170. /* group */
  1171. if (dropOnCollapsed) {
  1172. stm->beginRemoveRows(QModelIndex(), firstIdx, lastIdx);
  1173. items.remove(firstIdx, lastIdx - firstIdx + 1);
  1174. stm->endRemoveRows();
  1175. }
  1176. /* --------------------------------------- */
  1177. /* update widgets and accept event */
  1178. UpdateWidgets(true);
  1179. event->accept();
  1180. event->setDropAction(Qt::CopyAction);
  1181. QListView::dropEvent(event);
  1182. }
  1183. void SourceTree::selectionChanged(const QItemSelection &selected,
  1184. const QItemSelection &deselected)
  1185. {
  1186. {
  1187. SignalBlocker sourcesSignalBlocker(this);
  1188. SourceTreeModel *stm = GetStm();
  1189. QModelIndexList selectedIdxs = selected.indexes();
  1190. QModelIndexList deselectedIdxs = deselected.indexes();
  1191. for (int i = 0; i < selectedIdxs.count(); i++) {
  1192. int idx = selectedIdxs[i].row();
  1193. obs_sceneitem_select(stm->items[idx], true);
  1194. }
  1195. for (int i = 0; i < deselectedIdxs.count(); i++) {
  1196. int idx = deselectedIdxs[i].row();
  1197. obs_sceneitem_select(stm->items[idx], false);
  1198. }
  1199. }
  1200. QListView::selectionChanged(selected, deselected);
  1201. }
  1202. void SourceTree::NewGroupEdit(int row)
  1203. {
  1204. if (!Edit(row)) {
  1205. OBSBasic *main = OBSBasic::Get();
  1206. main->undo_s.pop_disabled();
  1207. blog(LOG_WARNING, "Uh, somehow the edit didn't process, this "
  1208. "code should never be reached.\nAnd by "
  1209. "\"never be reached\", I mean that "
  1210. "theoretically, it should be\nimpossible "
  1211. "for this code to be reached. But if this "
  1212. "code is reached,\nfeel free to laugh at "
  1213. "Lain, because apparently it is, in fact, "
  1214. "actually\npossible for this code to be "
  1215. "reached. But I mean, again, theoretically\n"
  1216. "it should be impossible. So if you see "
  1217. "this in your log, just know that\nit's "
  1218. "really dumb, and depressing. But at least "
  1219. "the undo/redo action is\nstill covered, so "
  1220. "in theory things *should* be fine. But "
  1221. "it's entirely\npossible that they might "
  1222. "not be exactly. But again, yea. This "
  1223. "really\nshould not be possible.");
  1224. OBSData redoSceneData = main->BackupScene(GetCurrentScene());
  1225. QString text = QTStr("Undo.GroupItems").arg("Unknown");
  1226. main->CreateSceneUndoRedoAction(text, undoSceneData,
  1227. redoSceneData);
  1228. undoSceneData = nullptr;
  1229. }
  1230. }
  1231. bool SourceTree::Edit(int row)
  1232. {
  1233. SourceTreeModel *stm = GetStm();
  1234. if (row < 0 || row >= stm->items.count())
  1235. return false;
  1236. QModelIndex index = stm->createIndex(row, 0);
  1237. QWidget *widget = indexWidget(index);
  1238. SourceTreeItem *itemWidget = reinterpret_cast<SourceTreeItem *>(widget);
  1239. if (itemWidget->IsEditing()) {
  1240. #ifdef __APPLE__
  1241. itemWidget->ExitEditMode(true);
  1242. #endif
  1243. return false;
  1244. }
  1245. itemWidget->EnterEditMode();
  1246. edit(index);
  1247. return true;
  1248. }
  1249. bool SourceTree::MultipleBaseSelected() const
  1250. {
  1251. SourceTreeModel *stm = GetStm();
  1252. QModelIndexList selectedIndices = selectedIndexes();
  1253. OBSScene scene = GetCurrentScene();
  1254. if (selectedIndices.size() < 1) {
  1255. return false;
  1256. }
  1257. for (auto &idx : selectedIndices) {
  1258. obs_sceneitem_t *item = stm->items[idx.row()];
  1259. if (obs_sceneitem_is_group(item)) {
  1260. return false;
  1261. }
  1262. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1263. if (itemScene != scene) {
  1264. return false;
  1265. }
  1266. }
  1267. return true;
  1268. }
  1269. bool SourceTree::GroupsSelected() const
  1270. {
  1271. SourceTreeModel *stm = GetStm();
  1272. QModelIndexList selectedIndices = selectedIndexes();
  1273. OBSScene scene = GetCurrentScene();
  1274. if (selectedIndices.size() < 1) {
  1275. return false;
  1276. }
  1277. for (auto &idx : selectedIndices) {
  1278. obs_sceneitem_t *item = stm->items[idx.row()];
  1279. if (!obs_sceneitem_is_group(item)) {
  1280. return false;
  1281. }
  1282. }
  1283. return true;
  1284. }
  1285. bool SourceTree::GroupedItemsSelected() const
  1286. {
  1287. SourceTreeModel *stm = GetStm();
  1288. QModelIndexList selectedIndices = selectedIndexes();
  1289. OBSScene scene = GetCurrentScene();
  1290. if (!selectedIndices.size()) {
  1291. return false;
  1292. }
  1293. for (auto &idx : selectedIndices) {
  1294. obs_sceneitem_t *item = stm->items[idx.row()];
  1295. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1296. if (itemScene != scene) {
  1297. return true;
  1298. }
  1299. }
  1300. return false;
  1301. }
  1302. void SourceTree::Remove(OBSSceneItem item)
  1303. {
  1304. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1305. GetStm()->Remove(item);
  1306. main->SaveProject();
  1307. if (!main->SavingDisabled()) {
  1308. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1309. obs_source_t *sceneSource = obs_scene_get_source(scene);
  1310. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  1311. blog(LOG_INFO, "User Removed source '%s' (%s) from scene '%s'",
  1312. obs_source_get_name(itemSource),
  1313. obs_source_get_id(itemSource),
  1314. obs_source_get_name(sceneSource));
  1315. }
  1316. }
  1317. void SourceTree::GroupSelectedItems()
  1318. {
  1319. QModelIndexList indices = selectedIndexes();
  1320. std::sort(indices.begin(), indices.end());
  1321. GetStm()->GroupSelectedItems(indices);
  1322. }
  1323. void SourceTree::UngroupSelectedGroups()
  1324. {
  1325. QModelIndexList indices = selectedIndexes();
  1326. GetStm()->UngroupSelectedGroups(indices);
  1327. }
  1328. void SourceTree::AddGroup()
  1329. {
  1330. GetStm()->AddGroup();
  1331. }
  1332. void SourceTree::UpdateNoSourcesMessage()
  1333. {
  1334. std::string darkPath;
  1335. GetDataFilePath("themes/Dark/no_sources.svg", darkPath);
  1336. QString file = !App()->IsThemeDark() ? ":res/images/no_sources.svg"
  1337. : darkPath.c_str();
  1338. iconNoSources.load(file);
  1339. QTextOption opt(Qt::AlignHCenter);
  1340. opt.setWrapMode(QTextOption::WordWrap);
  1341. textNoSources.setTextOption(opt);
  1342. textNoSources.setText(QTStr("NoSources.Label").replace("\n", "<br/>"));
  1343. textPrepared = false;
  1344. }
  1345. void SourceTree::paintEvent(QPaintEvent *event)
  1346. {
  1347. SourceTreeModel *stm = GetStm();
  1348. if (stm && !stm->items.count()) {
  1349. QPainter p(viewport());
  1350. if (!textPrepared) {
  1351. textNoSources.prepare(QTransform(), p.font());
  1352. textPrepared = true;
  1353. }
  1354. QRectF iconRect = iconNoSources.viewBoxF();
  1355. iconRect.setSize(QSizeF(32.0, 32.0));
  1356. QSizeF iconSize = iconRect.size();
  1357. QSizeF textSize = textNoSources.size();
  1358. QSizeF thisSize = size();
  1359. const qreal spacing = 16.0;
  1360. qreal totalHeight =
  1361. iconSize.height() + spacing + textSize.height();
  1362. qreal x = thisSize.width() / 2.0 - iconSize.width() / 2.0;
  1363. qreal y = thisSize.height() / 2.0 - totalHeight / 2.0;
  1364. iconRect.moveTo(std::round(x), std::round(y));
  1365. iconNoSources.render(&p, iconRect);
  1366. x = thisSize.width() / 2.0 - textSize.width() / 2.0;
  1367. y += spacing + iconSize.height();
  1368. p.drawStaticText(x, y, textNoSources);
  1369. } else {
  1370. QListView::paintEvent(event);
  1371. }
  1372. }
  1373. SourceTreeDelegate::SourceTreeDelegate(QObject *parent)
  1374. : QStyledItemDelegate(parent)
  1375. {
  1376. }
  1377. QSize SourceTreeDelegate::sizeHint(const QStyleOptionViewItem &option,
  1378. const QModelIndex &index) const
  1379. {
  1380. SourceTree *tree = qobject_cast<SourceTree *>(parent());
  1381. QWidget *item = tree->indexWidget(index);
  1382. if (!item)
  1383. return (QSize(0, 0));
  1384. return (QSize(option.widget->minimumWidth(), item->height()));
  1385. }