SourceTreeItem.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #include "SourceTreeItem.hpp"
  2. #include <components/OBSSourceLabel.hpp>
  3. #include <widgets/OBSBasic.hpp>
  4. #include <qt-wrappers.hpp>
  5. #include <QCheckBox>
  6. #include <QLineEdit>
  7. #include <QPainter>
  8. #include "moc_SourceTreeItem.cpp"
  9. static inline OBSScene GetCurrentScene()
  10. {
  11. OBSBasic *main = OBSBasic::Get();
  12. return main->GetCurrentScene();
  13. }
  14. SourceTreeItem::SourceTreeItem(SourceTree *tree_, OBSSceneItem sceneitem_) : tree(tree_), sceneitem(sceneitem_)
  15. {
  16. setAttribute(Qt::WA_TranslucentBackground);
  17. setMouseTracking(true);
  18. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  19. const char *name = obs_source_get_name(source);
  20. OBSDataAutoRelease privData = obs_sceneitem_get_private_settings(sceneitem);
  21. int preset = obs_data_get_int(privData, "color-preset");
  22. if (preset == 1) {
  23. const char *color = obs_data_get_string(privData, "color");
  24. std::string col = "background: ";
  25. col += color;
  26. setStyleSheet(col.c_str());
  27. } else if (preset > 1) {
  28. setStyleSheet("");
  29. setProperty("bgColor", preset - 1);
  30. } else {
  31. setStyleSheet("background: none");
  32. }
  33. OBSBasic *main = OBSBasic::Get();
  34. const char *id = obs_source_get_id(source);
  35. bool sourceVisible = obs_sceneitem_visible(sceneitem);
  36. if (tree->iconsVisible) {
  37. QIcon icon;
  38. if (strcmp(id, "scene") == 0)
  39. icon = main->GetSceneIcon();
  40. else if (strcmp(id, "group") == 0)
  41. icon = main->GetGroupIcon();
  42. else
  43. icon = main->GetSourceIcon(id);
  44. QPixmap pixmap = icon.pixmap(QSize(16, 16));
  45. iconLabel = new QLabel();
  46. iconLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  47. iconLabel->setPixmap(pixmap);
  48. iconLabel->setEnabled(sourceVisible);
  49. iconLabel->setStyleSheet("background: none");
  50. iconLabel->setProperty("class", "source-icon");
  51. }
  52. vis = new QCheckBox();
  53. vis->setProperty("class", "checkbox-icon indicator-visibility");
  54. vis->setChecked(sourceVisible);
  55. vis->setAccessibleName(QTStr("Basic.Main.Sources.Visibility"));
  56. vis->setAccessibleDescription(QTStr("Basic.Main.Sources.VisibilityDescription").arg(name));
  57. lock = new QCheckBox();
  58. lock->setProperty("class", "checkbox-icon indicator-lock");
  59. lock->setChecked(obs_sceneitem_locked(sceneitem));
  60. lock->setAccessibleName(QTStr("Basic.Main.Sources.Lock"));
  61. lock->setAccessibleDescription(QTStr("Basic.Main.Sources.LockDescription").arg(name));
  62. label = new OBSSourceLabel(source);
  63. label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
  64. label->setAttribute(Qt::WA_TranslucentBackground);
  65. label->setEnabled(sourceVisible);
  66. #ifdef __APPLE__
  67. vis->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  68. lock->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  69. #endif
  70. boxLayout = new QHBoxLayout();
  71. boxLayout->setContentsMargins(0, 0, 0, 0);
  72. boxLayout->setSpacing(0);
  73. if (iconLabel) {
  74. boxLayout->addWidget(iconLabel);
  75. boxLayout->addSpacing(2);
  76. }
  77. boxLayout->addWidget(label);
  78. boxLayout->addWidget(vis);
  79. boxLayout->addWidget(lock);
  80. Update(false);
  81. setLayout(boxLayout);
  82. /* --------------------------------------------------------- */
  83. auto setItemVisible = [this](bool val) {
  84. obs_scene_t *scene = obs_sceneitem_get_scene(sceneitem);
  85. obs_source_t *scenesource = obs_scene_get_source(scene);
  86. int64_t id = obs_sceneitem_get_id(sceneitem);
  87. const char *name = obs_source_get_name(scenesource);
  88. const char *uuid = obs_source_get_uuid(scenesource);
  89. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  90. auto undo_redo = [](const std::string &uuid, int64_t id, bool val) {
  91. OBSSourceAutoRelease s = obs_get_source_by_uuid(uuid.c_str());
  92. obs_scene_t *sc = obs_group_or_scene_from_source(s);
  93. obs_sceneitem_t *si = obs_scene_find_sceneitem_by_id(sc, id);
  94. if (si)
  95. obs_sceneitem_set_visible(si, val);
  96. };
  97. QString str = QTStr(val ? "Undo.ShowSceneItem" : "Undo.HideSceneItem");
  98. OBSBasic *main = OBSBasic::Get();
  99. main->undo_s.add_action(str.arg(obs_source_get_name(source), name),
  100. std::bind(undo_redo, std::placeholders::_1, id, !val),
  101. std::bind(undo_redo, std::placeholders::_1, id, val), uuid, uuid);
  102. QSignalBlocker sourcesSignalBlocker(this);
  103. obs_sceneitem_set_visible(sceneitem, val);
  104. };
  105. auto setItemLocked = [this](bool checked) {
  106. QSignalBlocker sourcesSignalBlocker(this);
  107. obs_sceneitem_set_locked(sceneitem, checked);
  108. };
  109. connect(vis, &QAbstractButton::clicked, setItemVisible);
  110. connect(lock, &QAbstractButton::clicked, setItemLocked);
  111. }
  112. void SourceTreeItem::paintEvent(QPaintEvent *event)
  113. {
  114. QStyleOption opt;
  115. opt.initFrom(this);
  116. QPainter p(this);
  117. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  118. QWidget::paintEvent(event);
  119. }
  120. void SourceTreeItem::DisconnectSignals()
  121. {
  122. sigs.clear();
  123. }
  124. void SourceTreeItem::Clear()
  125. {
  126. DisconnectSignals();
  127. sceneitem = nullptr;
  128. }
  129. void SourceTreeItem::ReconnectSignals()
  130. {
  131. if (!sceneitem)
  132. return;
  133. DisconnectSignals();
  134. /* --------------------------------------------------------- */
  135. auto removeItem = [](void *data, calldata_t *cd) {
  136. SourceTreeItem *this_ = static_cast<SourceTreeItem *>(data);
  137. obs_sceneitem_t *curItem = (obs_sceneitem_t *)calldata_ptr(cd, "item");
  138. obs_scene_t *curScene = (obs_scene_t *)calldata_ptr(cd, "scene");
  139. if (curItem == this_->sceneitem) {
  140. QMetaObject::invokeMethod(this_->tree, "Remove", Q_ARG(OBSSceneItem, curItem),
  141. Q_ARG(OBSScene, curScene));
  142. curItem = nullptr;
  143. }
  144. if (!curItem)
  145. QMetaObject::invokeMethod(this_, "Clear");
  146. };
  147. auto itemVisible = [](void *data, calldata_t *cd) {
  148. SourceTreeItem *this_ = static_cast<SourceTreeItem *>(data);
  149. obs_sceneitem_t *curItem = (obs_sceneitem_t *)calldata_ptr(cd, "item");
  150. bool visible = calldata_bool(cd, "visible");
  151. if (curItem == this_->sceneitem)
  152. QMetaObject::invokeMethod(this_, "VisibilityChanged", Q_ARG(bool, visible));
  153. };
  154. auto itemLocked = [](void *data, calldata_t *cd) {
  155. SourceTreeItem *this_ = static_cast<SourceTreeItem *>(data);
  156. obs_sceneitem_t *curItem = (obs_sceneitem_t *)calldata_ptr(cd, "item");
  157. bool locked = calldata_bool(cd, "locked");
  158. if (curItem == this_->sceneitem)
  159. QMetaObject::invokeMethod(this_, "LockedChanged", Q_ARG(bool, locked));
  160. };
  161. auto itemSelect = [](void *data, calldata_t *cd) {
  162. SourceTreeItem *this_ = static_cast<SourceTreeItem *>(data);
  163. obs_sceneitem_t *curItem = (obs_sceneitem_t *)calldata_ptr(cd, "item");
  164. if (curItem == this_->sceneitem)
  165. QMetaObject::invokeMethod(this_, "Select");
  166. };
  167. auto itemDeselect = [](void *data, calldata_t *cd) {
  168. SourceTreeItem *this_ = static_cast<SourceTreeItem *>(data);
  169. obs_sceneitem_t *curItem = (obs_sceneitem_t *)calldata_ptr(cd, "item");
  170. if (curItem == this_->sceneitem)
  171. QMetaObject::invokeMethod(this_, "Deselect");
  172. };
  173. auto reorderGroup = [](void *data, calldata_t *) {
  174. SourceTreeItem *this_ = static_cast<SourceTreeItem *>(data);
  175. QMetaObject::invokeMethod(this_->tree, "ReorderItems");
  176. };
  177. obs_scene_t *scene = obs_sceneitem_get_scene(sceneitem);
  178. obs_source_t *sceneSource = obs_scene_get_source(scene);
  179. signal_handler_t *signal = obs_source_get_signal_handler(sceneSource);
  180. sigs.emplace_back(signal, "remove", removeItem, this);
  181. sigs.emplace_back(signal, "item_remove", removeItem, this);
  182. sigs.emplace_back(signal, "item_visible", itemVisible, this);
  183. sigs.emplace_back(signal, "item_locked", itemLocked, this);
  184. sigs.emplace_back(signal, "item_select", itemSelect, this);
  185. sigs.emplace_back(signal, "item_deselect", itemDeselect, this);
  186. if (obs_sceneitem_is_group(sceneitem)) {
  187. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  188. signal = obs_source_get_signal_handler(source);
  189. sigs.emplace_back(signal, "reorder", reorderGroup, this);
  190. }
  191. /* --------------------------------------------------------- */
  192. auto removeSource = [](void *data, calldata_t *) {
  193. SourceTreeItem *this_ = static_cast<SourceTreeItem *>(data);
  194. this_->DisconnectSignals();
  195. this_->sceneitem = nullptr;
  196. QMetaObject::invokeMethod(this_->tree, "RefreshItems");
  197. };
  198. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  199. signal = obs_source_get_signal_handler(source);
  200. sigs.emplace_back(signal, "remove", removeSource, this);
  201. }
  202. void SourceTreeItem::mouseDoubleClickEvent(QMouseEvent *event)
  203. {
  204. QWidget::mouseDoubleClickEvent(event);
  205. if (expand) {
  206. expand->setChecked(!expand->isChecked());
  207. } else {
  208. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  209. OBSBasic *main = OBSBasic::Get();
  210. if (obs_source_configurable(source)) {
  211. main->CreatePropertiesWindow(source);
  212. }
  213. }
  214. }
  215. void SourceTreeItem::enterEvent(QEnterEvent *event)
  216. {
  217. QWidget::enterEvent(event);
  218. OBSBasicPreview *preview = OBSBasicPreview::Get();
  219. std::lock_guard<std::mutex> lock(preview->selectMutex);
  220. preview->hoveredPreviewItems.clear();
  221. preview->hoveredPreviewItems.push_back(sceneitem);
  222. }
  223. void SourceTreeItem::leaveEvent(QEvent *event)
  224. {
  225. QWidget::leaveEvent(event);
  226. OBSBasicPreview *preview = OBSBasicPreview::Get();
  227. std::lock_guard<std::mutex> lock(preview->selectMutex);
  228. preview->hoveredPreviewItems.clear();
  229. }
  230. bool SourceTreeItem::IsEditing()
  231. {
  232. return editor != nullptr;
  233. }
  234. void SourceTreeItem::EnterEditMode()
  235. {
  236. setFocusPolicy(Qt::StrongFocus);
  237. int index = boxLayout->indexOf(label);
  238. boxLayout->removeWidget(label);
  239. editor = new QLineEdit(label->text());
  240. editor->setStyleSheet("background: none");
  241. editor->selectAll();
  242. editor->installEventFilter(this);
  243. boxLayout->insertWidget(index, editor);
  244. setFocusProxy(editor);
  245. }
  246. void SourceTreeItem::ExitEditMode(bool save)
  247. {
  248. ExitEditModeInternal(save);
  249. if (tree->undoSceneData) {
  250. OBSBasic *main = OBSBasic::Get();
  251. main->undo_s.pop_disabled();
  252. OBSData redoSceneData = main->BackupScene(GetCurrentScene());
  253. QString text = QTStr("Undo.GroupItems").arg(newName.c_str());
  254. main->CreateSceneUndoRedoAction(text, tree->undoSceneData, redoSceneData);
  255. tree->undoSceneData = nullptr;
  256. }
  257. }
  258. void SourceTreeItem::ExitEditModeInternal(bool save)
  259. {
  260. if (!editor) {
  261. return;
  262. }
  263. OBSBasic *main = OBSBasic::Get();
  264. OBSScene scene = main->GetCurrentScene();
  265. newName = QT_TO_UTF8(editor->text());
  266. setFocusProxy(nullptr);
  267. int index = boxLayout->indexOf(editor);
  268. boxLayout->removeWidget(editor);
  269. delete editor;
  270. editor = nullptr;
  271. setFocusPolicy(Qt::NoFocus);
  272. boxLayout->insertWidget(index, label);
  273. setFocus();
  274. /* ----------------------------------------- */
  275. /* check for empty string */
  276. if (!save)
  277. return;
  278. if (newName.empty()) {
  279. OBSMessageBox::information(main, QTStr("NoNameEntered.Title"), QTStr("NoNameEntered.Text"));
  280. return;
  281. }
  282. /* ----------------------------------------- */
  283. /* Check for same name */
  284. obs_source_t *source = obs_sceneitem_get_source(sceneitem);
  285. if (newName == obs_source_get_name(source))
  286. return;
  287. /* ----------------------------------------- */
  288. /* check for existing source */
  289. OBSSourceAutoRelease existingSource = obs_get_source_by_name(newName.c_str());
  290. bool exists = !!existingSource;
  291. if (exists) {
  292. OBSMessageBox::information(main, QTStr("NameExists.Title"), QTStr("NameExists.Text"));
  293. return;
  294. }
  295. /* ----------------------------------------- */
  296. /* rename */
  297. QSignalBlocker sourcesSignalBlocker(this);
  298. std::string prevName(obs_source_get_name(source));
  299. std::string scene_uuid = obs_source_get_uuid(main->GetCurrentSceneSource());
  300. auto undo = [scene_uuid, prevName, main](const std::string &data) {
  301. OBSSourceAutoRelease source = obs_get_source_by_uuid(data.c_str());
  302. obs_source_set_name(source, prevName.c_str());
  303. OBSSourceAutoRelease scene_source = obs_get_source_by_uuid(scene_uuid.c_str());
  304. main->SetCurrentScene(scene_source.Get(), true);
  305. };
  306. std::string editedName = newName;
  307. auto redo = [scene_uuid, main, editedName](const std::string &data) {
  308. OBSSourceAutoRelease source = obs_get_source_by_uuid(data.c_str());
  309. obs_source_set_name(source, editedName.c_str());
  310. OBSSourceAutoRelease scene_source = obs_get_source_by_uuid(scene_uuid.c_str());
  311. main->SetCurrentScene(scene_source.Get(), true);
  312. };
  313. const char *uuid = obs_source_get_uuid(source);
  314. main->undo_s.add_action(QTStr("Undo.Rename").arg(newName.c_str()), undo, redo, uuid, uuid);
  315. obs_source_set_name(source, newName.c_str());
  316. }
  317. bool SourceTreeItem::eventFilter(QObject *object, QEvent *event)
  318. {
  319. if (editor != object)
  320. return false;
  321. if (LineEditCanceled(event)) {
  322. QMetaObject::invokeMethod(this, "ExitEditMode", Qt::QueuedConnection, Q_ARG(bool, false));
  323. return true;
  324. }
  325. if (LineEditChanged(event)) {
  326. QMetaObject::invokeMethod(this, "ExitEditMode", Qt::QueuedConnection, Q_ARG(bool, true));
  327. return true;
  328. }
  329. return false;
  330. }
  331. void SourceTreeItem::VisibilityChanged(bool visible)
  332. {
  333. if (iconLabel) {
  334. iconLabel->setEnabled(visible);
  335. }
  336. label->setEnabled(visible);
  337. vis->setChecked(visible);
  338. }
  339. void SourceTreeItem::LockedChanged(bool locked)
  340. {
  341. lock->setChecked(locked);
  342. OBSBasic::Get()->UpdateEditMenu();
  343. }
  344. void SourceTreeItem::Update(bool force)
  345. {
  346. OBSScene scene = GetCurrentScene();
  347. obs_scene_t *itemScene = obs_sceneitem_get_scene(sceneitem);
  348. Type newType;
  349. /* ------------------------------------------------- */
  350. /* if it's a group item, insert group checkbox */
  351. if (obs_sceneitem_is_group(sceneitem)) {
  352. newType = Type::Group;
  353. /* ------------------------------------------------- */
  354. /* if it's a group sub-item */
  355. } else if (itemScene != scene) {
  356. newType = Type::SubItem;
  357. /* ------------------------------------------------- */
  358. /* if it's a regular item */
  359. } else {
  360. newType = Type::Item;
  361. }
  362. /* ------------------------------------------------- */
  363. if (!force && newType == type) {
  364. return;
  365. }
  366. /* ------------------------------------------------- */
  367. ReconnectSignals();
  368. if (spacer) {
  369. boxLayout->removeItem(spacer);
  370. delete spacer;
  371. spacer = nullptr;
  372. }
  373. if (type == Type::Group) {
  374. boxLayout->removeWidget(expand);
  375. expand->deleteLater();
  376. expand = nullptr;
  377. }
  378. type = newType;
  379. if (type == Type::SubItem) {
  380. spacer = new QSpacerItem(16, 1);
  381. boxLayout->insertItem(0, spacer);
  382. } else if (type == Type::Group) {
  383. expand = new QCheckBox();
  384. expand->setProperty("class", "checkbox-icon indicator-expand");
  385. #ifdef __APPLE__
  386. expand->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  387. #endif
  388. boxLayout->insertWidget(0, expand);
  389. OBSDataAutoRelease data = obs_sceneitem_get_private_settings(sceneitem);
  390. expand->blockSignals(true);
  391. expand->setChecked(obs_data_get_bool(data, "collapsed"));
  392. expand->blockSignals(false);
  393. connect(expand, &QPushButton::toggled, this, &SourceTreeItem::ExpandClicked);
  394. } else {
  395. spacer = new QSpacerItem(3, 1);
  396. boxLayout->insertItem(0, spacer);
  397. }
  398. }
  399. void SourceTreeItem::ExpandClicked(bool checked)
  400. {
  401. OBSDataAutoRelease data = obs_sceneitem_get_private_settings(sceneitem);
  402. obs_data_set_bool(data, "collapsed", checked);
  403. if (!checked)
  404. tree->GetStm()->ExpandGroup(sceneitem);
  405. else
  406. tree->GetStm()->CollapseGroup(sceneitem);
  407. }
  408. void SourceTreeItem::Select()
  409. {
  410. tree->SelectItem(sceneitem, true);
  411. OBSBasic::Get()->UpdateContextBarDeferred();
  412. OBSBasic::Get()->UpdateEditMenu();
  413. }
  414. void SourceTreeItem::Deselect()
  415. {
  416. tree->SelectItem(sceneitem, false);
  417. OBSBasic::Get()->UpdateContextBarDeferred();
  418. OBSBasic::Get()->UpdateEditMenu();
  419. }