ToggleSwitch.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /******************************************************************************
  2. Copyright (C) 2023 by Dennis Sädtler <[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. #pragma once
  15. #include <Idian/Utils.hpp>
  16. #include <QAbstractButton>
  17. #include <QAccessibleWidget>
  18. #include <QBrush>
  19. #include <QEvent>
  20. #include <QMouseEvent>
  21. #include <QPainter>
  22. #include <QPropertyAnimation>
  23. #include <QStyleOptionButton>
  24. #include <QWidget>
  25. namespace idian {
  26. class ToggleSwitch : public QAbstractButton, public Utils {
  27. Q_OBJECT
  28. Q_PROPERTY(int xpos MEMBER xPos WRITE setPos)
  29. Q_PROPERTY(QColor background MEMBER backgroundInactive DESIGNABLE true)
  30. Q_PROPERTY(QColor background_hover MEMBER backgroundInactiveHover DESIGNABLE true)
  31. Q_PROPERTY(QColor background_checked MEMBER backgroundActive DESIGNABLE true)
  32. Q_PROPERTY(QColor background_checked_hover MEMBER backgroundActiveHover DESIGNABLE true)
  33. Q_PROPERTY(QColor handleColor MEMBER handleColor DESIGNABLE true)
  34. Q_PROPERTY(int handleSize MEMBER handleSize DESIGNABLE true)
  35. Q_PROPERTY(float blend MEMBER blend WRITE setBlend DESIGNABLE false)
  36. public:
  37. ToggleSwitch(QWidget *parent = nullptr);
  38. ToggleSwitch(bool defaultState, QWidget *parent = nullptr);
  39. QSize sizeHint() const override;
  40. void setPos(int x)
  41. {
  42. xPos = x;
  43. update();
  44. }
  45. void setBlend(float newBlend)
  46. {
  47. blend = newBlend;
  48. update();
  49. }
  50. void setDelayed(bool state);
  51. bool isDelayed() { return delayed; }
  52. void setStatus(bool status);
  53. void setPending(bool pending);
  54. public slots:
  55. void click();
  56. signals:
  57. void pendingChecked();
  58. void pendingUnchecked();
  59. protected:
  60. void changeEvent(QEvent *event) override;
  61. void paintEvent(QPaintEvent *) override;
  62. void showEvent(QShowEvent *) override;
  63. void enterEvent(QEnterEvent *) override;
  64. void leaveEvent(QEvent *) override;
  65. void keyReleaseEvent(QKeyEvent *) override;
  66. void mouseReleaseEvent(QMouseEvent *) override;
  67. void focusInEvent(QFocusEvent *e) override
  68. {
  69. Utils::showKeyFocused(e);
  70. QAbstractButton::focusInEvent(e);
  71. }
  72. void focusOutEvent(QFocusEvent *e) override
  73. {
  74. Utils::hideKeyFocused(e);
  75. QAbstractButton::focusOutEvent(e);
  76. }
  77. private slots:
  78. void onClicked(bool checked);
  79. private:
  80. int xPos;
  81. int onPos;
  82. int offPos;
  83. int margin;
  84. float blend = 0.0f;
  85. bool delayed = false;
  86. bool pendingStatus = false;
  87. void animateHandlePosition();
  88. void updateBackgroundColor();
  89. QColor backgroundInactive;
  90. QColor backgroundInactiveHover;
  91. QColor backgroundActive;
  92. QColor backgroundActiveHover;
  93. QColor handleColor;
  94. int handleSize = 18;
  95. QPropertyAnimation *animHandle = nullptr;
  96. QPropertyAnimation *animBgColor = nullptr;
  97. };
  98. } // namespace idian