NodeGroupLayoutComponent.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/StylingComponent.h>
  13. #include <GraphCanvas/Components/GeometryBus.h>
  14. #include <GraphCanvas/Components/Nodes/Comment/CommentBus.h>
  15. #include <GraphCanvas/Components/Slots/SlotBus.h>
  16. #include <GraphCanvas/tools.h>
  17. #include <GraphCanvas/Styling/StyleHelper.h>
  18. #include <Components/Nodes/NodeComponent.h>
  19. #include <Components/Nodes/Group/NodeGroupFrameComponent.h>
  20. #include <Components/Nodes/Group/NodeGroupLayerControllerComponent.h>
  21. #include <Components/Nodes/Group/NodeGroupLayoutComponent.h>
  22. #include <Components/Nodes/Comment/CommentNodeTextComponent.h>
  23. namespace GraphCanvas
  24. {
  25. /////////////////////////////
  26. // NodeGroupLayoutComponent
  27. /////////////////////////////
  28. void NodeGroupLayoutComponent::Reflect(AZ::ReflectContext* context)
  29. {
  30. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  31. if (serializeContext)
  32. {
  33. serializeContext->Class<NodeGroupLayoutComponent, NodeLayoutComponent>()
  34. ->Version(1)
  35. ;
  36. }
  37. }
  38. AZ::Entity* NodeGroupLayoutComponent::CreateNodeGroupEntity()
  39. {
  40. // Create this Node's entity.
  41. NodeConfiguration config;
  42. config.SetShowInOutliner(false);
  43. AZ::Entity* entity = NodeComponent::CreateCoreNodeEntity(config);
  44. entity->SetName("NodeGroup");
  45. entity->CreateComponent<StylingComponent>(Styling::Elements::NodeGroup::NodeGroup, AZ::EntityId());
  46. entity->CreateComponent<NodeGroupFrameComponent>();
  47. entity->CreateComponent<NodeGroupLayoutComponent>();
  48. entity->CreateComponent<CommentNodeTextComponent>("Untitled Group");
  49. entity->CreateComponent<NodeGroupLayerControllerComponent>();
  50. return entity;
  51. }
  52. NodeGroupLayoutComponent::NodeGroupLayoutComponent()
  53. {
  54. }
  55. NodeGroupLayoutComponent::~NodeGroupLayoutComponent()
  56. {
  57. }
  58. void NodeGroupLayoutComponent::OnStyleChanged()
  59. {
  60. m_style.SetStyle(GetEntityId());
  61. UpdateLayoutParameters();
  62. }
  63. void NodeGroupLayoutComponent::Init()
  64. {
  65. NodeLayoutComponent::Init();
  66. m_layout = new QGraphicsLinearLayout(Qt::Vertical);
  67. m_comment = new QGraphicsLinearLayout(Qt::Horizontal);
  68. }
  69. void NodeGroupLayoutComponent::Activate()
  70. {
  71. NodeLayoutComponent::Activate();
  72. NodeNotificationBus::Handler::BusConnect(GetEntityId());
  73. StyleNotificationBus::Handler::BusConnect(GetEntityId());
  74. }
  75. void NodeGroupLayoutComponent::Deactivate()
  76. {
  77. NodeLayoutComponent::Deactivate();
  78. StyleNotificationBus::Handler::BusDisconnect();
  79. NodeNotificationBus::Handler::BusDisconnect();
  80. }
  81. void NodeGroupLayoutComponent::OnNodeActivated()
  82. {
  83. QGraphicsLayoutItem* commentGraphicsItem = nullptr;
  84. CommentLayoutRequestBus::EventResult(commentGraphicsItem, GetEntityId(), &CommentLayoutRequestBus::Events::GetGraphicsLayoutItem);
  85. if (commentGraphicsItem)
  86. {
  87. m_comment->addItem(commentGraphicsItem);
  88. }
  89. GetLayoutAs<QGraphicsLinearLayout>()->addItem(m_comment);
  90. UpdateLayoutParameters();
  91. }
  92. void NodeGroupLayoutComponent::UpdateLayoutParameters()
  93. {
  94. qreal border = m_style.GetAttribute(Styling::Attribute::BorderWidth, 0.);
  95. qreal spacing = m_style.GetAttribute(Styling::Attribute::Spacing, 4.);
  96. qreal margin = m_style.GetAttribute(Styling::Attribute::Margin, 4.);
  97. m_layout->setContentsMargins(border, border, border, border);
  98. for (QGraphicsLinearLayout* internalLayout : { m_comment })
  99. {
  100. internalLayout->setContentsMargins(margin, margin, margin, margin);
  101. internalLayout->setSpacing(spacing);
  102. }
  103. m_layout->invalidate();
  104. }
  105. }