1
0

source-tree.cpp 43 KB

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