123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- //
- // Created by Louis on 25-4-1.
- //
- #include "QAntPrimaryButton.h"
- QAntPrimaryButton::QAntPrimaryButton(QWidget *parent) : QAntAbstractButton(parent) {
- m_normalColor = QColor("#1677FF");
- m_pressedColor = QColor("#0958D9");
- m_hoverColor = QColor("#4096FF");
- m_disabledColor = QColor("#F5F5F5");
- m_textColor = Qt::white;
- m_textDisabledColor = QColor("#BFBFBF");
- m_xRadius = 8;
- m_yRadius = 8;
- m_borderWidth = 2;
- setAttribute(Qt::WA_TranslucentBackground);
- }
- QAntPrimaryButton::~QAntPrimaryButton() {}
- QColor QAntPrimaryButton::normalColor() const {
- return m_normalColor;
- }
- void QAntPrimaryButton::setNormalColor(const QColor &color) {
- if (m_normalColor != color) {
- m_normalColor = color;
- update();
- }
- }
- QColor QAntPrimaryButton::hoverColor() const {
- return m_hoverColor;
- }
- void QAntPrimaryButton::setHoverColor(const QColor &color) {
- if (m_hoverColor != color) {
- m_hoverColor = color;
- update();
- }
- }
- QColor QAntPrimaryButton::pressedColor() const {
- return m_pressedColor;
- }
- void QAntPrimaryButton::setPressedColor(const QColor &color) {
- if (m_pressedColor != color) {
- m_pressedColor = color;
- update();
- }
- }
- QColor QAntPrimaryButton::disabledColor() const {
- return m_disabledColor;
- }
- void QAntPrimaryButton::setDisabledColor(const QColor &color) {
- if (color != m_disabledColor) {
- m_disabledColor = color;
- update();
- }
- }
- QColor QAntPrimaryButton::textColor() const {
- return m_textColor;
- }
- void QAntPrimaryButton::setTextColor(const QColor &color) {
- if (m_textColor != color) {
- m_textColor = color;
- update();
- }
- }
- QColor QAntPrimaryButton::textDisabledColor() const {
- return m_textDisabledColor;
- }
- void QAntPrimaryButton::setTextDisabledColor(const QColor &color) {
- if (m_textDisabledColor != color) {
- m_textDisabledColor = color;
- update();
- }
- }
- qreal QAntPrimaryButton::xRadius() const {
- return m_xRadius;
- }
- void QAntPrimaryButton::setXRadius(const qreal &x) {
- if (m_xRadius != x) {
- m_xRadius = x;
- update();
- }
- }
- qreal QAntPrimaryButton::yRadius() const {
- return m_yRadius;
- }
- void QAntPrimaryButton::setYRadius(const qreal &y) {
- if (m_yRadius != y) {
- m_yRadius = y;
- update();
- }
- }
- qreal QAntPrimaryButton::borderWidth() const {
- return m_borderWidth;
- }
- void QAntPrimaryButton::setBorderWidth(qreal &w) {
- if (m_borderWidth != w) {
- m_borderWidth = w;
- update();
- }
- }
- QSize QAntPrimaryButton::sizeHint()
- {
- return QSize(200, 100);
- }
- void QAntPrimaryButton::paintNormalState(QPainter &painter) {
- painter.setRenderHint(QPainter::Antialiasing);
- QRectF btnRect = rect();
- QPainterPath path;
- path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
- painter.fillPath(path, m_normalColor);
- }
- void QAntPrimaryButton::paintHoverState(QPainter &painter) {
- painter.setRenderHint(QPainter::Antialiasing);
- QRectF btnRect = rect();
- QPainterPath path;
- path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
- painter.fillPath(path, m_hoverColor);
- }
- void QAntPrimaryButton::paintPressedState(QPainter &painter) {
- painter.setRenderHint(QPainter::Antialiasing);
- QRectF btnRect = rect();
- QPainterPath path;
- path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
- painter.fillPath(path, m_pressedColor);
- }
- void QAntPrimaryButton::paintDisabledState(QPainter &painter) {
- painter.setRenderHint(QPainter::Antialiasing);
- QRectF btnRect = rect();
- QPainterPath path;
- path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
- painter.fillPath(path, m_disabledColor);
- QPen pen(m_textDisabledColor, m_borderWidth);
- pen.setJoinStyle(Qt::MiterJoin);
- painter.setPen(pen);
- painter.setBrush(Qt::NoBrush);
- QPainterPath borderPath;
- QRectF borderRect = btnRect.adjusted(m_borderWidth / 2, m_borderWidth / 2,
- -m_borderWidth / 2, -m_borderWidth / 2);
- borderPath.addRoundedRect(borderRect, m_xRadius, m_yRadius);
- painter.drawPath(borderPath);
- }
- void QAntPrimaryButton::paintLoadingState(QPainter &painter) {}
- void QAntPrimaryButton::paintText(QPainter &painter, const QRect &textRect) {
- painter.save();
- QColor textColor = isEnabled() ? m_textColor : m_textDisabledColor;
- painter.setPen(textColor);
- painter.setFont(font());
- painter.drawText(textRect, Qt::AlignCenter, text());
- painter.restore();
- }
- QColor QAntPrimaryButton::rippleColor() const {
- return m_normalColor.lighter(120); // 使用主题色的亮版本作为光晕颜色
- }
|