source-tree.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. QLabel *iconLabel = nullptr;
  53. VisibilityCheckBox *vis = nullptr;
  54. LockedCheckBox *lock = nullptr;
  55. QHBoxLayout *boxLayout = nullptr;
  56. QLabel *label = nullptr;
  57. QLineEdit *editor = nullptr;
  58. std::string newName;
  59. SourceTree *tree;
  60. OBSSceneItem sceneitem;
  61. OBSSignal sceneRemoveSignal;
  62. OBSSignal itemRemoveSignal;
  63. OBSSignal groupReorderSignal;
  64. OBSSignal selectSignal;
  65. OBSSignal deselectSignal;
  66. OBSSignal visibleSignal;
  67. OBSSignal lockedSignal;
  68. OBSSignal renameSignal;
  69. OBSSignal removeSignal;
  70. virtual void paintEvent(QPaintEvent *event) override;
  71. void ExitEditModeInternal(bool save);
  72. private slots:
  73. void Clear();
  74. void EnterEditMode();
  75. void ExitEditMode(bool save);
  76. void VisibilityChanged(bool visible);
  77. void LockedChanged(bool locked);
  78. void Renamed(const QString &name);
  79. void ExpandClicked(bool checked);
  80. void Select();
  81. void Deselect();
  82. };
  83. class SourceTreeModel : public QAbstractListModel {
  84. Q_OBJECT
  85. friend class SourceTree;
  86. friend class SourceTreeItem;
  87. SourceTree *st;
  88. QVector<OBSSceneItem> items;
  89. bool hasGroups = false;
  90. static void OBSFrontendEvent(enum obs_frontend_event event, void *ptr);
  91. void Clear();
  92. void SceneChanged();
  93. void ReorderItems();
  94. void Add(obs_sceneitem_t *item);
  95. void Remove(obs_sceneitem_t *item);
  96. OBSSceneItem Get(int idx);
  97. QString GetNewGroupName();
  98. void AddGroup();
  99. void GroupSelectedItems(QModelIndexList &indices);
  100. void UngroupSelectedGroups(QModelIndexList &indices);
  101. void ExpandGroup(obs_sceneitem_t *item);
  102. void CollapseGroup(obs_sceneitem_t *item);
  103. void UpdateGroupState(bool update);
  104. public:
  105. explicit SourceTreeModel(SourceTree *st);
  106. ~SourceTreeModel();
  107. virtual int rowCount(const QModelIndex &parent) const override;
  108. virtual QVariant data(const QModelIndex &index,
  109. int role) const override;
  110. virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
  111. virtual Qt::DropActions supportedDropActions() const override;
  112. };
  113. class SourceTree : public QListView {
  114. Q_OBJECT
  115. bool ignoreReorder = false;
  116. friend class SourceTreeModel;
  117. friend class SourceTreeItem;
  118. bool textPrepared = false;
  119. QStaticText textNoSources;
  120. QSvgRenderer iconNoSources;
  121. OBSData undoSceneData;
  122. bool iconsVisible = true;
  123. void UpdateNoSourcesMessage();
  124. void ResetWidgets();
  125. void UpdateWidget(const QModelIndex &idx, obs_sceneitem_t *item);
  126. void UpdateWidgets(bool force = false);
  127. inline SourceTreeModel *GetStm() const
  128. {
  129. return reinterpret_cast<SourceTreeModel *>(model());
  130. }
  131. public:
  132. inline SourceTreeItem *GetItemWidget(int idx)
  133. {
  134. QWidget *widget = indexWidget(GetStm()->createIndex(idx, 0));
  135. return reinterpret_cast<SourceTreeItem *>(widget);
  136. }
  137. explicit SourceTree(QWidget *parent = nullptr);
  138. inline bool IgnoreReorder() const { return ignoreReorder; }
  139. inline void Clear() { GetStm()->Clear(); }
  140. inline void Add(obs_sceneitem_t *item) { GetStm()->Add(item); }
  141. inline OBSSceneItem Get(int idx) { return GetStm()->Get(idx); }
  142. inline QString GetNewGroupName() { return GetStm()->GetNewGroupName(); }
  143. void SelectItem(obs_sceneitem_t *sceneitem, bool select);
  144. bool MultipleBaseSelected() const;
  145. bool GroupsSelected() const;
  146. bool GroupedItemsSelected() const;
  147. void UpdateIcons();
  148. void SetIconsVisible(bool visible);
  149. public slots:
  150. inline void ReorderItems() { GetStm()->ReorderItems(); }
  151. inline void RefreshItems() { GetStm()->SceneChanged(); }
  152. void Remove(OBSSceneItem item);
  153. void GroupSelectedItems();
  154. void UngroupSelectedGroups();
  155. void AddGroup();
  156. bool Edit(int idx);
  157. void NewGroupEdit(int idx);
  158. protected:
  159. virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
  160. virtual void dropEvent(QDropEvent *event) override;
  161. virtual void mouseMoveEvent(QMouseEvent *event) override;
  162. virtual void leaveEvent(QEvent *event) override;
  163. virtual void paintEvent(QPaintEvent *event) override;
  164. virtual void
  165. selectionChanged(const QItemSelection &selected,
  166. const QItemSelection &deselected) override;
  167. };