source-tree.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #pragma once
  2. #include <QList>
  3. #include <QVector>
  4. #include <QPointer>
  5. #include <QListView>
  6. #include <QCheckBox>
  7. #include <QStaticText>
  8. #include <QSvgRenderer>
  9. #include <QAbstractListModel>
  10. #include <obs.hpp>
  11. #include <obs-frontend-api.h>
  12. class QLabel;
  13. class QCheckBox;
  14. class QLineEdit;
  15. class SourceTree;
  16. class QSpacerItem;
  17. class QHBoxLayout;
  18. class LockedCheckBox;
  19. class VisibilityCheckBox;
  20. class VisibilityItemWidget;
  21. class SourceTreeSubItemCheckBox : public QCheckBox {
  22. Q_OBJECT
  23. };
  24. class SourceTreeItem : public QWidget {
  25. Q_OBJECT
  26. friend class SourceTree;
  27. friend class SourceTreeModel;
  28. void mouseDoubleClickEvent(QMouseEvent *event) override;
  29. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  30. void enterEvent(QEnterEvent *event) override;
  31. #else
  32. void enterEvent(QEvent *event) override;
  33. #endif
  34. void leaveEvent(QEvent *event) override;
  35. virtual bool eventFilter(QObject *object, QEvent *event) override;
  36. void Update(bool force);
  37. enum class Type {
  38. Unknown,
  39. Item,
  40. Group,
  41. SubItem,
  42. };
  43. void DisconnectSignals();
  44. void ReconnectSignals();
  45. Type type = Type::Unknown;
  46. public:
  47. explicit SourceTreeItem(SourceTree *tree, OBSSceneItem sceneitem);
  48. bool IsEditing();
  49. private:
  50. QSpacerItem *spacer = nullptr;
  51. QCheckBox *expand = nullptr;
  52. VisibilityCheckBox *vis = nullptr;
  53. LockedCheckBox *lock = nullptr;
  54. QHBoxLayout *boxLayout = nullptr;
  55. QLabel *label = nullptr;
  56. QLineEdit *editor = nullptr;
  57. std::string newName;
  58. SourceTree *tree;
  59. OBSSceneItem sceneitem;
  60. OBSSignal sceneRemoveSignal;
  61. OBSSignal itemRemoveSignal;
  62. OBSSignal groupReorderSignal;
  63. OBSSignal selectSignal;
  64. OBSSignal deselectSignal;
  65. OBSSignal visibleSignal;
  66. OBSSignal lockedSignal;
  67. OBSSignal renameSignal;
  68. OBSSignal removeSignal;
  69. virtual void paintEvent(QPaintEvent *event) override;
  70. void ExitEditModeInternal(bool save);
  71. private slots:
  72. void Clear();
  73. void EnterEditMode();
  74. void ExitEditMode(bool save);
  75. void VisibilityChanged(bool visible);
  76. void LockedChanged(bool locked);
  77. void Renamed(const QString &name);
  78. void ExpandClicked(bool checked);
  79. void Select();
  80. void Deselect();
  81. };
  82. class SourceTreeModel : public QAbstractListModel {
  83. Q_OBJECT
  84. friend class SourceTree;
  85. friend class SourceTreeItem;
  86. SourceTree *st;
  87. QVector<OBSSceneItem> items;
  88. bool hasGroups = false;
  89. static void OBSFrontendEvent(enum obs_frontend_event event, void *ptr);
  90. void Clear();
  91. void SceneChanged();
  92. void ReorderItems();
  93. void Add(obs_sceneitem_t *item);
  94. void Remove(obs_sceneitem_t *item);
  95. OBSSceneItem Get(int idx);
  96. QString GetNewGroupName();
  97. void AddGroup();
  98. void GroupSelectedItems(QModelIndexList &indices);
  99. void UngroupSelectedGroups(QModelIndexList &indices);
  100. void ExpandGroup(obs_sceneitem_t *item);
  101. void CollapseGroup(obs_sceneitem_t *item);
  102. void UpdateGroupState(bool update);
  103. public:
  104. explicit SourceTreeModel(SourceTree *st);
  105. ~SourceTreeModel();
  106. virtual int rowCount(const QModelIndex &parent) const override;
  107. virtual QVariant data(const QModelIndex &index,
  108. int role) const override;
  109. virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
  110. virtual Qt::DropActions supportedDropActions() const override;
  111. };
  112. class SourceTree : public QListView {
  113. Q_OBJECT
  114. bool ignoreReorder = false;
  115. friend class SourceTreeModel;
  116. friend class SourceTreeItem;
  117. bool textPrepared = false;
  118. QStaticText textNoSources;
  119. QSvgRenderer iconNoSources;
  120. OBSData undoSceneData;
  121. bool iconsVisible = true;
  122. void UpdateNoSourcesMessage();
  123. void ResetWidgets();
  124. void UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item);
  125. void UpdateWidgets(bool force = false);
  126. inline SourceTreeModel *GetStm() const
  127. {
  128. return reinterpret_cast<SourceTreeModel *>(model());
  129. }
  130. public:
  131. inline SourceTreeItem *GetItemWidget(int idx)
  132. {
  133. QWidget *widget = indexWidget(GetStm()->createIndex(idx, 0));
  134. return reinterpret_cast<SourceTreeItem *>(widget);
  135. }
  136. explicit SourceTree(QWidget *parent = nullptr);
  137. inline bool IgnoreReorder() const { return ignoreReorder; }
  138. inline void Clear() { GetStm()->Clear(); }
  139. inline void Add(obs_sceneitem_t *item) { GetStm()->Add(item); }
  140. inline OBSSceneItem Get(int idx) { return GetStm()->Get(idx); }
  141. inline QString GetNewGroupName() { return GetStm()->GetNewGroupName(); }
  142. void SelectItem(obs_sceneitem_t *sceneitem, bool select);
  143. bool MultipleBaseSelected() const;
  144. bool GroupsSelected() const;
  145. bool GroupedItemsSelected() const;
  146. void UpdateIcons();
  147. void SetIconsVisible(bool visible);
  148. public slots:
  149. inline void ReorderItems() { GetStm()->ReorderItems(); }
  150. inline void RefreshItems() { GetStm()->SceneChanged(); }
  151. void Remove(OBSSceneItem item);
  152. void GroupSelectedItems();
  153. void UngroupSelectedGroups();
  154. void AddGroup();
  155. bool Edit(int idx);
  156. void NewGroupEdit(int idx);
  157. protected:
  158. virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
  159. virtual void dropEvent(QDropEvent *event) override;
  160. virtual void mouseMoveEvent(QMouseEvent *event) override;
  161. virtual void leaveEvent(QEvent *event) override;
  162. virtual void paintEvent(QPaintEvent *event) override;
  163. virtual void
  164. selectionChanged(const QItemSelection &selected,
  165. const QItemSelection &deselected) override;
  166. };