source-tree.cpp 43 KB

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