123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- //
- // Created by Louis on 25-4-1.
- //
- #ifndef QANTABSTRACTBUTTON_H
- #define QANTABSTRACTBUTTON_H
- #include <qwidget.h>
- #include <qpainter.h>
- #include <qicon.h>
- #include <qtimer.h>
- #include <qevent.h>
- #include <qfontmetrics.h>
- #include <QPropertyAnimation>
- #include <QGraphicsDropShadowEffect>
- #include <QParallelAnimationGroup>
- class QAntAbstractButton : public QWidget {
- Q_OBJECT
- public:
- enum ButtonState {
- NormalState,
- HoverState,
- PressedState,
- DisabledState,
- LoadingState
- };
- Q_ENUM(ButtonState)
- explicit QAntAbstractButton(QWidget* parent = nullptr);
- virtual ~QAntAbstractButton();
- QString text() const;
- void setText(const QString& text);
- QIcon icon() const;
- void setIcon(const QIcon& icon);
- QSize iconSize() const;
- void setIconSize(const QSize& size);
- QMargins padding() const;
- void setPadding(const QMargins& margins);
- ButtonState currentState() const;
- signals:
- void clicked();
- void pressed();
- void release();
- void textChanged(const QString& text);
- void iconChanged(const QIcon& icon);
- void enabledChanged(bool enabled);
- void loadingChanged(bool loading);
- protected:
- virtual void paintNormalState(QPainter& painter) = 0;
- virtual void paintHoverState(QPainter& painter) = 0;
- virtual void paintPressedState(QPainter& painter) = 0;
- virtual void paintDisabledState(QPainter& painter) = 0;
- virtual void paintLoadingState(QPainter& painter) = 0;
- virtual void paintText(QPainter& painter, const QRect& textRect);
- virtual void paintIcon(QPainter& painter, const QRect& iconRect);
- virtual void paintFocusIndicator(QPainter& painter);
- virtual void paintBackground(QPainter& painter);
- virtual void paintBorder(QPainter &painter);
- virtual QRect calculateTextRect(const QRect& contentsRect) const;
- virtual QRect calculateIconRect(const QRect& contentsRect) const;
- void paintEvent(QPaintEvent* event) override;
- void mousePressEvent(QMouseEvent *event) override;
- void mouseReleaseEvent(QMouseEvent *event) override;
- void mouseMoveEvent(QMouseEvent *event) override;
- void enterEvent(QEnterEvent *event) override;
- void leaveEvent(QEvent *event) override;
- void focusInEvent(QFocusEvent *event) override;
- void focusOutEvent(QFocusEvent *event) override;
- void changeEvent(QEvent *) override;
- void updateButtonState(ButtonState newState);
- virtual QColor rippleColor() const = 0;
- void startRippleEffect();
- private:
- QString m_text;
- QIcon m_icon;
- QSize m_iconSize = QSize(16, 16);
- QMargins m_padding = QMargins(8, 4, 8, 4);
- ButtonState m_state = NormalState;
- bool m_loading = false;
- bool m_mousePressed = false;
- bool m_hasFocus = false;
- QParallelAnimationGroup* m_animationGroup = nullptr;
- QGraphicsDropShadowEffect* m_shadowEffect = nullptr;
- };
- #endif //QANTABSTRACTBUTTON_H
|