| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- #include "detailview.h"
- #include "appconfig.h"
- #include "localplans.h"
- #include "stationdata.h"
- #include "qcustomplot.h"
- #include "ui_mainwindow.h"
- #include <QCheckBox>
- #include <QCoreApplication>
- #include <QDateTime>
- #include <QDebug>
- #include <QDir>
- #include <QFile>
- #include <QFontMetrics>
- #include <QFileInfo>
- #include <QHBoxLayout>
- #include <QFrame>
- #include <QJsonDocument>
- #include <QJsonObject>
- #include <QIntValidator>
- #include <QJsonValue>
- #include <QLabel>
- #include <QLineEdit>
- #include <QMouseEvent>
- #include <QPlainTextEdit>
- #include <QPushButton>
- #include <QScrollBar>
- #include <QStyle>
- namespace {
- const int kTabCount = 3; // 温控曲线 / 异常记录 / 内存参数设置
- // 顺序必须与 tools/gen_ui.py 的 PARAM_COLS / PARAM_CHECKS 保持一致
- const char *const kParamKeys[] = {
- "mem vdd", "mem vddq", "tCL", "tRCD", "tRP", "tRAS",
- "tRFC", "tRTP", "trrd_l", "tWTR_S", "tWTR_L", "tWR",
- "tCCD_L", "trefi", "tRFCsb",
- };
- const int kParamKeyCount = int(sizeof(kParamKeys) / sizeof(kParamKeys[0]));
- const char *const kParamChecks[] = {"errhandle", "ap"};
- const int kParamCheckCount = int(sizeof(kParamChecks) / sizeof(kParamChecks[0]));
- // 图例前面那个小色块的画法。
- // QCustomPlot 默认是照抄曲线画笔(1.6px)画一条细线,右端还会多画 5px,
- // 缩到 18×8 的图标框里就成了「一根毛边小横杠」。这里自己画:
- // Pill = 3px 圆头短棒(默认)、Dot = 小圆点、None = 只要文字
- enum LegendSwatch { SwatchPill, SwatchDot, SwatchNone };
- const LegendSwatch kSwatch = SwatchPill;
- // 色块的尺寸由 setupCurvePlot() 按图例字体算好,画的时候直接用:
- // g_swatchH = 大写字母高度(色块要和字一样高)
- // g_swatchTop = ascent - capHeight(图标框里色块该往下挪多少才和字对齐)
- int g_swatchH = 10;
- int g_swatchTop = 3;
- class CurveGraph : public QCPGraph
- {
- public:
- CurveGraph(QCPAxis *keyAxis, QCPAxis *valueAxis)
- : QCPGraph(keyAxis, valueAxis)
- {
- }
- protected:
- // 只在图例里生效:曲线本身仍然用 setPen() 设的 1.6px 细线
- void drawLegendIcon(QCPPainter *painter, const QRectF &rect) const override
- {
- if (kSwatch == SwatchNone)
- return;
- const QColor color("#2f7fc1");
- painter->setRenderHint(QPainter::Antialiasing, true);
- if (kSwatch == SwatchDot) {
- painter->setPen(Qt::NoPen);
- painter->setBrush(color);
- const qreal r = qMin(rect.height() / 2.0 - 0.5, 4.0);
- painter->drawEllipse(rect.center(), r, r);
- return;
- }
- // 直角矩形,高度 = 大写字母高度,垂直位置和文字的大写字母对齐(不要圆角)
- const QRectF bar(rect.left(), rect.top() + g_swatchTop, rect.width(), g_swatchH);
- painter->setPen(Qt::NoPen);
- painter->setBrush(color);
- painter->drawRect(bar);
- }
- };
- } // namespace
- DetailView::DetailView(Ui::MainWindow *ui, StationStore *store, QObject *parent)
- : QObject(parent)
- , m_ui(ui)
- , m_store(store)
- {
- // 布局伸缩比例(.ui 里写 stretch 会被 uic 拒绝)
- m_ui->detailCols->setStretch(0, 56);
- m_ui->detailCols->setStretch(1, 44);
- m_ui->detailLeftCol->setStretch(0, 4);
- m_ui->detailLeftCol->setStretch(1, 5);
- // 内存参数表单:三列等宽(索引 0/2/4 是三列,1/3 是列间分隔线);
- // 表单本身吃满页面高度,列里的 Expanding spacer 把行距均分,下方不再留大片空白
- m_ui->paramForm->setStretch(0, 1);
- m_ui->paramForm->setStretch(2, 1);
- m_ui->paramForm->setStretch(4, 1);
- m_ui->tabPageParamLayout->setStretch(0, 1);
- // 行内让输入框优先吃富余宽度(涨到 160 就停),剩下的才给行尾的弹性空隙。
- // 不给 stretch 的话,富余宽度会被行尾 spacer 全吃掉,输入框反而停在 sizeHint(≈124)上。
- // 开关行(第 2 个是 QCheckBox)不参与,免得复选框被拉宽。
- const QList<QHBoxLayout *> paramRows = m_ui->tabPageParam->findChildren<QHBoxLayout *>();
- for (QHBoxLayout *row : paramRows) {
- if (!row->objectName().startsWith(QStringLiteral("paramRow_")) || row->count() < 2)
- continue;
- if (qobject_cast<QLineEdit *>(row->itemAt(1)->widget()))
- row->setStretch(1, 1);
- }
- cacheChannelWidgets();
- // 内存参数只接受非负整数:挡住小数点、正负号、字母和粘贴进来的脏数据
- const QList<QLineEdit *> paramEdits = m_ui->tabPageParam->findChildren<QLineEdit *>();
- for (QLineEdit *e : paramEdits)
- e->setValidator(new QIntValidator(0, 99999999, e));
- auto createStageCheck = [this](const QString &labelText, const QString &objSuffix, int insertIdx) -> QCheckBox * {
- QHBoxLayout *box = new QHBoxLayout();
- box->setSpacing(6);
- box->setContentsMargins(0, 0, 16, 0);
- QLabel *lbl = new QLabel(labelText, m_ui->tabPageParam);
- lbl->setObjectName(QStringLiteral("paramChkName_") + objSuffix);
- lbl->setProperty("role", QStringLiteral("paramName"));
- lbl->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
- QCheckBox *chk = new QCheckBox(m_ui->tabPageParam);
- chk->setObjectName(QStringLiteral("paramCheck_") + objSuffix);
- chk->setProperty("role", QStringLiteral("paramCheck"));
- chk->setMinimumSize(16, 16);
- chk->setMaximumSize(16, 16);
- chk->setChecked(true);
- box->addWidget(lbl);
- box->addWidget(chk);
- m_ui->paramSubmitRow->insertLayout(insertIdx, box);
- connect(chk, &QCheckBox::clicked, this, [this]() {
- m_stageToggled = true;
- if (m_index >= 0 && m_chkRmt && m_chkAmt && m_chkMt)
- emit stageTogglesChanged(m_index, m_chkRmt->isChecked(), m_chkAmt->isChecked(), m_chkMt->isChecked());
- });
- return chk;
- };
- m_chkRmt = createStageCheck(QStringLiteral("RMT"), QStringLiteral("rmt"), 0);
- m_chkAmt = createStageCheck(QStringLiteral("AMT"), QStringLiteral("amt"), 1);
- m_chkMt = createStageCheck(QStringLiteral("MT"), QStringLiteral("mt"), 2);
- loadParamConfig();
- m_flush.setSingleShot(true);
- m_flush.setInterval(120);
- connect(&m_flush, &QTimer::timeout, this, &DetailView::flushPending);
- connect(m_ui->btnBackMonitor, &QPushButton::clicked, this, &DetailView::backRequested);
- connect(m_ui->btnParamSubmit, &QPushButton::clicked, this, &DetailView::submitParams);
- QPushButton *tabs[kTabCount] = {m_ui->detailTab_0, m_ui->detailTab_1, m_ui->detailTab_2};
- for (int i = 0; i < kTabCount; ++i)
- connect(tabs[i], &QPushButton::clicked, this, [this, i] { selectTab(i); });
- connect(m_store, &StationStore::logAppended, this, &DetailView::onLogAppended);
- connect(m_store, &StationStore::channelsChanged, this, &DetailView::onChannelsChanged);
- connect(m_store, &StationStore::infoChanged, this, &DetailView::onInfoChanged);
- connect(m_store, &StationStore::temperatureChanged, this, &DetailView::onTemperatureChanged);
- setupCurvePlot();
- }
- // ---------------------------------------------------------------------------
- // 参数先读当前工位 Qt 本地计划,未保存的工位读 config.json 默认值。
- // ---------------------------------------------------------------------------
- void DetailView::loadParamConfig()
- {
- const LocalPlans plans;
- const QJsonObject params = plans.memoryParams(qMax(0, m_index));
- int filled = 0;
- for (int i = 0; i < kParamKeyCount; ++i) {
- // 行号规则与 gen_ui.py 一致:前 6 个在第一列,接着 6 个第二列,最后 3 个第三列
- const int col = (i < 6) ? 0 : (i < 12 ? 1 : 2);
- const int row = (i < 6) ? i : (i < 12 ? i - 6 : i - 12);
- const QJsonValue v = params.value(QLatin1String(kParamKeys[i]));
- QLineEdit *edit = m_ui->tabPageParam->findChild<QLineEdit *>(
- QStringLiteral("paramEdit_%1_%2").arg(col).arg(row));
- if (!edit)
- continue;
- edit->clear();
- if (v.isUndefined() || v.isNull())
- continue;
- edit->setText(v.isDouble() ? QString::number(v.toDouble(), 'g', 12)
- : v.toVariant().toString());
- ++filled;
- }
- for (int i = 0; i < kParamCheckCount; ++i) {
- const QJsonValue v = params.value(QLatin1String(kParamChecks[i]));
- QCheckBox *chk = m_ui->tabPageParam->findChild<QCheckBox *>(
- QStringLiteral("paramCheck_2_%1").arg(3 + i)); // 第三列第 4、5 行,接在 3 个输入框后面
- if (!chk)
- continue;
- chk->setChecked(false);
- if (v.isUndefined() || v.isNull())
- continue;
- chk->setChecked(v.isBool() ? v.toBool() : v.toInt() != 0);
- ++filled;
- }
- const QJsonObject stationPlan = plans.plan(qMax(0, m_index));
- const QJsonObject uip = stationPlan.value(QStringLiteral("uip")).toObject();
- const QJsonObject amt = stationPlan.value(QStringLiteral("amt")).toObject();
- const QJsonObject uefi = stationPlan.value(QStringLiteral("uefi")).toObject();
- const bool defaultRmt = !uip.contains(QStringLiteral("action"))
- || uip.contains(QStringLiteral("run_rmt"))
- || stationPlan.contains(QStringLiteral("run_rmt"));
- const bool rmtChecked = stationPlan.value(QStringLiteral("run_rmt")).toBool(
- uip.value(QStringLiteral("run_rmt")).toBool(
- params.value(QStringLiteral("run_rmt")).toBool(
- params.value(QStringLiteral("rmt")).toBool(defaultRmt))));
- const QString amtAction = amt.value(QStringLiteral("action")).toString();
- const bool defaultAmt = amtAction.isEmpty() ? true : (amtAction == QStringLiteral("start_amt"));
- const bool amtChecked = stationPlan.value(QStringLiteral("run_amt")).toBool(
- amt.value(QStringLiteral("run_amt")).toBool(
- params.value(QStringLiteral("run_amt")).toBool(
- params.value(QStringLiteral("amt")).toBool(defaultAmt))));
- const QString uefiAction = uefi.value(QStringLiteral("action")).toString();
- const bool defaultMt = uefiAction.isEmpty() ? true : (uefiAction == QStringLiteral("start_mt"));
- const bool mtChecked = stationPlan.value(QStringLiteral("run_mt")).toBool(
- uefi.value(QStringLiteral("run_mt")).toBool(
- params.value(QStringLiteral("run_mt")).toBool(
- params.value(QStringLiteral("mt")).toBool(defaultMt))));
- if (m_chkRmt) m_chkRmt->setChecked(rmtChecked);
- if (m_chkAmt) m_chkAmt->setChecked(amtChecked);
- if (m_chkMt) m_chkMt->setChecked(mtChecked);
- m_stageToggled = false;
- qInfo().noquote() << "[param] 从 Qt 本地计划加载工位" << qMax(0, m_index) + 1
- << filled << "项参数";
- }
- // ---------------------------------------------------------------------------
- // 内存参数页:界面输入框 / 开关 → memoryParams 同构 JSON(确定按钮用)
- // 只收集;上层原子保存到 Qt 的 qt-plans.json,config.json 默认值不改。
- // ---------------------------------------------------------------------------
- QJsonObject DetailView::collectParams(bool *valid)
- {
- *valid = true;
- QJsonObject params;
- for (int i = 0; i < kParamKeyCount; ++i) {
- const int col = (i < 6) ? 0 : (i < 12 ? 1 : 2);
- const int row = (i < 6) ? i : (i < 12 ? i - 6 : i - 12);
- const QLineEdit *edit = m_ui->tabPageParam->findChild<QLineEdit *>(
- QStringLiteral("paramEdit_%1_%2").arg(col).arg(row));
- if (!edit || edit->text().trimmed().isEmpty())
- continue;
- bool ok = false;
- const qlonglong value = edit->text().trimmed().toLongLong(&ok);
- if (!ok || !edit->hasAcceptableInput() || value < 0 || value > 99999999) {
- *valid = false;
- qWarning().noquote() << "[param] 工位" << m_index + 1 << QLatin1String(kParamKeys[i]) << "必须为非负整数,未保存任何修改";
- return QJsonObject();
- }
- params.insert(QLatin1String(kParamKeys[i]), static_cast<double>(value));
- }
- for (int i = 0; i < kParamCheckCount; ++i) {
- const QCheckBox *chk = m_ui->tabPageParam->findChild<QCheckBox *>(
- QStringLiteral("paramCheck_2_%1").arg(3 + i));
- if (chk)
- params.insert(QLatin1String(kParamChecks[i]), chk->isChecked());
- }
- const QJsonObject stationPlan = LocalPlans().plan(qMax(0, m_index));
- if (m_stageToggled || stationPlan.contains(QStringLiteral("run_rmt"))
- || stationPlan.contains(QStringLiteral("run_amt"))
- || stationPlan.contains(QStringLiteral("run_mt"))) {
- if (m_chkRmt) params.insert(QStringLiteral("run_rmt"), m_chkRmt->isChecked());
- if (m_chkAmt) params.insert(QStringLiteral("run_amt"), m_chkAmt->isChecked());
- if (m_chkMt) params.insert(QStringLiteral("run_mt"), m_chkMt->isChecked());
- }
- return params;
- }
- void DetailView::submitParams()
- {
- if (m_index < 0)
- return;
- bool valid = false;
- const QJsonObject params = collectParams(&valid);
- if (!valid) return;
- qInfo().noquote() << "[param] 工位" << m_index + 1 << "点确定,收集到"
- << params.count() << "项内存参数,保存到 Qt 本地计划";
- emit paramsSubmitted(m_index, params);
- }
- // ---------------------------------------------------------------------------
- // 通道切换:点 24 通道卡片里的任意格子,曲线切到该通道
- // ---------------------------------------------------------------------------
- bool DetailView::eventFilter(QObject *watched, QEvent *event)
- {
- if (event->type() == QEvent::MouseButtonRelease) {
- QMouseEvent *me = static_cast<QMouseEvent *>(event);
- if (me->button() == Qt::LeftButton) {
- const QString name = watched->objectName();
- if (name.startsWith(QLatin1String("chCell_")))
- selectCurveChannel(name.mid(7)); // 去掉 "chCell_" 前缀
- }
- }
- return QObject::eventFilter(watched, event);
- }
- void DetailView::selectCurveChannel(const QString &name)
- {
- if (m_curveChannel == name || name.isEmpty())
- return;
- m_curveChannel = name;
- markCurveChannel();
- renderCurve();
- }
- void DetailView::markCurveChannel()
- {
- const QString want = QStringLiteral("chCell_") + m_curveChannel;
- for (QFrame *f : m_cellFrames) {
- const bool on = (f->objectName() == want);
- if (f->property("selected").toBool() == on)
- continue;
- f->setProperty("selected", on);
- f->style()->unpolish(f);
- f->style()->polish(f);
- }
- }
- // ---------------------------------------------------------------------------
- // 温控曲线:QCustomPlot,横轴最近 200 个采样点,纵轴 20~90 °C
- // 样式全部在这里设,.ui 里只有控件本体
- // ---------------------------------------------------------------------------
- void DetailView::setupCurvePlot()
- {
- QCustomPlot *p = m_ui->curvePlot;
- if (!p)
- return;
- p->setBackground(QBrush(QColor("#ffffff")));
- p->setNoAntialiasingOnDrag(true);
- QFont font = p->font();
- font.setPointSize(9);
- p->setFont(font);
- const QColor axisColor("#c6d2df");
- const QColor subTickColor("#dde5ee");
- const QColor labelColor("#6b7d90");
- const QColor gridColor("#eef2f7");
- for (QCPAxis *ax : {p->xAxis, p->yAxis}) {
- ax->setBasePen(QPen(axisColor, 1));
- ax->setTickPen(QPen(axisColor, 1));
- ax->setSubTickPen(QPen(subTickColor, 1));
- ax->setTickLabelColor(labelColor);
- ax->setLabelColor(labelColor);
- ax->setTickLength(0, 4);
- ax->grid()->setPen(QPen(gridColor, 1, Qt::DotLine));
- ax->grid()->setSubGridVisible(false);
- ax->grid()->setZeroLinePen(Qt::NoPen);
- }
- p->xAxis->setLabel(QStringLiteral("采样点"));
- p->yAxis->setLabel(QStringLiteral("温度 (°C)"));
- p->xAxis->setRange(0, kTempHistorySize);
- p->yAxis->setRange(20, 90);
- p->xAxis->ticker()->setTickCount(6); // 0 / 40 / 80 / 120 / 160 / 200
- p->yAxis->ticker()->setTickCount(8); // 20 ~ 90,每 10 一格
- p->xAxis->setSubTicks(false);
- p->yAxis->setSubTicks(false);
- // 用 CurveGraph 代替 addGraph():曲线画法不变,只是把图例里的小色块换成圆头短棒
- QCPGraph *g = new CurveGraph(p->xAxis, p->yAxis);
- g->setPen(QPen(QColor("#2f7fc1"), 1.6));
- g->setLineStyle(QCPGraph::lsLine);
- g->setScatterStyle(QCPScatterStyle::ssNone);
- g->setAntialiased(true);
- g->setName(QStringLiteral("腔体温度")); // 曲线名,进详情页后会换成「工位号 · 腔体温度」
- // 图例:右上角白色小框,曲线名由 renderCurve() 随工位更新
- QCPLegend *lg = p->legend;
- lg->setVisible(true);
- lg->setBrush(QBrush(QColor(255, 255, 255, 225)));
- lg->setBorderPen(QPen(QColor("#dde5ee")));
- lg->setTextColor(QColor("#5d7086"));
- lg->setFont(font);
- // 图标框取整行高(这样它在图例行里就是垂直居中的),
- // 色块本身再按字体度量放进去:高度 = 大写字母高度,顶边 = ascent - capHeight
- const QFontMetrics lgFm(font);
- g_swatchH = qMax(8, lgFm.capHeight());
- g_swatchTop = qMax(0, lgFm.ascent() - lgFm.capHeight());
- lg->setIconSize(20, lgFm.height());
- lg->setIconTextPadding(6);
- lg->setRowSpacing(0);
- p->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
- // 最新点:一个白底蓝边的小圆点 + 下面的温度数值(跟着数据一起走,见 renderCurve)
- m_lastDot = new QCPItemTracer(p);
- m_lastDot->setGraph(g);
- m_lastDot->setStyle(QCPItemTracer::tsCircle);
- m_lastDot->setSize(5);
- m_lastDot->setPen(QPen(QColor("#2f7fc1"), 1.6));
- m_lastDot->setBrush(QBrush(Qt::white));
- m_lastDot->setSelectable(false);
- m_lastDot->setClipToAxisRect(false);
- m_lastLabel = new QCPItemText(p);
- m_lastLabel->setSelectable(false);
- m_lastLabel->setClipToAxisRect(false);
- m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
- m_lastLabel->position->setType(QCPItemPosition::ptPlotCoords);
- m_lastLabel->setPadding(QMargins(2, 6, 2, 1)); // 上边多留点,让标签离开圆点
- m_lastLabel->setPen(Qt::NoPen);
- m_lastLabel->setBrush(Qt::NoBrush); // 不要底色块,只留文字
- m_lastLabel->setColor(QColor("#2f7fc1"));
- {
- QFont lf = font;
- lf.setPointSizeF(8.5);
- lf.setBold(true);
- m_lastLabel->setFont(lf);
- }
- p->replot();
- }
- void DetailView::renderCurve()
- {
- if (m_index < 0 || m_index >= m_store->count())
- return;
- QCustomPlot *p = m_ui->curvePlot;
- if (!p || p->graphCount() == 0)
- return;
- const StationData &d = m_store->station(m_index);
- // 找当前选中的通道;工位换了但通道名还在(24 个通道各工位同名),所以直接按名字找
- const ChannelInfo *sel = nullptr;
- for (const ChannelInfo &c : d.channels) {
- if (c.name == m_curveChannel) {
- sel = &c;
- break;
- }
- }
- if (!sel && !d.channels.isEmpty())
- sel = &d.channels.first();
- if (!sel)
- return;
- const int n = sel->history.size();
- const int start = sel->historyStart;
- QVector<double> keys(n);
- QVector<double> values(n);
- for (int i = 0; i < n; ++i) {
- keys[i] = start + i; // 全局采样序号,不回绕
- values[i] = sel->history.at(i);
- }
- QCPGraph *g = p->graph(0);
- g->setData(keys, values, true);
- // 图例显示通道名:CPU0_CHA
- if (g->name() != sel->name)
- g->setName(sel->name);
- // 横轴滚动:可见宽度恒为 kTempHistorySize 点,起点跟着最新数据走(刻度数字会一起滚)
- double lo = start;
- if (n < kTempHistorySize)
- lo = 0; // 还没满 200 点时从 0 开始铺,不从负数起
- p->xAxis->setRange(lo, lo + kTempHistorySize);
- // 最新点标记 + 数值标签:贴在最后一个采样点下方
- if (m_lastDot && m_lastLabel && n > 0) {
- const double k = keys.last();
- const double v = values.last();
- m_lastDot->setGraphKey(k);
- m_lastDot->setVisible(true);
- m_lastLabel->setText(QStringLiteral("%1°C").arg(QString::number(v, 'f', 1)));
- m_lastLabel->position->setCoords(k, v);
- m_lastLabel->setVisible(true);
- // 点贴近轴的右边缘时改成右对齐,否则标签会甩到图外
- const double px = p->xAxis->coordToPixel(k);
- const QRect rect = p->axisRect()->rect();
- if (px > rect.right() - 40)
- m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignRight);
- else if (px < rect.left() + 40)
- m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignLeft);
- else
- m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
- } else if (m_lastDot && m_lastLabel) {
- m_lastDot->setVisible(false);
- m_lastLabel->setVisible(false);
- }
- p->replot(QCustomPlot::rpQueuedReplot);
- }
- void DetailView::onTemperatureChanged(int index)
- {
- if (index != m_index)
- return; // 不在当前工位:数据留在模型里,切回来整体重铺
- if (m_ui->detailTabStack->currentIndex() != 0)
- return; // 曲线页没显示,画了也看不见
- renderCurve();
- }
- void DetailView::cacheChannelWidgets()
- {
- m_cellFrames.clear();
- m_cellValues.clear();
- const QStringList names = channelNames();
- for (const QString &name : names) {
- QFrame *frame = m_ui->detailPage->findChild<QFrame *>(QStringLiteral("chCell_") + name);
- QLabel *value = m_ui->detailPage->findChild<QLabel *>(QStringLiteral("chCellValue_") + name);
- if (!frame || !value)
- continue;
- // 点通道格子 → 曲线切到该通道
- frame->installEventFilter(this);
- frame->setCursor(Qt::PointingHandCursor);
- m_cellFrames.append(frame);
- m_cellValues.append(value);
- }
- }
- // ---------------------------------------------------------------------------
- // 打开 / 关闭
- // ---------------------------------------------------------------------------
- void DetailView::showStation(int index)
- {
- if (index < 0 || index >= m_store->count())
- return;
- m_index = index;
- loadParamConfig();
- m_ui->pageStack->setCurrentIndex(1);
- // 纯内存渲染,不依赖任何异步取数 —— 点开即出
- markCurveChannel(); // 高亮当前曲线对应的通道(默认 CPU0_CHA)
- renderAll();
- }
- void DetailView::leave()
- {
- m_index = -1;
- m_flush.stop();
- m_needTitle = m_needChannels = false;
- }
- // ---------------------------------------------------------------------------
- // 全量渲染(首次进入 / 切换工位)
- // ---------------------------------------------------------------------------
- void DetailView::renderAll()
- {
- renderTitle();
- renderChannels();
- renderCurve();
- renderLogs();
- }
- void DetailView::renderTitle()
- {
- // 标题栏的“设备详情页 ·”是静态控件,代码只填工位号
- const StationData &d = m_store->station(m_index);
- m_ui->detailDevice->setText(d.deviceId.isEmpty() ? QStringLiteral("—") : d.deviceId);
- }
- void DetailView::renderChannels()
- {
- const StationData &d = m_store->station(m_index);
- const int n = qMin(m_cellFrames.size(), d.channels.size());
- for (int i = 0; i < n; ++i) {
- const ChannelInfo &ch = d.channels.at(i);
- // 未装载不显示温度,用 --℃ 占位;已装载只显示温度,不区分 PASS / FAIL
- m_cellValues[i]->setText(ch.temperature < 0.0
- ? QStringLiteral("--℃")
- : QStringLiteral("%1 ℃").arg(QString::number(ch.temperature, 'f', 1)));
- QFrame *frame = m_cellFrames[i];
- const QString state = (ch.temperature < 0.0) ? QStringLiteral("idle")
- : QStringLiteral("loaded");
- if (frame->property("state").toString() == state)
- continue;
- frame->setProperty("state", state);
- // 兜底重刷:QSS 里值文字的颜色挂在父框的 state 上
- frame->style()->unpolish(frame);
- frame->style()->polish(frame);
- m_cellValues[i]->style()->unpolish(m_cellValues[i]);
- m_cellValues[i]->style()->polish(m_cellValues[i]);
- }
- }
- void DetailView::renderLogs()
- {
- const StationData &d = m_store->station(m_index);
- QPlainTextEdit *log = m_ui->detailLog;
- const int total = d.logs.size();
- if (m_logStation == m_index && total >= m_logLines) {
- // 同一工位:只把新增的尾巴整块追加进去。
- // 实测 7.5 万行:整块 append 约 0.2s,逐行 append 会退化到 13s 以上,
- // 所以这里必须一次性插入,绝不能 for 循环 appendPlainText。
- if (total > m_logLines) {
- log->appendPlainText(d.logs.mid(m_logLines).join(QLatin1Char('\n')));
- m_logLines = total;
- }
- } else {
- // 换工位:整篇重铺(7.5 万行约 0.4s,属可接受的一次性开销)
- log->setPlainText(d.logs.join(QLatin1Char('\n')));
- m_logStation = m_index;
- m_logLines = total;
- }
- log->verticalScrollBar()->setValue(log->verticalScrollBar()->maximum());
- }
- void DetailView::selectTab(int index)
- {
- QPushButton *tabs[kTabCount] = {m_ui->detailTab_0, m_ui->detailTab_1, m_ui->detailTab_2};
- for (int i = 0; i < kTabCount; ++i) {
- tabs[i]->setProperty("active", i == index ? QStringLiteral("true") : QStringLiteral("false"));
- tabs[i]->style()->unpolish(tabs[i]);
- tabs[i]->style()->polish(tabs[i]);
- }
- m_ui->detailTabStack->setCurrentIndex(index);
- if (index == 0) // 切回曲线页时补画一次,避免看到离开期间的旧曲线
- renderCurve();
- }
- // ---------------------------------------------------------------------------
- // 实时刷新:日志走快路径,其余 120ms 合并
- // ---------------------------------------------------------------------------
- void DetailView::onLogAppended(int index, const QString &line)
- {
- if (index != m_index)
- return; // 不在当前工位:只留在模型里,切回来时一次性铺全
- QPlainTextEdit *log = m_ui->detailLog;
- QScrollBar *bar = log->verticalScrollBar();
- const bool follow = (bar->value() >= bar->maximum() - 4); // 用户往上翻就不抢滚动条
- log->appendPlainText(line);
- m_logStation = index;
- m_logLines = m_store->station(index).logs.size();
- if (follow)
- bar->setValue(bar->maximum());
- }
- void DetailView::onChannelsChanged(int index)
- {
- if (index != m_index)
- return;
- m_needChannels = true;
- requestFlush();
- }
- void DetailView::onInfoChanged(int index)
- {
- if (index != m_index)
- return;
- m_needTitle = true; // 标题栏的工位号跟着任务信息一起变
- requestFlush();
- }
- void DetailView::requestFlush()
- {
- if (!m_flush.isActive())
- m_flush.start();
- }
- void DetailView::flushPending()
- {
- if (m_index < 0)
- return;
- if (m_needTitle) {
- m_needTitle = false;
- renderTitle();
- }
- if (m_needChannels) {
- m_needChannels = false;
- renderChannels();
- // 曲线看的是通道温度,所以通道数据一变也要重画(只在曲线页可见时做)
- if (m_ui->detailTabStack->currentIndex() == 0)
- renderCurve();
- }
- }
|