powerbutton.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "powerbutton.h"
  2. #include <QEvent>
  3. #include <QFontMetrics>
  4. #include <QLinearGradient>
  5. #include <QPainter>
  6. #include <QPainterPath>
  7. #include <QPixmap>
  8. #include <QPaintEvent>
  9. PowerButton::PowerButton(QWidget *parent)
  10. : QAbstractButton(parent)
  11. {
  12. setCursor(Qt::PointingHandCursor);
  13. setFocusPolicy(Qt::NoFocus); // 键盘焦点交给页面里的输入框,按钮不抢
  14. setAttribute(Qt::WA_Hover, true); // 保证 enterEvent/leaveEvent 可靠触发
  15. setText(QStringLiteral("开始测试")); // .ui 里没写 text,构造时给默认文案
  16. // 不挂 tooltip:按钮下方本来就写着「开始测试 / 执行中」,再弹一个气泡只是多余的一层浮框
  17. }
  18. QSize PowerButton::sizeHint() const
  19. {
  20. return QSize(140, 42);
  21. }
  22. QSize PowerButton::minimumSizeHint() const
  23. {
  24. return QSize(110, 36);
  25. }
  26. void PowerButton::setRunning(bool running)
  27. {
  28. if (m_running == running)
  29. return;
  30. m_running = running;
  31. setCursor(Qt::PointingHandCursor);
  32. update();
  33. }
  34. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  35. void PowerButton::enterEvent(QEnterEvent *event)
  36. #else
  37. void PowerButton::enterEvent(QEvent *event)
  38. #endif
  39. {
  40. QAbstractButton::enterEvent(event);
  41. update();
  42. }
  43. void PowerButton::leaveEvent(QEvent *event)
  44. {
  45. QAbstractButton::leaveEvent(event);
  46. update();
  47. }
  48. void PowerButton::paintEvent(QPaintEvent *)
  49. {
  50. QPainter p(this);
  51. p.setRenderHint(QPainter::Antialiasing, true);
  52. // 实心蓝底圆角按钮:白色图标在左、白色粗体文案在右,整组居中。
  53. const QRectF box = QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5);
  54. const qreal radius = 8.0;
  55. QColor top, bottom, border;
  56. if (!isEnabled()) {
  57. top = QColor("#c9d3de"); bottom = QColor("#b7c3d0"); border = QColor("#aab6c4");
  58. } else if (m_running) {
  59. // 运行中「重新开始」:绿色实心 + 悬停/按下态反馈(可随时点击重跑)
  60. if (isDown()) {
  61. top = QColor("#217c4c"); bottom = QColor("#175d37"); border = QColor("#145230");
  62. } else if (underMouse()) {
  63. top = QColor("#38ae6f"); bottom = QColor("#258a54"); border = QColor("#1e7647");
  64. } else {
  65. top = QColor("#2f9e63"); bottom = QColor("#1f7a4a"); border = QColor("#1a6b41");
  66. }
  67. } else if (isDown()) {
  68. top = QColor("#1b4190"); bottom = QColor("#163a83"); border = QColor("#14357a");
  69. } else if (underMouse()) {
  70. top = QColor("#3a6cbd"); bottom = QColor("#2a58a8"); border = QColor("#25509c");
  71. } else {
  72. top = QColor("#2f5fae"); bottom = QColor("#204a9c"); border = QColor("#1d4392");
  73. }
  74. QLinearGradient g(box.topLeft(), box.bottomLeft());
  75. g.setColorAt(0, top);
  76. g.setColorAt(1, bottom);
  77. p.setPen(QPen(border, 1));
  78. p.setBrush(g);
  79. p.drawRoundedRect(box, radius, radius);
  80. // 文案:白色粗体
  81. QFont f = font();
  82. f.setPixelSize(17);
  83. f.setBold(true);
  84. // 图标 + 文字整体居中:先量总宽再算起点,换文案(执行中 3 个字)也不会偏
  85. const qreal iconSide = qMin<qreal>(box.height() - 10.0, 22.0);
  86. const qreal gap = 7.0;
  87. const QFontMetricsF fm(f);
  88. const qreal contentW = iconSide + gap + fm.horizontalAdvance(text());
  89. qreal startX = box.left() + (box.width() - contentW) / 2.0;
  90. if (startX < box.left() + 8.0)
  91. startX = box.left() + 8.0;
  92. const QRectF iconRect(startX, box.center().y() - iconSide / 2.0, iconSide, iconSide);
  93. // 图标按状态取:待机=圆圈播放键,执行中=圆圈实心点(都是白色描边,蓝/绿底上都清楚)。
  94. // 两张图由设计图裁出并转成白色+alpha,放在 :/assets/ 下;缺失时退回矢量电源符号。
  95. static const QPixmap kStartIcon(QStringLiteral(":/assets/btn-start.png"));
  96. static const QPixmap kRunIcon(QStringLiteral(":/assets/btn-run.png"));
  97. const QPixmap &stateIcon = m_running ? kRunIcon : kStartIcon;
  98. if (!stateIcon.isNull()) {
  99. const qreal dpr = devicePixelRatioF();
  100. const int px = qMax(1, qRound(iconSide * dpr));
  101. QPixmap scaled = stateIcon.scaled(px, px, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  102. scaled.setDevicePixelRatio(dpr);
  103. p.drawPixmap(QPointF(iconRect.center().x() - scaled.width() / dpr / 2.0,
  104. iconRect.center().y() - scaled.height() / dpr / 2.0), scaled);
  105. } else {
  106. paintPowerGlyph(&p, iconRect);
  107. }
  108. p.setFont(f);
  109. p.setPen(isEnabled() ? QColor(Qt::white) : QColor("#f4f7fa"));
  110. const QRectF textRect(iconRect.right() + gap, box.top(),
  111. box.right() - iconRect.right() - gap, box.height());
  112. p.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text());
  113. }
  114. void PowerButton::paintPowerGlyph(QPainter *p, const QRectF &circle) const
  115. {
  116. const qreal d = circle.width();
  117. const qreal inset = d * 0.24;
  118. const QRectF ring = circle.adjusted(inset, inset, -inset, -inset);
  119. const QColor glyph = isEnabled() ? QColor(Qt::white) : QColor("#f2f6fa");
  120. p->setBrush(Qt::NoBrush);
  121. p->setPen(QPen(glyph, qMax(1.9, d * 0.10), Qt::SolidLine, Qt::RoundCap));
  122. // 从 120° 起画 300°,顶部留 60° 缺口给竖杠
  123. p->drawArc(ring, 120 * 16, 300 * 16);
  124. const QPointF c = circle.center();
  125. p->drawLine(QPointF(c.x(), ring.top() - d * 0.06), QPointF(c.x(), c.y() - d * 0.05));
  126. }