source-tree.cpp 43 KB

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