GraphCanvasCheckBox.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <QFont>
  10. #include <QGraphicsItem>
  11. #include <qgraphicssceneevent.h>
  12. #include <QPainter>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <Widgets/GraphCanvasCheckBox.h>
  15. #include <GraphCanvas/Editor/GraphCanvasProfiler.h>
  16. #include <GraphCanvas/tools.h>
  17. namespace GraphCanvas
  18. {
  19. ////////////////////////
  20. // GraphCanvasCheckBox
  21. ////////////////////////
  22. GraphCanvasCheckBox::GraphCanvasCheckBox(QGraphicsItem* parent)
  23. : QGraphicsWidget(parent)
  24. , m_checked(false)
  25. , m_pressed(false)
  26. {
  27. setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
  28. setFlag(ItemIsMovable, false);
  29. setAcceptHoverEvents(true);
  30. setAcceptedMouseButtons(Qt::MouseButton::LeftButton);
  31. }
  32. void GraphCanvasCheckBox::SetStyle(const AZ::EntityId& entityId, const char* styleElement)
  33. {
  34. prepareGeometryChange();
  35. m_styleHelper.SetStyle(entityId, styleElement);
  36. updateGeometry();
  37. update();
  38. }
  39. void GraphCanvasCheckBox::SetSceneStyle(const AZ::EntityId& sceneId, const char* style)
  40. {
  41. prepareGeometryChange();
  42. m_styleHelper.SetScene(sceneId);
  43. m_styleHelper.SetStyle(style);
  44. updateGeometry();
  45. update();
  46. }
  47. void GraphCanvasCheckBox::SetChecked(bool checked)
  48. {
  49. if (m_checked != checked)
  50. {
  51. m_checked = checked;
  52. GraphCanvasCheckBoxNotificationBus::Event(this, &GraphCanvasCheckBoxNotifications::OnValueChanged, m_checked);
  53. update();
  54. }
  55. }
  56. bool GraphCanvasCheckBox::IsChecked() const
  57. {
  58. return m_checked;
  59. }
  60. void GraphCanvasCheckBox::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/ /*= nullptr*/)
  61. {
  62. GRAPH_CANVAS_DETAILED_PROFILE_FUNCTION();
  63. painter->save();
  64. // Background
  65. qreal borderRadius = m_styleHelper.GetAttribute(Styling::Attribute::BorderRadius, 0);
  66. qreal halfBorder = m_styleHelper.GetAttribute(Styling::Attribute::BorderWidth, 0.0f) * 0.5f;
  67. QRectF drawRectangle = boundingRect();
  68. QSizeF size = m_styleHelper.GetSize(minimumSize());
  69. qreal halfWidthDiff = (drawRectangle.width() - size.width()) * 0.5f;
  70. qreal halfHeightDiff = (drawRectangle.height() - size.height()) * 0.5f;
  71. drawRectangle.setX(halfWidthDiff);
  72. drawRectangle.setY(halfHeightDiff);
  73. drawRectangle.setWidth(size.width());
  74. drawRectangle.setHeight(size.height());
  75. drawRectangle.adjust(halfBorder, halfBorder, -halfBorder, -halfBorder);
  76. QPainterPath borderPath;
  77. borderPath.addRoundedRect(drawRectangle, borderRadius, borderRadius);
  78. painter->fillPath(borderPath, m_styleHelper.GetBrush(Styling::Attribute::BackgroundColor));
  79. if (m_styleHelper.HasAttribute(Styling::Attribute::BorderWidth))
  80. {
  81. QPen restorePen = painter->pen();
  82. painter->setPen(m_styleHelper.GetBorder());
  83. painter->drawPath(borderPath);
  84. painter->setPen(restorePen);
  85. }
  86. if (m_checked)
  87. {
  88. qreal spacing = m_styleHelper.GetAttribute(Styling::Attribute::Spacing, 2.0);
  89. drawRectangle.adjust(spacing, spacing, -spacing, -spacing);
  90. painter->setBrush(m_styleHelper.GetBrush(Styling::Attribute::Color));
  91. painter->drawRoundedRect(drawRectangle, borderRadius, borderRadius);
  92. qreal margin = m_styleHelper.GetAttribute(Styling::Attribute::Margin, 0.0f);
  93. drawRectangle.adjust(margin, margin, -margin, -margin);
  94. QPen checkPen;
  95. checkPen.setColor(m_styleHelper.GetAttribute(Styling::Attribute::LineColor, QColor(0,0,0)));
  96. checkPen.setWidth(m_styleHelper.GetAttribute(Styling::Attribute::LineWidth, 2));
  97. painter->setPen(checkPen);
  98. // Check mark drawing
  99. QPointF firstPoint = drawRectangle.topLeft();
  100. firstPoint.setY(firstPoint.y() + drawRectangle.height() * 0.65f);
  101. QPointF secondPoint = drawRectangle.center();
  102. secondPoint.setX(secondPoint.x() - drawRectangle.width() * 0.15f);
  103. secondPoint.setY(drawRectangle.bottom());
  104. QPointF thirdPoint = drawRectangle.topRight();
  105. painter->drawLine(firstPoint, secondPoint);
  106. painter->drawLine(secondPoint, thirdPoint);
  107. }
  108. painter->restore();
  109. }
  110. QSizeF GraphCanvasCheckBox::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
  111. {
  112. QSizeF size = m_styleHelper.GetSize(QSizeF());
  113. switch (which)
  114. {
  115. case Qt::PreferredSize:
  116. return size;
  117. case Qt::MinimumSize:
  118. return size;
  119. case Qt::MaximumSize:
  120. {
  121. QSizeF maximumSize = m_styleHelper.GetMaximumSize();
  122. if (maximumSize.width() > size.width())
  123. {
  124. maximumSize.setWidth(size.width());
  125. }
  126. return maximumSize;
  127. }
  128. default:
  129. break;
  130. }
  131. return QGraphicsWidget::sizeHint(which, constraint);
  132. }
  133. void GraphCanvasCheckBox::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent)
  134. {
  135. QGraphicsWidget::mousePressEvent(mouseEvent);
  136. mouseEvent->accept();
  137. m_pressed = true;
  138. m_styleHelper.AddSelector(Styling::States::Pressed);
  139. update();
  140. }
  141. void GraphCanvasCheckBox::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent)
  142. {
  143. QGraphicsWidget::mouseMoveEvent(mouseEvent);
  144. if (m_pressed)
  145. {
  146. if (!mapRectToScene(boundingRect()).contains(mouseEvent->scenePos()))
  147. {
  148. m_styleHelper.RemoveSelector(Styling::States::Pressed);
  149. m_pressed = false;
  150. update();
  151. }
  152. }
  153. else if (!m_pressed)
  154. {
  155. if (mapRectToScene(boundingRect()).contains(mouseEvent->scenePos()))
  156. {
  157. m_styleHelper.AddSelector(Styling::States::Pressed);
  158. m_pressed = true;
  159. update();
  160. }
  161. }
  162. }
  163. void GraphCanvasCheckBox::mouseReleaseEvent(QGraphicsSceneMouseEvent* mouseEvent)
  164. {
  165. QGraphicsWidget::mouseReleaseEvent(mouseEvent);
  166. if (m_pressed)
  167. {
  168. m_styleHelper.RemoveSelector(Styling::States::Pressed);
  169. GraphCanvas::GraphCanvasCheckBoxNotificationBus::Event(this, &GraphCanvasCheckBoxNotifications::OnClicked);
  170. SetChecked(!IsChecked());
  171. }
  172. m_pressed = false;
  173. }
  174. void GraphCanvasCheckBox::hoverEnterEvent(QGraphicsSceneHoverEvent* hoverEvent)
  175. {
  176. QGraphicsWidget::hoverEnterEvent(hoverEvent);
  177. hoverEvent->accept();
  178. m_styleHelper.AddSelector(Styling::States::Hovered);
  179. update();
  180. }
  181. void GraphCanvasCheckBox::hoverLeaveEvent(QGraphicsSceneHoverEvent* hoverEvent)
  182. {
  183. QGraphicsWidget::hoverLeaveEvent(hoverEvent);
  184. m_styleHelper.RemoveSelector(Styling::States::Hovered);
  185. update();
  186. }
  187. }