| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #include "powerbutton.h"
- #include <QEvent>
- #include <QFontMetrics>
- #include <QLinearGradient>
- #include <QPainter>
- #include <QPainterPath>
- #include <QPixmap>
- #include <QPaintEvent>
- 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;
- setCursor(Qt::PointingHandCursor);
- update();
- }
- #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
- void PowerButton::enterEvent(QEnterEvent *event)
- #else
- void PowerButton::enterEvent(QEvent *event)
- #endif
- {
- 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) {
- // 运行中「重新开始」:绿色实心 + 悬停/按下态反馈(可随时点击重跑)
- if (isDown()) {
- top = QColor("#217c4c"); bottom = QColor("#175d37"); border = QColor("#145230");
- } else if (underMouse()) {
- top = QColor("#38ae6f"); bottom = QColor("#258a54"); border = QColor("#1e7647");
- } else {
- 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<qreal>(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));
- }
|