source-tree.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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 (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. label->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 ((int)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. }
  519. }
  520. void SourceTreeModel::Clear()
  521. {
  522. beginResetModel();
  523. items.clear();
  524. endResetModel();
  525. hasGroups = false;
  526. }
  527. static bool enumItem(obs_scene_t *, obs_sceneitem_t *item, void *ptr)
  528. {
  529. QVector<OBSSceneItem> &items =
  530. *reinterpret_cast<QVector<OBSSceneItem> *>(ptr);
  531. obs_source_t *src = obs_sceneitem_get_source(item);
  532. if (obs_source_removed(src)) {
  533. return true;
  534. }
  535. if (obs_sceneitem_is_group(item)) {
  536. OBSDataAutoRelease data =
  537. obs_sceneitem_get_private_settings(item);
  538. bool collapse = obs_data_get_bool(data, "collapsed");
  539. if (!collapse) {
  540. obs_scene_t *scene =
  541. obs_sceneitem_group_get_scene(item);
  542. obs_scene_enum_items(scene, enumItem, &items);
  543. }
  544. }
  545. items.insert(0, item);
  546. return true;
  547. }
  548. void SourceTreeModel::SceneChanged()
  549. {
  550. OBSScene scene = GetCurrentScene();
  551. beginResetModel();
  552. items.clear();
  553. obs_scene_enum_items(scene, enumItem, &items);
  554. endResetModel();
  555. UpdateGroupState(false);
  556. st->ResetWidgets();
  557. for (int i = 0; i < items.count(); i++) {
  558. bool select = obs_sceneitem_selected(items[i]);
  559. QModelIndex index = createIndex(i, 0);
  560. st->selectionModel()->select(
  561. index, select ? QItemSelectionModel::Select
  562. : QItemSelectionModel::Deselect);
  563. }
  564. }
  565. /* moves a scene item index (blame linux distros for using older Qt builds) */
  566. static inline void MoveItem(QVector<OBSSceneItem> &items, int oldIdx,
  567. int newIdx)
  568. {
  569. OBSSceneItem item = items[oldIdx];
  570. items.remove(oldIdx);
  571. items.insert(newIdx, item);
  572. }
  573. /* reorders list optimally with model reorder funcs */
  574. void SourceTreeModel::ReorderItems()
  575. {
  576. OBSScene scene = GetCurrentScene();
  577. QVector<OBSSceneItem> newitems;
  578. obs_scene_enum_items(scene, enumItem, &newitems);
  579. /* if item list has changed size, do full reset */
  580. if (newitems.count() != items.count()) {
  581. SceneChanged();
  582. return;
  583. }
  584. for (;;) {
  585. int idx1Old = 0;
  586. int idx1New = 0;
  587. int count;
  588. int i;
  589. /* find first starting changed item index */
  590. for (i = 0; i < newitems.count(); i++) {
  591. obs_sceneitem_t *oldItem = items[i];
  592. obs_sceneitem_t *newItem = newitems[i];
  593. if (oldItem != newItem) {
  594. idx1Old = i;
  595. break;
  596. }
  597. }
  598. /* if everything is the same, break */
  599. if (i == newitems.count()) {
  600. break;
  601. }
  602. /* find new starting index */
  603. for (i = idx1Old + 1; i < newitems.count(); i++) {
  604. obs_sceneitem_t *oldItem = items[idx1Old];
  605. obs_sceneitem_t *newItem = newitems[i];
  606. if (oldItem == newItem) {
  607. idx1New = i;
  608. break;
  609. }
  610. }
  611. /* if item could not be found, do full reset */
  612. if (i == newitems.count()) {
  613. SceneChanged();
  614. return;
  615. }
  616. /* get move count */
  617. for (count = 1; (idx1New + count) < newitems.count(); count++) {
  618. int oldIdx = idx1Old + count;
  619. int newIdx = idx1New + count;
  620. obs_sceneitem_t *oldItem = items[oldIdx];
  621. obs_sceneitem_t *newItem = newitems[newIdx];
  622. if (oldItem != newItem) {
  623. break;
  624. }
  625. }
  626. /* move items */
  627. beginMoveRows(QModelIndex(), idx1Old, idx1Old + count - 1,
  628. QModelIndex(), idx1New + count);
  629. for (i = 0; i < count; i++) {
  630. int to = idx1New + count;
  631. if (to > idx1Old)
  632. to--;
  633. MoveItem(items, idx1Old, to);
  634. }
  635. endMoveRows();
  636. }
  637. }
  638. void SourceTreeModel::Add(obs_sceneitem_t *item)
  639. {
  640. if (obs_sceneitem_is_group(item)) {
  641. SceneChanged();
  642. } else {
  643. beginInsertRows(QModelIndex(), 0, 0);
  644. items.insert(0, item);
  645. endInsertRows();
  646. st->UpdateWidget(createIndex(0, 0, nullptr), item);
  647. }
  648. }
  649. void SourceTreeModel::Remove(obs_sceneitem_t *item)
  650. {
  651. int idx = -1;
  652. for (int i = 0; i < items.count(); i++) {
  653. if (items[i] == item) {
  654. idx = i;
  655. break;
  656. }
  657. }
  658. if (idx == -1)
  659. return;
  660. int startIdx = idx;
  661. int endIdx = idx;
  662. bool is_group = obs_sceneitem_is_group(item);
  663. if (is_group) {
  664. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  665. for (int i = endIdx + 1; i < items.count(); i++) {
  666. obs_sceneitem_t *subitem = items[i];
  667. obs_scene_t *subscene =
  668. obs_sceneitem_get_scene(subitem);
  669. if (subscene == scene)
  670. endIdx = i;
  671. else
  672. break;
  673. }
  674. }
  675. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  676. items.remove(idx, endIdx - startIdx + 1);
  677. endRemoveRows();
  678. if (is_group)
  679. UpdateGroupState(true);
  680. }
  681. OBSSceneItem SourceTreeModel::Get(int idx)
  682. {
  683. if (idx == -1 || idx >= items.count())
  684. return OBSSceneItem();
  685. return items[idx];
  686. }
  687. SourceTreeModel::SourceTreeModel(SourceTree *st_)
  688. : QAbstractListModel(st_), st(st_)
  689. {
  690. obs_frontend_add_event_callback(OBSFrontendEvent, this);
  691. }
  692. SourceTreeModel::~SourceTreeModel()
  693. {
  694. obs_frontend_remove_event_callback(OBSFrontendEvent, this);
  695. }
  696. int SourceTreeModel::rowCount(const QModelIndex &parent) const
  697. {
  698. return parent.isValid() ? 0 : items.count();
  699. }
  700. QVariant SourceTreeModel::data(const QModelIndex &index, int role) const
  701. {
  702. if (role == Qt::AccessibleTextRole) {
  703. OBSSceneItem item = items[index.row()];
  704. obs_source_t *source = obs_sceneitem_get_source(item);
  705. return QVariant(QT_UTF8(obs_source_get_name(source)));
  706. }
  707. return QVariant();
  708. }
  709. Qt::ItemFlags SourceTreeModel::flags(const QModelIndex &index) const
  710. {
  711. if (!index.isValid())
  712. return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled;
  713. obs_sceneitem_t *item = items[index.row()];
  714. bool is_group = obs_sceneitem_is_group(item);
  715. return QAbstractListModel::flags(index) | Qt::ItemIsEditable |
  716. Qt::ItemIsDragEnabled |
  717. (is_group ? Qt::ItemIsDropEnabled : Qt::NoItemFlags);
  718. }
  719. Qt::DropActions SourceTreeModel::supportedDropActions() const
  720. {
  721. return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
  722. }
  723. QString SourceTreeModel::GetNewGroupName()
  724. {
  725. OBSScene scene = GetCurrentScene();
  726. QString name = QTStr("Group");
  727. int i = 2;
  728. for (;;) {
  729. OBSSourceAutoRelease group =
  730. obs_get_source_by_name(QT_TO_UTF8(name));
  731. if (!group)
  732. break;
  733. name = QTStr("Basic.Main.Group").arg(QString::number(i++));
  734. }
  735. return name;
  736. }
  737. void SourceTreeModel::AddGroup()
  738. {
  739. QString name = GetNewGroupName();
  740. obs_sceneitem_t *group =
  741. obs_scene_add_group(GetCurrentScene(), QT_TO_UTF8(name));
  742. if (!group)
  743. return;
  744. beginInsertRows(QModelIndex(), 0, 0);
  745. items.insert(0, group);
  746. endInsertRows();
  747. st->UpdateWidget(createIndex(0, 0, nullptr), group);
  748. UpdateGroupState(true);
  749. QMetaObject::invokeMethod(st, "Edit", Qt::QueuedConnection,
  750. Q_ARG(int, 0));
  751. }
  752. void SourceTreeModel::GroupSelectedItems(QModelIndexList &indices)
  753. {
  754. if (indices.count() == 0)
  755. return;
  756. OBSBasic *main = OBSBasic::Get();
  757. OBSScene scene = GetCurrentScene();
  758. QString name = GetNewGroupName();
  759. QVector<obs_sceneitem_t *> item_order;
  760. for (int i = indices.count() - 1; i >= 0; i--) {
  761. obs_sceneitem_t *item = items[indices[i].row()];
  762. item_order << item;
  763. }
  764. st->undoSceneData = main->BackupScene(scene);
  765. obs_sceneitem_t *item = obs_scene_insert_group(
  766. scene, QT_TO_UTF8(name), item_order.data(), item_order.size());
  767. if (!item) {
  768. st->undoSceneData = nullptr;
  769. return;
  770. }
  771. main->undo_s.push_disabled();
  772. for (obs_sceneitem_t *item : item_order)
  773. obs_sceneitem_select(item, false);
  774. hasGroups = true;
  775. st->UpdateWidgets(true);
  776. obs_sceneitem_select(item, true);
  777. /* ----------------------------------------------------------------- */
  778. /* obs_scene_insert_group triggers a full refresh of scene items via */
  779. /* the item_add signal. No need to insert a row, just edit the one */
  780. /* that's created automatically. */
  781. int newIdx = indices[0].row();
  782. QMetaObject::invokeMethod(st, "NewGroupEdit", Qt::QueuedConnection,
  783. Q_ARG(int, newIdx));
  784. }
  785. void SourceTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
  786. {
  787. OBSBasic *main = OBSBasic::Get();
  788. if (indices.count() == 0)
  789. return;
  790. OBSScene scene = main->GetCurrentScene();
  791. OBSData undoData = main->BackupScene(scene);
  792. for (int i = indices.count() - 1; i >= 0; i--) {
  793. obs_sceneitem_t *item = items[indices[i].row()];
  794. obs_sceneitem_group_ungroup(item);
  795. }
  796. SceneChanged();
  797. OBSData redoData = main->BackupScene(scene);
  798. main->CreateSceneUndoRedoAction(QTStr("Basic.Main.Ungroup"), undoData,
  799. redoData);
  800. }
  801. void SourceTreeModel::ExpandGroup(obs_sceneitem_t *item)
  802. {
  803. int itemIdx = items.indexOf(item);
  804. if (itemIdx == -1)
  805. return;
  806. itemIdx++;
  807. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  808. QVector<OBSSceneItem> subItems;
  809. obs_scene_enum_items(scene, enumItem, &subItems);
  810. if (!subItems.size())
  811. return;
  812. beginInsertRows(QModelIndex(), itemIdx, itemIdx + subItems.size() - 1);
  813. for (int i = 0; i < subItems.size(); i++)
  814. items.insert(i + itemIdx, subItems[i]);
  815. endInsertRows();
  816. st->UpdateWidgets();
  817. }
  818. void SourceTreeModel::CollapseGroup(obs_sceneitem_t *item)
  819. {
  820. int startIdx = -1;
  821. int endIdx = -1;
  822. obs_scene_t *scene = obs_sceneitem_group_get_scene(item);
  823. for (int i = 0; i < items.size(); i++) {
  824. obs_scene_t *itemScene = obs_sceneitem_get_scene(items[i]);
  825. if (itemScene == scene) {
  826. if (startIdx == -1)
  827. startIdx = i;
  828. endIdx = i;
  829. }
  830. }
  831. if (startIdx == -1)
  832. return;
  833. beginRemoveRows(QModelIndex(), startIdx, endIdx);
  834. items.remove(startIdx, endIdx - startIdx + 1);
  835. endRemoveRows();
  836. }
  837. void SourceTreeModel::UpdateGroupState(bool update)
  838. {
  839. bool nowHasGroups = false;
  840. for (auto &item : items) {
  841. if (obs_sceneitem_is_group(item)) {
  842. nowHasGroups = true;
  843. break;
  844. }
  845. }
  846. if (nowHasGroups != hasGroups) {
  847. hasGroups = nowHasGroups;
  848. if (update) {
  849. st->UpdateWidgets(true);
  850. }
  851. }
  852. }
  853. /* ========================================================================= */
  854. SourceTree::SourceTree(QWidget *parent_) : QListView(parent_)
  855. {
  856. SourceTreeModel *stm_ = new SourceTreeModel(this);
  857. setModel(stm_);
  858. setStyleSheet(QString(
  859. "*[bgColor=\"1\"]{background-color:rgba(255,68,68,33%);}"
  860. "*[bgColor=\"2\"]{background-color:rgba(255,255,68,33%);}"
  861. "*[bgColor=\"3\"]{background-color:rgba(68,255,68,33%);}"
  862. "*[bgColor=\"4\"]{background-color:rgba(68,255,255,33%);}"
  863. "*[bgColor=\"5\"]{background-color:rgba(68,68,255,33%);}"
  864. "*[bgColor=\"6\"]{background-color:rgba(255,68,255,33%);}"
  865. "*[bgColor=\"7\"]{background-color:rgba(68,68,68,33%);}"
  866. "*[bgColor=\"8\"]{background-color:rgba(255,255,255,33%);}"));
  867. UpdateNoSourcesMessage();
  868. connect(App(), &OBSApp::StyleChanged, this,
  869. &SourceTree::UpdateNoSourcesMessage);
  870. connect(App(), &OBSApp::StyleChanged, this, &SourceTree::UpdateIcons);
  871. setItemDelegate(new SourceTreeDelegate(this));
  872. }
  873. void SourceTree::UpdateIcons()
  874. {
  875. SourceTreeModel *stm = GetStm();
  876. stm->SceneChanged();
  877. }
  878. void SourceTree::SetIconsVisible(bool visible)
  879. {
  880. SourceTreeModel *stm = GetStm();
  881. iconsVisible = visible;
  882. stm->SceneChanged();
  883. }
  884. void SourceTree::ResetWidgets()
  885. {
  886. OBSScene scene = GetCurrentScene();
  887. SourceTreeModel *stm = GetStm();
  888. stm->UpdateGroupState(false);
  889. for (int i = 0; i < stm->items.count(); i++) {
  890. QModelIndex index = stm->createIndex(i, 0, nullptr);
  891. setIndexWidget(index, new SourceTreeItem(this, stm->items[i]));
  892. }
  893. }
  894. void SourceTree::UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item)
  895. {
  896. setIndexWidget(idx, new SourceTreeItem(this, item));
  897. }
  898. void SourceTree::UpdateWidgets(bool force)
  899. {
  900. SourceTreeModel *stm = GetStm();
  901. for (int i = 0; i < stm->items.size(); i++) {
  902. obs_sceneitem_t *item = stm->items[i];
  903. SourceTreeItem *widget = GetItemWidget(i);
  904. if (!widget) {
  905. UpdateWidget(stm->createIndex(i, 0), item);
  906. } else {
  907. widget->Update(force);
  908. }
  909. }
  910. }
  911. void SourceTree::SelectItem(obs_sceneitem_t *sceneitem, bool select)
  912. {
  913. SourceTreeModel *stm = GetStm();
  914. int i = 0;
  915. for (; i < stm->items.count(); i++) {
  916. if (stm->items[i] == sceneitem)
  917. break;
  918. }
  919. if (i == stm->items.count())
  920. return;
  921. QModelIndex index = stm->createIndex(i, 0);
  922. if (index.isValid())
  923. selectionModel()->select(
  924. index, select ? QItemSelectionModel::Select
  925. : QItemSelectionModel::Deselect);
  926. }
  927. Q_DECLARE_METATYPE(OBSSceneItem);
  928. void SourceTree::mouseDoubleClickEvent(QMouseEvent *event)
  929. {
  930. if (event->button() == Qt::LeftButton)
  931. QListView::mouseDoubleClickEvent(event);
  932. }
  933. void SourceTree::dropEvent(QDropEvent *event)
  934. {
  935. if (event->source() != this) {
  936. QListView::dropEvent(event);
  937. return;
  938. }
  939. OBSBasic *main = OBSBasic::Get();
  940. OBSScene scene = GetCurrentScene();
  941. obs_source_t *scenesource = obs_scene_get_source(scene);
  942. SourceTreeModel *stm = GetStm();
  943. auto &items = stm->items;
  944. QModelIndexList indices = selectedIndexes();
  945. DropIndicatorPosition indicator = dropIndicatorPosition();
  946. int row = indexAt(
  947. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  948. event->position().toPoint()
  949. #else
  950. event->pos()
  951. #endif
  952. )
  953. .row();
  954. bool emptyDrop = row == -1;
  955. if (emptyDrop) {
  956. if (!items.size()) {
  957. QListView::dropEvent(event);
  958. return;
  959. }
  960. row = items.size() - 1;
  961. indicator = QAbstractItemView::BelowItem;
  962. }
  963. /* --------------------------------------- */
  964. /* store destination group if moving to a */
  965. /* group */
  966. obs_sceneitem_t *dropItem = items[row]; /* item being dropped on */
  967. bool itemIsGroup = obs_sceneitem_is_group(dropItem);
  968. obs_sceneitem_t *dropGroup =
  969. itemIsGroup ? dropItem
  970. : obs_sceneitem_get_group(scene, dropItem);
  971. /* not a group if moving above the group */
  972. if (indicator == QAbstractItemView::AboveItem && itemIsGroup)
  973. dropGroup = nullptr;
  974. if (emptyDrop)
  975. dropGroup = nullptr;
  976. /* --------------------------------------- */
  977. /* remember to remove list items if */
  978. /* dropping on collapsed group */
  979. bool dropOnCollapsed = false;
  980. if (dropGroup) {
  981. obs_data_t *data =
  982. obs_sceneitem_get_private_settings(dropGroup);
  983. dropOnCollapsed = obs_data_get_bool(data, "collapsed");
  984. obs_data_release(data);
  985. }
  986. if (indicator == QAbstractItemView::BelowItem ||
  987. indicator == QAbstractItemView::OnItem ||
  988. indicator == QAbstractItemView::OnViewport)
  989. row++;
  990. if (row < 0 || row > stm->items.count()) {
  991. QListView::dropEvent(event);
  992. return;
  993. }
  994. /* --------------------------------------- */
  995. /* determine if any base group is selected */
  996. bool hasGroups = false;
  997. for (int i = 0; i < indices.size(); i++) {
  998. obs_sceneitem_t *item = items[indices[i].row()];
  999. if (obs_sceneitem_is_group(item)) {
  1000. hasGroups = true;
  1001. break;
  1002. }
  1003. }
  1004. /* --------------------------------------- */
  1005. /* if dropping a group, detect if it's */
  1006. /* below another group */
  1007. obs_sceneitem_t *itemBelow;
  1008. if (row == stm->items.count())
  1009. itemBelow = nullptr;
  1010. else
  1011. itemBelow = stm->items[row];
  1012. if (hasGroups) {
  1013. if (!itemBelow ||
  1014. obs_sceneitem_get_group(scene, itemBelow) != dropGroup) {
  1015. indicator = QAbstractItemView::BelowItem;
  1016. dropGroup = nullptr;
  1017. dropOnCollapsed = false;
  1018. }
  1019. }
  1020. /* --------------------------------------- */
  1021. /* if dropping groups on other groups, */
  1022. /* disregard as invalid drag/drop */
  1023. if (dropGroup && hasGroups) {
  1024. QListView::dropEvent(event);
  1025. return;
  1026. }
  1027. /* --------------------------------------- */
  1028. /* save undo data */
  1029. std::vector<obs_source_t *> sources;
  1030. for (int i = 0; i < indices.size(); i++) {
  1031. obs_sceneitem_t *item = items[indices[i].row()];
  1032. if (obs_sceneitem_get_scene(item) != scene)
  1033. sources.push_back(obs_scene_get_source(
  1034. obs_sceneitem_get_scene(item)));
  1035. }
  1036. if (dropGroup)
  1037. sources.push_back(obs_sceneitem_get_source(dropGroup));
  1038. OBSData undo_data = main->BackupScene(scene, &sources);
  1039. /* --------------------------------------- */
  1040. /* if selection includes base group items, */
  1041. /* include all group sub-items and treat */
  1042. /* them all as one */
  1043. if (hasGroups) {
  1044. /* remove sub-items if selected */
  1045. for (int i = indices.size() - 1; i >= 0; i--) {
  1046. obs_sceneitem_t *item = items[indices[i].row()];
  1047. obs_scene_t *itemScene = obs_sceneitem_get_scene(item);
  1048. if (itemScene != scene) {
  1049. indices.removeAt(i);
  1050. }
  1051. }
  1052. /* add all sub-items of selected groups */
  1053. for (int i = indices.size() - 1; i >= 0; i--) {
  1054. obs_sceneitem_t *item = items[indices[i].row()];
  1055. if (obs_sceneitem_is_group(item)) {
  1056. for (int j = items.size() - 1; j >= 0; j--) {
  1057. obs_sceneitem_t *subitem = items[j];
  1058. obs_sceneitem_t *subitemGroup =
  1059. obs_sceneitem_get_group(
  1060. scene, subitem);
  1061. if (subitemGroup == item) {
  1062. QModelIndex idx =
  1063. stm->createIndex(j, 0);
  1064. indices.insert(i + 1, idx);
  1065. }
  1066. }
  1067. }
  1068. }
  1069. }
  1070. /* --------------------------------------- */
  1071. /* build persistent indices */
  1072. QList<QPersistentModelIndex> persistentIndices;
  1073. persistentIndices.reserve(indices.count());
  1074. for (QModelIndex &index : indices)
  1075. persistentIndices.append(index);
  1076. std::sort(persistentIndices.begin(), persistentIndices.end());
  1077. /* --------------------------------------- */
  1078. /* move all items to destination index */
  1079. int r = row;
  1080. for (auto &persistentIdx : persistentIndices) {
  1081. int from = persistentIdx.row();
  1082. int to = r;
  1083. int itemTo = to;
  1084. if (itemTo > from)
  1085. itemTo--;
  1086. if (itemTo != from) {
  1087. stm->beginMoveRows(QModelIndex(), from, from,
  1088. QModelIndex(), to);
  1089. MoveItem(items, from, itemTo);
  1090. stm->endMoveRows();
  1091. }
  1092. r = persistentIdx.row() + 1;
  1093. }
  1094. std::sort(persistentIndices.begin(), persistentIndices.end());
  1095. int firstIdx = persistentIndices.front().row();
  1096. int lastIdx = persistentIndices.back().row();
  1097. /* --------------------------------------- */
  1098. /* reorder scene items in back-end */
  1099. QVector<struct obs_sceneitem_order_info> orderList;
  1100. obs_sceneitem_t *lastGroup = nullptr;
  1101. int insertCollapsedIdx = 0;
  1102. auto insertCollapsed = [&](obs_sceneitem_t *item) {
  1103. struct obs_sceneitem_order_info info;
  1104. info.group = lastGroup;
  1105. info.item = item;
  1106. orderList.insert(insertCollapsedIdx++, info);
  1107. };
  1108. using insertCollapsed_t = decltype(insertCollapsed);
  1109. auto preInsertCollapsed = [](obs_scene_t *, obs_sceneitem_t *item,
  1110. void *param) {
  1111. (*reinterpret_cast<insertCollapsed_t *>(param))(item);
  1112. return true;
  1113. };
  1114. auto insertLastGroup = [&]() {
  1115. OBSDataAutoRelease data =
  1116. obs_sceneitem_get_private_settings(lastGroup);
  1117. bool collapsed = obs_data_get_bool(data, "collapsed");
  1118. if (collapsed) {
  1119. insertCollapsedIdx = 0;
  1120. obs_sceneitem_group_enum_items(lastGroup,
  1121. preInsertCollapsed,
  1122. &insertCollapsed);
  1123. }
  1124. struct obs_sceneitem_order_info info;
  1125. info.group = nullptr;
  1126. info.item = lastGroup;
  1127. orderList.insert(0, info);
  1128. };
  1129. auto updateScene = [&]() {
  1130. struct obs_sceneitem_order_info info;
  1131. for (int i = 0; i < items.size(); i++) {
  1132. obs_sceneitem_t *item = items[i];
  1133. obs_sceneitem_t *group;
  1134. if (obs_sceneitem_is_group(item)) {
  1135. if (lastGroup) {
  1136. insertLastGroup();
  1137. }
  1138. lastGroup = item;
  1139. continue;
  1140. }
  1141. if (!hasGroups && i >= firstIdx && i <= lastIdx)
  1142. group = dropGroup;
  1143. else
  1144. group = obs_sceneitem_get_group(scene, item);
  1145. if (lastGroup && lastGroup != group) {
  1146. insertLastGroup();
  1147. }
  1148. lastGroup = group;
  1149. info.group = group;
  1150. info.item = item;
  1151. orderList.insert(0, info);
  1152. }
  1153. if (lastGroup) {
  1154. insertLastGroup();
  1155. }
  1156. obs_scene_reorder_items2(scene, orderList.data(),
  1157. orderList.size());
  1158. };
  1159. using updateScene_t = decltype(updateScene);
  1160. auto preUpdateScene = [](void *data, obs_scene_t *) {
  1161. (*reinterpret_cast<updateScene_t *>(data))();
  1162. };
  1163. ignoreReorder = true;
  1164. obs_scene_atomic_update(scene, preUpdateScene, &updateScene);
  1165. ignoreReorder = false;
  1166. /* --------------------------------------- */
  1167. /* save redo data */
  1168. OBSData redo_data = main->BackupScene(scene, &sources);
  1169. /* --------------------------------------- */
  1170. /* add undo/redo action */
  1171. const char *scene_name = obs_source_get_name(scenesource);
  1172. QString action_name = QTStr("Undo.ReorderSources").arg(scene_name);
  1173. main->CreateSceneUndoRedoAction(action_name, undo_data, redo_data);
  1174. /* --------------------------------------- */
  1175. /* remove items if dropped in to collapsed */
  1176. /* group */
  1177. if (dropOnCollapsed) {
  1178. stm->beginRemoveRows(QModelIndex(), firstIdx, lastIdx);
  1179. items.remove(firstIdx, lastIdx - firstIdx + 1);
  1180. stm->endRemoveRows();
  1181. }
  1182. /* --------------------------------------- */
  1183. /* update widgets and accept event */
  1184. UpdateWidgets(true);
  1185. event->accept();
  1186. event->setDropAction(Qt::CopyAction);
  1187. QListView::dropEvent(event);
  1188. }
  1189. void SourceTree::selectionChanged(const QItemSelection &selected,
  1190. const QItemSelection &deselected)
  1191. {
  1192. {
  1193. SignalBlocker sourcesSignalBlocker(this);
  1194. SourceTreeModel *stm = GetStm();
  1195. QModelIndexList selectedIdxs = selected.indexes();
  1196. QModelIndexList deselectedIdxs = deselected.indexes();
  1197. for (int i = 0; i < selectedIdxs.count(); i++) {
  1198. int idx = selectedIdxs[i].row();
  1199. obs_sceneitem_select(stm->items[idx], true);
  1200. }
  1201. for (int i = 0; i < deselectedIdxs.count(); i++) {
  1202. int idx = deselectedIdxs[i].row();
  1203. obs_sceneitem_select(stm->items[idx], false);
  1204. }
  1205. }
  1206. QListView::selectionChanged(selected, deselected);
  1207. }
  1208. void SourceTree::NewGroupEdit(int row)
  1209. {
  1210. if (!Edit(row)) {
  1211. OBSBasic *main = OBSBasic::Get();
  1212. main->undo_s.pop_disabled();
  1213. blog(LOG_WARNING, "Uh, somehow the edit didn't process, this "
  1214. "code should never be reached.\nAnd by "
  1215. "\"never be reached\", I mean that "
  1216. "theoretically, it should be\nimpossible "
  1217. "for this code to be reached. But if this "
  1218. "code is reached,\nfeel free to laugh at "
  1219. "Jim, because apparently it is, in fact, "
  1220. "actually\npossible for this code to be "
  1221. "reached. But I mean, again, theoretically\n"
  1222. "it should be impossible. So if you see "
  1223. "this in your log, just know that\nit's "
  1224. "really dumb, and depressing. But at least "
  1225. "the undo/redo action is\nstill covered, so "
  1226. "in theory things *should* be fine. But "
  1227. "it's entirely\npossible that they might "
  1228. "not be exactly. But again, yea. This "
  1229. "really\nshould not be possible.");
  1230. OBSData redoSceneData = main->BackupScene(GetCurrentScene());
  1231. QString text = QTStr("Undo.GroupItems").arg("Unknown");
  1232. main->CreateSceneUndoRedoAction(text, undoSceneData,
  1233. redoSceneData);
  1234. undoSceneData = nullptr;
  1235. }
  1236. }
  1237. bool SourceTree::Edit(int row)
  1238. {
  1239. SourceTreeModel *stm = GetStm();
  1240. if (row < 0 || row >= stm->items.count())
  1241. return false;
  1242. QModelIndex index = stm->createIndex(row, 0);
  1243. QWidget *widget = indexWidget(index);
  1244. SourceTreeItem *itemWidget = reinterpret_cast<SourceTreeItem *>(widget);
  1245. if (itemWidget->IsEditing())
  1246. return false;
  1247. itemWidget->EnterEditMode();
  1248. edit(index);
  1249. return true;
  1250. }
  1251. bool SourceTree::MultipleBaseSelected() const
  1252. {
  1253. SourceTreeModel *stm = GetStm();
  1254. QModelIndexList selectedIndices = selectedIndexes();
  1255. OBSScene scene = GetCurrentScene();
  1256. if (selectedIndices.size() < 1) {
  1257. return false;
  1258. }
  1259. for (auto &idx : selectedIndices) {
  1260. obs_sceneitem_t *item = stm->items[idx.row()];
  1261. if (obs_sceneitem_is_group(item)) {
  1262. return false;
  1263. }
  1264. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1265. if (itemScene != scene) {
  1266. return false;
  1267. }
  1268. }
  1269. return true;
  1270. }
  1271. bool SourceTree::GroupsSelected() const
  1272. {
  1273. SourceTreeModel *stm = GetStm();
  1274. QModelIndexList selectedIndices = selectedIndexes();
  1275. OBSScene scene = GetCurrentScene();
  1276. if (selectedIndices.size() < 1) {
  1277. return false;
  1278. }
  1279. for (auto &idx : selectedIndices) {
  1280. obs_sceneitem_t *item = stm->items[idx.row()];
  1281. if (!obs_sceneitem_is_group(item)) {
  1282. return false;
  1283. }
  1284. }
  1285. return true;
  1286. }
  1287. bool SourceTree::GroupedItemsSelected() const
  1288. {
  1289. SourceTreeModel *stm = GetStm();
  1290. QModelIndexList selectedIndices = selectedIndexes();
  1291. OBSScene scene = GetCurrentScene();
  1292. if (!selectedIndices.size()) {
  1293. return false;
  1294. }
  1295. for (auto &idx : selectedIndices) {
  1296. obs_sceneitem_t *item = stm->items[idx.row()];
  1297. obs_scene *itemScene = obs_sceneitem_get_scene(item);
  1298. if (itemScene != scene) {
  1299. return true;
  1300. }
  1301. }
  1302. return false;
  1303. }
  1304. void SourceTree::Remove(OBSSceneItem item)
  1305. {
  1306. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1307. GetStm()->Remove(item);
  1308. main->SaveProject();
  1309. if (!main->SavingDisabled()) {
  1310. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1311. obs_source_t *sceneSource = obs_scene_get_source(scene);
  1312. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  1313. blog(LOG_INFO, "User Removed source '%s' (%s) from scene '%s'",
  1314. obs_source_get_name(itemSource),
  1315. obs_source_get_id(itemSource),
  1316. obs_source_get_name(sceneSource));
  1317. }
  1318. }
  1319. void SourceTree::GroupSelectedItems()
  1320. {
  1321. QModelIndexList indices = selectedIndexes();
  1322. std::sort(indices.begin(), indices.end());
  1323. GetStm()->GroupSelectedItems(indices);
  1324. }
  1325. void SourceTree::UngroupSelectedGroups()
  1326. {
  1327. QModelIndexList indices = selectedIndexes();
  1328. GetStm()->UngroupSelectedGroups(indices);
  1329. }
  1330. void SourceTree::AddGroup()
  1331. {
  1332. GetStm()->AddGroup();
  1333. }
  1334. void SourceTree::UpdateNoSourcesMessage()
  1335. {
  1336. std::string darkPath;
  1337. GetDataFilePath("themes/Dark/no_sources.svg", darkPath);
  1338. QString file = !App()->IsThemeDark() ? ":res/images/no_sources.svg"
  1339. : darkPath.c_str();
  1340. iconNoSources.load(file);
  1341. QTextOption opt(Qt::AlignHCenter);
  1342. opt.setWrapMode(QTextOption::WordWrap);
  1343. textNoSources.setTextOption(opt);
  1344. textNoSources.setText(QTStr("NoSources.Label").replace("\n", "<br/>"));
  1345. textPrepared = false;
  1346. }
  1347. void SourceTree::paintEvent(QPaintEvent *event)
  1348. {
  1349. SourceTreeModel *stm = GetStm();
  1350. if (stm && !stm->items.count()) {
  1351. QPainter p(viewport());
  1352. if (!textPrepared) {
  1353. textNoSources.prepare(QTransform(), p.font());
  1354. textPrepared = true;
  1355. }
  1356. QRectF iconRect = iconNoSources.viewBoxF();
  1357. iconRect.setSize(QSizeF(32.0, 32.0));
  1358. QSizeF iconSize = iconRect.size();
  1359. QSizeF textSize = textNoSources.size();
  1360. QSizeF thisSize = size();
  1361. const qreal spacing = 16.0;
  1362. qreal totalHeight =
  1363. iconSize.height() + spacing + textSize.height();
  1364. qreal x = thisSize.width() / 2.0 - iconSize.width() / 2.0;
  1365. qreal y = thisSize.height() / 2.0 - totalHeight / 2.0;
  1366. iconRect.moveTo(std::round(x), std::round(y));
  1367. iconNoSources.render(&p, iconRect);
  1368. x = thisSize.width() / 2.0 - textSize.width() / 2.0;
  1369. y += spacing + iconSize.height();
  1370. p.drawStaticText(x, y, textNoSources);
  1371. } else {
  1372. QListView::paintEvent(event);
  1373. }
  1374. }
  1375. SourceTreeDelegate::SourceTreeDelegate(QObject *parent)
  1376. : QStyledItemDelegate(parent)
  1377. {
  1378. }
  1379. QSize SourceTreeDelegate::sizeHint(const QStyleOptionViewItem &option,
  1380. const QModelIndex &index) const
  1381. {
  1382. SourceTree *tree = qobject_cast<SourceTree *>(parent());
  1383. QWidget *item = tree->indexWidget(index);
  1384. if (!item)
  1385. return (QSize(0, 0));
  1386. return (QSize(option.widget->minimumWidth(), item->height()));
  1387. }