powerbutton.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // 执行中不当按钮用:光标回到箭头,避免「还能点」的暗示;
  32. // 是否响应点击由 hitButton() 一并拦掉。
  33. setCursor(running ? Qt::ArrowCursor : Qt::PointingHandCursor);
  34. update();
  35. }
  36. bool PowerButton::hitButton(const QPoint &pos) const
  37. {
  38. // 执行中完全点不动:按下既不进入按下态,也不会发出 clicked()。
  39. // 这一轮要重跑,等它自己跑完(或先在别处停掉)再点。
  40. if (m_running)
  41. return false;
  42. return QAbstractButton::hitButton(pos);
  43. }
  44. void PowerButton::enterEvent(QEvent *event)
  45. {
  46. QAbstractButton::enterEvent(event);
  47. update();
  48. }
  49. void PowerButton::leaveEvent(QEvent *event)
  50. {
  51. QAbstractButton::leaveEvent(event);
  52. update();
  53. }
  54. void PowerButton::paintEvent(QPaintEvent *)
  55. {
  56. QPainter p(this);
  57. p.setRenderHint(QPainter::Antialiasing, true);
  58. // 实心蓝底圆角按钮:白色图标在左、白色粗体文案在右,整组居中。
  59. const QRectF box = QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5);
  60. const qreal radius = 8.0;
  61. QColor top, bottom, border;
  62. if (!isEnabled()) {
  63. top = QColor("#c9d3de"); bottom = QColor("#b7c3d0"); border = QColor("#aab6c4");
  64. } else if (m_running) {
  65. // 执行中:绿色实心 + 白字(不可点,只当状态灯)
  66. top = QColor("#2f9e63"); bottom = QColor("#1f7a4a"); border = QColor("#1a6b41");
  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. }