source-tree.cpp 42 KB

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