source-tree.cpp 43 KB

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