source-tree.cpp 42 KB

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