source-tree.cpp 42 KB

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