QAntPrimaryButton.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Created by Louis on 25-4-1.
  3. //
  4. #include "QAntPrimaryButton.h"
  5. QAntPrimaryButton::QAntPrimaryButton(QWidget *parent) : QAntAbstractButton(parent) {
  6. m_normalColor = QColor("#1677FF");
  7. m_pressedColor = QColor("#0958D9");
  8. m_hoverColor = QColor("#4096FF");
  9. m_disabledColor = QColor("#F5F5F5");
  10. m_textColor = Qt::white;
  11. m_textDisabledColor = QColor("#BFBFBF");
  12. m_xRadius = 8;
  13. m_yRadius = 8;
  14. m_borderWidth = 2;
  15. setAttribute(Qt::WA_TranslucentBackground);
  16. }
  17. QAntPrimaryButton::~QAntPrimaryButton() {}
  18. QColor QAntPrimaryButton::normalColor() const {
  19. return m_normalColor;
  20. }
  21. void QAntPrimaryButton::setNormalColor(const QColor &color) {
  22. if (m_normalColor != color) {
  23. m_normalColor = color;
  24. update();
  25. }
  26. }
  27. QColor QAntPrimaryButton::hoverColor() const {
  28. return m_hoverColor;
  29. }
  30. void QAntPrimaryButton::setHoverColor(const QColor &color) {
  31. if (m_hoverColor != color) {
  32. m_hoverColor = color;
  33. update();
  34. }
  35. }
  36. QColor QAntPrimaryButton::pressedColor() const {
  37. return m_pressedColor;
  38. }
  39. void QAntPrimaryButton::setPressedColor(const QColor &color) {
  40. if (m_pressedColor != color) {
  41. m_pressedColor = color;
  42. update();
  43. }
  44. }
  45. QColor QAntPrimaryButton::disabledColor() const {
  46. return m_disabledColor;
  47. }
  48. void QAntPrimaryButton::setDisabledColor(const QColor &color) {
  49. if (color != m_disabledColor) {
  50. m_disabledColor = color;
  51. update();
  52. }
  53. }
  54. QColor QAntPrimaryButton::textColor() const {
  55. return m_textColor;
  56. }
  57. void QAntPrimaryButton::setTextColor(const QColor &color) {
  58. if (m_textColor != color) {
  59. m_textColor = color;
  60. update();
  61. }
  62. }
  63. QColor QAntPrimaryButton::textDisabledColor() const {
  64. return m_textDisabledColor;
  65. }
  66. void QAntPrimaryButton::setTextDisabledColor(const QColor &color) {
  67. if (m_textDisabledColor != color) {
  68. m_textDisabledColor = color;
  69. update();
  70. }
  71. }
  72. qreal QAntPrimaryButton::xRadius() const {
  73. return m_xRadius;
  74. }
  75. void QAntPrimaryButton::setXRadius(const qreal &x) {
  76. if (m_xRadius != x) {
  77. m_xRadius = x;
  78. update();
  79. }
  80. }
  81. qreal QAntPrimaryButton::yRadius() const {
  82. return m_yRadius;
  83. }
  84. void QAntPrimaryButton::setYRadius(const qreal &y) {
  85. if (m_yRadius != y) {
  86. m_yRadius = y;
  87. update();
  88. }
  89. }
  90. qreal QAntPrimaryButton::borderWidth() const {
  91. return m_borderWidth;
  92. }
  93. void QAntPrimaryButton::setBorderWidth(qreal &w) {
  94. if (m_borderWidth != w) {
  95. m_borderWidth = w;
  96. update();
  97. }
  98. }
  99. QSize QAntPrimaryButton::sizeHint()
  100. {
  101. return QSize(200, 100);
  102. }
  103. void QAntPrimaryButton::paintNormalState(QPainter &painter) {
  104. painter.setRenderHint(QPainter::Antialiasing);
  105. QRectF btnRect = rect();
  106. QPainterPath path;
  107. path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
  108. painter.fillPath(path, m_normalColor);
  109. }
  110. void QAntPrimaryButton::paintHoverState(QPainter &painter) {
  111. painter.setRenderHint(QPainter::Antialiasing);
  112. QRectF btnRect = rect();
  113. QPainterPath path;
  114. path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
  115. painter.fillPath(path, m_hoverColor);
  116. }
  117. void QAntPrimaryButton::paintPressedState(QPainter &painter) {
  118. painter.setRenderHint(QPainter::Antialiasing);
  119. QRectF btnRect = rect();
  120. QPainterPath path;
  121. path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
  122. painter.fillPath(path, m_pressedColor);
  123. }
  124. void QAntPrimaryButton::paintDisabledState(QPainter &painter) {
  125. painter.setRenderHint(QPainter::Antialiasing);
  126. QRectF btnRect = rect();
  127. QPainterPath path;
  128. path.addRoundedRect(btnRect, m_xRadius, m_yRadius);
  129. painter.fillPath(path, m_disabledColor);
  130. QPen pen(m_textDisabledColor, m_borderWidth);
  131. pen.setJoinStyle(Qt::MiterJoin);
  132. painter.setPen(pen);
  133. painter.setBrush(Qt::NoBrush);
  134. QPainterPath borderPath;
  135. QRectF borderRect = btnRect.adjusted(m_borderWidth / 2, m_borderWidth / 2,
  136. -m_borderWidth / 2, -m_borderWidth / 2);
  137. borderPath.addRoundedRect(borderRect, m_xRadius, m_yRadius);
  138. painter.drawPath(borderPath);
  139. }
  140. void QAntPrimaryButton::paintLoadingState(QPainter &painter) {}
  141. void QAntPrimaryButton::paintText(QPainter &painter, const QRect &textRect) {
  142. painter.save();
  143. QColor textColor = isEnabled() ? m_textColor : m_textDisabledColor;
  144. painter.setPen(textColor);
  145. painter.setFont(font());
  146. painter.drawText(textRect, Qt::AlignCenter, text());
  147. painter.restore();
  148. }
  149. QColor QAntPrimaryButton::rippleColor() const {
  150. return m_normalColor.lighter(120); // 使用主题色的亮版本作为光晕颜色
  151. }