source-tree.hpp 4.8 KB

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