source-tree.cpp 43 KB

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