123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- //
- // Created by Louis on 25-4-1.
- //
- #include "QAntAbstractButton.h"
- QAntAbstractButton::QAntAbstractButton(QWidget *parent) {
- // 接收悬停事件
- setAttribute(Qt::WA_Hover);
- // 设置布局方案
- setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
- }
- QAntAbstractButton::~QAntAbstractButton() = default;
- QString QAntAbstractButton::text() const {
- return m_text;
- }
- void QAntAbstractButton::setText(const QString &text) {
- if (text != m_text) {
- m_text = text;
- update();
- emit textChanged(text);
- }
- }
- QIcon QAntAbstractButton::icon() const {
- return m_icon;
- }
- void QAntAbstractButton::setIcon(const QIcon &icon) {
- if (icon.cacheKey() != m_icon.cacheKey()) {
- m_icon = icon;
- update();
- emit iconChanged(icon);
- }
- }
- QSize QAntAbstractButton::iconSize() const {
- return m_iconSize;
- }
- void QAntAbstractButton::setIconSize(const QSize &size) {
- if (m_iconSize != size) {
- m_iconSize = size;
- updateGeometry();
- update();
- }
- }
- QMargins QAntAbstractButton::padding() const {
- return m_padding;
- }
- void QAntAbstractButton::setPadding(const QMargins &margins) {
- if (m_padding != margins) {
- m_padding = margins;
- updateGeometry();
- update();
- }
- }
- QAntAbstractButton::ButtonState QAntAbstractButton::currentState() const {
- return m_state;
- }
- void QAntAbstractButton::paintText(QPainter &painter, const QRect &textRect) {
- painter.save();
- painter.setPen(palette().color(foregroundRole()));
- painter.setFont(font());
- painter.drawText(textRect, Qt::AlignCenter, m_text);
- painter.restore();
- }
- void QAntAbstractButton::paintIcon(QPainter &painter, const QRect &iconRect) {
- QIcon::Mode mode = QIcon::Normal;
- if (!isEnabled()) {
- mode = QIcon::Disabled;
- }
- else if (m_state == PressedState) {
- mode = QIcon::Selected;
- }
- m_icon.paint(&painter, iconRect, Qt::AlignCenter, mode);
- }
- void QAntAbstractButton::paintFocusIndicator(QPainter &painter) {
- painter.save();
- QPen pen(palette().highlight(), 1, Qt::DotLine);
- painter.setPen(pen);
- painter.setBrush(Qt::NoBrush);
- painter.drawRect(rect().adjusted(1, 1, -1, -1));
- painter.restore();
- }
- void QAntAbstractButton::paintBackground(QPainter &painter) {
- painter.fillRect(rect(), palette().window());
- }
- void QAntAbstractButton::paintBorder(QPainter &painter) {
- painter.save();
- QPen pen(palette().shadow(), 1);
- painter.setPen(pen);
- painter.setBrush(Qt::NoBrush);
- painter.drawRect(rect().adjusted(0, 0, -1, -1));
- painter.restore();
- }
- QRect QAntAbstractButton::calculateTextRect(const QRect &contentsRect) const {
- QFontMetrics fm(font());
- QRect textRect = fm.boundingRect(contentsRect, Qt::AlignCenter, m_text);
- if (!m_icon.isNull()) {
- textRect.moveLeft(contentsRect.left() + m_iconSize.width() + 4);
- }
- return textRect;
- }
- QRect QAntAbstractButton::calculateIconRect(const QRect &contentsRect) const {
- return QRect(contentsRect.left(), contentsRect.center().y() - m_iconSize.height() / 2,
- m_iconSize.width(), m_iconSize.height());
- }
- void QAntAbstractButton::paintEvent(QPaintEvent *event) {
- Q_UNUSED(event);
- QPainter painter(this);
- paintBackground(painter);
- switch (m_state) {
- case NormalState:
- paintNormalState(painter);
- break;
- case HoverState:
- paintHoverState(painter);
- break;
- case PressedState:
- paintPressedState(painter);
- break;
- case DisabledState:
- paintDisabledState(painter);
- break;
- case LoadingState:
- paintLoadingState(painter);
- break;
- }
- paintBorder(painter);
- QRect contentsRect = rect().marginsRemoved(m_padding);
- if (!m_icon.isNull() && m_state != LoadingState) {
- QRect iconRect = calculateIconRect(contentsRect);
- paintText(painter, iconRect);
- }
- if (!m_text.isEmpty() && m_state != LoadingState) {
- QRect textRect = calculateTextRect(contentsRect);
- paintText(painter, textRect);
- }
- if (hasFocus()) {
- paintFocusIndicator(painter);
- }
- }
- void QAntAbstractButton::mousePressEvent(QMouseEvent *event) {
- if (event->button() == Qt::LeftButton && !m_loading && isEnabled()) {
- m_mousePressed = true;
- updateButtonState(PressedState);
- emit pressed();
- event->accept();
- }
- }
- void QAntAbstractButton::mouseReleaseEvent(QMouseEvent *event) {
- if (m_mousePressed && event->button() == Qt::LeftButton) {
- m_mousePressed = false;
- if (rect().contains(event->pos())) {
- emit clicked();
- updateButtonState(HoverState);
- }
- else {
- updateButtonState(NormalState);
- }
- }
- emit release();
- event->accept();
- }
- void QAntAbstractButton::mouseMoveEvent(QMouseEvent *event) {
- if (m_mousePressed) {
- if (rect().contains(event->pos())) {
- updateButtonState(PressedState);
- }
- else {
- QWidget::mouseMoveEvent(event);
- }
- }
- }
- void QAntAbstractButton::enterEvent(QEnterEvent *event) {
- if (isEnabled() && !m_loading && !m_mousePressed) {
- updateButtonState(HoverState);
- }
- QWidget::enterEvent(event);
- }
- void QAntAbstractButton::leaveEvent(QEvent *event) {
- if (isEnabled() && !m_loading) {
- updateButtonState(NormalState);
- }
- QWidget::leaveEvent(event);
- }
- void QAntAbstractButton::focusInEvent(QFocusEvent *event) {
- update();
- QWidget::focusInEvent(event);
- }
- void QAntAbstractButton::focusOutEvent(QFocusEvent *event) {
- update();
- QWidget::focusOutEvent(event);
- }
- void QAntAbstractButton::changeEvent(QEvent *event) {
- if (event->type() == QEvent::EnabledChange) {
- if (isEnabled()) {
- updateButtonState(NormalState);
- }
- else {
- updateButtonState(DisabledState);
- }
- emit enabledChanged(isEnabled());
- }
- QWidget::changeEvent(event);
- }
- void QAntAbstractButton::updateButtonState(ButtonState newState) {
- if (newState == m_state)
- return;
- ButtonState oldState = m_state;
- m_state = newState;
- animateStateChange(oldState, m_state);
- update();
- }
- void QAntAbstractButton::animateStateChange(ButtonState oldState, ButtonState newState) {
- Q_UNUSED(oldState);
- Q_UNUSED(newState);
- }
|