EntityIdNodePropertyDisplay.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <QGraphicsSceneDragDropEvent>
  9. #include <QGraphicsProxyWidget>
  10. #include <QGraphicsView>
  11. #include <QMenu>
  12. #include <QMimeData>
  13. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  14. #include <AzToolsFramework/ToolsComponents/EditorEntityIdContainer.h>
  15. #include <Components/NodePropertyDisplays/EntityIdNodePropertyDisplay.h>
  16. #include <GraphCanvas/Components/Slots/SlotBus.h>
  17. #include <GraphCanvas/Components/Slots/Data/DataSlotBus.h>
  18. #include <GraphCanvas/Components/VisualBus.h>
  19. #include <GraphCanvas/Widgets/NodePropertyBus.h>
  20. #include <Widgets/GraphCanvasLabel.h>
  21. namespace GraphCanvas
  22. {
  23. ////////////////////////////////
  24. // EntityIdNodePropertyDisplay
  25. ////////////////////////////////
  26. EntityIdNodePropertyDisplay::EntityIdNodePropertyDisplay(EntityIdDataInterface* dataInterface)
  27. : NodePropertyDisplay(dataInterface)
  28. , m_dataInterface(dataInterface)
  29. , m_propertyEntityIdCtrl(nullptr)
  30. , m_proxyWidget(nullptr)
  31. {
  32. m_dataInterface->RegisterDisplay(this);
  33. m_disabledLabel = aznew GraphCanvasLabel();
  34. m_displayLabel = aznew GraphCanvasLabel();
  35. }
  36. void EntityIdNodePropertyDisplay::ShowContextMenu(const QPoint& pos)
  37. {
  38. if (m_propertyEntityIdCtrl)
  39. {
  40. m_dataInterface->OnShowContextMenu(m_propertyEntityIdCtrl, pos);
  41. }
  42. else
  43. {
  44. AZ_Error("GraphCanvas", false, "m_propertyEntityIdCtrl doesn't exist!");
  45. }
  46. }
  47. EntityIdNodePropertyDisplay::~EntityIdNodePropertyDisplay()
  48. {
  49. CleanupProxyWidget();
  50. delete m_disabledLabel;
  51. delete m_displayLabel;
  52. delete m_dataInterface;
  53. }
  54. void EntityIdNodePropertyDisplay::RefreshStyle()
  55. {
  56. m_disabledLabel->SetSceneStyle(GetSceneId(), NodePropertyDisplay::CreateDisabledLabelStyle("entityId").c_str());
  57. AZStd::string styleName = NodePropertyDisplay::CreateDisplayLabelStyle("entityId");
  58. m_displayLabel->SetSceneStyle(GetSceneId(), styleName.c_str());
  59. QSizeF minimumSize = m_displayLabel->minimumSize();
  60. QSizeF maximumSize = m_displayLabel->maximumSize();
  61. if (m_propertyEntityIdCtrl)
  62. {
  63. m_propertyEntityIdCtrl->setMinimumSize(aznumeric_cast<int>(minimumSize.width()), aznumeric_cast<int>(minimumSize.height()));
  64. m_propertyEntityIdCtrl->setMaximumSize(aznumeric_cast<int>(maximumSize.width()), aznumeric_cast<int>(maximumSize.height()));
  65. }
  66. }
  67. void EntityIdNodePropertyDisplay::UpdateDisplay()
  68. {
  69. AZ::EntityId valueEntityId = m_dataInterface->GetEntityId();
  70. if (!AZ::EntityBus::Handler::BusIsConnectedId(valueEntityId))
  71. {
  72. AZ::EntityBus::Handler::BusDisconnect();
  73. if (valueEntityId.IsValid())
  74. {
  75. AZ::EntityBus::Handler::BusConnect(valueEntityId);
  76. }
  77. }
  78. const AZStd::string& name = m_dataInterface->GetNameOverride();
  79. if (m_propertyEntityIdCtrl)
  80. {
  81. m_propertyEntityIdCtrl->SetCurrentEntityId(valueEntityId, false, name);
  82. }
  83. const AZStd::string entityName = AzToolsFramework::GetEntityName(valueEntityId, name);
  84. if (entityName.empty())
  85. {
  86. const QString notFoundMessage = QObject::tr("(Entity not found)");
  87. m_displayLabel->SetLabel(notFoundMessage.toUtf8().data());
  88. }
  89. else
  90. {
  91. m_displayLabel->SetLabel(entityName);
  92. }
  93. if (m_proxyWidget)
  94. {
  95. m_proxyWidget->update();
  96. }
  97. }
  98. QGraphicsLayoutItem* EntityIdNodePropertyDisplay::GetDisabledGraphicsLayoutItem()
  99. {
  100. CleanupProxyWidget();
  101. return m_disabledLabel;
  102. }
  103. QGraphicsLayoutItem* EntityIdNodePropertyDisplay::GetDisplayGraphicsLayoutItem()
  104. {
  105. CleanupProxyWidget();
  106. return m_displayLabel;
  107. }
  108. QGraphicsLayoutItem* EntityIdNodePropertyDisplay::GetEditableGraphicsLayoutItem()
  109. {
  110. SetupProxyWidget();
  111. return m_proxyWidget;
  112. }
  113. void EntityIdNodePropertyDisplay::OnEntityNameChanged(const AZStd::string& /*name*/)
  114. {
  115. UpdateDisplay();
  116. }
  117. void EntityIdNodePropertyDisplay::OnDragDropStateStateChanged(const DragDropState& dragState)
  118. {
  119. Styling::StyleHelper& styleHelper = m_displayLabel->GetStyleHelper();
  120. UpdateStyleForDragDrop(dragState, styleHelper);
  121. m_displayLabel->update();
  122. }
  123. void EntityIdNodePropertyDisplay::EditStart()
  124. {
  125. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::LockEditState, this);
  126. TryAndSelectNode();
  127. }
  128. void EntityIdNodePropertyDisplay::SubmitValue()
  129. {
  130. if (m_propertyEntityIdCtrl)
  131. {
  132. m_dataInterface->SetEntityId(m_propertyEntityIdCtrl->GetEntityId());
  133. }
  134. else
  135. {
  136. AZ_Error("GraphCanvas", false, "m_propertyEntityIdCtrl doesn't exist!");
  137. }
  138. UpdateDisplay();
  139. }
  140. void EntityIdNodePropertyDisplay::EditFinished()
  141. {
  142. SubmitValue();
  143. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::UnlockEditState, this);
  144. }
  145. void EntityIdNodePropertyDisplay::SetupProxyWidget()
  146. {
  147. if (!m_propertyEntityIdCtrl)
  148. {
  149. m_proxyWidget = new QGraphicsProxyWidget();
  150. m_proxyWidget->setFlag(QGraphicsItem::ItemIsFocusable, true);
  151. m_proxyWidget->setFocusPolicy(Qt::StrongFocus);
  152. m_propertyEntityIdCtrl = aznew AzToolsFramework::PropertyEntityIdCtrl();
  153. m_propertyEntityIdCtrl->setProperty("HasNoWindowDecorations", true);
  154. m_propertyEntityIdCtrl->setContextMenuPolicy(Qt::CustomContextMenu);
  155. QObject::connect(m_propertyEntityIdCtrl, &AzToolsFramework::PropertyEntityIdCtrl::customContextMenuRequested, [this](const QPoint& pos) { this->ShowContextMenu(pos); });
  156. QObject::connect(m_propertyEntityIdCtrl, &AzToolsFramework::PropertyEntityIdCtrl::OnPickStart, [this]() { EditStart(); });
  157. QObject::connect(m_propertyEntityIdCtrl, &AzToolsFramework::PropertyEntityIdCtrl::OnPickComplete, [this]() { EditFinished(); });
  158. QObject::connect(m_propertyEntityIdCtrl, &AzToolsFramework::PropertyEntityIdCtrl::OnEntityIdChanged, [this]() { SubmitValue(); });
  159. m_proxyWidget->setWidget(m_propertyEntityIdCtrl);
  160. RegisterShortcutDispatcher(m_propertyEntityIdCtrl);
  161. UpdateDisplay();
  162. RefreshStyle();
  163. }
  164. }
  165. void EntityIdNodePropertyDisplay::CleanupProxyWidget()
  166. {
  167. if (m_propertyEntityIdCtrl)
  168. {
  169. UnregisterShortcutDispatcher(m_propertyEntityIdCtrl);
  170. delete m_propertyEntityIdCtrl; // NB: this implicitly deletes m_proxy widget
  171. m_propertyEntityIdCtrl = nullptr;
  172. m_proxyWidget = nullptr;
  173. }
  174. }
  175. }