#include "powerbutton.h" #include #include #include #include #include #include #include PowerButton::PowerButton(QWidget *parent) : QAbstractButton(parent) { setCursor(Qt::PointingHandCursor); setFocusPolicy(Qt::NoFocus); // 键盘焦点交给页面里的输入框,按钮不抢 setAttribute(Qt::WA_Hover, true); // 保证 enterEvent/leaveEvent 可靠触发 setText(QStringLiteral("开始测试")); // .ui 里没写 text,构造时给默认文案 // 不挂 tooltip:按钮下方本来就写着「开始测试 / 执行中」,再弹一个气泡只是多余的一层浮框 } QSize PowerButton::sizeHint() const { return QSize(140, 42); } QSize PowerButton::minimumSizeHint() const { return QSize(110, 36); } void PowerButton::setRunning(bool running) { if (m_running == running) return; m_running = running; // 执行中不当按钮用:光标回到箭头,避免「还能点」的暗示; // 是否响应点击由 hitButton() 一并拦掉。 setCursor(running ? Qt::ArrowCursor : Qt::PointingHandCursor); update(); } bool PowerButton::hitButton(const QPoint &pos) const { // 执行中完全点不动:按下既不进入按下态,也不会发出 clicked()。 // 这一轮要重跑,等它自己跑完(或先在别处停掉)再点。 if (m_running) return false; return QAbstractButton::hitButton(pos); } void PowerButton::enterEvent(QEvent *event) { QAbstractButton::enterEvent(event); update(); } void PowerButton::leaveEvent(QEvent *event) { QAbstractButton::leaveEvent(event); update(); } void PowerButton::paintEvent(QPaintEvent *) { QPainter p(this); p.setRenderHint(QPainter::Antialiasing, true); // 实心蓝底圆角按钮:白色图标在左、白色粗体文案在右,整组居中。 const QRectF box = QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5); const qreal radius = 8.0; QColor top, bottom, border; if (!isEnabled()) { top = QColor("#c9d3de"); bottom = QColor("#b7c3d0"); border = QColor("#aab6c4"); } else if (m_running) { // 执行中:绿色实心 + 白字(不可点,只当状态灯) top = QColor("#2f9e63"); bottom = QColor("#1f7a4a"); border = QColor("#1a6b41"); } else if (isDown()) { top = QColor("#1b4190"); bottom = QColor("#163a83"); border = QColor("#14357a"); } else if (underMouse()) { top = QColor("#3a6cbd"); bottom = QColor("#2a58a8"); border = QColor("#25509c"); } else { top = QColor("#2f5fae"); bottom = QColor("#204a9c"); border = QColor("#1d4392"); } QLinearGradient g(box.topLeft(), box.bottomLeft()); g.setColorAt(0, top); g.setColorAt(1, bottom); p.setPen(QPen(border, 1)); p.setBrush(g); p.drawRoundedRect(box, radius, radius); // 文案:白色粗体 QFont f = font(); f.setPixelSize(17); f.setBold(true); // 图标 + 文字整体居中:先量总宽再算起点,换文案(执行中 3 个字)也不会偏 const qreal iconSide = qMin(box.height() - 10.0, 22.0); const qreal gap = 7.0; const QFontMetricsF fm(f); const qreal contentW = iconSide + gap + fm.horizontalAdvance(text()); qreal startX = box.left() + (box.width() - contentW) / 2.0; if (startX < box.left() + 8.0) startX = box.left() + 8.0; const QRectF iconRect(startX, box.center().y() - iconSide / 2.0, iconSide, iconSide); // 图标按状态取:待机=圆圈播放键,执行中=圆圈实心点(都是白色描边,蓝/绿底上都清楚)。 // 两张图由设计图裁出并转成白色+alpha,放在 :/assets/ 下;缺失时退回矢量电源符号。 static const QPixmap kStartIcon(QStringLiteral(":/assets/btn-start.png")); static const QPixmap kRunIcon(QStringLiteral(":/assets/btn-run.png")); const QPixmap &stateIcon = m_running ? kRunIcon : kStartIcon; if (!stateIcon.isNull()) { const qreal dpr = devicePixelRatioF(); const int px = qMax(1, qRound(iconSide * dpr)); QPixmap scaled = stateIcon.scaled(px, px, Qt::KeepAspectRatio, Qt::SmoothTransformation); scaled.setDevicePixelRatio(dpr); p.drawPixmap(QPointF(iconRect.center().x() - scaled.width() / dpr / 2.0, iconRect.center().y() - scaled.height() / dpr / 2.0), scaled); } else { paintPowerGlyph(&p, iconRect); } p.setFont(f); p.setPen(isEnabled() ? QColor(Qt::white) : QColor("#f4f7fa")); const QRectF textRect(iconRect.right() + gap, box.top(), box.right() - iconRect.right() - gap, box.height()); p.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text()); } void PowerButton::paintPowerGlyph(QPainter *p, const QRectF &circle) const { const qreal d = circle.width(); const qreal inset = d * 0.24; const QRectF ring = circle.adjusted(inset, inset, -inset, -inset); const QColor glyph = isEnabled() ? QColor(Qt::white) : QColor("#f2f6fa"); p->setBrush(Qt::NoBrush); p->setPen(QPen(glyph, qMax(1.9, d * 0.10), Qt::SolidLine, Qt::RoundCap)); // 从 120° 起画 300°,顶部留 60° 缺口给竖杠 p->drawArc(ring, 120 * 16, 300 * 16); const QPointF c = circle.center(); p->drawLine(QPointF(c.x(), ring.top() - d * 0.06), QPointF(c.x(), c.y() - d * 0.05)); }