CommentNodeLayoutComponent.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <QGraphicsLayoutItem>
  10. #include <QGraphicsGridLayout>
  11. #include <AzCore/RTTI/TypeInfo.h>
  12. #include <Components/Nodes/Comment/CommentNodeLayoutComponent.h>
  13. #include <Components/Nodes/Comment/CommentLayerControllerComponent.h>
  14. #include <Components/Nodes/Comment/CommentNodeFrameComponent.h>
  15. #include <Components/Nodes/Comment/CommentNodeTextComponent.h>
  16. #include <Components/Nodes/General/GeneralNodeFrameComponent.h>
  17. #include <Components/Nodes/NodeComponent.h>
  18. #include <Components/StylingComponent.h>
  19. #include <GraphCanvas/Components/Nodes/Comment/CommentBus.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. // CommentNodeLayoutComponent
  27. ///////////////////////////////
  28. void CommentNodeLayoutComponent::Reflect(AZ::ReflectContext* context)
  29. {
  30. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  31. if (serializeContext)
  32. {
  33. serializeContext->Class<CommentNodeLayoutComponent, NodeLayoutComponent>()
  34. ->Version(1)
  35. ;
  36. }
  37. }
  38. AZ::Entity* CommentNodeLayoutComponent::CreateCommentNodeEntity()
  39. {
  40. // Create this Node's entity.
  41. NodeConfiguration config;
  42. config.SetShowInOutliner(false);
  43. AZ::Entity* entity = NodeComponent::CreateCoreNodeEntity(config);
  44. entity->SetName("Comment");
  45. entity->CreateComponent<StylingComponent>(Styling::Elements::Comment, AZ::EntityId());
  46. entity->CreateComponent<CommentNodeFrameComponent>();
  47. entity->CreateComponent<CommentNodeLayoutComponent>();
  48. entity->CreateComponent<CommentNodeTextComponent>();
  49. entity->CreateComponent<CommentLayerControllerComponent>();
  50. return entity;
  51. }
  52. CommentNodeLayoutComponent::CommentNodeLayoutComponent()
  53. {
  54. }
  55. CommentNodeLayoutComponent::~CommentNodeLayoutComponent()
  56. {
  57. }
  58. void CommentNodeLayoutComponent::Init()
  59. {
  60. NodeLayoutComponent::Init();
  61. AZ::EntityBus::Handler::BusConnect(GetEntityId());
  62. m_layout = new QGraphicsLinearLayout(Qt::Vertical);
  63. m_comment = new QGraphicsLinearLayout(Qt::Horizontal);
  64. }
  65. void CommentNodeLayoutComponent::Activate()
  66. {
  67. NodeLayoutComponent::Activate();
  68. NodeNotificationBus::Handler::BusConnect(GetEntityId());
  69. StyleNotificationBus::Handler::BusConnect(GetEntityId());
  70. }
  71. void CommentNodeLayoutComponent::Deactivate()
  72. {
  73. NodeLayoutComponent::Deactivate();
  74. StyleNotificationBus::Handler::BusDisconnect();
  75. NodeNotificationBus::Handler::BusDisconnect();
  76. }
  77. void CommentNodeLayoutComponent::OnEntityExists(const AZ::EntityId& /*entityId*/)
  78. {
  79. AZ::Entity* entity = GetEntity();
  80. CommentNodeFrameComponent* commentFrameComponent = entity->FindComponent<CommentNodeFrameComponent>();
  81. if (!commentFrameComponent)
  82. {
  83. GeneralNodeFrameComponent* generalNodeFrameComponent = entity->FindComponent<GeneralNodeFrameComponent>();
  84. entity->RemoveComponent(generalNodeFrameComponent);
  85. delete generalNodeFrameComponent;
  86. entity->CreateComponent<CommentNodeFrameComponent>();
  87. }
  88. AZ::EntityBus::Handler::BusDisconnect(GetEntityId());
  89. }
  90. void CommentNodeLayoutComponent::OnStyleChanged()
  91. {
  92. m_style.SetStyle(GetEntityId());
  93. UpdateLayoutParameters();
  94. }
  95. void CommentNodeLayoutComponent::OnNodeActivated()
  96. {
  97. QGraphicsLayoutItem* commentGraphicsItem = nullptr;
  98. CommentLayoutRequestBus::EventResult(commentGraphicsItem, GetEntityId(), &CommentLayoutRequestBus::Events::GetGraphicsLayoutItem);
  99. if (commentGraphicsItem)
  100. {
  101. m_comment->addItem(commentGraphicsItem);
  102. }
  103. GetLayoutAs<QGraphicsLinearLayout>()->addItem(m_comment);
  104. UpdateLayoutParameters();
  105. }
  106. void CommentNodeLayoutComponent::UpdateLayoutParameters()
  107. {
  108. qreal border = m_style.GetAttribute(Styling::Attribute::BorderWidth, 0.);
  109. qreal spacing = m_style.GetAttribute(Styling::Attribute::Spacing, 4.);
  110. qreal margin = m_style.GetAttribute(Styling::Attribute::Margin, 4.);
  111. m_layout->setContentsMargins(border, border, border, border);
  112. for (QGraphicsLinearLayout* internalLayout : { m_comment })
  113. {
  114. internalLayout->setContentsMargins(margin, margin, margin, margin);
  115. internalLayout->setSpacing(spacing);
  116. }
  117. m_layout->invalidate();
  118. }
  119. }