Files
QT_wuhan_YT/widgets/titlebar.cpp
T
2026-10-14 09:59:42 +08:00

97 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "titlebar.h"
#include "ledindicator.h"
#include "appcontext.h"
#include "car.h"
#include <QHBoxLayout>
#include <QLabel>
#include "global.h"
TitleBar::TitleBar(AppContext* ctx, QWidget* parent)
: QWidget(parent), m_ctx(ctx)
{
setObjectName("TitleBar");
setFixedHeight(98);
setStyleSheet(QString("QWidget#TitleBar{background-color:%1;border-bottom:1px solid %2;}")
.arg(C::PANEL, C::BORDER));
// 左侧区(车载端通信状态)
QWidget* leftBox = new QWidget(this);
QHBoxLayout* leftLay = new QHBoxLayout(leftBox);
leftLay->setContentsMargins(260, 0, 0, 0);
leftLay->setSpacing(20);
m_comm = new LedIndicator("通信正常", leftBox);
m_comm->setFixedHeight(80); // 容纳放大后的文字,避免上下裁切
m_comm->setTextPixelSize(45);
m_comm->setState(LedIndicator::Green);
leftLay->addWidget(m_comm);
leftLay->addStretch(1);
// 右侧区(遥控器状态:链路 + 电量)
QWidget* rightBox = new QWidget(this);
QHBoxLayout* rightLay = new QHBoxLayout(rightBox);
rightLay->setContentsMargins(0, 0, 20, 0);
rightLay->setSpacing(30);
rightLay->addStretch(1);
m_rc = new LedIndicator("遥控器断开", rightBox);
m_rc->setFixedHeight(80);
m_rc->setTextPixelSize(45);
m_rc->setState(LedIndicator::RedBlink);
rightLay->addWidget(m_rc);
m_rcBat = new QLabel("--%", rightBox);
m_rcBat->setStyleSheet(QString("color:%1;font-size:60px;").arg(C::TEXT_SUB));
m_rcBat->setAlignment(Qt::AlignCenter);
rightLay->addWidget(m_rcBat);
// 左右两侧等宽 → 中间标题恰好居中于屏幕
// 左区需 260(左边距)+ 240(圆点区与留白 60 + 文字 4×45)= 500
// 右区需 285(圆点区与留白 60 + 文字 5×45)+ 30 + 130(电量)+ 20 = 465
const int sideW = 520;
leftBox->setFixedWidth(sideW);
rightBox->setFixedWidth(sideW);
// 标题(居中)
m_title = new QLabel("", this);
m_title->setAlignment(Qt::AlignCenter);
m_title->setStyleSheet(QString("color:%1;font-size:60px;font-weight:700;").arg(C::TEXT));
QHBoxLayout* lay = new QHBoxLayout(this);
lay->setContentsMargins(24, 6, 24, 6);
lay->setSpacing(0);
lay->addWidget(leftBox);
lay->addWidget(m_title, 1);
lay->addWidget(rightBox);
// 车载端通信状态刷新
connect(m_ctx->comm(), &CarCommunication::commOkChanged, this, [this](bool ok) {
if (ok) { m_comm->setText("通信正常"); m_comm->setState(LedIndicator::Green); }
else { m_comm->setText("通信异常"); m_comm->setState(LedIndicator::RedBlink); }
});
// 遥控器链路与电量刷新
connect(m_ctx->rc(), &RcCommunication::linkChanged, this, &TitleBar::onRcLink);
connect(m_ctx->rc(), &RcCommunication::frameReceived, this, &TitleBar::onRcFrame);
onRcLink(m_ctx->rc()->linkOk());
}
void TitleBar::setTitle(const QString& title)
{
m_title->setText(title);
}
void TitleBar::onRcLink(bool ok)
{
if (ok) {
m_rc->setText("遥控器正常");
m_rc->setState(LedIndicator::Green);
} else {
m_rc->setText("遥控器断开");
m_rc->setState(LedIndicator::RedBlink);
m_rcBat->setText("--%");
}
}
void TitleBar::onRcFrame(const RcFrame& f)
{
m_rcBat->setText(QString("%1%").arg(f.battery));
}