source-tree.cpp 43 KB

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