source-tree.cpp 36 KB

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