source-tree.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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 (LineEditCanceled(event)) {
  278. QMetaObject::invokeMethod(this, "ExitEditMode",
  279. Qt::QueuedConnection,
  280. Q_ARG(bool, false));
  281. return true;
  282. }
  283. if (LineEditChanged(event)) {
  284. QMetaObject::invokeMethod(this, "ExitEditMode",
  285. Qt::QueuedConnection,
  286. Q_ARG(bool, true));
  287. return true;
  288. }
  289. return false;
  290. }
  291. void SourceTreeItem::VisibilityChanged(bool visible)
  292. {
  293. vis->setChecked(visible);
  294. }
  295. void SourceTreeItem::LockedChanged(bool locked)
  296. {
  297. lock->setChecked(locked);
  298. }
  299. void SourceTreeItem::Renamed(const QString &name)
  300. {
  301. label->setText(name);
  302. }
  303. void SourceTreeItem::Update(bool force)
  304. {
  305. OBSScene scene = GetCurrentScene();
  306. obs_scene_t *itemScene = obs_sceneitem_get_scene(sceneitem);
  307. Type newType;
  308. /* ------------------------------------------------- */
  309. /* if it's a group item, insert group checkbox */
  310. if (obs_sceneitem_is_group(sceneitem)) {
  311. newType = Type::Group;
  312. /* ------------------------------------------------- */
  313. /* if it's a group sub-item */
  314. } else if (itemScene != scene) {
  315. newType = Type::SubItem;
  316. /* ------------------------------------------------- */
  317. /* if it's a regular item */
  318. } else {
  319. newType = Type::Item;
  320. }
  321. /* ------------------------------------------------- */
  322. if (!force && newType == type) {
  323. return;
  324. }
  325. /* ------------------------------------------------- */
  326. ReconnectSignals();
  327. if (spacer) {
  328. boxLayout->removeItem(spacer);
  329. delete spacer;
  330. spacer = nullptr;
  331. }
  332. if (type == Type::Group) {
  333. boxLayout->removeWidget(expand);
  334. expand->deleteLater();
  335. expand = nullptr;
  336. }
  337. type = newType;
  338. if (type == Type::SubItem) {
  339. spacer = new QSpacerItem(16, 1);
  340. boxLayout->insertItem(0, spacer);
  341. } else if (type == Type::Group) {
  342. expand = new SourceTreeSubItemCheckBox();
  343. expand->setSizePolicy(QSizePolicy::Maximum,
  344. QSizePolicy::Maximum);
  345. expand->setMaximumSize(10, 16);
  346. expand->setMinimumSize(10, 0);
  347. #ifdef __APPLE__
  348. expand->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  349. #endif
  350. boxLayout->insertWidget(0, expand);
  351. obs_data_t *data =
  352. obs_sceneitem_get_private_settings(sceneitem);
  353. expand->blockSignals(true);
  354. expand->setChecked(obs_data_get_bool(data, "collapsed"));
  355. expand->blockSignals(false);
  356. obs_data_release(data);
  357. connect(expand, &QPushButton::toggled, this,
  358. &SourceTreeItem::ExpandClicked);
  359. } else {
  360. spacer = new QSpacerItem(3, 1);
  361. boxLayout->insertItem(0, spacer);
  362. }
  363. }
  364. void SourceTreeItem::ExpandClicked(bool checked)
  365. {
  366. OBSData data = obs_sceneitem_get_private_settings(sceneitem);
  367. obs_data_release(data);
  368. obs_data_set_bool(data, "collapsed", checked);
  369. if (!checked)
  370. tree->GetStm()->ExpandGroup(sceneitem);
  371. else
  372. tree->GetStm()->CollapseGroup(sceneitem);
  373. }
  374. void SourceTreeItem::Deselect()
  375. {
  376. tree->SelectItem(sceneitem, false);
  377. }
  378. /* ========================================================================= */
  379. void SourceTreeModel::OBSFrontendEvent(enum obs_frontend_event event, void *ptr)
  380. {
  381. SourceTreeModel *stm = reinterpret_cast<SourceTreeModel *>(ptr);
  382. switch ((int)event) {
  383. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  384. stm->SceneChanged();
  385. break;
  386. case OBS_FRONTEND_EVENT_EXIT:
  387. case OBS_FRONTEND_EVENT_SCENE_COLLECTION_CLEANUP:
  388. stm->Clear();
  389. break;
  390. }
  391. }
  392. void SourceTreeModel::Clear()
  393. {
  394. beginResetModel();
  395. items.clear();
  396. endResetModel();
  397. hasGroups = false;
  398. }
  399. static bool enumItem(obs_scene_t *, obs_sceneitem_t *item, void *ptr)
  400. {
  401. QVector<OBSSceneItem> &items =
  402. *reinterpret_cast<QVector<OBSSceneItem> *>(ptr);
  403. if (obs_sceneitem_is_group(item)) {
  404. obs_data_t *data = obs_sceneitem_get_private_settings(item);
  405. bool collapse = obs_data_get_bool(data, "collapsed");
  406. if (!collapse) {
  407. obs_scene_t *scene =
  408. obs_sceneitem_group_get_scene(item);
  409. obs_scene_enum_items(scene, enumItem, &items);
  410. }
  411. obs_data_release(data);
  412. }
  413. items.insert(0, item);
  414. return true;
  415. }
  416. void SourceTreeModel::SceneChanged()
  417. {
  418. OBSScene scene = GetCurrentScene();
  419. beginResetModel();
  420. items.clear();
  421. obs_scene_enum_items(scene, enumItem, &items);
  422. endResetModel();
  423. UpdateGroupState(false);
  424. st->ResetWidgets();
  425. for (int i = 0; i < items.count(); i++) {
  426. bool select = obs_sceneitem_selected(items[i]);
  427. QModelIndex index = createIndex(i, 0);
  428. st->selectionModel()->select(
  429. index, select ? QItemSelectionModel::Select
  430. : QItemSelectionModel::Deselect);
  431. }
  432. }
  433. /* moves a scene item index (blame linux distros for using older Qt builds) */
  434. static inline void MoveItem(QVector<OBSSceneItem> &items, int oldIdx,
  435. int newIdx)
  436. {
  437. OBSSceneItem item = items[oldIdx];
  438. items.remove(oldIdx);
  439. items.insert(newIdx, item);
  440. }
  441. /* reorders list optimally with model reorder funcs */
  442. void SourceTreeModel::ReorderItems()
  443. {
  444. OBSScene scene = GetCurrentScene();
  445. QVector<OBSSceneItem> newitems;
  446. obs_scene_enum_items(scene, enumItem, &newitems);
  447. /* if item list has changed size, do full reset */
  448. if (newitems.count() != items.count()) {
  449. SceneChanged();
  450. return;
  451. }
  452. for (;;) {
  453. int idx1Old = 0;
  454. int idx1New = 0;
  455. int count;
  456. int i;
  457. /* find first starting changed item index */
  458. for (i = 0; i < newitems.count(); i++) {
  459. obs_sceneitem_t *oldItem = items[i];
  460. obs_sceneitem_t *newItem = newitems[i];
  461. if (oldItem != newItem) {
  462. idx1Old = i;
  463. break;
  464. }
  465. }
  466. /* if everything is the same, break */
  467. if (i == newitems.count()) {
  468. break;
  469. }
  470. /* find new starting index */
  471. for (i = idx1Old + 1; i < newitems.count(); i++) {
  472. obs_sceneitem_t *oldItem = items[idx1Old];
  473. obs_sceneitem_t *newItem = newitems[i];
  474. if (oldItem == newItem) {
  475. idx1New = i;
  476. break;
  477. }
  478. }
  479. /* if item could not be found, do full reset */
  480. if (i == newitems.count()) {
  481. SceneChanged();
  482. return;
  483. }
  484. /* get move count */
  485. for (count = 1; (idx1New + count) < newitems.count(); count++) {
  486. int oldIdx = idx1Old + count;
  487. int newIdx = idx1New + count;
  488. obs_sceneitem_t *oldItem = items[oldIdx];
  489. obs_sceneitem_t *newItem = newitems[newIdx];
  490. if (oldItem != newItem) {
  491. break;
  492. }
  493. }
  494. /* move items */
  495. beginMoveRows(QModelIndex(), idx1Old, idx1Old + count - 1,
  496. QModelIndex(), idx1New + count);
  497. for (i = 0; i < count; i++) {
  498. int to = idx1New + count;
  499. if (to > idx1Old)
  500. to--;
  501. MoveItem(items, idx1Old, to);
  502. }
  503. endMoveRows();
  504. }
  505. }
  506. void SourceTreeModel::Add(obs_sceneitem_t *item)
  507. {
  508. if (obs_sceneitem_is_group(item)) {
  509. SceneChanged();
  510. } else {
  511. beginInsertRows(QModelIndex(), 0, 0);
  512. items.insert(0, item);
  513. endInsertRows();
  514. st->UpdateWidget(createIndex(0, 0, nullptr), item);
  515. }
  516. }
  517. void SourceTreeModel::Remove(obs_sceneitem_t *item)
  518. {
  519. int idx = -1;
  520. for (int i = 0; i < items.count(); i++) {
  521. if (items[i] == item) {
  522. idx = i;
  523. break;
  524. }
  525. }
  526. if (idx == -1)
  527. return;
  528. int startIdx = idx;
  529. int endIdx = idx;
  530. bool is_group = obs_sceneitem_is_group(item);
  531. if (is_group) {
  532. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  533. for (int i = endIdx + 1; i < items.count(); i++) {
  534. obs_sceneitem_t *subitem = items[i];
  535. obs_scene_t *subscene =
  536. obs_sceneitem_get_scene(subitem);
  537. if (subscene == scene)
  538. endIdx = i;
  539. else
  540. break;
  541. }
  542. }
  543. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  544. items.remove(idx, endIdx - startIdx + 1);
  545. endRemoveRows();
  546. if (is_group)
  547. UpdateGroupState(true);
  548. }
  549. OBSSceneItem SourceTreeModel::Get(int idx)
  550. {
  551. if (idx == -1 || idx >= items.count())
  552. return OBSSceneItem();
  553. return items[idx];
  554. }
  555. SourceTreeModel::SourceTreeModel(SourceTree *st_)
  556. : QAbstractListModel(st_), st(st_)
  557. {
  558. obs_frontend_add_event_callback(OBSFrontendEvent, this);
  559. }
  560. SourceTreeModel::~SourceTreeModel()
  561. {
  562. obs_frontend_remove_event_callback(OBSFrontendEvent, this);
  563. }
  564. int SourceTreeModel::rowCount(const QModelIndex &parent) const
  565. {
  566. return parent.isValid() ? 0 : items.count();
  567. }
  568. QVariant SourceTreeModel::data(const QModelIndex &index, int role) const
  569. {
  570. if (role == Qt::AccessibleTextRole) {
  571. OBSSceneItem item = items[index.row()];
  572. obs_source_t *source = obs_sceneitem_get_source(item);
  573. return QVariant(QT_UTF8(obs_source_get_name(source)));
  574. }
  575. return QVariant();
  576. }
  577. Qt::ItemFlags SourceTreeModel::flags(const QModelIndex &index) const
  578. {
  579. if (!index.isValid())
  580. return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled;
  581. obs_sceneitem_t *item = items[index.row()];
  582. bool is_group = obs_sceneitem_is_group(item);
  583. return QAbstractListModel::flags(index) | Qt::ItemIsEditable |
  584. Qt::ItemIsDragEnabled |
  585. (is_group ? Qt::ItemIsDropEnabled : Qt::NoItemFlags);
  586. }
  587. Qt::DropActions SourceTreeModel::supportedDropActions() const
  588. {
  589. return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
  590. }
  591. QString SourceTreeModel::GetNewGroupName()
  592. {
  593. OBSScene scene = GetCurrentScene();
  594. QString name = QTStr("Group");
  595. int i = 2;
  596. for (;;) {
  597. obs_source_t *group = obs_get_source_by_name(QT_TO_UTF8(name));
  598. obs_source_release(group);
  599. if (!group)
  600. break;
  601. name = QTStr("Basic.Main.Group").arg(QString::number(i++));
  602. }
  603. return name;
  604. }
  605. void SourceTreeModel::AddGroup()
  606. {
  607. QString name = GetNewGroupName();
  608. obs_sceneitem_t *group =
  609. obs_scene_add_group(GetCurrentScene(), QT_TO_UTF8(name));
  610. if (!group)
  611. return;
  612. beginInsertRows(QModelIndex(), 0, 0);
  613. items.insert(0, group);
  614. endInsertRows();
  615. st->UpdateWidget(createIndex(0, 0, nullptr), group);
  616. UpdateGroupState(true);
  617. QMetaObject::invokeMethod(st, "Edit", Qt::QueuedConnection,
  618. Q_ARG(int, 0));
  619. }
  620. void SourceTreeModel::GroupSelectedItems(QModelIndexList &indices)
  621. {
  622. if (indices.count() == 0)
  623. return;
  624. OBSScene scene = GetCurrentScene();
  625. QString name = GetNewGroupName();
  626. QVector<obs_sceneitem_t *> item_order;
  627. for (int i = indices.count() - 1; i >= 0; i--) {
  628. obs_sceneitem_t *item = items[indices[i].row()];
  629. item_order << item;
  630. }
  631. obs_sceneitem_t *item = obs_scene_insert_group(
  632. scene, QT_TO_UTF8(name), item_order.data(), item_order.size());
  633. if (!item) {
  634. return;
  635. }
  636. for (obs_sceneitem_t *item : item_order)
  637. obs_sceneitem_select(item, false);
  638. int newIdx = indices[0].row();
  639. beginInsertRows(QModelIndex(), newIdx, newIdx);
  640. items.insert(newIdx, item);
  641. endInsertRows();
  642. for (int i = 0; i < indices.size(); i++) {
  643. int fromIdx = indices[i].row() + 1;
  644. int toIdx = newIdx + i + 1;
  645. if (fromIdx != toIdx) {
  646. beginMoveRows(QModelIndex(), fromIdx, fromIdx,
  647. QModelIndex(), toIdx);
  648. MoveItem(items, fromIdx, toIdx);
  649. endMoveRows();
  650. }
  651. }
  652. hasGroups = true;
  653. st->UpdateWidgets(true);
  654. obs_sceneitem_select(item, true);
  655. QMetaObject::invokeMethod(st, "Edit", Qt::QueuedConnection,
  656. Q_ARG(int, newIdx));
  657. }
  658. void SourceTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
  659. {
  660. if (indices.count() == 0)
  661. return;
  662. for (int i = indices.count() - 1; i >= 0; i--) {
  663. obs_sceneitem_t *item = items[indices[i].row()];
  664. obs_sceneitem_group_ungroup(item);
  665. }
  666. SceneChanged();
  667. }
  668. void SourceTreeModel::ExpandGroup(obs_sceneitem_t *item)
  669. {
  670. int itemIdx = items.indexOf(item);
  671. if (itemIdx == -1)
  672. return;
  673. itemIdx++;
  674. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  675. QVector<OBSSceneItem> subItems;
  676. obs_scene_enum_items(scene, enumItem, &subItems);
  677. if (!subItems.size())
  678. return;
  679. beginInsertRows(QModelIndex(), itemIdx, itemIdx + subItems.size() - 1);
  680. for (int i = 0; i < subItems.size(); i++)
  681. items.insert(i + itemIdx, subItems[i]);
  682. endInsertRows();
  683. st->UpdateWidgets();
  684. }
  685. void SourceTreeModel::CollapseGroup(obs_sceneitem_t *item)
  686. {
  687. int startIdx = -1;
  688. int endIdx = -1;
  689. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  690. for (int i = 0; i < items.size(); i++) {
  691. obs_scene_t *itemScene = obs_sceneitem_get_scene(items[i]);
  692. if (itemScene == scene) {
  693. if (startIdx == -1)
  694. startIdx = i;
  695. endIdx = i;
  696. }
  697. }
  698. if (startIdx == -1)
  699. return;
  700. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  701. items.remove(startIdx, endIdx - startIdx + 1);
  702. endRemoveRows();
  703. }
  704. void SourceTreeModel::UpdateGroupState(bool update)
  705. {
  706. bool nowHasGroups = false;
  707. for (auto &item : items) {
  708. if (obs_sceneitem_is_group(item)) {
  709. nowHasGroups = true;
  710. break;
  711. }
  712. }
  713. if (nowHasGroups != hasGroups) {
  714. hasGroups = nowHasGroups;
  715. if (update) {
  716. st->UpdateWidgets(true);
  717. }
  718. }
  719. }
  720. /* ========================================================================= */
  721. SourceTree::SourceTree(QWidget *parent_) : QListView(parent_)
  722. {
  723. SourceTreeModel *stm_ = new SourceTreeModel(this);
  724. setModel(stm_);
  725. setStyleSheet(QString(
  726. "*[bgColor=\"1\"]{background-color:rgba(255,68,68,33%);}"
  727. "*[bgColor=\"2\"]{background-color:rgba(255,255,68,33%);}"
  728. "*[bgColor=\"3\"]{background-color:rgba(68,255,68,33%);}"
  729. "*[bgColor=\"4\"]{background-color:rgba(68,255,255,33%);}"
  730. "*[bgColor=\"5\"]{background-color:rgba(68,68,255,33%);}"
  731. "*[bgColor=\"6\"]{background-color:rgba(255,68,255,33%);}"
  732. "*[bgColor=\"7\"]{background-color:rgba(68,68,68,33%);}"
  733. "*[bgColor=\"8\"]{background-color:rgba(255,255,255,33%);}"));
  734. setMouseTracking(true);
  735. UpdateNoSourcesMessage();
  736. connect(App(), &OBSApp::StyleChanged, this,
  737. &SourceTree::UpdateNoSourcesMessage);
  738. }
  739. void SourceTree::ResetWidgets()
  740. {
  741. OBSScene scene = GetCurrentScene();
  742. SourceTreeModel *stm = GetStm();
  743. stm->UpdateGroupState(false);
  744. for (int i = 0; i < stm->items.count(); i++) {
  745. QModelIndex index = stm->createIndex(i, 0, nullptr);
  746. setIndexWidget(index, new SourceTreeItem(this, stm->items[i]));
  747. }
  748. }
  749. void SourceTree::UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item)
  750. {
  751. setIndexWidget(idx, new SourceTreeItem(this, item));
  752. }
  753. void SourceTree::UpdateWidgets(bool force)
  754. {
  755. SourceTreeModel *stm = GetStm();
  756. for (int i = 0; i < stm->items.size(); i++) {
  757. obs_sceneitem_t *item = stm->items[i];
  758. SourceTreeItem *widget = GetItemWidget(i);
  759. if (!widget) {
  760. UpdateWidget(stm->createIndex(i, 0), item);
  761. } else {
  762. widget->Update(force);
  763. }
  764. }
  765. }
  766. void SourceTree::SelectItem(obs_sceneitem_t *sceneitem, bool select)
  767. {
  768. SourceTreeModel *stm = GetStm();
  769. int i = 0;
  770. for (; i < stm->items.count(); i++) {
  771. if (stm->items[i] == sceneitem)
  772. break;
  773. }
  774. if (i == stm->items.count())
  775. return;
  776. QModelIndex index = stm->createIndex(i, 0);
  777. if (index.isValid())
  778. selectionModel()->select(
  779. index, select ? QItemSelectionModel::Select
  780. : QItemSelectionModel::Deselect);
  781. }
  782. Q_DECLARE_METATYPE(OBSSceneItem);
  783. void SourceTree::mouseDoubleClickEvent(QMouseEvent *event)
  784. {
  785. if (event->button() == Qt::LeftButton)
  786. QListView::mouseDoubleClickEvent(event);
  787. }
  788. void SourceTree::dropEvent(QDropEvent *event)
  789. {
  790. if (event->source() != this) {
  791. QListView::dropEvent(event);
  792. return;
  793. }
  794. OBSScene scene = GetCurrentScene();
  795. SourceTreeModel *stm = GetStm();
  796. auto &items = stm->items;
  797. QModelIndexList indices = selectedIndexes();
  798. DropIndicatorPosition indicator = dropIndicatorPosition();
  799. int row = indexAt(event->pos()).row();
  800. bool emptyDrop = row == -1;
  801. if (emptyDrop) {
  802. if (!items.size()) {
  803. QListView::dropEvent(event);
  804. return;
  805. }
  806. row = items.size() - 1;
  807. indicator = QAbstractItemView::BelowItem;
  808. }
  809. /* --------------------------------------- */
  810. /* store destination group if moving to a */
  811. /* group */
  812. obs_sceneitem_t *dropItem = items[row]; /* item being dropped on */
  813. bool itemIsGroup = obs_sceneitem_is_group(dropItem);
  814. obs_sceneitem_t *dropGroup =
  815. itemIsGroup ? dropItem
  816. : obs_sceneitem_get_group(scene, dropItem);
  817. /* not a group if moving above the group */
  818. if (indicator == QAbstractItemView::AboveItem && itemIsGroup)
  819. dropGroup = nullptr;
  820. if (emptyDrop)
  821. dropGroup = nullptr;
  822. /* --------------------------------------- */
  823. /* remember to remove list items if */
  824. /* dropping on collapsed group */
  825. bool dropOnCollapsed = false;
  826. if (dropGroup) {
  827. obs_data_t *data =
  828. obs_sceneitem_get_private_settings(dropGroup);
  829. dropOnCollapsed = obs_data_get_bool(data, "collapsed");
  830. obs_data_release(data);
  831. }
  832. if (indicator == QAbstractItemView::BelowItem ||
  833. indicator == QAbstractItemView::OnItem)
  834. row++;
  835. if (row < 0 || row > stm->items.count()) {
  836. QListView::dropEvent(event);
  837. return;
  838. }
  839. /* --------------------------------------- */
  840. /* determine if any base group is selected */
  841. bool hasGroups = false;
  842. for (int i = 0; i < indices.size(); i++) {
  843. obs_sceneitem_t *item = items[indices[i].row()];
  844. if (obs_sceneitem_is_group(item)) {
  845. hasGroups = true;
  846. break;
  847. }
  848. }
  849. /* --------------------------------------- */
  850. /* if dropping a group, detect if it's */
  851. /* below another group */
  852. obs_sceneitem_t *itemBelow;
  853. if (row == stm->items.count())
  854. itemBelow = nullptr;
  855. else
  856. itemBelow = stm->items[row];
  857. if (hasGroups) {
  858. if (!itemBelow ||
  859. obs_sceneitem_get_group(scene, itemBelow) != dropGroup) {
  860. indicator = QAbstractItemView::BelowItem;
  861. dropGroup = nullptr;
  862. dropOnCollapsed = false;
  863. }
  864. }
  865. /* --------------------------------------- */
  866. /* if dropping groups on other groups, */
  867. /* disregard as invalid drag/drop */
  868. if (dropGroup && hasGroups) {
  869. QListView::dropEvent(event);
  870. return;
  871. }
  872. /* --------------------------------------- */
  873. /* if selection includes base group items, */
  874. /* include all group sub-items and treat */
  875. /* them all as one */
  876. if (hasGroups) {
  877. /* remove sub-items if selected */
  878. for (int i = indices.size() - 1; i >= 0; i--) {
  879. obs_sceneitem_t *item = items[indices[i].row()];
  880. obs_scene_t *itemScene = obs_sceneitem_get_scene(item);
  881. if (itemScene != scene) {
  882. indices.removeAt(i);
  883. }
  884. }
  885. /* add all sub-items of selected groups */
  886. for (int i = indices.size() - 1; i >= 0; i--) {
  887. obs_sceneitem_t *item = items[indices[i].row()];
  888. if (obs_sceneitem_is_group(item)) {
  889. for (int j = items.size() - 1; j >= 0; j--) {
  890. obs_sceneitem_t *subitem = items[j];
  891. obs_sceneitem_t *subitemGroup =
  892. obs_sceneitem_get_group(
  893. scene, subitem);
  894. if (subitemGroup == item) {
  895. QModelIndex idx =
  896. stm->createIndex(j, 0);
  897. indices.insert(i + 1, idx);
  898. }
  899. }
  900. }
  901. }
  902. }
  903. /* --------------------------------------- */
  904. /* build persistent indices */
  905. QList<QPersistentModelIndex> persistentIndices;
  906. persistentIndices.reserve(indices.count());
  907. for (QModelIndex &index : indices)
  908. persistentIndices.append(index);
  909. std::sort(persistentIndices.begin(), persistentIndices.end());
  910. /* --------------------------------------- */
  911. /* move all items to destination index */
  912. int r = row;
  913. for (auto &persistentIdx : persistentIndices) {
  914. int from = persistentIdx.row();
  915. int to = r;
  916. int itemTo = to;
  917. if (itemTo > from)
  918. itemTo--;
  919. if (itemTo != from) {
  920. stm->beginMoveRows(QModelIndex(), from, from,
  921. QModelIndex(), to);
  922. MoveItem(items, from, itemTo);
  923. stm->endMoveRows();
  924. }
  925. r = persistentIdx.row() + 1;
  926. }
  927. std::sort(persistentIndices.begin(), persistentIndices.end());
  928. int firstIdx = persistentIndices.front().row();
  929. int lastIdx = persistentIndices.back().row();
  930. /* --------------------------------------- */
  931. /* reorder scene items in back-end */
  932. QVector<struct obs_sceneitem_order_info> orderList;
  933. obs_sceneitem_t *lastGroup = nullptr;
  934. int insertCollapsedIdx = 0;
  935. auto insertCollapsed = [&](obs_sceneitem_t *item) {
  936. struct obs_sceneitem_order_info info;
  937. info.group = lastGroup;
  938. info.item = item;
  939. orderList.insert(insertCollapsedIdx++, info);
  940. };
  941. using insertCollapsed_t = decltype(insertCollapsed);
  942. auto preInsertCollapsed = [](obs_scene_t *, obs_sceneitem_t *item,
  943. void *param) {
  944. (*reinterpret_cast<insertCollapsed_t *>(param))(item);
  945. return true;
  946. };
  947. auto insertLastGroup = [&]() {
  948. obs_data_t *data =
  949. obs_sceneitem_get_private_settings(lastGroup);
  950. bool collapsed = obs_data_get_bool(data, "collapsed");
  951. obs_data_release(data);
  952. if (collapsed) {
  953. insertCollapsedIdx = 0;
  954. obs_sceneitem_group_enum_items(lastGroup,
  955. preInsertCollapsed,
  956. &insertCollapsed);
  957. }
  958. struct obs_sceneitem_order_info info;
  959. info.group = nullptr;
  960. info.item = lastGroup;
  961. orderList.insert(0, info);
  962. };
  963. auto updateScene = [&]() {
  964. struct obs_sceneitem_order_info info;
  965. for (int i = 0; i < items.size(); i++) {
  966. obs_sceneitem_t *item = items[i];
  967. obs_sceneitem_t *group;
  968. if (obs_sceneitem_is_group(item)) {
  969. if (lastGroup) {
  970. insertLastGroup();
  971. }
  972. lastGroup = item;
  973. continue;
  974. }
  975. if (!hasGroups && i >= firstIdx && i <= lastIdx)
  976. group = dropGroup;
  977. else
  978. group = obs_sceneitem_get_group(scene, item);
  979. if (lastGroup && lastGroup != group) {
  980. insertLastGroup();
  981. }
  982. lastGroup = group;
  983. info.group = group;
  984. info.item = item;
  985. orderList.insert(0, info);
  986. }
  987. if (lastGroup) {
  988. insertLastGroup();
  989. }
  990. obs_scene_reorder_items2(scene, orderList.data(),
  991. orderList.size());
  992. };
  993. using updateScene_t = decltype(updateScene);
  994. auto preUpdateScene = [](void *data, obs_scene_t *) {
  995. (*reinterpret_cast<updateScene_t *>(data))();
  996. };
  997. ignoreReorder = true;
  998. obs_scene_atomic_update(scene, preUpdateScene, &updateScene);
  999. ignoreReorder = false;
  1000. /* --------------------------------------- */
  1001. /* remove items if dropped in to collapsed */
  1002. /* group */
  1003. if (dropOnCollapsed) {
  1004. stm->beginRemoveRows(QModelIndex(), firstIdx, lastIdx);
  1005. items.remove(firstIdx, lastIdx - firstIdx + 1);
  1006. stm->endRemoveRows();
  1007. }
  1008. /* --------------------------------------- */
  1009. /* update widgets and accept event */
  1010. UpdateWidgets(true);
  1011. event->accept();
  1012. event->setDropAction(Qt::CopyAction);
  1013. QListView::dropEvent(event);
  1014. }
  1015. void SourceTree::mouseMoveEvent(QMouseEvent *event)
  1016. {
  1017. QPoint pos = event->pos();
  1018. SourceTreeItem *item = qobject_cast<SourceTreeItem *>(childAt(pos));
  1019. OBSBasicPreview *preview = OBSBasicPreview::Get();
  1020. if (item)
  1021. preview->hoveredListItem = item->sceneitem;
  1022. else
  1023. preview->hoveredListItem = nullptr;
  1024. QListView::mouseMoveEvent(event);
  1025. }
  1026. void SourceTree::leaveEvent(QEvent *event)
  1027. {
  1028. OBSBasicPreview *preview = OBSBasicPreview::Get();
  1029. preview->hoveredListItem = nullptr;
  1030. QListView::leaveEvent(event);
  1031. }
  1032. void SourceTree::selectionChanged(const QItemSelection &selected,
  1033. const QItemSelection &deselected)
  1034. {
  1035. {
  1036. SignalBlocker sourcesSignalBlocker(this);
  1037. SourceTreeModel *stm = GetStm();
  1038. QModelIndexList selectedIdxs = selected.indexes();
  1039. QModelIndexList deselectedIdxs = deselected.indexes();
  1040. for (int i = 0; i < selectedIdxs.count(); i++) {
  1041. int idx = selectedIdxs[i].row();
  1042. obs_sceneitem_select(stm->items[idx], true);
  1043. }
  1044. for (int i = 0; i < deselectedIdxs.count(); i++) {
  1045. int idx = deselectedIdxs[i].row();
  1046. obs_sceneitem_select(stm->items[idx], false);
  1047. }
  1048. }
  1049. QListView::selectionChanged(selected, deselected);
  1050. }
  1051. void SourceTree::Edit(int row)
  1052. {
  1053. SourceTreeModel *stm = GetStm();
  1054. if (row < 0 || row >= stm->items.count())
  1055. return;
  1056. QModelIndex index = stm->createIndex(row, 0);
  1057. QWidget *widget = indexWidget(index);
  1058. SourceTreeItem *itemWidget = reinterpret_cast<SourceTreeItem *>(widget);
  1059. if (itemWidget->IsEditing())
  1060. return;
  1061. itemWidget->EnterEditMode();
  1062. edit(index);
  1063. }
  1064. bool SourceTree::MultipleBaseSelected() const
  1065. {
  1066. SourceTreeModel *stm = GetStm();
  1067. QModelIndexList selectedIndices = selectedIndexes();
  1068. OBSScene scene = GetCurrentScene();
  1069. if (selectedIndices.size() < 1) {
  1070. return false;
  1071. }
  1072. for (auto &idx : selectedIndices) {
  1073. obs_sceneitem_t *item = stm->items[idx.row()];
  1074. if (obs_sceneitem_is_group(item)) {
  1075. return false;
  1076. }
  1077. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1078. if (itemScene != scene) {
  1079. return false;
  1080. }
  1081. }
  1082. return true;
  1083. }
  1084. bool SourceTree::GroupsSelected() const
  1085. {
  1086. SourceTreeModel *stm = GetStm();
  1087. QModelIndexList selectedIndices = selectedIndexes();
  1088. OBSScene scene = GetCurrentScene();
  1089. if (selectedIndices.size() < 1) {
  1090. return false;
  1091. }
  1092. for (auto &idx : selectedIndices) {
  1093. obs_sceneitem_t *item = stm->items[idx.row()];
  1094. if (!obs_sceneitem_is_group(item)) {
  1095. return false;
  1096. }
  1097. }
  1098. return true;
  1099. }
  1100. bool SourceTree::GroupedItemsSelected() const
  1101. {
  1102. SourceTreeModel *stm = GetStm();
  1103. QModelIndexList selectedIndices = selectedIndexes();
  1104. OBSScene scene = GetCurrentScene();
  1105. if (!selectedIndices.size()) {
  1106. return false;
  1107. }
  1108. for (auto &idx : selectedIndices) {
  1109. obs_sceneitem_t *item = stm->items[idx.row()];
  1110. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1111. if (itemScene != scene) {
  1112. return true;
  1113. }
  1114. }
  1115. return false;
  1116. }
  1117. void SourceTree::Remove(OBSSceneItem item)
  1118. {
  1119. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1120. GetStm()->Remove(item);
  1121. main->SaveProject();
  1122. if (!main->SavingDisabled()) {
  1123. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1124. obs_source_t *sceneSource = obs_scene_get_source(scene);
  1125. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  1126. blog(LOG_INFO, "User Removed source '%s' (%s) from scene '%s'",
  1127. obs_source_get_name(itemSource),
  1128. obs_source_get_id(itemSource),
  1129. obs_source_get_name(sceneSource));
  1130. }
  1131. }
  1132. void SourceTree::GroupSelectedItems()
  1133. {
  1134. QModelIndexList indices = selectedIndexes();
  1135. std::sort(indices.begin(), indices.end());
  1136. GetStm()->GroupSelectedItems(indices);
  1137. }
  1138. void SourceTree::UngroupSelectedGroups()
  1139. {
  1140. QModelIndexList indices = selectedIndexes();
  1141. GetStm()->UngroupSelectedGroups(indices);
  1142. }
  1143. void SourceTree::AddGroup()
  1144. {
  1145. GetStm()->AddGroup();
  1146. }
  1147. void SourceTree::UpdateNoSourcesMessage()
  1148. {
  1149. std::string darkPath;
  1150. GetDataFilePath("themes/Dark/no_sources.svg", darkPath);
  1151. QColor color = palette().text().color();
  1152. bool lightTheme = (color.redF() < 0.5);
  1153. QString file = lightTheme ? ":res/images/no_sources.svg"
  1154. : darkPath.c_str();
  1155. iconNoSources.load(file);
  1156. QTextOption opt(Qt::AlignHCenter);
  1157. opt.setWrapMode(QTextOption::WordWrap);
  1158. textNoSources.setTextOption(opt);
  1159. textNoSources.setText(QTStr("NoSources.Label").replace("\n", "<br/>"));
  1160. textPrepared = false;
  1161. }
  1162. void SourceTree::paintEvent(QPaintEvent *event)
  1163. {
  1164. SourceTreeModel *stm = GetStm();
  1165. if (stm && !stm->items.count()) {
  1166. QPainter p(viewport());
  1167. if (!textPrepared) {
  1168. textNoSources.prepare(QTransform(), p.font());
  1169. textPrepared = true;
  1170. }
  1171. QRectF iconRect = iconNoSources.viewBoxF();
  1172. QSizeF iconSize = iconRect.size();
  1173. QSizeF textSize = textNoSources.size();
  1174. QSizeF thisSize = size();
  1175. qreal totalHeight = textSize.height() + iconSize.height();
  1176. qreal x = thisSize.width() / 2.0 - textSize.width() / 2.0;
  1177. qreal y = thisSize.height() / 2.0 - totalHeight / 2.0;
  1178. p.drawStaticText(x, y, textNoSources);
  1179. x = thisSize.width() / 2.0 - iconSize.width() / 2.0;
  1180. y += textSize.height();
  1181. iconRect.moveTo(x, y);
  1182. iconNoSources.render(&p, iconRect);
  1183. } else {
  1184. QListView::paintEvent(event);
  1185. }
  1186. }