GeneralNodeFrameComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <AzCore/PlatformDef.h>
  9. AZ_PUSH_DISABLE_WARNING(4251 4800 4244, "-Wunknown-warning-option")
  10. #include <QPainter>
  11. #include <QStyleOption>
  12. AZ_POP_DISABLE_WARNING
  13. #include <Components/Nodes/General/GeneralNodeFrameComponent.h>
  14. #include <GraphCanvas/Components/GeometryBus.h>
  15. #include <GraphCanvas/Components/GridBus.h>
  16. #include <GraphCanvas/Editor/GraphCanvasProfiler.h>
  17. #include <GraphCanvas/tools.h>
  18. #include <GraphCanvas/Styling/StyleHelper.h>
  19. namespace GraphCanvas
  20. {
  21. //////////////////////////////
  22. // GeneralNodeFrameComponent
  23. //////////////////////////////
  24. void GeneralNodeFrameComponent::Reflect(AZ::ReflectContext* context)
  25. {
  26. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  27. if (serializeContext)
  28. {
  29. serializeContext->Class<GeneralNodeFrameComponent, AZ::Component>()
  30. ->Version(1)
  31. ;
  32. }
  33. }
  34. GeneralNodeFrameComponent::GeneralNodeFrameComponent()
  35. : m_frameWidget(nullptr)
  36. , m_shouldDeleteFrame(true)
  37. {
  38. }
  39. GeneralNodeFrameComponent::~GeneralNodeFrameComponent()
  40. {
  41. if (m_shouldDeleteFrame)
  42. {
  43. delete m_frameWidget;
  44. }
  45. }
  46. void GeneralNodeFrameComponent::Init()
  47. {
  48. m_frameWidget = aznew GeneralNodeFrameGraphicsWidget(GetEntityId());
  49. }
  50. void GeneralNodeFrameComponent::Activate()
  51. {
  52. NodeNotificationBus::Handler::BusConnect(GetEntityId());
  53. m_frameWidget->Activate();
  54. }
  55. void GeneralNodeFrameComponent::Deactivate()
  56. {
  57. m_frameWidget->Deactivate();
  58. NodeNotificationBus::Handler::BusDisconnect();
  59. }
  60. void GeneralNodeFrameComponent::OnNodeActivated()
  61. {
  62. QGraphicsLayout* layout = nullptr;
  63. NodeLayoutRequestBus::EventResult(layout, GetEntityId(), &NodeLayoutRequests::GetLayout);
  64. m_frameWidget->setLayout(layout);
  65. }
  66. void GeneralNodeFrameComponent::OnNodeWrapped(const AZ::EntityId& /*wrappingNode*/)
  67. {
  68. // When wrapped, our NodeFrame widget is part of another objects layout, and will
  69. // be deleted when that object gets deleted.
  70. m_shouldDeleteFrame = false;
  71. }
  72. void GeneralNodeFrameComponent::OnNodeUnwrapped(const AZ::EntityId& /*wrappingNode*/)
  73. {
  74. m_shouldDeleteFrame = true;
  75. }
  76. ///////////////////////////////////
  77. // GeneralNodeFrameGraphicsWidget
  78. ///////////////////////////////////
  79. GeneralNodeFrameGraphicsWidget::GeneralNodeFrameGraphicsWidget(const AZ::EntityId& entityKey)
  80. : NodeFrameGraphicsWidget(entityKey)
  81. {
  82. }
  83. QPainterPath GeneralNodeFrameGraphicsWidget::GetOutline() const
  84. {
  85. QPainterPath path;
  86. QPen border = m_style.GetBorder();
  87. qreal cornerRadius = GetCornerRadius();
  88. qreal halfBorder = border.widthF() / 2.;
  89. QRectF adjusted = sceneBoundingRect().marginsRemoved(QMarginsF(halfBorder, halfBorder, halfBorder, halfBorder));
  90. if (cornerRadius >= 1.0)
  91. {
  92. path.addRoundedRect(adjusted, cornerRadius, cornerRadius);
  93. }
  94. else
  95. {
  96. path.addRect(adjusted);
  97. }
  98. return path;
  99. }
  100. void GeneralNodeFrameGraphicsWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
  101. {
  102. GRAPH_CANVAS_DETAILED_PROFILE_FUNCTION();
  103. QPen border = m_style.GetBorder();
  104. QBrush background = m_style.GetBrush(Styling::Attribute::BackgroundColor);
  105. if (border.style() != Qt::NoPen || background.color().alpha() > 0)
  106. {
  107. qreal cornerRadius = GetCornerRadius();
  108. border.setJoinStyle(Qt::PenJoinStyle::MiterJoin); // sharp corners
  109. painter->setPen(border);
  110. painter->setBrush(background);
  111. qreal halfBorder = border.widthF() / 2.;
  112. QRectF adjusted = boundingRect().marginsRemoved(QMarginsF(halfBorder, halfBorder, halfBorder, halfBorder));
  113. if (cornerRadius >= 1.0)
  114. {
  115. painter->drawRoundedRect(adjusted, cornerRadius, cornerRadius);
  116. }
  117. else
  118. {
  119. painter->drawRect(adjusted);
  120. }
  121. }
  122. QGraphicsWidget::paint(painter, option, widget);
  123. }
  124. }