PropertySlotLayoutComponent.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <QCoreApplication>
  9. #include <qgraphicslayoutitem.h>
  10. #include <qgraphicsscene.h>
  11. #include <qsizepolicy.h>
  12. #include <Components/Slots/Property/PropertySlotLayoutComponent.h>
  13. #include <GraphCanvas/Components/NodePropertyDisplay/NodePropertyDisplay.h>
  14. #include <GraphCanvas/Components/Slots/Property/PropertySlotBus.h>
  15. #include <GraphCanvas/Editor/GraphModelBus.h>
  16. #include <GraphCanvas/tools.h>
  17. #include <Widgets/GraphCanvasLabel.h>
  18. namespace GraphCanvas
  19. {
  20. ///////////////////////
  21. // PropertySlotLayout
  22. ///////////////////////
  23. PropertySlotLayout::PropertySlotLayout(PropertySlotLayoutComponent& owner)
  24. : m_connectionType(ConnectionType::CT_Invalid)
  25. , m_owner(owner)
  26. , m_nodePropertyDisplay(nullptr)
  27. {
  28. setInstantInvalidatePropagation(true);
  29. setOrientation(Qt::Horizontal);
  30. m_spacer = new QGraphicsWidget();
  31. m_spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
  32. m_spacer->setAutoFillBackground(true);
  33. m_spacer->setMinimumSize(0, 0);
  34. m_spacer->setPreferredWidth(0);
  35. m_spacer->setMaximumHeight(0);
  36. m_nodePropertyDisplay = aznew NodePropertyDisplayWidget();
  37. m_slotText = aznew GraphCanvasLabel();
  38. }
  39. PropertySlotLayout::~PropertySlotLayout()
  40. {
  41. }
  42. void PropertySlotLayout::Activate()
  43. {
  44. SceneMemberNotificationBus::Handler::BusConnect(m_owner.GetEntityId());
  45. SlotNotificationBus::Handler::BusConnect(m_owner.GetEntityId());
  46. StyleNotificationBus::Handler::BusConnect(m_owner.GetEntityId());
  47. }
  48. void PropertySlotLayout::Deactivate()
  49. {
  50. StyleNotificationBus::Handler::BusDisconnect();
  51. SlotNotificationBus::Handler::BusDisconnect();
  52. SceneMemberNotificationBus::Handler::BusDisconnect();
  53. }
  54. void PropertySlotLayout::OnSceneSet(const AZ::EntityId&)
  55. {
  56. SlotRequestBus::EventResult(m_connectionType, m_owner.GetEntityId(), &SlotRequests::GetConnectionType);
  57. AZStd::string slotName;
  58. SlotRequestBus::EventResult(slotName, m_owner.GetEntityId(), &SlotRequests::GetName);
  59. m_slotText->SetLabel(slotName);
  60. AZStd::string toolTip;
  61. SlotRequestBus::EventResult(toolTip, m_owner.GetEntityId(), &SlotRequests::GetTooltip);
  62. OnTooltipChanged(toolTip);
  63. TryAndSetupSlot();
  64. }
  65. void PropertySlotLayout::OnSceneReady()
  66. {
  67. TryAndSetupSlot();
  68. }
  69. void PropertySlotLayout::OnRegisteredToNode(const AZ::EntityId& /*nodeId*/)
  70. {
  71. TryAndSetupSlot();
  72. }
  73. void PropertySlotLayout::OnTooltipChanged(const AZStd::string& tooltip)
  74. {
  75. m_slotText->setToolTip(Tools::qStringFromUtf8(tooltip));
  76. m_nodePropertyDisplay->setToolTip(Tools::qStringFromUtf8(tooltip));
  77. }
  78. void PropertySlotLayout::OnStyleChanged()
  79. {
  80. m_style.SetStyle(m_owner.GetEntityId());
  81. m_nodePropertyDisplay->RefreshStyle();
  82. switch (m_connectionType)
  83. {
  84. case ConnectionType::CT_Input:
  85. m_slotText->SetStyle(m_owner.GetEntityId(), ".inputSlotName");
  86. break;
  87. case ConnectionType::CT_Output:
  88. m_slotText->SetStyle(m_owner.GetEntityId(), ".outputSlotName");
  89. break;
  90. default:
  91. m_slotText->SetStyle(m_owner.GetEntityId(), ".slotName");
  92. break;
  93. };
  94. qreal padding = m_style.GetAttribute(Styling::Attribute::Padding, 2.);
  95. setContentsMargins(padding, padding, padding, padding);
  96. setSpacing(m_style.GetAttribute(Styling::Attribute::Spacing, 2.));
  97. UpdateGeometry();
  98. }
  99. void PropertySlotLayout::TryAndSetupSlot()
  100. {
  101. if (m_nodePropertyDisplay->GetNodePropertyDisplay() == nullptr)
  102. {
  103. CreateDataDisplay();
  104. }
  105. }
  106. void PropertySlotLayout::CreateDataDisplay()
  107. {
  108. if (m_connectionType == ConnectionType::CT_Input)
  109. {
  110. AZ::EntityId sceneId;
  111. SceneMemberRequestBus::EventResult(sceneId, m_owner.GetEntityId(), &SceneMemberRequests::GetScene);
  112. AZ::EntityId nodeId;
  113. SlotRequestBus::EventResult(nodeId, m_owner.GetEntityId(), &SlotRequests::GetNode);
  114. AZ::Crc32 propertyId;
  115. PropertySlotRequestBus::EventResult(propertyId, m_owner.GetEntityId(), &PropertySlotRequests::GetPropertyId);
  116. NodePropertyDisplay* nodePropertyDisplay = nullptr;
  117. GraphModelRequestBus::EventResult(nodePropertyDisplay, sceneId, &GraphModelRequests::CreatePropertySlotPropertyDisplay, propertyId, nodeId, m_owner.GetEntityId());
  118. if (nodePropertyDisplay)
  119. {
  120. nodePropertyDisplay->SetNodeId(nodeId);
  121. nodePropertyDisplay->SetSlotId(m_owner.GetEntityId());
  122. m_nodePropertyDisplay->SetNodePropertyDisplay(nodePropertyDisplay);
  123. UpdateLayout();
  124. OnStyleChanged();
  125. }
  126. }
  127. else
  128. {
  129. UpdateLayout();
  130. OnStyleChanged();
  131. }
  132. }
  133. void PropertySlotLayout::UpdateLayout()
  134. {
  135. for (int i = count() - 1; i >= 0; --i)
  136. {
  137. removeAt(i);
  138. }
  139. switch (m_connectionType)
  140. {
  141. case ConnectionType::CT_Input:
  142. addItem(m_slotText);
  143. setAlignment(m_slotText, Qt::AlignLeft);
  144. addItem(m_nodePropertyDisplay);
  145. setAlignment(m_slotText, Qt::AlignLeft);
  146. addItem(m_spacer);
  147. setAlignment(m_spacer, Qt::AlignLeft);
  148. break;
  149. case ConnectionType::CT_Output:
  150. addItem(m_spacer);
  151. setAlignment(m_spacer, Qt::AlignRight);
  152. addItem(m_slotText);
  153. setAlignment(m_slotText, Qt::AlignRight);
  154. break;
  155. default:
  156. addItem(m_slotText);
  157. addItem(m_spacer);
  158. break;
  159. }
  160. UpdateGeometry();
  161. }
  162. void PropertySlotLayout::UpdateGeometry()
  163. {
  164. m_slotText->update();
  165. invalidate();
  166. updateGeometry();
  167. }
  168. ////////////////////////////////
  169. // PropertySlotLayoutComponent
  170. ////////////////////////////////
  171. void PropertySlotLayoutComponent::Reflect(AZ::ReflectContext* reflectContext)
  172. {
  173. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  174. if (serializeContext)
  175. {
  176. serializeContext->Class<PropertySlotLayoutComponent, AZ::Component>()
  177. ->Version(1)
  178. ;
  179. }
  180. }
  181. PropertySlotLayoutComponent::PropertySlotLayoutComponent()
  182. : m_layout(nullptr)
  183. {
  184. }
  185. void PropertySlotLayoutComponent::Init()
  186. {
  187. SlotLayoutComponent::Init();
  188. m_layout = aznew PropertySlotLayout((*this));
  189. SetLayout(m_layout);
  190. }
  191. void PropertySlotLayoutComponent::Activate()
  192. {
  193. SlotLayoutComponent::Activate();
  194. m_layout->Activate();
  195. }
  196. void PropertySlotLayoutComponent::Deactivate()
  197. {
  198. m_layout->Deactivate();
  199. SlotLayoutComponent::Deactivate();
  200. }
  201. }