ToggleCheckbox.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <ToggleCheckbox.h>
  9. #include <QPaintEvent>
  10. #include <QPainter>
  11. namespace O3DE::ProjectManager
  12. {
  13. static QSize s_toggleButtonSize = QSize(32, 16);
  14. static int s_toggleButtonBorderRadius = 8;
  15. static int s_toggleButtonCircleRadius = 6;
  16. static QColor s_toggleButtonEnabledColor = "#1E70EB";
  17. static QColor s_toggleButtonDisabledColor = "#3C4D65";
  18. static QColor s_toggleButtonCircleColor = "#FFFFFF";
  19. static QColor s_toggleButtonDisabledCircleColor = "#AAAAAA";
  20. ToggleCheckbox::ToggleCheckbox(QWidget* parent)
  21. : QCheckBox(parent)
  22. {
  23. setMinimumSize(s_toggleButtonSize);
  24. }
  25. void ToggleCheckbox::paintEvent(QPaintEvent* e)
  26. {
  27. QPainter painter(this);
  28. painter.setRenderHint(QPainter::Antialiasing);
  29. QPoint circleCenter;
  30. QRect toggleRect(e->rect().topLeft(), s_toggleButtonSize);
  31. QColor primaryColor, secondaryColor;
  32. if (isEnabled())
  33. {
  34. primaryColor = s_toggleButtonEnabledColor;
  35. secondaryColor = s_toggleButtonCircleColor;
  36. }
  37. else
  38. {
  39. primaryColor = s_toggleButtonDisabledColor;
  40. secondaryColor = s_toggleButtonDisabledCircleColor;
  41. }
  42. if (isChecked())
  43. {
  44. painter.setBrush(primaryColor);
  45. painter.setPen(primaryColor);
  46. circleCenter = toggleRect.center() + QPoint(toggleRect.width() / 2 - s_toggleButtonBorderRadius + 1, 1);
  47. }
  48. else
  49. {
  50. painter.setPen(secondaryColor);
  51. circleCenter = toggleRect.center() + QPoint(-toggleRect.width() / 2 + s_toggleButtonBorderRadius + 1, 1);
  52. }
  53. // Rounded rect
  54. painter.drawRoundedRect(toggleRect, s_toggleButtonBorderRadius, s_toggleButtonBorderRadius);
  55. // Circle
  56. painter.setBrush(secondaryColor);
  57. painter.drawEllipse(circleCenter, s_toggleButtonCircleRadius, s_toggleButtonCircleRadius);
  58. }
  59. bool ToggleCheckbox::hitButton(const QPoint& pos) const
  60. {
  61. Q_UNUSED(pos);
  62. return true;
  63. }
  64. } // namespace O3DE::ProjectManager