switchbutton.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef SWITCHBUTTON_H
  2. #define SWITCHBUTTON_H
  3. #include <QWidget>
  4. #include <QLabel>
  5. #include <QPropertyAnimation>
  6. class SwitchButton : public QWidget
  7. {
  8. Q_OBJECT
  9. Q_DISABLE_COPY(SwitchButton)
  10. public:
  11. enum Style
  12. {
  13. YESNO,
  14. ONOFF,
  15. BOOL,
  16. EMPTY
  17. };
  18. public:
  19. explicit SwitchButton(QWidget* parent = nullptr, Style style = Style::ONOFF);
  20. ~SwitchButton() override;
  21. //-- QWidget methods
  22. void mousePressEvent(QMouseEvent *) override;
  23. void paintEvent(QPaintEvent* event) override;
  24. void setEnabled(bool);
  25. //-- Setters
  26. void setDuration(int);
  27. void setValue(bool);
  28. //-- Getters
  29. bool value() const;
  30. signals:
  31. void valueChanged(bool newvalue);
  32. private:
  33. class SwitchCircle;
  34. class SwitchBackground;
  35. void _update();
  36. private:
  37. bool _value;
  38. int _duration;
  39. QLinearGradient _lg;
  40. QLinearGradient _lg2;
  41. QLinearGradient _lg_disabled;
  42. QColor _pencolor;
  43. QColor _offcolor;
  44. QColor _oncolor;
  45. int _tol;
  46. int _borderradius;
  47. // This order for definition is important (these widgets overlap)
  48. QLabel* _labeloff;
  49. SwitchBackground* _background;
  50. QLabel* _labelon;
  51. SwitchCircle* _circle;
  52. bool _enabled;
  53. QPropertyAnimation* __btn_move;
  54. QPropertyAnimation* __back_move;
  55. };
  56. class SwitchButton::SwitchBackground : public QWidget
  57. {
  58. Q_OBJECT
  59. Q_DISABLE_COPY(SwitchBackground)
  60. public:
  61. explicit SwitchBackground(QWidget* parent = nullptr, QColor color = QColor(154, 205, 50), bool rect = false);
  62. ~SwitchBackground() override;
  63. //-- QWidget methods
  64. void paintEvent(QPaintEvent* event) override;
  65. void setEnabled(bool);
  66. private:
  67. bool _rect;
  68. int _borderradius;
  69. QColor _color;
  70. QColor _pencolor;
  71. QLinearGradient _lg;
  72. QLinearGradient _lg_disabled;
  73. bool _enabled;
  74. };
  75. class SwitchButton::SwitchCircle : public QWidget
  76. {
  77. Q_OBJECT
  78. Q_DISABLE_COPY(SwitchCircle)
  79. public:
  80. explicit SwitchCircle(QWidget* parent = nullptr, QColor color = QColor(255, 255, 255), bool rect = false);
  81. ~SwitchCircle() override;
  82. //-- QWidget methods
  83. void paintEvent(QPaintEvent* event) override;
  84. void setEnabled(bool);
  85. private:
  86. bool _rect;
  87. int _borderradius;
  88. QColor _color;
  89. QColor _pencolor;
  90. QRadialGradient _rg;
  91. QLinearGradient _lg;
  92. QLinearGradient _lg_disabled;
  93. bool _enabled;
  94. };
  95. #endif // SWITCHBUTTON_H