QAntAbstractButton.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Created by Louis on 25-4-1.
  3. //
  4. #ifndef QANTABSTRACTBUTTON_H
  5. #define QANTABSTRACTBUTTON_H
  6. #include <qwidget.h>
  7. #include <qpainter.h>
  8. #include <qicon.h>
  9. #include <qtimer.h>
  10. #include <qevent.h>
  11. #include <qfontmetrics.h>
  12. #include <QPropertyAnimation>
  13. #include <QGraphicsDropShadowEffect>
  14. #include <QParallelAnimationGroup>
  15. class QAntAbstractButton : public QWidget {
  16. Q_OBJECT
  17. public:
  18. enum ButtonState {
  19. NormalState,
  20. HoverState,
  21. PressedState,
  22. DisabledState,
  23. LoadingState
  24. };
  25. Q_ENUM(ButtonState)
  26. explicit QAntAbstractButton(QWidget* parent = nullptr);
  27. virtual ~QAntAbstractButton();
  28. QString text() const;
  29. void setText(const QString& text);
  30. QIcon icon() const;
  31. void setIcon(const QIcon& icon);
  32. QSize iconSize() const;
  33. void setIconSize(const QSize& size);
  34. QMargins padding() const;
  35. void setPadding(const QMargins& margins);
  36. ButtonState currentState() const;
  37. signals:
  38. void clicked();
  39. void pressed();
  40. void release();
  41. void textChanged(const QString& text);
  42. void iconChanged(const QIcon& icon);
  43. void enabledChanged(bool enabled);
  44. void loadingChanged(bool loading);
  45. protected:
  46. virtual void paintNormalState(QPainter& painter) = 0;
  47. virtual void paintHoverState(QPainter& painter) = 0;
  48. virtual void paintPressedState(QPainter& painter) = 0;
  49. virtual void paintDisabledState(QPainter& painter) = 0;
  50. virtual void paintLoadingState(QPainter& painter) = 0;
  51. virtual void paintText(QPainter& painter, const QRect& textRect);
  52. virtual void paintIcon(QPainter& painter, const QRect& iconRect);
  53. virtual void paintFocusIndicator(QPainter& painter);
  54. virtual void paintBackground(QPainter& painter);
  55. virtual void paintBorder(QPainter &painter);
  56. virtual QRect calculateTextRect(const QRect& contentsRect) const;
  57. virtual QRect calculateIconRect(const QRect& contentsRect) const;
  58. void paintEvent(QPaintEvent* event) override;
  59. void mousePressEvent(QMouseEvent *event) override;
  60. void mouseReleaseEvent(QMouseEvent *event) override;
  61. void mouseMoveEvent(QMouseEvent *event) override;
  62. void enterEvent(QEnterEvent *event) override;
  63. void leaveEvent(QEvent *event) override;
  64. void focusInEvent(QFocusEvent *event) override;
  65. void focusOutEvent(QFocusEvent *event) override;
  66. void changeEvent(QEvent *) override;
  67. void updateButtonState(ButtonState newState);
  68. virtual QColor rippleColor() const = 0;
  69. void startRippleEffect();
  70. private:
  71. QString m_text;
  72. QIcon m_icon;
  73. QSize m_iconSize = QSize(16, 16);
  74. QMargins m_padding = QMargins(8, 4, 8, 4);
  75. ButtonState m_state = NormalState;
  76. bool m_loading = false;
  77. bool m_mousePressed = false;
  78. bool m_hasFocus = false;
  79. QParallelAnimationGroup* m_animationGroup = nullptr;
  80. QGraphicsDropShadowEffect* m_shadowEffect = nullptr;
  81. };
  82. #endif //QANTABSTRACTBUTTON_H