detailview.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. #include "detailview.h"
  2. #include "appconfig.h"
  3. #include "localplans.h"
  4. #include "stationdata.h"
  5. #include "qcustomplot.h"
  6. #include "ui_mainwindow.h"
  7. #include <QCheckBox>
  8. #include <QCoreApplication>
  9. #include <QDateTime>
  10. #include <QDebug>
  11. #include <QDir>
  12. #include <QFile>
  13. #include <QFontMetrics>
  14. #include <QFileInfo>
  15. #include <QHBoxLayout>
  16. #include <QFrame>
  17. #include <QJsonDocument>
  18. #include <QJsonObject>
  19. #include <QIntValidator>
  20. #include <QJsonValue>
  21. #include <QLabel>
  22. #include <QLineEdit>
  23. #include <QMouseEvent>
  24. #include <QPlainTextEdit>
  25. #include <QPushButton>
  26. #include <QScrollBar>
  27. #include <QStyle>
  28. namespace {
  29. const int kTabCount = 3; // 温控曲线 / 异常记录 / 内存参数设置
  30. // 顺序必须与 tools/gen_ui.py 的 PARAM_COLS / PARAM_CHECKS 保持一致
  31. const char *const kParamKeys[] = {
  32. "mem vdd", "mem vddq", "tCL", "tRCD", "tRP", "tRAS",
  33. "tRFC", "tRTP", "trrd_l", "tWTR_S", "tWTR_L", "tWR",
  34. "tCCD_L", "trefi", "tRFCsb",
  35. };
  36. const int kParamKeyCount = int(sizeof(kParamKeys) / sizeof(kParamKeys[0]));
  37. const char *const kParamChecks[] = {"errhandle", "ap"};
  38. const int kParamCheckCount = int(sizeof(kParamChecks) / sizeof(kParamChecks[0]));
  39. // 图例前面那个小色块的画法。
  40. // QCustomPlot 默认是照抄曲线画笔(1.6px)画一条细线,右端还会多画 5px,
  41. // 缩到 18×8 的图标框里就成了「一根毛边小横杠」。这里自己画:
  42. // Pill = 3px 圆头短棒(默认)、Dot = 小圆点、None = 只要文字
  43. enum LegendSwatch { SwatchPill, SwatchDot, SwatchNone };
  44. const LegendSwatch kSwatch = SwatchPill;
  45. // 色块的尺寸由 setupCurvePlot() 按图例字体算好,画的时候直接用:
  46. // g_swatchH = 大写字母高度(色块要和字一样高)
  47. // g_swatchTop = ascent - capHeight(图标框里色块该往下挪多少才和字对齐)
  48. int g_swatchH = 10;
  49. int g_swatchTop = 3;
  50. class CurveGraph : public QCPGraph
  51. {
  52. public:
  53. CurveGraph(QCPAxis *keyAxis, QCPAxis *valueAxis)
  54. : QCPGraph(keyAxis, valueAxis)
  55. {
  56. }
  57. protected:
  58. // 只在图例里生效:曲线本身仍然用 setPen() 设的 1.6px 细线
  59. void drawLegendIcon(QCPPainter *painter, const QRectF &rect) const override
  60. {
  61. if (kSwatch == SwatchNone)
  62. return;
  63. const QColor color("#2f7fc1");
  64. painter->setRenderHint(QPainter::Antialiasing, true);
  65. if (kSwatch == SwatchDot) {
  66. painter->setPen(Qt::NoPen);
  67. painter->setBrush(color);
  68. const qreal r = qMin(rect.height() / 2.0 - 0.5, 4.0);
  69. painter->drawEllipse(rect.center(), r, r);
  70. return;
  71. }
  72. // 直角矩形,高度 = 大写字母高度,垂直位置和文字的大写字母对齐(不要圆角)
  73. const QRectF bar(rect.left(), rect.top() + g_swatchTop, rect.width(), g_swatchH);
  74. painter->setPen(Qt::NoPen);
  75. painter->setBrush(color);
  76. painter->drawRect(bar);
  77. }
  78. };
  79. } // namespace
  80. DetailView::DetailView(Ui::MainWindow *ui, StationStore *store, QObject *parent)
  81. : QObject(parent)
  82. , m_ui(ui)
  83. , m_store(store)
  84. {
  85. // 布局伸缩比例(.ui 里写 stretch 会被 uic 拒绝)
  86. m_ui->detailCols->setStretch(0, 56);
  87. m_ui->detailCols->setStretch(1, 44);
  88. m_ui->detailLeftCol->setStretch(0, 4);
  89. m_ui->detailLeftCol->setStretch(1, 5);
  90. // 内存参数表单:三列等宽(索引 0/2/4 是三列,1/3 是列间分隔线);
  91. // 表单本身吃满页面高度,列里的 Expanding spacer 把行距均分,下方不再留大片空白
  92. m_ui->paramForm->setStretch(0, 1);
  93. m_ui->paramForm->setStretch(2, 1);
  94. m_ui->paramForm->setStretch(4, 1);
  95. m_ui->tabPageParamLayout->setStretch(0, 1);
  96. // 行内让输入框优先吃富余宽度(涨到 160 就停),剩下的才给行尾的弹性空隙。
  97. // 不给 stretch 的话,富余宽度会被行尾 spacer 全吃掉,输入框反而停在 sizeHint(≈124)上。
  98. // 开关行(第 2 个是 QCheckBox)不参与,免得复选框被拉宽。
  99. const QList<QHBoxLayout *> paramRows = m_ui->tabPageParam->findChildren<QHBoxLayout *>();
  100. for (QHBoxLayout *row : paramRows) {
  101. if (!row->objectName().startsWith(QStringLiteral("paramRow_")) || row->count() < 2)
  102. continue;
  103. if (qobject_cast<QLineEdit *>(row->itemAt(1)->widget()))
  104. row->setStretch(1, 1);
  105. }
  106. cacheChannelWidgets();
  107. // 内存参数只接受非负整数:挡住小数点、正负号、字母和粘贴进来的脏数据
  108. const QList<QLineEdit *> paramEdits = m_ui->tabPageParam->findChildren<QLineEdit *>();
  109. for (QLineEdit *e : paramEdits)
  110. e->setValidator(new QIntValidator(0, 99999999, e));
  111. auto createStageCheck = [this](const QString &labelText, const QString &objSuffix, int insertIdx) -> QCheckBox * {
  112. QHBoxLayout *box = new QHBoxLayout();
  113. box->setSpacing(6);
  114. box->setContentsMargins(0, 0, 16, 0);
  115. QLabel *lbl = new QLabel(labelText, m_ui->tabPageParam);
  116. lbl->setObjectName(QStringLiteral("paramChkName_") + objSuffix);
  117. lbl->setProperty("role", QStringLiteral("paramName"));
  118. lbl->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
  119. QCheckBox *chk = new QCheckBox(m_ui->tabPageParam);
  120. chk->setObjectName(QStringLiteral("paramCheck_") + objSuffix);
  121. chk->setProperty("role", QStringLiteral("paramCheck"));
  122. chk->setMinimumSize(16, 16);
  123. chk->setMaximumSize(16, 16);
  124. chk->setChecked(true);
  125. box->addWidget(lbl);
  126. box->addWidget(chk);
  127. m_ui->paramSubmitRow->insertLayout(insertIdx, box);
  128. connect(chk, &QCheckBox::clicked, this, [this]() {
  129. m_stageToggled = true;
  130. if (m_index >= 0 && m_chkRmt && m_chkAmt && m_chkMt)
  131. emit stageTogglesChanged(m_index, m_chkRmt->isChecked(), m_chkAmt->isChecked(), m_chkMt->isChecked());
  132. });
  133. return chk;
  134. };
  135. m_chkRmt = createStageCheck(QStringLiteral("RMT"), QStringLiteral("rmt"), 0);
  136. m_chkAmt = createStageCheck(QStringLiteral("AMT"), QStringLiteral("amt"), 1);
  137. m_chkMt = createStageCheck(QStringLiteral("MT"), QStringLiteral("mt"), 2);
  138. loadParamConfig();
  139. m_flush.setSingleShot(true);
  140. m_flush.setInterval(120);
  141. connect(&m_flush, &QTimer::timeout, this, &DetailView::flushPending);
  142. connect(m_ui->btnBackMonitor, &QPushButton::clicked, this, &DetailView::backRequested);
  143. connect(m_ui->btnParamSubmit, &QPushButton::clicked, this, &DetailView::submitParams);
  144. QPushButton *tabs[kTabCount] = {m_ui->detailTab_0, m_ui->detailTab_1, m_ui->detailTab_2};
  145. for (int i = 0; i < kTabCount; ++i)
  146. connect(tabs[i], &QPushButton::clicked, this, [this, i] { selectTab(i); });
  147. connect(m_store, &StationStore::logAppended, this, &DetailView::onLogAppended);
  148. connect(m_store, &StationStore::channelsChanged, this, &DetailView::onChannelsChanged);
  149. connect(m_store, &StationStore::infoChanged, this, &DetailView::onInfoChanged);
  150. connect(m_store, &StationStore::temperatureChanged, this, &DetailView::onTemperatureChanged);
  151. setupCurvePlot();
  152. }
  153. // ---------------------------------------------------------------------------
  154. // 参数先读当前工位 Qt 本地计划,未保存的工位读 config.json 默认值。
  155. // ---------------------------------------------------------------------------
  156. void DetailView::loadParamConfig()
  157. {
  158. const LocalPlans plans;
  159. const QJsonObject params = plans.memoryParams(qMax(0, m_index));
  160. int filled = 0;
  161. for (int i = 0; i < kParamKeyCount; ++i) {
  162. // 行号规则与 gen_ui.py 一致:前 6 个在第一列,接着 6 个第二列,最后 3 个第三列
  163. const int col = (i < 6) ? 0 : (i < 12 ? 1 : 2);
  164. const int row = (i < 6) ? i : (i < 12 ? i - 6 : i - 12);
  165. const QJsonValue v = params.value(QLatin1String(kParamKeys[i]));
  166. QLineEdit *edit = m_ui->tabPageParam->findChild<QLineEdit *>(
  167. QStringLiteral("paramEdit_%1_%2").arg(col).arg(row));
  168. if (!edit)
  169. continue;
  170. edit->clear();
  171. if (v.isUndefined() || v.isNull())
  172. continue;
  173. edit->setText(v.isDouble() ? QString::number(v.toDouble(), 'g', 12)
  174. : v.toVariant().toString());
  175. ++filled;
  176. }
  177. for (int i = 0; i < kParamCheckCount; ++i) {
  178. const QJsonValue v = params.value(QLatin1String(kParamChecks[i]));
  179. QCheckBox *chk = m_ui->tabPageParam->findChild<QCheckBox *>(
  180. QStringLiteral("paramCheck_2_%1").arg(3 + i)); // 第三列第 4、5 行,接在 3 个输入框后面
  181. if (!chk)
  182. continue;
  183. chk->setChecked(false);
  184. if (v.isUndefined() || v.isNull())
  185. continue;
  186. chk->setChecked(v.isBool() ? v.toBool() : v.toInt() != 0);
  187. ++filled;
  188. }
  189. const QJsonObject stationPlan = plans.plan(qMax(0, m_index));
  190. const QJsonObject uip = stationPlan.value(QStringLiteral("uip")).toObject();
  191. const QJsonObject amt = stationPlan.value(QStringLiteral("amt")).toObject();
  192. const QJsonObject uefi = stationPlan.value(QStringLiteral("uefi")).toObject();
  193. const bool defaultRmt = !uip.contains(QStringLiteral("action"))
  194. || uip.contains(QStringLiteral("run_rmt"))
  195. || stationPlan.contains(QStringLiteral("run_rmt"));
  196. const bool rmtChecked = stationPlan.value(QStringLiteral("run_rmt")).toBool(
  197. uip.value(QStringLiteral("run_rmt")).toBool(
  198. params.value(QStringLiteral("run_rmt")).toBool(
  199. params.value(QStringLiteral("rmt")).toBool(defaultRmt))));
  200. const QString amtAction = amt.value(QStringLiteral("action")).toString();
  201. const bool defaultAmt = amtAction.isEmpty() ? true : (amtAction == QStringLiteral("start_amt"));
  202. const bool amtChecked = stationPlan.value(QStringLiteral("run_amt")).toBool(
  203. amt.value(QStringLiteral("run_amt")).toBool(
  204. params.value(QStringLiteral("run_amt")).toBool(
  205. params.value(QStringLiteral("amt")).toBool(defaultAmt))));
  206. const QString uefiAction = uefi.value(QStringLiteral("action")).toString();
  207. const bool defaultMt = uefiAction.isEmpty() ? true : (uefiAction == QStringLiteral("start_mt"));
  208. const bool mtChecked = stationPlan.value(QStringLiteral("run_mt")).toBool(
  209. uefi.value(QStringLiteral("run_mt")).toBool(
  210. params.value(QStringLiteral("run_mt")).toBool(
  211. params.value(QStringLiteral("mt")).toBool(defaultMt))));
  212. if (m_chkRmt) m_chkRmt->setChecked(rmtChecked);
  213. if (m_chkAmt) m_chkAmt->setChecked(amtChecked);
  214. if (m_chkMt) m_chkMt->setChecked(mtChecked);
  215. m_stageToggled = false;
  216. qInfo().noquote() << "[param] 从 Qt 本地计划加载工位" << qMax(0, m_index) + 1
  217. << filled << "项参数";
  218. }
  219. // ---------------------------------------------------------------------------
  220. // 内存参数页:界面输入框 / 开关 → memoryParams 同构 JSON(确定按钮用)
  221. // 只收集;上层原子保存到 Qt 的 qt-plans.json,config.json 默认值不改。
  222. // ---------------------------------------------------------------------------
  223. QJsonObject DetailView::collectParams(bool *valid)
  224. {
  225. *valid = true;
  226. QJsonObject params;
  227. for (int i = 0; i < kParamKeyCount; ++i) {
  228. const int col = (i < 6) ? 0 : (i < 12 ? 1 : 2);
  229. const int row = (i < 6) ? i : (i < 12 ? i - 6 : i - 12);
  230. const QLineEdit *edit = m_ui->tabPageParam->findChild<QLineEdit *>(
  231. QStringLiteral("paramEdit_%1_%2").arg(col).arg(row));
  232. if (!edit || edit->text().trimmed().isEmpty())
  233. continue;
  234. bool ok = false;
  235. const qlonglong value = edit->text().trimmed().toLongLong(&ok);
  236. if (!ok || !edit->hasAcceptableInput() || value < 0 || value > 99999999) {
  237. *valid = false;
  238. qWarning().noquote() << "[param] 工位" << m_index + 1 << QLatin1String(kParamKeys[i]) << "必须为非负整数,未保存任何修改";
  239. return QJsonObject();
  240. }
  241. params.insert(QLatin1String(kParamKeys[i]), static_cast<double>(value));
  242. }
  243. for (int i = 0; i < kParamCheckCount; ++i) {
  244. const QCheckBox *chk = m_ui->tabPageParam->findChild<QCheckBox *>(
  245. QStringLiteral("paramCheck_2_%1").arg(3 + i));
  246. if (chk)
  247. params.insert(QLatin1String(kParamChecks[i]), chk->isChecked());
  248. }
  249. const QJsonObject stationPlan = LocalPlans().plan(qMax(0, m_index));
  250. if (m_stageToggled || stationPlan.contains(QStringLiteral("run_rmt"))
  251. || stationPlan.contains(QStringLiteral("run_amt"))
  252. || stationPlan.contains(QStringLiteral("run_mt"))) {
  253. if (m_chkRmt) params.insert(QStringLiteral("run_rmt"), m_chkRmt->isChecked());
  254. if (m_chkAmt) params.insert(QStringLiteral("run_amt"), m_chkAmt->isChecked());
  255. if (m_chkMt) params.insert(QStringLiteral("run_mt"), m_chkMt->isChecked());
  256. }
  257. return params;
  258. }
  259. void DetailView::submitParams()
  260. {
  261. if (m_index < 0)
  262. return;
  263. bool valid = false;
  264. const QJsonObject params = collectParams(&valid);
  265. if (!valid) return;
  266. qInfo().noquote() << "[param] 工位" << m_index + 1 << "点确定,收集到"
  267. << params.count() << "项内存参数,保存到 Qt 本地计划";
  268. emit paramsSubmitted(m_index, params);
  269. }
  270. // ---------------------------------------------------------------------------
  271. // 通道切换:点 24 通道卡片里的任意格子,曲线切到该通道
  272. // ---------------------------------------------------------------------------
  273. bool DetailView::eventFilter(QObject *watched, QEvent *event)
  274. {
  275. if (event->type() == QEvent::MouseButtonRelease) {
  276. QMouseEvent *me = static_cast<QMouseEvent *>(event);
  277. if (me->button() == Qt::LeftButton) {
  278. const QString name = watched->objectName();
  279. if (name.startsWith(QLatin1String("chCell_")))
  280. selectCurveChannel(name.mid(7)); // 去掉 "chCell_" 前缀
  281. }
  282. }
  283. return QObject::eventFilter(watched, event);
  284. }
  285. void DetailView::selectCurveChannel(const QString &name)
  286. {
  287. if (m_curveChannel == name || name.isEmpty())
  288. return;
  289. m_curveChannel = name;
  290. markCurveChannel();
  291. renderCurve();
  292. }
  293. void DetailView::markCurveChannel()
  294. {
  295. const QString want = QStringLiteral("chCell_") + m_curveChannel;
  296. for (QFrame *f : m_cellFrames) {
  297. const bool on = (f->objectName() == want);
  298. if (f->property("selected").toBool() == on)
  299. continue;
  300. f->setProperty("selected", on);
  301. f->style()->unpolish(f);
  302. f->style()->polish(f);
  303. }
  304. }
  305. // ---------------------------------------------------------------------------
  306. // 温控曲线:QCustomPlot,横轴最近 200 个采样点,纵轴 20~90 °C
  307. // 样式全部在这里设,.ui 里只有控件本体
  308. // ---------------------------------------------------------------------------
  309. void DetailView::setupCurvePlot()
  310. {
  311. QCustomPlot *p = m_ui->curvePlot;
  312. if (!p)
  313. return;
  314. p->setBackground(QBrush(QColor("#ffffff")));
  315. p->setNoAntialiasingOnDrag(true);
  316. QFont font = p->font();
  317. font.setPointSize(9);
  318. p->setFont(font);
  319. const QColor axisColor("#c6d2df");
  320. const QColor subTickColor("#dde5ee");
  321. const QColor labelColor("#6b7d90");
  322. const QColor gridColor("#eef2f7");
  323. for (QCPAxis *ax : {p->xAxis, p->yAxis}) {
  324. ax->setBasePen(QPen(axisColor, 1));
  325. ax->setTickPen(QPen(axisColor, 1));
  326. ax->setSubTickPen(QPen(subTickColor, 1));
  327. ax->setTickLabelColor(labelColor);
  328. ax->setLabelColor(labelColor);
  329. ax->setTickLength(0, 4);
  330. ax->grid()->setPen(QPen(gridColor, 1, Qt::DotLine));
  331. ax->grid()->setSubGridVisible(false);
  332. ax->grid()->setZeroLinePen(Qt::NoPen);
  333. }
  334. p->xAxis->setLabel(QStringLiteral("采样点"));
  335. p->yAxis->setLabel(QStringLiteral("温度 (°C)"));
  336. p->xAxis->setRange(0, kTempHistorySize);
  337. p->yAxis->setRange(20, 90);
  338. p->xAxis->ticker()->setTickCount(6); // 0 / 40 / 80 / 120 / 160 / 200
  339. p->yAxis->ticker()->setTickCount(8); // 20 ~ 90,每 10 一格
  340. p->xAxis->setSubTicks(false);
  341. p->yAxis->setSubTicks(false);
  342. // 用 CurveGraph 代替 addGraph():曲线画法不变,只是把图例里的小色块换成圆头短棒
  343. QCPGraph *g = new CurveGraph(p->xAxis, p->yAxis);
  344. g->setPen(QPen(QColor("#2f7fc1"), 1.6));
  345. g->setLineStyle(QCPGraph::lsLine);
  346. g->setScatterStyle(QCPScatterStyle::ssNone);
  347. g->setAntialiased(true);
  348. g->setName(QStringLiteral("腔体温度")); // 曲线名,进详情页后会换成「工位号 · 腔体温度」
  349. // 图例:右上角白色小框,曲线名由 renderCurve() 随工位更新
  350. QCPLegend *lg = p->legend;
  351. lg->setVisible(true);
  352. lg->setBrush(QBrush(QColor(255, 255, 255, 225)));
  353. lg->setBorderPen(QPen(QColor("#dde5ee")));
  354. lg->setTextColor(QColor("#5d7086"));
  355. lg->setFont(font);
  356. // 图标框取整行高(这样它在图例行里就是垂直居中的),
  357. // 色块本身再按字体度量放进去:高度 = 大写字母高度,顶边 = ascent - capHeight
  358. const QFontMetrics lgFm(font);
  359. g_swatchH = qMax(8, lgFm.capHeight());
  360. g_swatchTop = qMax(0, lgFm.ascent() - lgFm.capHeight());
  361. lg->setIconSize(20, lgFm.height());
  362. lg->setIconTextPadding(6);
  363. lg->setRowSpacing(0);
  364. p->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
  365. // 最新点:一个白底蓝边的小圆点 + 下面的温度数值(跟着数据一起走,见 renderCurve)
  366. m_lastDot = new QCPItemTracer(p);
  367. m_lastDot->setGraph(g);
  368. m_lastDot->setStyle(QCPItemTracer::tsCircle);
  369. m_lastDot->setSize(5);
  370. m_lastDot->setPen(QPen(QColor("#2f7fc1"), 1.6));
  371. m_lastDot->setBrush(QBrush(Qt::white));
  372. m_lastDot->setSelectable(false);
  373. m_lastDot->setClipToAxisRect(false);
  374. m_lastLabel = new QCPItemText(p);
  375. m_lastLabel->setSelectable(false);
  376. m_lastLabel->setClipToAxisRect(false);
  377. m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
  378. m_lastLabel->position->setType(QCPItemPosition::ptPlotCoords);
  379. m_lastLabel->setPadding(QMargins(2, 6, 2, 1)); // 上边多留点,让标签离开圆点
  380. m_lastLabel->setPen(Qt::NoPen);
  381. m_lastLabel->setBrush(Qt::NoBrush); // 不要底色块,只留文字
  382. m_lastLabel->setColor(QColor("#2f7fc1"));
  383. {
  384. QFont lf = font;
  385. lf.setPointSizeF(8.5);
  386. lf.setBold(true);
  387. m_lastLabel->setFont(lf);
  388. }
  389. p->replot();
  390. }
  391. void DetailView::renderCurve()
  392. {
  393. if (m_index < 0 || m_index >= m_store->count())
  394. return;
  395. QCustomPlot *p = m_ui->curvePlot;
  396. if (!p || p->graphCount() == 0)
  397. return;
  398. const StationData &d = m_store->station(m_index);
  399. // 找当前选中的通道;工位换了但通道名还在(24 个通道各工位同名),所以直接按名字找
  400. const ChannelInfo *sel = nullptr;
  401. for (const ChannelInfo &c : d.channels) {
  402. if (c.name == m_curveChannel) {
  403. sel = &c;
  404. break;
  405. }
  406. }
  407. if (!sel && !d.channels.isEmpty())
  408. sel = &d.channels.first();
  409. if (!sel)
  410. return;
  411. const int n = sel->history.size();
  412. const int start = sel->historyStart;
  413. QVector<double> keys(n);
  414. QVector<double> values(n);
  415. for (int i = 0; i < n; ++i) {
  416. keys[i] = start + i; // 全局采样序号,不回绕
  417. values[i] = sel->history.at(i);
  418. }
  419. QCPGraph *g = p->graph(0);
  420. g->setData(keys, values, true);
  421. // 图例显示通道名:CPU0_CHA
  422. if (g->name() != sel->name)
  423. g->setName(sel->name);
  424. // 横轴滚动:可见宽度恒为 kTempHistorySize 点,起点跟着最新数据走(刻度数字会一起滚)
  425. double lo = start;
  426. if (n < kTempHistorySize)
  427. lo = 0; // 还没满 200 点时从 0 开始铺,不从负数起
  428. p->xAxis->setRange(lo, lo + kTempHistorySize);
  429. // 最新点标记 + 数值标签:贴在最后一个采样点下方
  430. if (m_lastDot && m_lastLabel && n > 0) {
  431. const double k = keys.last();
  432. const double v = values.last();
  433. m_lastDot->setGraphKey(k);
  434. m_lastDot->setVisible(true);
  435. m_lastLabel->setText(QStringLiteral("%1°C").arg(QString::number(v, 'f', 1)));
  436. m_lastLabel->position->setCoords(k, v);
  437. m_lastLabel->setVisible(true);
  438. // 点贴近轴的右边缘时改成右对齐,否则标签会甩到图外
  439. const double px = p->xAxis->coordToPixel(k);
  440. const QRect rect = p->axisRect()->rect();
  441. if (px > rect.right() - 40)
  442. m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignRight);
  443. else if (px < rect.left() + 40)
  444. m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignLeft);
  445. else
  446. m_lastLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
  447. } else if (m_lastDot && m_lastLabel) {
  448. m_lastDot->setVisible(false);
  449. m_lastLabel->setVisible(false);
  450. }
  451. p->replot(QCustomPlot::rpQueuedReplot);
  452. }
  453. void DetailView::onTemperatureChanged(int index)
  454. {
  455. if (index != m_index)
  456. return; // 不在当前工位:数据留在模型里,切回来整体重铺
  457. if (m_ui->detailTabStack->currentIndex() != 0)
  458. return; // 曲线页没显示,画了也看不见
  459. renderCurve();
  460. }
  461. void DetailView::cacheChannelWidgets()
  462. {
  463. m_cellFrames.clear();
  464. m_cellValues.clear();
  465. const QStringList names = channelNames();
  466. for (const QString &name : names) {
  467. QFrame *frame = m_ui->detailPage->findChild<QFrame *>(QStringLiteral("chCell_") + name);
  468. QLabel *value = m_ui->detailPage->findChild<QLabel *>(QStringLiteral("chCellValue_") + name);
  469. if (!frame || !value)
  470. continue;
  471. // 点通道格子 → 曲线切到该通道
  472. frame->installEventFilter(this);
  473. frame->setCursor(Qt::PointingHandCursor);
  474. m_cellFrames.append(frame);
  475. m_cellValues.append(value);
  476. }
  477. }
  478. // ---------------------------------------------------------------------------
  479. // 打开 / 关闭
  480. // ---------------------------------------------------------------------------
  481. void DetailView::showStation(int index)
  482. {
  483. if (index < 0 || index >= m_store->count())
  484. return;
  485. m_index = index;
  486. loadParamConfig();
  487. m_ui->pageStack->setCurrentIndex(1);
  488. // 纯内存渲染,不依赖任何异步取数 —— 点开即出
  489. markCurveChannel(); // 高亮当前曲线对应的通道(默认 CPU0_CHA)
  490. renderAll();
  491. }
  492. void DetailView::leave()
  493. {
  494. m_index = -1;
  495. m_flush.stop();
  496. m_needTitle = m_needChannels = false;
  497. }
  498. // ---------------------------------------------------------------------------
  499. // 全量渲染(首次进入 / 切换工位)
  500. // ---------------------------------------------------------------------------
  501. void DetailView::renderAll()
  502. {
  503. renderTitle();
  504. renderChannels();
  505. renderCurve();
  506. renderLogs();
  507. }
  508. void DetailView::renderTitle()
  509. {
  510. // 标题栏的“设备详情页 ·”是静态控件,代码只填工位号
  511. const StationData &d = m_store->station(m_index);
  512. m_ui->detailDevice->setText(d.deviceId.isEmpty() ? QStringLiteral("—") : d.deviceId);
  513. }
  514. void DetailView::renderChannels()
  515. {
  516. const StationData &d = m_store->station(m_index);
  517. const int n = qMin(m_cellFrames.size(), d.channels.size());
  518. for (int i = 0; i < n; ++i) {
  519. const ChannelInfo &ch = d.channels.at(i);
  520. // 未装载不显示温度,用 --℃ 占位;已装载只显示温度,不区分 PASS / FAIL
  521. m_cellValues[i]->setText(ch.temperature < 0.0
  522. ? QStringLiteral("--℃")
  523. : QStringLiteral("%1 ℃").arg(QString::number(ch.temperature, 'f', 1)));
  524. QFrame *frame = m_cellFrames[i];
  525. const QString state = (ch.temperature < 0.0) ? QStringLiteral("idle")
  526. : QStringLiteral("loaded");
  527. if (frame->property("state").toString() == state)
  528. continue;
  529. frame->setProperty("state", state);
  530. // 兜底重刷:QSS 里值文字的颜色挂在父框的 state 上
  531. frame->style()->unpolish(frame);
  532. frame->style()->polish(frame);
  533. m_cellValues[i]->style()->unpolish(m_cellValues[i]);
  534. m_cellValues[i]->style()->polish(m_cellValues[i]);
  535. }
  536. }
  537. void DetailView::renderLogs()
  538. {
  539. const StationData &d = m_store->station(m_index);
  540. QPlainTextEdit *log = m_ui->detailLog;
  541. const int total = d.logs.size();
  542. if (m_logStation == m_index && total >= m_logLines) {
  543. // 同一工位:只把新增的尾巴整块追加进去。
  544. // 实测 7.5 万行:整块 append 约 0.2s,逐行 append 会退化到 13s 以上,
  545. // 所以这里必须一次性插入,绝不能 for 循环 appendPlainText。
  546. if (total > m_logLines) {
  547. log->appendPlainText(d.logs.mid(m_logLines).join(QLatin1Char('\n')));
  548. m_logLines = total;
  549. }
  550. } else {
  551. // 换工位:整篇重铺(7.5 万行约 0.4s,属可接受的一次性开销)
  552. log->setPlainText(d.logs.join(QLatin1Char('\n')));
  553. m_logStation = m_index;
  554. m_logLines = total;
  555. }
  556. log->verticalScrollBar()->setValue(log->verticalScrollBar()->maximum());
  557. }
  558. void DetailView::selectTab(int index)
  559. {
  560. QPushButton *tabs[kTabCount] = {m_ui->detailTab_0, m_ui->detailTab_1, m_ui->detailTab_2};
  561. for (int i = 0; i < kTabCount; ++i) {
  562. tabs[i]->setProperty("active", i == index ? QStringLiteral("true") : QStringLiteral("false"));
  563. tabs[i]->style()->unpolish(tabs[i]);
  564. tabs[i]->style()->polish(tabs[i]);
  565. }
  566. m_ui->detailTabStack->setCurrentIndex(index);
  567. if (index == 0) // 切回曲线页时补画一次,避免看到离开期间的旧曲线
  568. renderCurve();
  569. }
  570. // ---------------------------------------------------------------------------
  571. // 实时刷新:日志走快路径,其余 120ms 合并
  572. // ---------------------------------------------------------------------------
  573. void DetailView::onLogAppended(int index, const QString &line)
  574. {
  575. if (index != m_index)
  576. return; // 不在当前工位:只留在模型里,切回来时一次性铺全
  577. QPlainTextEdit *log = m_ui->detailLog;
  578. QScrollBar *bar = log->verticalScrollBar();
  579. const bool follow = (bar->value() >= bar->maximum() - 4); // 用户往上翻就不抢滚动条
  580. log->appendPlainText(line);
  581. m_logStation = index;
  582. m_logLines = m_store->station(index).logs.size();
  583. if (follow)
  584. bar->setValue(bar->maximum());
  585. }
  586. void DetailView::onChannelsChanged(int index)
  587. {
  588. if (index != m_index)
  589. return;
  590. m_needChannels = true;
  591. requestFlush();
  592. }
  593. void DetailView::onInfoChanged(int index)
  594. {
  595. if (index != m_index)
  596. return;
  597. m_needTitle = true; // 标题栏的工位号跟着任务信息一起变
  598. requestFlush();
  599. }
  600. void DetailView::requestFlush()
  601. {
  602. if (!m_flush.isActive())
  603. m_flush.start();
  604. }
  605. void DetailView::flushPending()
  606. {
  607. if (m_index < 0)
  608. return;
  609. if (m_needTitle) {
  610. m_needTitle = false;
  611. renderTitle();
  612. }
  613. if (m_needChannels) {
  614. m_needChannels = false;
  615. renderChannels();
  616. // 曲线看的是通道温度,所以通道数据一变也要重画(只在曲线页可见时做)
  617. if (m_ui->detailTabStack->currentIndex() == 0)
  618. renderCurve();
  619. }
  620. }