NodeFrameGraphicsWidget.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <QPainter>
  9. #include <QGraphicsLayout>
  10. #include <QGraphicsSceneEvent>
  11. #include <Components/Nodes/NodeFrameGraphicsWidget.h>
  12. #include <GraphCanvas/Components/GridBus.h>
  13. #include <GraphCanvas/tools.h>
  14. #include <GraphCanvas/Styling/StyleHelper.h>
  15. namespace GraphCanvas
  16. {
  17. ////////////////////////////
  18. // NodeFrameGraphicsWidget
  19. ////////////////////////////
  20. NodeFrameGraphicsWidget::NodeFrameGraphicsWidget(const AZ::EntityId& entityKey)
  21. : RootGraphicsItem(entityKey)
  22. , m_displayState(NodeFrameDisplayState::None)
  23. {
  24. setFlags(ItemIsSelectable | ItemIsFocusable | ItemIsMovable | ItemSendsScenePositionChanges);
  25. setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
  26. setData(GraphicsItemName, QStringLiteral("DefaultNodeVisual/%1").arg(static_cast<AZ::u64>(GetEntityId()), 16, 16, QChar('0')));
  27. setCacheMode(QGraphicsItem::CacheMode::DeviceCoordinateCache);
  28. }
  29. void NodeFrameGraphicsWidget::Activate()
  30. {
  31. SceneMemberUIRequestBus::Handler::BusConnect(GetEntityId());
  32. GeometryNotificationBus::Handler::BusConnect(GetEntityId());
  33. StyleNotificationBus::Handler::BusConnect(GetEntityId());
  34. NodeNotificationBus::Handler::BusConnect(GetEntityId());
  35. NodeUIRequestBus::Handler::BusConnect(GetEntityId());
  36. VisualRequestBus::Handler::BusConnect(GetEntityId());
  37. OnActivated();
  38. }
  39. void NodeFrameGraphicsWidget::Deactivate()
  40. {
  41. StyleNotificationBus::Handler::BusDisconnect();
  42. NodeNotificationBus::Handler::BusDisconnect();
  43. NodeUIRequestBus::Handler::BusDisconnect();
  44. VisualRequestBus::Handler::BusDisconnect();
  45. GeometryNotificationBus::Handler::BusDisconnect();
  46. SceneMemberUIRequestBus::Handler::BusDisconnect();
  47. }
  48. QRectF NodeFrameGraphicsWidget::GetBoundingRect() const
  49. {
  50. return boundingRect();
  51. }
  52. QGraphicsItem* NodeFrameGraphicsWidget::GetRootGraphicsItem()
  53. {
  54. return this;
  55. }
  56. QGraphicsLayoutItem* NodeFrameGraphicsWidget::GetRootGraphicsLayoutItem()
  57. {
  58. return this;
  59. }
  60. void NodeFrameGraphicsWidget::SetSelected(bool selected)
  61. {
  62. setSelected(selected);
  63. }
  64. bool NodeFrameGraphicsWidget::IsSelected() const
  65. {
  66. return isSelected();
  67. }
  68. void NodeFrameGraphicsWidget::SetZValue(qreal zValue)
  69. {
  70. setZValue(zValue);
  71. }
  72. qreal NodeFrameGraphicsWidget::GetZValue() const
  73. {
  74. return aznumeric_cast<int>(zValue());
  75. }
  76. void NodeFrameGraphicsWidget::OnPositionChanged(const AZ::EntityId& /*entityId*/, const AZ::Vector2& position)
  77. {
  78. setPos(QPointF(position.GetX(), position.GetY()));
  79. }
  80. void NodeFrameGraphicsWidget::OnStyleChanged()
  81. {
  82. m_style.SetStyle(GetEntityId());
  83. setOpacity(m_style.GetAttribute(Styling::Attribute::Opacity, 1.0f));
  84. OnRefreshStyle();
  85. update();
  86. }
  87. QSizeF NodeFrameGraphicsWidget::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
  88. {
  89. QSizeF retVal = QGraphicsWidget::sizeHint(which, constraint);
  90. if (IsResizedToGrid())
  91. {
  92. qreal borderWidth = 2 * GetBorderWidth();
  93. int width = static_cast<int>(retVal.width() - borderWidth);
  94. int height = static_cast<int>(retVal.height() - borderWidth);
  95. width = GrowToNextStep(width, GetGridXStep(), StepAxis::Width);
  96. height = GrowToNextStep(height, GetGridYStep(), StepAxis::Height);
  97. retVal = QSizeF(width, height);
  98. }
  99. return retVal;
  100. }
  101. void NodeFrameGraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent* resizeEvent)
  102. {
  103. QGraphicsWidget::resizeEvent(resizeEvent);
  104. // For some reason when you first begin to drag a node widget, it resizes itself from old size to 0. Causing it to resize the group it's in.
  105. //
  106. // Kind of a quick patch to avoid that happening since there's nothing obvious in a callstack where the faulty resize is coming from.
  107. if (!resizeEvent->newSize().isEmpty())
  108. {
  109. GeometryRequestBus::Event(GetEntityId(), &GeometryRequests::SignalBoundsChanged);
  110. }
  111. }
  112. void NodeFrameGraphicsWidget::OnDeleteItem()
  113. {
  114. AZ::EntityId graphId;
  115. SceneMemberRequestBus::EventResult(graphId, GetEntityId(), &SceneMemberRequests::GetScene);
  116. SceneRequestBus::Event(graphId, &SceneRequests::DeleteNodeAndStitchConnections, GetEntityId());
  117. }
  118. QGraphicsItem* NodeFrameGraphicsWidget::AsGraphicsItem()
  119. {
  120. return this;
  121. }
  122. bool NodeFrameGraphicsWidget::Contains(const AZ::Vector2& position) const
  123. {
  124. auto local = mapFromScene(QPointF(position.GetX(), position.GetY()));
  125. return boundingRect().contains(local);
  126. }
  127. void NodeFrameGraphicsWidget::SetVisible(bool visible)
  128. {
  129. setVisible(visible);
  130. }
  131. bool NodeFrameGraphicsWidget::IsVisible() const
  132. {
  133. return isVisible();
  134. }
  135. void NodeFrameGraphicsWidget::OnNodeActivated()
  136. {
  137. }
  138. void NodeFrameGraphicsWidget::OnAddedToScene(const AZ::EntityId& sceneId)
  139. {
  140. AZStd::string tooltip;
  141. NodeRequestBus::EventResult(tooltip, GetEntityId(), &NodeRequests::GetTooltip);
  142. setToolTip(Tools::qStringFromUtf8(tooltip));
  143. //TODO setEnabled(node->IsEnabled());
  144. AZ::Vector2 position;
  145. GeometryRequestBus::EventResult(position, GetEntityId(), &GeometryRequests::GetPosition);
  146. OnPositionChanged(GetEntityId(), position);
  147. SceneRequestBus::EventResult(m_editorId, sceneId, &SceneRequests::GetEditorId);
  148. }
  149. void NodeFrameGraphicsWidget::OnNodeWrapped(const AZ::EntityId& wrappingNode)
  150. {
  151. GeometryNotificationBus::Handler::BusDisconnect();
  152. setFlag(QGraphicsItem::ItemIsMovable, false);
  153. SetSnapToGridEnabled(false);
  154. SetResizeToGridEnabled(false);
  155. SetSteppedSizingEnabled(false);
  156. m_wrapperNode = wrappingNode;
  157. m_isWrapped = true;
  158. }
  159. void NodeFrameGraphicsWidget::AdjustSize()
  160. {
  161. QRectF originalSize = boundingRect();
  162. if (!m_isWrapped)
  163. {
  164. adjustSize();
  165. }
  166. else
  167. {
  168. NodeUIRequestBus::Event(m_wrapperNode, &NodeUIRequests::AdjustSize);
  169. }
  170. QRectF newSize = boundingRect();
  171. if (originalSize != newSize)
  172. {
  173. GeometryRequestBus::Event(GetEntityId(), &GeometryRequests::SignalBoundsChanged);
  174. }
  175. }
  176. void NodeFrameGraphicsWidget::SetSnapToGrid(bool snapToGrid)
  177. {
  178. SetSnapToGridEnabled(snapToGrid);
  179. }
  180. void NodeFrameGraphicsWidget::SetResizeToGrid(bool resizeToGrid)
  181. {
  182. SetResizeToGridEnabled(resizeToGrid);
  183. }
  184. void NodeFrameGraphicsWidget::SetGrid(AZ::EntityId gridId)
  185. {
  186. AZ::Vector2 gridSize;
  187. GridRequestBus::EventResult(gridSize, gridId, &GridRequests::GetMinorPitch);
  188. SetGridSize(gridSize);
  189. }
  190. qreal NodeFrameGraphicsWidget::GetCornerRadius() const
  191. {
  192. return m_style.GetAttribute(Styling::Attribute::BorderRadius, 5.0);
  193. }
  194. qreal NodeFrameGraphicsWidget::GetBorderWidth() const
  195. {
  196. return m_style.GetAttribute(Styling::Attribute::BorderWidth, 1.0);
  197. }
  198. void NodeFrameGraphicsWidget::SetSteppedSizingEnabled(bool enabled)
  199. {
  200. if (enabled != m_enabledSteppedSizing)
  201. {
  202. m_enabledSteppedSizing = enabled;
  203. }
  204. }
  205. int NodeFrameGraphicsWidget::GrowToNextStep(int value, int step, StepAxis stepAxis) const
  206. {
  207. int finalSize = value;
  208. int delta = value % step;
  209. if (delta != 0)
  210. {
  211. finalSize = value + (step - delta);
  212. }
  213. int gridSteps = finalSize / step;
  214. if (m_enabledSteppedSizing)
  215. {
  216. if (stepAxis == StepAxis::Width)
  217. {
  218. StyleManagerRequestBus::EventResult(gridSteps, m_editorId, &StyleManagerRequests::GetSteppedWidth, gridSteps);
  219. }
  220. else if (stepAxis == StepAxis::Height)
  221. {
  222. StyleManagerRequestBus::EventResult(gridSteps, m_editorId, &StyleManagerRequests::GetSteppedHeight, gridSteps);
  223. }
  224. }
  225. return gridSteps * step;
  226. }
  227. int NodeFrameGraphicsWidget::RoundToClosestStep(int value, int step) const
  228. {
  229. if (step == 1)
  230. {
  231. return value;
  232. }
  233. int halfStep = step / 2;
  234. value += halfStep;
  235. return ShrinkToPreviousStep(value, step);
  236. }
  237. int NodeFrameGraphicsWidget::ShrinkToPreviousStep(int value, int step) const
  238. {
  239. int absValue = (value%step);
  240. if (absValue < 0)
  241. {
  242. absValue = step + absValue;
  243. }
  244. return value - absValue;
  245. }
  246. void NodeFrameGraphicsWidget::OnActivated()
  247. {
  248. }
  249. void NodeFrameGraphicsWidget::OnDeactivated()
  250. {
  251. }
  252. void NodeFrameGraphicsWidget::OnRefreshStyle()
  253. {
  254. }
  255. }