source-tree.cpp 40 KB

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