gen_ui.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. # -*- coding: utf-8 -*-
  2. """
  3. 生成 src/mainwindow.ui (Qt Designer XML)
  4. DDR5 RDIMM SLT 上位机监控平台 —— 多工位测试监控界面
  5. 用法: python tools/gen_ui.py
  6. 说明:
  7. * 界面结构 100% 落在 .ui 文件里,可直接用 Qt Designer 打开继续拖拽编辑;
  8. * 所有颜色/字号/圆角集中在 src/style/app.qss,.ui 里只保留结构 + role 属性,
  9. 便于改主题;动态属性(role / tone / pill)用作 QSS 选择器钩子。
  10. """
  11. import os
  12. import textwrap
  13. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  14. OUT = os.path.join(ROOT, "src", "mainwindow.ui")
  15. MAXW = 16777215
  16. # --------------------------------------------------------------------------
  17. # XML 片段工具
  18. # --------------------------------------------------------------------------
  19. def esc(v):
  20. return (v.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
  21. .replace('"', "&quot;"))
  22. def prop(name, inner, stdset=False):
  23. a = ' stdset="0"' if stdset else ""
  24. return '<property name="%s"%s>%s</property>\n' % (name, a, inner)
  25. def pstr(name, v, stdset=False):
  26. return prop(name, "<string>%s</string>" % esc(v), stdset)
  27. def pnum(name, v):
  28. return prop(name, "<number>%s</number>" % v)
  29. def ppixmap(name, path):
  30. return prop(name, '<pixmap resource="resources.qrc">%s</pixmap>' % esc(path))
  31. def pbool(name, v):
  32. return prop(name, "<bool>%s</bool>" % ("true" if v else "false"))
  33. def psize(name, w, h, stdset=False):
  34. return prop(name, "<size><width>%d</width><height>%d</height></size>" % (w, h), stdset)
  35. def penum(name, v):
  36. return prop(name, "<enum>%s</enum>" % v)
  37. def palign(expr):
  38. return prop("alignment", "<set>%s</set>" % expr)
  39. def prect(name, x, y, w, h):
  40. return prop(name, "<rect><x>%d</x><y>%d</y><width>%d</width><height>%d</height></rect>"
  41. % (x, y, w, h))
  42. def pspol(h="Preferred", v="Preferred", hs=0, vs=0):
  43. return prop("sizePolicy",
  44. '<sizepolicy hsizetype="%s" vsizetype="%s">'
  45. "<horstretch>%d</horstretch><verstretch>%d</verstretch></sizepolicy>"
  46. % (h, v, hs, vs))
  47. def pfixh(h):
  48. """固定高度:min=max=h,配合 Fixed 垂直策略使用"""
  49. return (psize("minimumSize", 0, h) + psize("maximumSize", MAXW, h) +
  50. pspol("Preferred", "Fixed"))
  51. def role(name):
  52. return pstr("role", name, stdset=True)
  53. def widget(cls, name, props="", body=""):
  54. s = '<widget class="%s" name="%s">\n' % (cls, name)
  55. if props:
  56. s += textwrap.indent(props, " ")
  57. if body:
  58. s += textwrap.indent(body, " ")
  59. s += "</widget>\n"
  60. return s
  61. def layout(cls, name, items, spacing=0, margins=(0, 0, 0, 0), props=""):
  62. s = '<layout class="%s" name="%s">\n' % (cls, name)
  63. l, t, r, b = margins
  64. inner = (pnum("leftMargin", l) + pnum("topMargin", t) +
  65. pnum("rightMargin", r) + pnum("bottomMargin", b) + pnum("spacing", spacing) + props)
  66. s += textwrap.indent(inner, " ")
  67. for it in items:
  68. s += textwrap.indent(it, " ")
  69. s += "</layout>\n"
  70. return s
  71. def it(w, row=None, col=None, rowspan=None):
  72. # 注意:uic 只允许 <item> 上的 row/column/rowspan/colspan,写 stretch 会报 Unexpected attribute,
  73. # 布局伸缩比例统一在 mainwindow.cpp 里用 setStretch() / setRowStretch() 设置
  74. attr = ' row="%d" column="%d"' % (row, col) if row is not None else ""
  75. if rowspan and rowspan > 1:
  76. attr += ' rowspan="%d"' % rowspan
  77. return "<item%s>\n" % attr + textwrap.indent(w, " ") + "</item>\n"
  78. def stack(name, pages, current=0):
  79. """QStackedWidget:子页面直接内联,不能用 <item> 包裹"""
  80. s = '<widget class="QStackedWidget" name="%s">\n' % name
  81. s += textwrap.indent(penum("frameShape", "QFrame::NoFrame") +
  82. pnum("currentIndex", current), " ")
  83. for pg in pages:
  84. s += textwrap.indent(pg, " ")
  85. s += "</widget>\n"
  86. return s
  87. def spacer(name, w=20, h=20, orient="Qt::Horizontal", size_type="QSizePolicy::Fixed"):
  88. s = '<spacer name="%s">\n' % name
  89. s += textwrap.indent(penum("orientation", orient), " ")
  90. if size_type:
  91. s += textwrap.indent(penum("sizeType", size_type), " ")
  92. s += textwrap.indent(psize("sizeHint", w, h, stdset=True), " ")
  93. s += "</spacer>\n"
  94. return s
  95. def label(name, text, role_name=None, align=None, rich=False, size=None, maxsize=None, policy=None,
  96. props=""):
  97. p = pstr("text", text)
  98. if role_name:
  99. p += role(role_name)
  100. if align:
  101. p += palign(align)
  102. if rich:
  103. p += penum("textFormat", "Qt::RichText")
  104. if size:
  105. p += psize("minimumSize", size[0], size[1])
  106. if maxsize:
  107. p += psize("maximumSize", maxsize[0], maxsize[1])
  108. if policy:
  109. p += pspol(*policy)
  110. p += props
  111. return widget("QLabel", name, p)
  112. def vsep(name):
  113. return widget("QFrame", name, penum("frameShape", "QFrame::NoFrame") + role("vsep") +
  114. psize("minimumSize", 1, 0) + psize("maximumSize", 1, MAXW))
  115. def hline(name, role_name="hsep"):
  116. return widget("QFrame", name, penum("frameShape", "QFrame::NoFrame") + role(role_name) +
  117. psize("minimumSize", 0, 1) + psize("maximumSize", MAXW, 1))
  118. # --------------------------------------------------------------------------
  119. # 顶部标题栏(只保留品牌区 + 窗口按钮;一级页签已按需求移除)
  120. # --------------------------------------------------------------------------
  121. def build_header():
  122. logo = widget("QLabel", "logoMark",
  123. ppixmap("pixmap", ":/assets/logo-mark.png") + role("logo") +
  124. palign("Qt::AlignCenter") +
  125. psize("minimumSize", 33, 32) + psize("maximumSize", 33, 32))
  126. title = label("appTitle", "HOVSE SLT Pro", "appTitle", "Qt::AlignLeft|Qt::AlignVCenter")
  127. win_items = [it(widget("QPushButton", n, pstr("text", t) + role("win") +
  128. psize("minimumSize", 44, 44) + psize("maximumSize", 44, 44)))
  129. for n, t in (("btnWinMin", "—"), ("btnWinMax", "❐"), ("btnWinClose", "✕"))]
  130. bar = layout("QHBoxLayout", "headerLayout",
  131. [it(logo), it(title),
  132. it(spacer("headerSpacer", 40, 20, size_type="QSizePolicy::Expanding")),
  133. it(widget("QFrame", "headerSep", penum("frameShape", "QFrame::NoFrame") + role("headerSep") +
  134. psize("minimumSize", 1, 26) + psize("maximumSize", 1, 26)))]
  135. + win_items,
  136. spacing=10, margins=(18, 0, 0, 0))
  137. return widget("QFrame", "header",
  138. penum("frameShape", "QFrame::NoFrame") + role("header") +
  139. psize("minimumSize", 0, 70) + psize("maximumSize", MAXW, 70), bar)
  140. # --------------------------------------------------------------------------
  141. # 工位卡片
  142. # --------------------------------------------------------------------------
  143. STATE_MAP = {"p": "pass", "r": "run", "f": "fail", "i": "idle"}
  144. STATIONS = [
  145. dict(sid="SLT-HT-001",
  146. temp="68.4°C",
  147. runtime="02:36:18",
  148. ch=["ppprpippppfp", "prppppprpppr"]),
  149. dict(sid="SLT-HT-002",
  150. temp="67.8°C",
  151. runtime="01:48:32",
  152. ch=["pppripppprpp", "pppppppprppp"]),
  153. dict(sid="SLT-HT-003",
  154. temp="70.2°C",
  155. runtime="00:58:41",
  156. ch=["ppprpppppprf", "prpppfpprprr"]),
  157. dict(sid="SLT-HT-004",
  158. temp="65.6°C",
  159. runtime="01:15:27",
  160. ch=["ppprrpppprpp", "prppppprpprr"]),
  161. ]
  162. def info_cell(idx, key, label_text, value, min_w=0, hs=2, bold=False):
  163. """信息列:字段名 / 数值 两行(下面的说明小字已按需求去掉),行高固定,两张卡节奏一致"""
  164. # 水平 Expanding:整行四块(温度 / 运行时间 / 开始测试 / 串口)等比例均分宽度,
  165. # 不再靠行尾 spacer 把富余一口吃掉、把内容全挤到左边
  166. p = (penum("frameShape", "QFrame::NoFrame") + role("infoCell") +
  167. pspol("Expanding", "Preferred", hs))
  168. if min_w:
  169. p += psize("minimumSize", min_w, 0)
  170. # 上下各一个弹性 spacer:两行文字在 70px 的行高里垂直居中(和右边的圆钮对齐)
  171. cell = layout("QVBoxLayout", "infoLayout_%d_%s" % (idx, key), [
  172. it(spacer("infoPadTop_%d_%s" % (idx, key), 4, 2, orient="Qt::Vertical",
  173. size_type="QSizePolicy::Expanding")),
  174. it(label("infoLabel_%d_%s" % (idx, key), label_text, "fieldLabel", "Qt::AlignLeft|Qt::AlignBottom")),
  175. it(label("infoValue_%d_%s" % (idx, key), value, "fieldValueBold" if bold else "fieldValue",
  176. "Qt::AlignLeft|Qt::AlignVCenter")),
  177. it(spacer("infoPad_%d_%s" % (idx, key), 4, 2, orient="Qt::Vertical",
  178. size_type="QSizePolicy::Expanding")),
  179. ], spacing=1, margins=(10, 2, 6, 2))
  180. return widget("QFrame", "infoCell_%d_%s" % (idx, key), p, cell)
  181. CARD_MIN_H = 0
  182. OV_CELL_H = 36 # 最小高度:两行文字(16+18)留 2px 余量,窗口只有 700 时四行仍完整
  183. OV_CELL_MAX_H = 56 # 窗口拉高时格子跟着长,最多到这里(再高就留白)
  184. OV_GRID_SPACING = 3
  185. OV_STATE_TEXT = {"pass": "PASS", "fail": "FAIL", "idle": "IDLE"}
  186. def channel_names():
  187. """24 通道顺序:CPU0_CHA..CHL、CPU1_CHA..CHL,与详情页保持一致"""
  188. return ["CPU%d_CH%s" % (c, s) for c in (0, 1) for s in "ABCDEFGHIJKL"]
  189. def ov_cell(idx, name, state):
  190. """总览卡里的通道格:上排通道名、下排状态字(PASS / FAIL / IDLE),配色由 QSS 按 state 切。
  191. 上下各放一个弹性 spacer:格子被拉高时两行文字保持贴在一起并垂直居中,而不是被拉散。"""
  192. body = layout("QVBoxLayout", "ovChCellLayout_%d_%s" % (idx, name), [
  193. it(spacer("ovChPadTop_%d_%s" % (idx, name), 4, 2, orient="Qt::Vertical",
  194. size_type="QSizePolicy::Expanding")),
  195. it(label("ovChCellName_%d_%s" % (idx, name), name, "chCellName", "Qt::AlignCenter")),
  196. it(label("ovChCellValue_%d_%s" % (idx, name), OV_STATE_TEXT[state],
  197. "chCellValue", "Qt::AlignCenter")),
  198. it(spacer("ovChPadBot_%d_%s" % (idx, name), 4, 2, orient="Qt::Vertical",
  199. size_type="QSizePolicy::Expanding")),
  200. ], spacing=0, margins=(2, 1, 2, 1))
  201. p = (penum("frameShape", "QFrame::NoFrame") + role("chCell") +
  202. pstr("state", state, stdset=True) +
  203. psize("minimumSize", 0, OV_CELL_H) +
  204. # 不再封顶高度:格子吃掉 GroupBox 里的全部剩余空间,
  205. # 否则多余高度会跑到图例行上,把图例顶到半空
  206. pspol("Expanding", "Expanding"))
  207. return widget("QFrame", "ovChCell_%d_%s" % (idx, name), p, body)
  208. def build_station(idx, st):
  209. icon = widget("QLabel", "cardIcon_%d" % idx,
  210. pstr("text", "▤") + role("cardIcon") + palign("Qt::AlignCenter") +
  211. psize("minimumSize", 24, 24) + psize("maximumSize", 24, 24))
  212. # 卡头右侧:COM 串口名 + COM 连接状态(状态用动态属性 state 切换 QSS 配色)
  213. # 串口名和状态灯都是纯文字(QSS 里已去掉外框),两行统一水平居中
  214. com_name = widget("QLabel", "cardComName_%d" % idx,
  215. pstr("text", "—") + role("cardPort") + palign("Qt::AlignCenter") +
  216. psize("minimumSize", 0, 20))
  217. com_chip = widget("QLabel", "cardCom_%d" % idx,
  218. pstr("text", "● 未配置") + role("comState") +
  219. pstr("state", "offline", stdset=True) +
  220. pstr("plain", "true", stdset=True) + palign("Qt::AlignCenter"))
  221. head = widget("QFrame", "cardHead_%d" % idx, penum("frameShape", "QFrame::NoFrame") +
  222. role("cardHead") + pfixh(26),
  223. layout("QHBoxLayout", "cardHeadLayout_%d" % idx, [
  224. it(icon),
  225. it(label("cardName_%d" % idx, st["sid"], "cardName", "Qt::AlignLeft|Qt::AlignVCenter")),
  226. it(spacer("cardSpacer_%d" % idx, 12, 10, size_type="QSizePolicy::Expanding")),
  227. it(widget("QPushButton", "cardDetail_%d" % idx,
  228. pstr("text", "查看详情 >") + role("cardDetail"))),
  229. ], spacing=13, margins=(0, 0, 0, 0)))
  230. # 四块等比例分布:温度 │ 运行时间 │ 串口 │ 开始测试(串口和按钮的位置按需求对调),
  231. # 竖分界线固定 1px 不参与均分;按钮块内部左右各垫一个 Expanding spacer,
  232. # 圆钮始终落在自己那四分之一的正中。
  233. btn_block = widget("QWidget", "btnBlock_%d" % idx,
  234. pspol("Expanding", "Preferred") + psize("minimumSize", 96, 0),
  235. layout("QHBoxLayout", "btnBlockLayout_%d" % idx, [
  236. it(spacer("btnPadL_%d" % idx, 0, 0, size_type="QSizePolicy::Expanding")),
  237. it(widget("PowerButton", "startTest_%d" % idx,
  238. psize("minimumSize", 76, 78) + psize("maximumSize", 76, 78))),
  239. it(spacer("btnPadR_%d" % idx, 0, 0, size_type="QSizePolicy::Expanding")),
  240. ], spacing=0, margins=(0, 0, 0, 0)))
  241. com_block = widget("QWidget", "comWrap_%d" % idx,
  242. pspol("Expanding", "Preferred") + psize("minimumSize", 84, 0),
  243. layout("QVBoxLayout", "comWrapLayout_%d" % idx, [
  244. it(com_name), it(com_chip),
  245. ], spacing=1, margins=(8, 0, 8, 0)))
  246. info_items = [
  247. it(info_cell(idx, "temp", "腔体温度", st["temp"], 92, 2, bold=True)),
  248. it(vsep("vsepB_%d" % idx)),
  249. it(info_cell(idx, "runtime", "运行时间", st["runtime"], 100, 2, bold=True)),
  250. it(vsep("vsepC_%d" % idx)),
  251. it(com_block),
  252. it(vsep("vsepD_%d" % idx)),
  253. it(btn_block),
  254. ]
  255. # 行高 80:46px 的圆钮 + 下面一行 8.5pt 的「开始测试」,按钮实际占 78px。
  256. # 文字区 = 78 - 47(圆底) - 2 = 29px,居中后上下各留 4~5px 呼吸;
  257. # 给到 70 会被切掉、给到 74 只是不切但仍然贴底,所以留到 80。
  258. info_row = widget("QFrame", "infoRow_%d" % idx, penum("frameShape", "QFrame::NoFrame") + role("infoRow") +
  259. pfixh(80),
  260. layout("QHBoxLayout", "infoRowLayout_%d" % idx, info_items, spacing=0, margins=(0, 0, 0, 0)))
  261. # 24 通道:4 行 × 6 列,与详情页同一套格子样式(PASS / FAIL / IDLE)
  262. names = channel_names()
  263. states = [STATE_MAP[ch] for ch in (st["ch"][0] + st["ch"][1])]
  264. cells = []
  265. for i, (nm, state) in enumerate(zip(names, states)):
  266. if state == "run": # RUN 状态已移除,统一按 PASS 展示
  267. state = "pass"
  268. cells.append(it(ov_cell(idx, nm, state), i // 6, i % 6))
  269. ch_grid = layout("QGridLayout", "ovChGrid_%d" % idx, cells, spacing=OV_GRID_SPACING,
  270. margins=(0, 0, 0, 0))
  271. # 通道区块:普通文字标题(与详情页「24 通道内存测试状态」同一套 panelTitle 样式),不再加边框和图例
  272. # 标题单独用 ovChTitle:上下各留 5px 呼吸空间(间距写在 QSS 的 padding 里)
  273. ch_block = layout("QVBoxLayout", "ovChLayout_%d" % idx,
  274. [it(panel_title("ovChTitle_%d" % idx, "24 通道内存测试状态", "ovChTitle")), it(ch_grid)],
  275. spacing=4, margins=(0, 0, 0, 0))
  276. body = layout("QVBoxLayout", "cardLayout_%d" % idx, [
  277. it(head),
  278. it(spacer("cardGapA_%d" % idx, 10, 4, orient="Qt::Vertical")),
  279. it(hline("cardLine1_%d" % idx)),
  280. it(spacer("cardGapB_%d" % idx, 10, 4, orient="Qt::Vertical")),
  281. it(info_row),
  282. it(spacer("cardGapC_%d" % idx, 10, 4, orient="Qt::Vertical")),
  283. it(ch_block),
  284. ], spacing=0, margins=(13, 9, 13, 8))
  285. return widget("QFrame", "stationCard_%d" % idx,
  286. penum("frameShape", "QFrame::NoFrame") + role("station") +
  287. pspol("Expanding", "Expanding") + psize("minimumSize", 0, CARD_MIN_H), body)
  288. # --------------------------------------------------------------------------
  289. # 内容区
  290. # --------------------------------------------------------------------------
  291. def build_content():
  292. page_title = widget("QLabel", "pageTitle",
  293. pstr("text", "<span style='font-size:21px;font-weight:700;color:#16324a;'>多工位测试监控</span>"
  294. "&nbsp;&nbsp;<span style='font-size:15px;color:#3a5772;'>Multi-Station Monitor</span>") +
  295. role("pageTitle") + penum("textFormat", "Qt::RichText") +
  296. palign("Qt::AlignLeft|Qt::AlignVCenter"))
  297. title_row = widget("QFrame", "titleRow", penum("frameShape", "QFrame::NoFrame") + role("titleRow") +
  298. pfixh(34),
  299. layout("QHBoxLayout", "titleRowLayout", [
  300. it(page_title),
  301. it(spacer("titleSpacer", 30, 10, size_type="QSizePolicy::Expanding")),
  302. ], spacing=10))
  303. grid = layout("QGridLayout", "stationGrid", [
  304. it(build_station(0, STATIONS[0]), 0, 0),
  305. it(build_station(1, STATIONS[1]), 0, 1),
  306. it(build_station(2, STATIONS[2]), 1, 0),
  307. it(build_station(3, STATIONS[3]), 1, 1),
  308. ], spacing=14)
  309. body = layout("QVBoxLayout", "contentBody", [
  310. it(title_row),
  311. it(grid),
  312. ], spacing=12, margins=(20, 8, 20, 14))
  313. return widget("QWidget", "contentArea", role("contentArea"),
  314. layout("QVBoxLayout", "contentLayout", [
  315. it(body),
  316. ], spacing=0))
  317. # --------------------------------------------------------------------------
  318. # 设备详情页(工位卡「查看详情 >」进入,返回键回多机监控)
  319. # --------------------------------------------------------------------------
  320. def panel_title(name, text, role_name="panelTitle"):
  321. return label(name, text, role_name, "Qt::AlignLeft|Qt::AlignVCenter")
  322. # 详情页 24 通道:4 行 × 6 列(CPU0 十二通道 / CPU1 十二通道)
  323. # 状态合计:pass 16 + run 4 + fail 2 + idle 2 = 24(已装载 22,异常 2)
  324. DETAIL_CH = [
  325. [("CPU0_CHA", "64.0", "pass"), ("CPU0_CHB", "67.5", "pass"), ("CPU0_CHC", "66.5", "pass"),
  326. ("CPU0_CHD", "65.5", "pass"), ("CPU0_CHE", "64.5", "pass"), ("CPU0_CHF", "68.0", "pass")],
  327. [("CPU0_CHG", "67.0", "pass"), ("CPU0_CHH", "66.0", "pass"), ("CPU0_CHI", "65.0", "pass"),
  328. ("CPU0_CHJ", "65.0", "fail"), ("CPU0_CHK", "67.5", "pass"), ("CPU0_CHL", "66.5", "pass")],
  329. [("CPU1_CHA", "65.5", "pass"), ("CPU1_CHB", "64.5", "pass"), ("CPU1_CHC", "68.0", "fail"),
  330. ("CPU1_CHD", "67.0", "pass"), ("CPU1_CHE", "66.0", "pass"), ("CPU1_CHF", "65.0", "pass")],
  331. [("CPU1_CHG", "64.0", "pass"), ("CPU1_CHH", "67.5", "pass"), ("CPU1_CHI", "66.5", "pass"),
  332. ("CPU1_CHJ", "—", "idle"), ("CPU1_CHK", "—", "idle"), ("CPU1_CHL", "68.0", "pass")],
  333. ]
  334. def ch_cell(name, label_text, temp, state):
  335. # 详情页只区分「已装载 / 未装载」:已装载统一蓝色(不再分 PASS/FAIL 配色)
  336. ui_state = "idle" if state == "idle" else "loaded"
  337. value = "--℃" if state == "idle" else "%s ℃" % temp
  338. body = layout("QVBoxLayout", "chCellLayout_%s" % name, [
  339. it(spacer("chCellPadTop_%s" % name, 4, 2, orient="Qt::Vertical",
  340. size_type="QSizePolicy::Expanding")),
  341. it(label("chCellName_%s" % name, label_text, "chCellName", "Qt::AlignCenter")),
  342. it(label("chCellValue_%s" % name, value, "chCellValue", "Qt::AlignCenter")),
  343. it(spacer("chCellPadBot_%s" % name, 4, 2, orient="Qt::Vertical",
  344. size_type="QSizePolicy::Expanding")),
  345. ], spacing=0, margins=(2, 2, 2, 2))
  346. # 高度 42~64 弹性:卡片高时四行一起撑开,不会在表头下面留一条空白
  347. p = (penum("frameShape", "QFrame::NoFrame") + role("chCell") +
  348. pstr("state", ui_state, stdset=True) +
  349. psize("minimumSize", 0, 42) + psize("maximumSize", MAXW, 64) +
  350. pspol("Expanding", "Expanding"))
  351. return widget("QFrame", "chCell_%s" % name, p, body)
  352. def tab_page(name, hint):
  353. return widget("QWidget", name, "",
  354. layout("QVBoxLayout", name + "Layout", [
  355. it(spacer(name + "PadTop", 10, 2, orient="Qt::Vertical",
  356. size_type="QSizePolicy::Expanding")),
  357. it(label(name + "Hint", hint, "placeholder", "Qt::AlignCenter")),
  358. it(spacer(name + "PadBot", 10, 2, orient="Qt::Vertical",
  359. size_type="QSizePolicy::Expanding")),
  360. ], spacing=0))
  361. def build_curve_page():
  362. """温控曲线页:QCustomPlot(.ui 里 declare 为 promoted widget,见 build() 的 customwidgets 段)
  363. 坐标轴范围 / 曲线样式都在 detailview.cpp 的 setupCurvePlot() 里设,.ui 只放控件"""
  364. plot = widget("QCustomPlot", "curvePlot", role("curvePlot") + pspol("Expanding", "Expanding"))
  365. return widget("QWidget", "tabPageCurve", "",
  366. layout("QVBoxLayout", "tabPageCurveLayout", [it(plot)],
  367. spacing=0, margins=(6, 8, 8, 8)))
  368. def build_ch_card():
  369. # 图例已按需求移除:详情页只区分已装载(蓝)与 IDLE(灰)
  370. head = layout("QHBoxLayout", "detailChHead", [
  371. it(panel_title("detailChTitle", "24 通道内存测试状态")),
  372. it(spacer("detailChSpacer", 16, 10, size_type="QSizePolicy::Expanding")),
  373. ], spacing=12, margins=(0, 0, 0, 0))
  374. cells = []
  375. for r, row in enumerate(DETAIL_CH):
  376. for c, (nm, temp, st) in enumerate(row):
  377. cells.append(it(ch_cell(nm, nm, temp, st), r, c))
  378. grid = layout("QGridLayout", "detailChGrid", cells, spacing=6, margins=(0, 0, 0, 0))
  379. # 卡片高度封顶在 330:格子撑到 64 就停,避免出现"表头下面一大片空白"
  380. return widget("QFrame", "detailChCard",
  381. penum("frameShape", "QFrame::NoFrame") + role("card") + pspol("Expanding", "Expanding") +
  382. psize("maximumSize", MAXW, 330),
  383. layout("QVBoxLayout", "detailChCardLayout", [it(head), it(grid)],
  384. spacing=10, margins=(14, 12, 14, 14)))
  385. # --------------------------------------------------------------------------
  386. # 详情页「内存参数设置」:三列参数表单(参数名 / 输入框 / 单位)
  387. # 数值由后端下发,界面只提供输入位;输入框默认 0
  388. # --------------------------------------------------------------------------
  389. PARAM_NAME_W = 86 # 参数名列宽(最长 "mem vddq")
  390. PARAM_EDIT_H = 24 # 输入框高
  391. PARAM_EDIT_W = 160 # 输入框最大宽(再宽就只剩空白了,窗口变窄时自动缩)
  392. PARAM_GRID_ROWS = 6 # 三列统一 6 个行槽位(第三列最后一槽空着),行距才能对齐并一起弹性分配
  393. PARAM_COLS = [
  394. ["mem vdd", "mem vddq", "tCL", "tRCD", "tRP", "tRAS"],
  395. ["tRFC", "tRTP", "trrd_l", "tWTR_S", "tWTR_L", "tWR"],
  396. ["tCCD_L", "trefi", "tRFCsb"],
  397. ]
  398. PARAM_CHECKS = ["errhandle", "ap"]
  399. def param_name(name, text):
  400. return label(name, text, "paramName", "Qt::AlignLeft|Qt::AlignVCenter",
  401. size=(PARAM_NAME_W, PARAM_EDIT_H), maxsize=(PARAM_NAME_W, MAXW),
  402. policy=("Fixed", "Preferred"))
  403. def param_edit(col, row):
  404. return widget("QLineEdit", "paramEdit_%d_%d" % (col, row),
  405. pstr("text", "0") + role("paramEdit") +
  406. psize("minimumSize", 0, PARAM_EDIT_H) +
  407. psize("maximumSize", PARAM_EDIT_W, PARAM_EDIT_H) +
  408. pspol("Expanding", "Fixed"))
  409. def param_check(col, row):
  410. return widget("QCheckBox", "paramCheck_%d_%d" % (col, row),
  411. pstr("text", "") + role("paramCheck") +
  412. psize("minimumSize", 16, 16) + psize("maximumSize", 16, 16))
  413. def param_row(col, row, text, is_check):
  414. """一行 = 参数名 + 输入框(或复选框);输入框横向伸展吃满该列(单位已按需求去掉)"""
  415. if is_check:
  416. return layout("QHBoxLayout", "paramRow_%d_%d" % (col, row), [
  417. it(param_name("paramChkName_%d_%d" % (col, row), text)),
  418. it(param_check(col, row)),
  419. it(spacer("paramChkPad_%d_%d" % (col, row), 10, 1,
  420. size_type="QSizePolicy::Expanding")),
  421. ], spacing=8)
  422. # 行尾补一个弹性空隙:输入框封顶 160px 后,列里剩下的宽度必须有东西去吃掉,
  423. # 否则窗口拉宽时 Qt 会把富余量塞到「参数名」和「输入框」中间(第三列就是这么被撑歪的)
  424. return layout("QHBoxLayout", "paramRow_%d_%d" % (col, row), [
  425. it(param_name("paramName_%d_%d" % (col, row), text)),
  426. it(param_edit(col, row)),
  427. it(spacer("paramPad_%d_%d" % (col, row), 8, 1,
  428. size_type="QSizePolicy::Expanding")),
  429. ], spacing=8)
  430. def build_param_page():
  431. # 三列各自一个 QVBoxLayout,行槽位固定 6 个、行间是 Expanding spacer,
  432. # 这样行距会随页面高度均分(把下方空白吃掉),三列又因为槽位相同而严格对齐。
  433. # 注意:不用 QGridLayout + rowStretch —— 网格的行最大高度被输入框的 maximumHeight(24) 卡死,行长不高。
  434. blocks = []
  435. for c, rows in enumerate(PARAM_COLS):
  436. entries = [(nm, False) for nm in rows]
  437. if c == len(PARAM_COLS) - 1: # 第三列尾部是两个开关,没有输入框
  438. entries += [(nm, True) for nm in PARAM_CHECKS]
  439. items = []
  440. for r in range(PARAM_GRID_ROWS):
  441. if r:
  442. items.append(it(spacer("paramVGap_%d_%d" % (c, r), 6, 2, orient="Qt::Vertical",
  443. size_type="QSizePolicy::Expanding")))
  444. if r < len(entries):
  445. nm, is_chk = entries[r]
  446. items.append(it(param_row(c, r, nm, is_chk)))
  447. else: # 空槽位:给一个和输入框等高的占位,保持列高一致
  448. items.append(it(spacer("paramVPad_%d_%d" % (c, r), 6, PARAM_EDIT_H,
  449. orient="Qt::Vertical")))
  450. blocks.append(it(layout("QVBoxLayout", "paramCol_%d" % c, items,
  451. spacing=0, margins=(0, 0, 0, 0))))
  452. if c != len(PARAM_COLS) - 1: # 列间分隔线
  453. blocks.append(it(widget("QFrame", "paramSep_%d" % c,
  454. penum("frameShape", "QFrame::NoFrame") + role("paramSep") +
  455. psize("minimumSize", 1, 0) + psize("maximumSize", 1, MAXW))))
  456. form = layout("QHBoxLayout", "paramForm", blocks, spacing=14, margins=(0, 0, 0, 0))
  457. # 「确定」按钮:跟表单右边界对齐,功能暂未接(先占位,后续接下发)
  458. submit_row = layout("QHBoxLayout", "paramSubmitRow", [
  459. it(spacer("paramSubmitSpacer", 10, 10, size_type="QSizePolicy::Expanding")),
  460. it(widget("QPushButton", "btnParamSubmit",
  461. pstr("text", "确定") + role("paramSubmit") +
  462. psize("minimumSize", 88, 30) + psize("maximumSize", 88, 30))),
  463. ], spacing=0, margins=(0, 0, 0, 0))
  464. # 表单吃满整页高度(C++ 里给 form 设 stretch=1),底部贴「确定」按钮
  465. return widget("QWidget", "tabPageParam", "",
  466. layout("QVBoxLayout", "tabPageParamLayout", [
  467. it(form),
  468. it(spacer("paramGapSubmit", 10, 16, orient="Qt::Vertical")),
  469. it(submit_row),
  470. ], spacing=0, margins=(6, 10, 6, 10)))
  471. def build_tabs_card():
  472. # 只保留「温控曲线 / 异常记录」两页;测试数据集、测试项目进度已按需求移除
  473. tabs = [("温控曲线", True), ("异常记录", False), ("内存参数设置", False)]
  474. btns = []
  475. for i, (txt, active) in enumerate(tabs):
  476. p = (pstr("text", txt) + role("detailTab") +
  477. pstr("active", "true" if active else "false", stdset=True) +
  478. psize("minimumSize", 100, 26) + psize("maximumSize", MAXW, 26))
  479. btns.append(it(widget("QPushButton", "detailTab_%d" % i, p)))
  480. tab_bar = layout("QHBoxLayout", "detailTabBar", btns + [
  481. it(spacer("detailTabSpacer", 10, 10, size_type="QSizePolicy::Expanding"))],
  482. spacing=6, margins=(14, 10, 14, 6))
  483. pages = [build_curve_page(),
  484. tab_page("tabPageAlarm", "暂无异常记录"),
  485. build_param_page()]
  486. tab_stack = stack("detailTabStack", pages)
  487. return widget("QFrame", "detailTabsCard",
  488. penum("frameShape", "QFrame::NoFrame") + role("card") + pspol("Expanding", "Expanding"),
  489. layout("QVBoxLayout", "detailTabsCardLayout", [it(tab_bar), it(tab_stack)],
  490. spacing=0, margins=(0, 0, 0, 0)))
  491. DETAIL_LOG_LINES = [
  492. "[10:12:00] 工位 SLT-HT-001 载入任务 TASK-20261021-001(DDR5 RDIMM)",
  493. "[10:12:04] 腔体升温至 68.4 °C,温控进入恒温模式",
  494. "[10:13:26] CPU0 · 12CH / CPU1 · 12CH 通道训练完成",
  495. "[10:28:12] 通道 CPU0_CHJ 读取延迟超阈值,已标记 FAIL",
  496. "[10:29:40] 测试进度 87.5%,预计剩余 5 分 20 秒",
  497. ]
  498. def build_log_card():
  499. head = layout("QHBoxLayout", "logHead", [
  500. it(panel_title("logTitle", "测试日志")),
  501. it(spacer("logHeadSpacer", 10, 10, size_type="QSizePolicy::Expanding")),
  502. ], spacing=8, margins=(0, 0, 0, 0))
  503. # 日志内容由 detailview.cpp 运行时写入:.ui 里的多行字符串会被 uic 按缩进重排,
  504. # 每行前面会多出一串空格(换行后自动缩进),所以这里只留空控件
  505. # 日志内容由后端实时推送;空的时候不显示任何提示文字
  506. log = widget("QPlainTextEdit", "detailLog",
  507. pbool("readOnly", True) + role("log") +
  508. penum("lineWrapMode", "QPlainTextEdit::NoWrap"))
  509. return widget("QFrame", "detailLogCard",
  510. penum("frameShape", "QFrame::NoFrame") + role("card") + pspol("Expanding", "Expanding"),
  511. layout("QVBoxLayout", "detailLogCardLayout", [it(head), it(log)],
  512. spacing=8, margins=(14, 12, 14, 12)))
  513. def build_detail():
  514. back = widget("QPushButton", "btnBackMonitor",
  515. pstr("text", "← 返回多机监控") + role("detailBack") +
  516. psize("minimumSize", 112, 28) + psize("maximumSize", 112, 28))
  517. title_bar = widget("QFrame", "detailTitleBar",
  518. penum("frameShape", "QFrame::NoFrame") + role("card") + pfixh(46),
  519. layout("QHBoxLayout", "detailTitleBarLayout", [
  520. it(label("detailTitle", "设备详情页", "detailTitle",
  521. "Qt::AlignLeft|Qt::AlignVCenter")),
  522. # 分隔符与工位号拆成两个静态控件:.cpp 只填 detailDevice 的内容
  523. it(label("detailTitleDot", "·", "detailTitleDot", "Qt::AlignCenter")),
  524. it(label("detailDevice", "—", "detailDevice",
  525. "Qt::AlignLeft|Qt::AlignVCenter")),
  526. it(spacer("detailTitleSpacer", 10, 10, size_type="QSizePolicy::Expanding")),
  527. it(back),
  528. ], spacing=8, margins=(14, 0, 14, 0)))
  529. left = layout("QVBoxLayout", "detailLeftCol",
  530. [it(build_ch_card()), it(build_tabs_card())],
  531. spacing=10, margins=(0, 0, 0, 0))
  532. right = layout("QVBoxLayout", "detailRightCol",
  533. [it(build_log_card())],
  534. spacing=10, margins=(0, 0, 0, 0))
  535. cols = layout("QHBoxLayout", "detailCols",
  536. [it(left), it(right)], spacing=10, margins=(0, 0, 0, 0))
  537. # KPI 条已按需求移除:标题栏下面直接是左右两栏
  538. body = layout("QVBoxLayout", "detailBody",
  539. [it(title_bar), it(cols)], spacing=10, margins=(20, 8, 20, 14))
  540. return widget("QWidget", "detailPage", role("contentArea"), body)
  541. # --------------------------------------------------------------------------
  542. def build():
  543. # pageStack: 0 = 多机监控总览,1 = 设备详情页(工位卡「查看详情 >」切换)
  544. pages = stack("pageStack", [build_content(), build_detail()])
  545. root = layout("QVBoxLayout", "rootLayout", [
  546. it(build_header()),
  547. it(pages),
  548. ], spacing=0)
  549. central = widget("QWidget", "centralwidget", "", root)
  550. ui = '<?xml version="1.0" encoding="UTF-8"?>\n<ui version="4.0">\n'
  551. ui += " <class>MainWindow</class>\n"
  552. ui += ' <widget class="QMainWindow" name="MainWindow">\n'
  553. ui += textwrap.indent(
  554. prect("geometry", 0, 0, 1640, 720) + psize("minimumSize", 1320, 800) +
  555. pstr("windowTitle", "HOVSE SLT Pro"), " ")
  556. ui += textwrap.indent(central, " ")
  557. ui += " </widget>\n"
  558. # 提升控件:QCustomPlot 作为普通 QWidget 放进 .ui,由 uic 生成 #include "qcustomplot.h"
  559. ui += (" <customwidgets>\n"
  560. " <customwidget>\n"
  561. " <class>QCustomPlot</class>\n"
  562. " <extends>QWidget</extends>\n"
  563. " <header>qcustomplot.h</header>\n"
  564. " <container>0</container>\n"
  565. " </customwidget>\n"
  566. " <customwidget>\n"
  567. " <class>PowerButton</class>\n"
  568. " <extends>QWidget</extends>\n"
  569. " <header>powerbutton.h</header>\n"
  570. " <container>0</container>\n"
  571. " </customwidget>\n"
  572. " </customwidgets>\n")
  573. ui += " <resources/>\n <connections/>\n</ui>\n"
  574. return ui
  575. if __name__ == "__main__":
  576. os.makedirs(os.path.dirname(OUT), exist_ok=True)
  577. data = build()
  578. with open(OUT, "w", encoding="utf-8", newline="\n") as f:
  579. f.write(data)
  580. print("written: %s (%d bytes)" % (OUT, len(data)))