GeneralNodeLayoutComponent.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <functional>
  9. #include <AzCore/RTTI/TypeInfo.h>
  10. #include <QGraphicsLayoutItem>
  11. #include <QGraphicsGridLayout>
  12. #include <Components/Nodes/General/GeneralNodeLayoutComponent.h>
  13. #include <Components/Nodes/NodeComponent.h>
  14. #include <Components/Nodes/NodeLayerControllerComponent.h>
  15. #include <Components/Nodes/General/GeneralNodeFrameComponent.h>
  16. #include <Components/Nodes/General/GeneralSlotLayoutComponent.h>
  17. #include <Components/Nodes/General/GeneralNodeTitleComponent.h>
  18. #include <Components/StylingComponent.h>
  19. #include <GraphCanvas/Components/GeometryBus.h>
  20. #include <GraphCanvas/Components/Slots/SlotBus.h>
  21. #include <GraphCanvas/tools.h>
  22. #include <GraphCanvas/Styling/StyleHelper.h>
  23. namespace GraphCanvas
  24. {
  25. ///////////////////////////////
  26. // GeneralNodeLayoutComponent
  27. ///////////////////////////////
  28. void GeneralNodeLayoutComponent::Reflect(AZ::ReflectContext* context)
  29. {
  30. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  31. if (serializeContext)
  32. {
  33. serializeContext->Class<GeneralNodeLayoutComponent, NodeLayoutComponent>()
  34. ->Version(1)
  35. ;
  36. }
  37. }
  38. AZ::Entity* GeneralNodeLayoutComponent::CreateGeneralNodeEntity(const char* nodeType, const NodeConfiguration& configuration)
  39. {
  40. // Create this Node's entity.
  41. AZ::Entity* entity = NodeComponent::CreateCoreNodeEntity(configuration);
  42. entity->CreateComponent<GeneralNodeFrameComponent>();
  43. entity->CreateComponent<StylingComponent>(Styling::Elements::Node, AZ::EntityId(), nodeType);
  44. entity->CreateComponent<GeneralNodeLayoutComponent>();
  45. entity->CreateComponent<GeneralNodeTitleComponent>();
  46. entity->CreateComponent<GeneralSlotLayoutComponent>();
  47. entity->CreateComponent<NodeLayerControllerComponent>();
  48. return entity;
  49. }
  50. GeneralNodeLayoutComponent::GeneralNodeLayoutComponent()
  51. : m_title(nullptr)
  52. , m_slots(nullptr)
  53. {
  54. }
  55. GeneralNodeLayoutComponent::~GeneralNodeLayoutComponent()
  56. {
  57. }
  58. void GeneralNodeLayoutComponent::Init()
  59. {
  60. NodeLayoutComponent::Init();
  61. m_slots = new QGraphicsLinearLayout(Qt::Vertical);
  62. m_slots->setInstantInvalidatePropagation(true);
  63. m_title = new QGraphicsLinearLayout(Qt::Horizontal);
  64. m_title->setInstantInvalidatePropagation(true);
  65. m_layout = new QGraphicsLinearLayout(Qt::Vertical);
  66. m_layout->setInstantInvalidatePropagation(true);
  67. }
  68. void GeneralNodeLayoutComponent::Activate()
  69. {
  70. NodeLayoutComponent::Activate();
  71. NodeNotificationBus::Handler::BusConnect(GetEntityId());
  72. }
  73. void GeneralNodeLayoutComponent::Deactivate()
  74. {
  75. NodeLayoutComponent::Deactivate();
  76. StyleNotificationBus::Handler::BusDisconnect();
  77. NodeNotificationBus::Handler::BusDisconnect();
  78. }
  79. void GeneralNodeLayoutComponent::OnStyleChanged()
  80. {
  81. Styling::StyleHelper style(GetEntityId());
  82. Qt::Orientation layoutOrientation = style.GetAttribute(Styling::Attribute::LayoutOrientation, Qt::Vertical);
  83. if (layoutOrientation == Qt::Horizontal)
  84. {
  85. UpdateHorizontalLayout();
  86. }
  87. UpdateLayoutParameters();
  88. }
  89. void GeneralNodeLayoutComponent::OnNodeActivated()
  90. {
  91. QGraphicsWidget* titleGraphicsItem = nullptr;
  92. NodeTitleRequestBus::EventResult(titleGraphicsItem, GetEntityId(), &NodeTitleRequests::GetGraphicsWidget);
  93. if (titleGraphicsItem)
  94. {
  95. m_title->addItem(titleGraphicsItem);
  96. m_title->setContentsMargins(0, 0, 0, 0);
  97. }
  98. GetLayoutAs<QGraphicsLinearLayout>()->addItem(m_title);
  99. QGraphicsLayoutItem* slotsGraphicsItem = nullptr;
  100. NodeSlotsRequestBus::EventResult(slotsGraphicsItem, GetEntityId(), &NodeSlotsRequestBus::Events::GetGraphicsLayoutItem);
  101. if (slotsGraphicsItem)
  102. {
  103. m_slots->addItem(slotsGraphicsItem);
  104. m_slots->setContentsMargins(0, 0, 0, 0);
  105. }
  106. GetLayoutAs<QGraphicsLinearLayout>()->addItem(m_slots);
  107. StyleNotificationBus::Handler::BusConnect(GetEntityId());
  108. UpdateLayoutParameters();
  109. }
  110. void GeneralNodeLayoutComponent::UpdateHorizontalLayout()
  111. {
  112. QGraphicsLinearLayout* slotLayout;
  113. QGraphicsWidget* horizontalSpacer;
  114. NodeSlotsRequestBus::EventResult(slotLayout, GetEntityId(), &NodeSlotsRequestBus::Events::GetLinearLayout, SlotGroups::DataGroup);
  115. NodeSlotsRequestBus::EventResult(horizontalSpacer, GetEntityId(), &NodeSlotsRequestBus::Events::GetSpacer, SlotGroups::DataGroup);
  116. GetLayoutAs<QGraphicsLinearLayout>()->setOrientation(Qt::Horizontal);
  117. if (slotLayout && horizontalSpacer && horizontalSpacer->parentLayoutItem() == slotLayout)
  118. {
  119. slotLayout->removeItem(horizontalSpacer);
  120. }
  121. if (m_title->parentLayoutItem() == GetLayoutAs<QGraphicsLinearLayout>())
  122. {
  123. GetLayoutAs<QGraphicsLinearLayout>()->removeItem(m_title);
  124. // Insert the title into slot layout so it appears between the input and output slots
  125. if (slotLayout)
  126. {
  127. slotLayout->insertItem(1, m_title);
  128. slotLayout->setContentsMargins(0, 0, 0, 0);
  129. }
  130. }
  131. m_title->setSpacing(0);
  132. m_title->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  133. m_title->setPreferredSize(0, 0);
  134. m_title->setContentsMargins(0, 0, 0, 0);
  135. m_slots->setContentsMargins(3.0, 0, 3.0, 0);
  136. }
  137. void GeneralNodeLayoutComponent::UpdateLayoutParameters()
  138. {
  139. Styling::StyleHelper style(GetEntityId());
  140. Qt::Orientation layoutOrientation = style.GetAttribute(Styling::Attribute::LayoutOrientation, Qt::Vertical);
  141. qreal border = layoutOrientation == Qt::Vertical ? style.GetAttribute(Styling::Attribute::BorderWidth, 0.0) : 0.0;
  142. qreal spacing = style.GetAttribute(Styling::Attribute::Spacing, 4.0);
  143. qreal margin = style.GetAttribute(Styling::Attribute::Margin, 4.0);
  144. GetLayoutAs<QGraphicsLinearLayout>()->setContentsMargins(margin + border, margin + border, margin + border, margin + border);
  145. GetLayoutAs<QGraphicsLinearLayout>()->setSpacing(spacing);
  146. GetLayout()->invalidate();
  147. }
  148. }