SlotLayoutItem.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <QGraphicsItem>
  10. #include <QGraphicsLayoutItem>
  11. #include <GraphCanvas/Components/VisualBus.h>
  12. #include <GraphCanvas/Styling/StyleHelper.h>
  13. namespace GraphCanvas
  14. {
  15. //! Generates Ebus notifications for QGraphicsItem event.
  16. //! Requires that the derived class has a GetEntityId() function
  17. class SlotLayoutItem
  18. : public QGraphicsItem
  19. , public QGraphicsLayoutItem
  20. {
  21. public:
  22. AZ_RTTI(SlotLayoutItem, "{ED76860E-35B8-4FEE-A2A0-04B467F778B6}", QGraphicsLayoutItem, QGraphicsItem);
  23. AZ_CLASS_ALLOCATOR(SlotLayoutItem, AZ::SystemAllocator);
  24. SlotLayoutItem()
  25. {
  26. setGraphicsItem(this);
  27. setAcceptHoverEvents(true);
  28. setOwnedByLayout(false);
  29. }
  30. virtual ~SlotLayoutItem() = default;
  31. const Styling::StyleHelper& GetStyle() const { return m_style; }
  32. protected:
  33. // QGraphicsItem
  34. void mousePressEvent(QGraphicsSceneMouseEvent* event) override
  35. {
  36. bool result = false;
  37. VisualNotificationBus::EventResult(result, GetEntityId(), &VisualNotifications::OnMousePress, GetEntityId(), event);
  38. if (!result)
  39. {
  40. QGraphicsItem::mousePressEvent(event);
  41. }
  42. }
  43. void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override
  44. {
  45. bool result = false;
  46. VisualNotificationBus::EventResult(result, GetEntityId(), &VisualNotifications::OnMouseRelease, GetEntityId(), event);
  47. if (!result)
  48. {
  49. QGraphicsItem::mouseReleaseEvent(event);
  50. }
  51. }
  52. QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value) override
  53. {
  54. if (GetEntityId().IsValid())
  55. {
  56. VisualNotificationBus::Event(GetEntityId(), &VisualNotifications::OnItemChange, GetEntityId(), change, value);
  57. }
  58. return QGraphicsItem::itemChange(change, value);
  59. }
  60. virtual void RefreshStyle() { };
  61. virtual AZ::EntityId GetEntityId() const = 0;
  62. Styling::StyleHelper m_style;
  63. };
  64. }