MenuCheckBox.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /******************************************************************************
  2. Copyright (C) 2025 by Taylor Giampaolo <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "MenuCheckBox.hpp"
  15. #include <QMenu>
  16. #include <QMouseEvent>
  17. #include <QStyleOptionButton>
  18. #include <QStylePainter>
  19. MenuCheckBox::MenuCheckBox(const QString &text, QWidget *parent) : QCheckBox(text, parent)
  20. {
  21. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  22. setContentsMargins(0, 0, 0, 0);
  23. if (auto menu = qobject_cast<QMenu *>(parent)) {
  24. connect(menu, &QMenu::hovered, this, [this, menu](QAction *action) {
  25. if (action != menuAction) {
  26. setHovered(false);
  27. update();
  28. }
  29. });
  30. connect(menu, &QMenu::aboutToHide, this, [this]() { setHovered(false); });
  31. }
  32. }
  33. void MenuCheckBox::setAction(QAction *action)
  34. {
  35. menuAction = action;
  36. }
  37. void MenuCheckBox::focusInEvent(QFocusEvent *)
  38. {
  39. setHovered(true);
  40. }
  41. void MenuCheckBox::mousePressEvent(QMouseEvent *event)
  42. {
  43. if (event->button() == Qt::LeftButton) {
  44. mousePressInside = true;
  45. event->accept();
  46. } else {
  47. QCheckBox::mousePressEvent(event);
  48. }
  49. }
  50. void MenuCheckBox::mouseMoveEvent(QMouseEvent *event)
  51. {
  52. if (!rect().contains(event->pos())) {
  53. mousePressInside = false;
  54. }
  55. event->accept();
  56. }
  57. void MenuCheckBox::mouseReleaseEvent(QMouseEvent *event)
  58. {
  59. if (event->button() == Qt::LeftButton) {
  60. if (mousePressInside && rect().contains(event->pos())) {
  61. toggle();
  62. }
  63. event->accept();
  64. } else {
  65. QCheckBox::mouseReleaseEvent(event);
  66. }
  67. mousePressInside = false;
  68. }
  69. void MenuCheckBox::enterEvent(QEnterEvent *)
  70. {
  71. setHovered(true);
  72. update();
  73. }
  74. void MenuCheckBox::leaveEvent(QEvent *)
  75. {
  76. setHovered(false);
  77. update();
  78. }
  79. void MenuCheckBox::paintEvent(QPaintEvent *)
  80. {
  81. QStylePainter p(this);
  82. QStyleOptionButton opt;
  83. initStyleOption(&opt);
  84. if (isHovered) {
  85. opt.state |= QStyle::State_MouseOver;
  86. opt.state |= QStyle::State_Selected;
  87. }
  88. p.drawControl(QStyle::CE_CheckBox, opt);
  89. }
  90. void MenuCheckBox::setHovered(bool hovered)
  91. {
  92. isHovered = hovered;
  93. }