source-tree.cpp 43 KB

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