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

117 lines
3.6 KiB
C++

#include "titlebar.h"
#include "ledindicator.h"
#include "appcontext.h"
#include "udpcommunication.h"
#include "confirmdialog.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTime>
#include "global.h"
#include <QStyle>
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(0, 0, 0, 0);
leftLay->setSpacing(20);
m_comm = new LedIndicator("通信正常", leftBox);
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, 0, 0);
rightLay->setSpacing(20);
m_time = new QLabel("", rightBox);
m_time->setStyleSheet(QString("color:%1;font-size:60px;").arg(C::TEXT_SUB));
m_time->setAlignment(Qt::AlignCenter);
m_estop = new QPushButton("急停", rightBox);
m_estop->setProperty("variant", "estop");
m_estop->setStyleSheet("font-size:60px;");
m_estop->setFixedSize(180, 80);
rightLay->addStretch(1);
rightLay->addWidget(m_time);
rightLay->addWidget(m_estop);
// 左右两侧等宽 → 中间标题恰好居中于屏幕
const int sideW = 380; // 需 >= 较宽一侧所需宽度(右侧:时间 + 急停)
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_estop, &QPushButton::clicked, this, [this]() {
if (m_ctx->estopTriggered) {
if (ConfirmDialog::confirm(window(), "急停复位", "确定复位急停并恢复系统?"))
m_ctx->resetEstop();
} else {
m_ctx->triggerEstop();
}
});
// 通信状态刷新
connect(m_ctx->comm(), &UdpCommunication::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, &AppContext::estopChanged, this, &TitleBar::updateEstop);
updateEstop();
// 时钟
m_clock = new QTimer(this);
connect(m_clock, &QTimer::timeout, this, &TitleBar::updateTime);
m_clock->start(1000);
updateTime();
}
void TitleBar::setTitle(const QString& title)
{
m_title->setText(title);
}
void TitleBar::setEstopVisible(bool visible)
{
m_estop->setVisible(visible);
}
void TitleBar::updateEstop()
{
if (m_ctx->estopTriggered) {
m_estop->setText("复位");
m_estop->setProperty("variant", "danger");
} else {
m_estop->setText("急停");
m_estop->setProperty("variant", "estop");
}
m_estop->style()->unpolish(m_estop);
m_estop->style()->polish(m_estop);
}
void TitleBar::updateTime()
{
m_time->setText(QTime::currentTime().toString("HH:mm"));
}