SlotLayoutComponent.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include <QGraphicsLayout>
  9. #include <AzCore/Math/Vector2.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <Components/Slots/SlotLayoutComponent.h>
  12. namespace GraphCanvas
  13. {
  14. ////////////////////////
  15. // SlotLayoutComponent
  16. ////////////////////////
  17. void SlotLayoutComponent::Reflect(AZ::ReflectContext* reflectContext)
  18. {
  19. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  20. if (serializeContext)
  21. {
  22. serializeContext->Class<SlotLayoutComponent, AZ::Component>()
  23. ->Version(1)
  24. ;
  25. }
  26. }
  27. SlotLayoutComponent::SlotLayoutComponent()
  28. : m_layoutWidget(nullptr)
  29. , m_layout(nullptr)
  30. {
  31. }
  32. SlotLayoutComponent::~SlotLayoutComponent()
  33. {
  34. }
  35. void SlotLayoutComponent::Init()
  36. {
  37. m_layoutWidget = new QGraphicsWidget();
  38. m_layoutWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
  39. m_layoutWidget->setFlag(QGraphicsItem::ItemIsFocusable, true);
  40. m_layoutWidget->setVisible(m_isVisible);
  41. }
  42. void SlotLayoutComponent::Activate()
  43. {
  44. VisualRequestBus::Handler::BusConnect(GetEntityId());
  45. }
  46. void SlotLayoutComponent::Deactivate()
  47. {
  48. VisualRequestBus::Handler::BusDisconnect();
  49. }
  50. QGraphicsItem* SlotLayoutComponent::AsGraphicsItem()
  51. {
  52. return m_layoutWidget;
  53. }
  54. QGraphicsLayoutItem* SlotLayoutComponent::AsGraphicsLayoutItem()
  55. {
  56. return m_layoutWidget;
  57. }
  58. bool SlotLayoutComponent::Contains(const AZ::Vector2& position) const
  59. {
  60. QPointF localRectPos = m_layoutWidget->mapFromScene(QPointF(position.GetX(), position.GetY()));
  61. return m_layoutWidget->contains(localRectPos);
  62. }
  63. void SlotLayoutComponent::SetVisible(bool visible)
  64. {
  65. if (m_isVisible != visible)
  66. {
  67. m_isVisible = visible;
  68. if (m_layoutWidget)
  69. {
  70. m_layoutWidget->setVisible(visible);
  71. }
  72. }
  73. }
  74. bool SlotLayoutComponent::IsVisible() const
  75. {
  76. return m_isVisible;
  77. }
  78. void SlotLayoutComponent::SetLayout(QGraphicsLayout* layout)
  79. {
  80. AZ_Error("SlotLayoutComponent", m_layout == nullptr, "Trying to register two layouts to the same layout component");
  81. if (m_layout == nullptr)
  82. {
  83. m_layoutWidget->setLayout(layout);
  84. m_layout = layout;
  85. }
  86. }
  87. QGraphicsLayout* SlotLayoutComponent::GetLayout()
  88. {
  89. return m_layout;
  90. }
  91. }