DataSlotLayoutComponent.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <QGraphicsLinearLayout>
  10. #include <QTimer>
  11. #include <AzToolsFramework/UI/Notifications/ToastBus.h>
  12. #include <Components/Slots/SlotLayoutComponent.h>
  13. #include <GraphCanvas/Components/SceneBus.h>
  14. #include <GraphCanvas/Components/Slots/SlotBus.h>
  15. #include <GraphCanvas/Components/Slots/Data/DataSlotBus.h>
  16. #include <GraphCanvas/Components/StyleBus.h>
  17. #include <GraphCanvas/Components/VisualBus.h>
  18. #include <GraphCanvas/GraphicsItems/GraphCanvasSceneEventFilter.h>
  19. #include <GraphCanvas/Styling/StyleHelper.h>
  20. #include <Widgets/GraphCanvasLabel.h>
  21. #include <Widgets/NodePropertyDisplayWidget.h>
  22. namespace GraphCanvas
  23. {
  24. class DataSlotLayoutComponent;
  25. class DataSlotConnectionPin;
  26. class GraphCanvasLabel;
  27. class DataSlotLayout
  28. : public QGraphicsLinearLayout
  29. , public SlotNotificationBus::Handler
  30. , public DataSlotLayoutRequestBus::Handler
  31. , public DataSlotNotificationBus::Handler
  32. , public NodeDataSlotRequestBus::Handler
  33. , public SceneMemberNotificationBus::Handler
  34. , public StyleNotificationBus::Handler
  35. , public VisualNotificationBus::Handler
  36. , public AZ::SystemTickBus::Handler
  37. {
  38. private:
  39. class DataTypeConversionDataSlotDragDropInterface
  40. : public DataSlotDragDropInterface
  41. {
  42. public:
  43. AZ_CLASS_ALLOCATOR(DataTypeConversionDataSlotDragDropInterface, AZ::SystemAllocator);
  44. DataTypeConversionDataSlotDragDropInterface(const SlotId& slotId);
  45. AZ::Outcome<DragDropState> OnDragEnterEvent(QGraphicsSceneDragDropEvent* dragDropEvent) override;
  46. void OnDragLeaveEvent(QGraphicsSceneDragDropEvent* dragDropEvent) override;
  47. void OnDropEvent(QGraphicsSceneDragDropEvent* dropEvent) override;
  48. void OnDropCancelled() override;
  49. private:
  50. SlotId m_slotId;
  51. ViewId m_viewId;
  52. AzToolsFramework::ToastId m_toastId;
  53. };
  54. class DoubleClickSceneEventFilter
  55. : public SceneEventFilter
  56. {
  57. public:
  58. AZ_CLASS_ALLOCATOR(DoubleClickSceneEventFilter, AZ::SystemAllocator);
  59. DoubleClickSceneEventFilter(DataSlotLayout& dataSlotLayout)
  60. : SceneEventFilter(nullptr)
  61. , m_owner(dataSlotLayout)
  62. {
  63. }
  64. bool sceneEventFilter(QGraphicsItem*, QEvent* sceneEvent) override
  65. {
  66. switch (sceneEvent->type())
  67. {
  68. case QEvent::GraphicsSceneMousePress:
  69. {
  70. return true;
  71. }
  72. case QEvent::GraphicsSceneMouseDoubleClick:
  73. {
  74. m_owner.OnSlotTextDoubleClicked();
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. private:
  81. DataSlotLayout& m_owner;
  82. };
  83. friend class DoubleClickSceneEventFilter;
  84. public:
  85. AZ_CLASS_ALLOCATOR(DataSlotLayout, AZ::SystemAllocator);
  86. DataSlotLayout(DataSlotLayoutComponent& owner);
  87. ~DataSlotLayout();
  88. void Activate();
  89. void Deactivate();
  90. // SystemTickBus
  91. void OnSystemTick() override;
  92. ////
  93. // SceneMemberNotificationBus
  94. void OnSceneSet(const AZ::EntityId&) override;
  95. void OnSceneReady() override;
  96. ////
  97. // SlotNotificationBus
  98. void OnRegisteredToNode(const AZ::EntityId& nodeId) override;
  99. void OnNameChanged(const AZStd::string&) override;
  100. void OnTooltipChanged(const AZStd::string&) override;
  101. ////
  102. // StyleNotificationBus
  103. void OnStyleChanged() override;
  104. ////
  105. // DataSlotLayoutRequestBus
  106. const DataSlotConnectionPin* GetConnectionPin() const override;
  107. void UpdateDisplay() override;
  108. QRectF GetWidgetSceneBoundingRect() const override;
  109. ////
  110. // DataSlotNotificationBus
  111. void OnDataSlotTypeChanged(const DataSlotType& dataSlotType) override;
  112. void OnDisplayTypeChanged(const AZ::Uuid& dataType, const AZStd::vector<AZ::Uuid>& typeIds) override;
  113. ////
  114. // NodeDataSlotRequestBus
  115. void RecreatePropertyDisplay() override;
  116. ////
  117. void OnDragEnterEvent(QGraphicsSceneDragDropEvent* dragDropEvent);
  118. void OnDragLeaveEvent(QGraphicsSceneDragDropEvent* dragDropEvent);
  119. void OnDropEvent(QGraphicsSceneDragDropEvent* dragDropEvent);
  120. private:
  121. void SetTextDecoration(const AZStd::string& textDecoration, const AZStd::string& toolTip);
  122. void ClearTextDecoration();
  123. void ApplyTextStyle(GraphCanvasLabel* graphCanvasLabel);
  124. void UpdateFilterState();
  125. void RegisterDataSlotDragDropInterface(DataSlotDragDropInterface* dragDropInterface);
  126. void RemoveDataSlotDragDropInterface(DataSlotDragDropInterface* dragDropInterface);
  127. void SetDragDropState(DragDropState dragDropState);
  128. AZ::EntityId GetSceneId() const;
  129. void TryAndSetupSlot();
  130. void CreateDataDisplay();
  131. void UpdateLayout();
  132. void UpdateGeometry();
  133. void OnSlotTextDoubleClicked();
  134. AZStd::unordered_set< DataSlotDragDropInterface* > m_dragDropInterfaces;
  135. DataSlotDragDropInterface* m_activeHandler;
  136. QGraphicsItem* m_eventFilter;
  137. DragDropState m_dragDropState;
  138. // Internal DragDrop Interface
  139. DataSlotDragDropInterface* m_valueReferenceInterface;
  140. ConnectionType m_connectionType;
  141. Styling::StyleHelper m_style;
  142. DataSlotLayoutComponent& m_owner;
  143. QGraphicsWidget* m_spacer;
  144. NodePropertyDisplayWidget* m_nodePropertyDisplay;
  145. DataSlotConnectionPin* m_slotConnectionPin;
  146. GraphCanvasLabel* m_slotText;
  147. DoubleClickSceneEventFilter* m_doubleClickFilter;
  148. GraphCanvasLabel* m_textDecoration = nullptr;
  149. bool m_isNameHidden = false;
  150. // track the last seen values of some members to prevent UpdateLayout doing unnecessary work
  151. struct
  152. {
  153. ConnectionType connectionType = CT_Invalid;
  154. DataSlotConnectionPin* slotConnectionPin = nullptr;
  155. GraphCanvasLabel* slotText = nullptr;
  156. NodePropertyDisplayWidget* nodePropertyDisplay = nullptr;
  157. QGraphicsWidget* spacer = nullptr;
  158. GraphCanvasLabel* textDecoration = nullptr;
  159. } m_atLastUpdate;
  160. };
  161. //! Lays out the parts of the Data Slot
  162. class DataSlotLayoutComponent
  163. : public SlotLayoutComponent
  164. {
  165. public:
  166. AZ_COMPONENT(DataSlotLayoutComponent, "{0DA3CBDA-1C43-4A18-8E01-AEEAA3C81882}");
  167. static void Reflect(AZ::ReflectContext* context);
  168. DataSlotLayoutComponent();
  169. virtual ~DataSlotLayoutComponent() = default;
  170. void Init();
  171. void Activate();
  172. void Deactivate();
  173. private:
  174. DataSlotLayout* m_layout;
  175. };
  176. }