source-tree.cpp 43 KB

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