source-tree.cpp 43 KB

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