PresetButton.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "PresetButton.h"
  9. PresetButton::PresetButton(const QString& defaultIconPath,
  10. const QString& hoverIconPath,
  11. const QString& selectedIconPath,
  12. const QSize& fixedButtonAndIconSize,
  13. const QString& text,
  14. OnClicked clicked,
  15. QWidget* parent)
  16. : QPushButton(QIcon(defaultIconPath), text, parent)
  17. , m_isHovering(false)
  18. , m_defaultIcon(defaultIconPath)
  19. , m_hoverIcon(hoverIconPath)
  20. , m_selectedIconPath(selectedIconPath)
  21. {
  22. setAttribute(Qt::WA_Hover, true);
  23. setCheckable(true);
  24. setFlat(true);
  25. setFocusPolicy(Qt::StrongFocus);
  26. setFixedSize(fixedButtonAndIconSize);
  27. setIconSize(fixedButtonAndIconSize);
  28. QObject::connect(this, &QPushButton::clicked, this, clicked);
  29. QObject::connect(this,
  30. &QAbstractButton::toggled,
  31. this,
  32. &PresetButton::UpdateIcon);
  33. }
  34. void PresetButton::enterEvent(QEvent* ev)
  35. {
  36. m_isHovering = true;
  37. UpdateIcon(isChecked());
  38. QPushButton::enterEvent(ev);
  39. }
  40. void PresetButton::leaveEvent(QEvent* ev)
  41. {
  42. m_isHovering = false;
  43. UpdateIcon(isChecked());
  44. QPushButton::leaveEvent(ev);
  45. }
  46. void PresetButton::UpdateIcon(bool isChecked)
  47. {
  48. if (isChecked)
  49. {
  50. setIcon(m_selectedIconPath);
  51. }
  52. else
  53. {
  54. if (m_isHovering && isEnabled())
  55. {
  56. setIcon(m_hoverIcon);
  57. }
  58. else
  59. {
  60. setIcon(m_defaultIcon);
  61. }
  62. }
  63. }
  64. #include <moc_PresetButton.cpp>