source-tree.cpp 42 KB

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