createtaskdialog.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. #include "createtaskdialog.h"
  2. #include "ui_createtaskdialog.h"
  3. #include "appconfig.h"
  4. #include "localplans.h"
  5. #include <QCheckBox>
  6. #include <QDialog>
  7. #include <QDialogButtonBox>
  8. #include <QDragEnterEvent>
  9. #include <QDropEvent>
  10. #include <QFile>
  11. #include <QColor>
  12. #include <QDebug>
  13. #include <QFont>
  14. #include <QFrame>
  15. #include <QGraphicsDropShadowEffect>
  16. #include <QGridLayout>
  17. #include <QGroupBox>
  18. #include <QGuiApplication>
  19. #include <QHBoxLayout>
  20. #include <QIntValidator>
  21. #include <QJsonArray>
  22. #include <QJsonDocument>
  23. #include <QJsonObject>
  24. #include <QLabel>
  25. #include <QLineEdit>
  26. #include <QListWidget>
  27. #include <QListWidgetItem>
  28. #include <QMimeData>
  29. #include <QPushButton>
  30. #include <QScreen>
  31. #include <QTabWidget>
  32. #include <QTimer>
  33. #include <QToolButton>
  34. #include <QVBoxLayout>
  35. #include <functional>
  36. namespace {
  37. const char *const kTaskMime = "application/x-slt-task";
  38. const char *const kKeys[] = {
  39. "mem vdd", "mem vddq", "tCL", "tRCD", "tRP", "tRAS",
  40. "tRFC", "tRTP", "trrd_l", "tWTR_S", "tWTR_L", "tWR",
  41. "tCCD_L", "trefi", "tRFCsb",
  42. };
  43. const int kKeyCount = int(sizeof(kKeys) / sizeof(kKeys[0]));
  44. // config.json 的 memoryParams,作为没有本地计划时的默认值
  45. QJsonObject configMemoryParams()
  46. {
  47. const QString path = appConfigPath(QStringLiteral("config.json"));
  48. if (path.isEmpty())
  49. return QJsonObject();
  50. QFile f(path);
  51. if (!f.open(QIODevice::ReadOnly))
  52. return QJsonObject();
  53. return QJsonDocument::fromJson(f.readAll()).object()
  54. .value(QStringLiteral("memoryParams")).toObject();
  55. }
  56. // 算法清单每行放一个真正的 QCheckBox(setItemWidget):点击由控件自己处理,
  57. // 不经过 QAbstractItemView 的双击判定,快速连点不会漏一次。
  58. QList<QCheckBox *> algoChecks(QListWidget *list)
  59. {
  60. QList<QCheckBox *> out;
  61. for (int i = 0; i < list->count(); ++i)
  62. if (QCheckBox *cb = qobject_cast<QCheckBox *>(list->itemWidget(list->item(i))))
  63. out << cb;
  64. return out;
  65. }
  66. QStringList checkedItems(const QListWidget *list)
  67. {
  68. QStringList out;
  69. for (int i = 0; i < list->count(); ++i)
  70. if (QCheckBox *cb = qobject_cast<QCheckBox *>(list->itemWidget(list->item(i))))
  71. if (cb->isChecked())
  72. out << cb->text();
  73. return out;
  74. }
  75. QJsonArray toArray(const QStringList &items)
  76. {
  77. QJsonArray a;
  78. for (const QString &s : items)
  79. a.append(s);
  80. return a;
  81. }
  82. QStringList fromArray(const QJsonValue &v)
  83. {
  84. QStringList out;
  85. const QJsonArray a = v.toArray();
  86. for (const QJsonValue &x : a)
  87. out << x.toString();
  88. return out;
  89. }
  90. // 阶段对象:保留 efi_path / config_path / test_command 等既有键,
  91. // 只覆盖 selected(勾选项)与 enabled(= 至少勾了一项)。
  92. QJsonObject withSelection(const QJsonObject &base, const QStringList &selected)
  93. {
  94. QJsonObject o = base;
  95. o.insert(QStringLiteral("selected"), toArray(selected));
  96. o.insert(QStringLiteral("enabled"), !selected.isEmpty());
  97. o.remove(QStringLiteral("only_tests"));
  98. o.remove(QStringLiteral("skip_tests"));
  99. return o;
  100. }
  101. QStringList allItems(QListWidget *list)
  102. {
  103. QStringList out;
  104. for (QCheckBox *cb : algoChecks(list))
  105. out << cb->text();
  106. return out;
  107. }
  108. void ensureDefaultSelection(QJsonObject &cfg, const QString &key, const QStringList &all)
  109. {
  110. QJsonObject sec = cfg.value(key).toObject();
  111. if (sec.contains(QStringLiteral("selected")))
  112. return;
  113. // 尊重 config.json 里显式配置的 enabled=false(如默认不开启 RMT/AMT 时,不能强行全选)
  114. if (sec.contains(QStringLiteral("enabled")) && !sec.value(QStringLiteral("enabled")).toBool(true)) {
  115. sec.insert(QStringLiteral("selected"), QJsonArray());
  116. sec.insert(QStringLiteral("enabled"), false);
  117. cfg.insert(key, sec);
  118. return;
  119. }
  120. sec.insert(QStringLiteral("selected"), toArray(all));
  121. sec.insert(QStringLiteral("enabled"), !all.isEmpty());
  122. cfg.insert(key, sec);
  123. }
  124. // 旧版存档的 memory_params 没有目标温度和 ecc / debuglog:补齐默认
  125. void ensureMemoryDefaults(QJsonObject &cfg)
  126. {
  127. QJsonObject mem = cfg.value(QStringLiteral("memory_params")).toObject();
  128. if (!mem.contains(QStringLiteral("target_temp")))
  129. mem.insert(QStringLiteral("target_temp"), 70);
  130. const char *const checks[] = {"errhandle", "ap", "ecc", "debuglog"};
  131. for (const char *k : checks) {
  132. const QString key = QString::fromLatin1(k);
  133. if (!mem.contains(key))
  134. mem.insert(key, false);
  135. }
  136. cfg.insert(QStringLiteral("memory_params"), mem);
  137. }
  138. // 任务池:拖出时只带任务 id,不放 Qt 默认的 model 数据。
  139. class TaskSourceList : public QListWidget
  140. {
  141. public:
  142. explicit TaskSourceList(QWidget *parent = nullptr) : QListWidget(parent) {}
  143. protected:
  144. QStringList mimeTypes() const override
  145. {
  146. return QStringList() << QString::fromLatin1(kTaskMime);
  147. }
  148. // Qt 5 的这个虚函数是「按值传参」,Qt 6 才改成 const 引用。
  149. // 写成引用等于没覆盖,拖出去的会是 Qt 默认 MIME,任务框就收不到了。
  150. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  151. QMimeData *mimeData(const QList<QListWidgetItem *> &items) const override
  152. #else
  153. QMimeData *mimeData(const QList<QListWidgetItem *> items) const override
  154. #endif
  155. {
  156. QMimeData *m = new QMimeData();
  157. if (!items.isEmpty() && items.first())
  158. m->setData(QString::fromLatin1(kTaskMime),
  159. QByteArray::number(items.first()->data(Qt::UserRole).toInt()));
  160. return m;
  161. }
  162. };
  163. // 机台里的「任务框」:接住拖来的任务 id。
  164. class TaskDropZone : public QFrame
  165. {
  166. public:
  167. explicit TaskDropZone(QWidget *parent = nullptr) : QFrame(parent)
  168. {
  169. setAcceptDrops(true);
  170. }
  171. std::function<void(int)> onTaskDropped;
  172. std::function<bool()> canDrop; // 机台离线时返回 false,任务拖不进来
  173. std::function<bool(int)> canDropTask; // 这条任务本身不许拖入(例如正在执行中)
  174. protected:
  175. void dragEnterEvent(QDragEnterEvent *e) override
  176. {
  177. if (!e->mimeData()->hasFormat(QString::fromLatin1(kTaskMime)))
  178. return;
  179. // 这个机台显示「控制台离线」时不接收任务:直接忽略,鼠标会显示禁止图标
  180. if (canDrop && !canDrop())
  181. return;
  182. // 执行中的任务不允许再添加:拖过来的 mimeData 里就是任务 id,
  183. // 在 enter 阶段拒掉,鼠标直接显示禁止,不用等松手再弹提示。
  184. if (canDropTask) {
  185. const QByteArray b = e->mimeData()->data(QString::fromLatin1(kTaskMime));
  186. bool ok = false;
  187. const int taskId = b.toInt(&ok);
  188. if (ok && !canDropTask(taskId))
  189. return;
  190. }
  191. e->acceptProposedAction();
  192. setProperty("dragOver", true);
  193. style()->polish(this);
  194. }
  195. void dragLeaveEvent(QDragLeaveEvent *e) override
  196. {
  197. Q_UNUSED(e)
  198. setProperty("dragOver", false);
  199. style()->polish(this);
  200. }
  201. void dropEvent(QDropEvent *e) override
  202. {
  203. setProperty("dragOver", false);
  204. style()->polish(this);
  205. const QByteArray b = e->mimeData()->data(QString::fromLatin1(kTaskMime));
  206. bool ok = false;
  207. const int taskId = b.toInt(&ok);
  208. e->acceptProposedAction();
  209. if (ok && onTaskDropped)
  210. onTaskDropped(taskId);
  211. }
  212. };
  213. // 删除类操作的确认框。样式跟项目其它弹窗一致:白底圆角卡片 + 蓝色主按钮,
  214. // 不用系统 QMessageBox(灰底 + 系统图标,和蓝白扁平界面不搭)。
  215. bool confirmRemove(QWidget *parent, const QString &title, const QString &detail,
  216. bool showCancel = true)
  217. {
  218. QDialog dlg(parent);
  219. dlg.setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  220. dlg.setAttribute(Qt::WA_TranslucentBackground); // 圆角外透出底下的界面,不留一圈灰底
  221. dlg.setWindowTitle(title);
  222. dlg.setFixedWidth(420);
  223. QVBoxLayout *outer = new QVBoxLayout(&dlg);
  224. outer->setContentsMargins(20, 20, 20, 20); // 给阴影留绘制空间
  225. QFrame *card = new QFrame(&dlg);
  226. card->setObjectName(QStringLiteral("confirmCard"));
  227. card->setStyleSheet(QStringLiteral(
  228. "QFrame#confirmCard { background:#ffffff; border:1px solid #d3dde9; border-radius:10px; }"
  229. "QLabel#confirmTitle { color:#20323f; font-size:19px; font-weight:600; }"
  230. "QLabel#confirmDetail { color:#5b7183; font-size:15px; }"));
  231. outer->addWidget(card);
  232. // 和 ResultToast 一样的柔和投影,让卡片从背景里浮起来
  233. QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(card);
  234. shadow->setBlurRadius(32);
  235. shadow->setOffset(0, 8);
  236. shadow->setColor(QColor(31, 56, 82, 95));
  237. card->setGraphicsEffect(shadow);
  238. QVBoxLayout *body = new QVBoxLayout(card);
  239. body->setContentsMargins(24, 22, 24, 18);
  240. body->setSpacing(18);
  241. QLabel *titleLabel = new QLabel(title, card);
  242. titleLabel->setObjectName(QStringLiteral("confirmTitle"));
  243. QLabel *detailLabel = new QLabel(detail, card);
  244. detailLabel->setObjectName(QStringLiteral("confirmDetail"));
  245. detailLabel->setWordWrap(true);
  246. body->addWidget(titleLabel);
  247. body->addWidget(detailLabel);
  248. QHBoxLayout *foot = new QHBoxLayout();
  249. foot->setContentsMargins(0, 0, 0, 0);
  250. foot->setSpacing(10);
  251. foot->addStretch(1);
  252. QPushButton *cancel = nullptr;
  253. if (showCancel) {
  254. cancel = new QPushButton(QStringLiteral("取消"), card);
  255. cancel->setStyleSheet(QStringLiteral(
  256. "QPushButton { color:#2f7fc1; background:#ffffff; border:1px solid #cdd7e1;"
  257. " border-radius:4px; padding:5px 16px; font-size:14px; }"
  258. "QPushButton:hover { background:#eef4fa; border-color:#b9cfe2; }"));
  259. cancel->setCursor(Qt::PointingHandCursor);
  260. foot->addWidget(cancel);
  261. QObject::connect(cancel, &QPushButton::clicked, &dlg, &QDialog::reject);
  262. }
  263. QPushButton *ok = new QPushButton(
  264. showCancel ? QStringLiteral("确定") : QStringLiteral("知道了"), card);
  265. ok->setStyleSheet(QStringLiteral(
  266. "QPushButton { color:#ffffff; background:#2f7fc1; border:1px solid #2f7fc1;"
  267. " border-radius:4px; padding:5px 16px; font-size:14px; }"
  268. "QPushButton:hover { background:#2a72ad; border-color:#2a72ad; }"));
  269. ok->setDefault(true);
  270. ok->setCursor(Qt::PointingHandCursor);
  271. foot->addWidget(ok);
  272. body->addLayout(foot);
  273. QObject::connect(ok, &QPushButton::clicked, &dlg, &QDialog::accept);
  274. return dlg.exec() == QDialog::Accepted;
  275. }
  276. // 只给一条说明、不需要用户做「确定/取消」的判断(例如执行中任务禁止删除)。
  277. void showNotice(QWidget *parent, const QString &title, const QString &detail)
  278. {
  279. confirmRemove(parent, title, detail, false);
  280. }
  281. // 输入任务名字的小弹窗。样式跟 confirmRemove / ResultToast 一套:
  282. // 白底圆角卡片 + 蓝色主按钮,不用系统 QInputDialog 的灰底。
  283. bool promptTaskName(QWidget *parent, const QString &defaultName,
  284. const QStringList &existingNames, QString *out)
  285. {
  286. QDialog dlg(parent);
  287. dlg.setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  288. dlg.setAttribute(Qt::WA_TranslucentBackground);
  289. dlg.setWindowTitle(QStringLiteral("创建任务"));
  290. dlg.setFixedWidth(420);
  291. QVBoxLayout *outer = new QVBoxLayout(&dlg);
  292. outer->setContentsMargins(20, 20, 20, 20);
  293. QFrame *card = new QFrame(&dlg);
  294. card->setObjectName(QStringLiteral("nameCard"));
  295. card->setStyleSheet(QStringLiteral(
  296. "QFrame#nameCard { background:#ffffff; border:1px solid #d3dde9; border-radius:10px; }"
  297. "QLabel#nameTitle { color:#20323f; font-size:19px; font-weight:600; }"
  298. "QLabel#nameLabel { color:#5b7183; font-size:15px; }"));
  299. outer->addWidget(card);
  300. QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(card);
  301. shadow->setBlurRadius(32);
  302. shadow->setOffset(0, 8);
  303. shadow->setColor(QColor(31, 56, 82, 95));
  304. card->setGraphicsEffect(shadow);
  305. QVBoxLayout *body = new QVBoxLayout(card);
  306. body->setContentsMargins(24, 22, 24, 18);
  307. body->setSpacing(12);
  308. QLabel *title = new QLabel(QStringLiteral("创建任务"), card);
  309. title->setObjectName(QStringLiteral("nameTitle"));
  310. QLabel *label = new QLabel(QStringLiteral("任务名:"), card);
  311. label->setObjectName(QStringLiteral("nameLabel"));
  312. QLineEdit *edit = new QLineEdit(card);
  313. edit->setText(defaultName);
  314. edit->selectAll();
  315. edit->setStyleSheet(QStringLiteral(
  316. "QLineEdit { background:#ffffff; border:1px solid #d7e0ea; border-radius:4px;"
  317. " color:#33465a; font-size:16px; padding:6px 10px; }"
  318. "QLineEdit:focus { border-color:#6ba6d8; }"));
  319. body->addWidget(title);
  320. body->addWidget(label);
  321. body->addWidget(edit);
  322. QLabel *err = new QLabel(card);
  323. err->setObjectName(QStringLiteral("nameError"));
  324. err->setStyleSheet(QStringLiteral("QLabel#nameError { color:#c92f2f; font-size:13px; }"));
  325. err->setWordWrap(true);
  326. err->hide();
  327. body->addWidget(err);
  328. QHBoxLayout *foot = new QHBoxLayout();
  329. foot->setContentsMargins(0, 0, 0, 0);
  330. foot->setSpacing(10);
  331. foot->addStretch(1);
  332. QPushButton *cancel = new QPushButton(QStringLiteral("取消"), card);
  333. cancel->setStyleSheet(QStringLiteral(
  334. "QPushButton { color:#2f7fc1; background:#ffffff; border:1px solid #cdd7e1;"
  335. " border-radius:4px; padding:5px 16px; font-size:14px; }"
  336. "QPushButton:hover { background:#eef4fa; border-color:#b9cfe2; }"));
  337. QPushButton *ok = new QPushButton(QStringLiteral("确定"), card);
  338. ok->setStyleSheet(QStringLiteral(
  339. "QPushButton { color:#ffffff; background:#2f7fc1; border:1px solid #2f7fc1;"
  340. " border-radius:4px; padding:5px 16px; font-size:14px; }"
  341. "QPushButton:hover { background:#2a72ad; border-color:#2a72ad; }"));
  342. ok->setDefault(true);
  343. cancel->setCursor(Qt::PointingHandCursor);
  344. ok->setCursor(Qt::PointingHandCursor);
  345. foot->addWidget(cancel);
  346. foot->addWidget(ok);
  347. body->addLayout(foot);
  348. QObject::connect(cancel, &QPushButton::clicked, &dlg, &QDialog::reject);
  349. // 任务名不能重复:重名时就在输入框下方红字提示,不关窗、让用户直接改。
  350. QObject::connect(ok, &QPushButton::clicked, &dlg, [&]() {
  351. const QString name = edit->text().trimmed();
  352. if (name.isEmpty()) {
  353. dlg.reject();
  354. return;
  355. }
  356. if (existingNames.contains(name)) {
  357. err->setText(QStringLiteral("任务名「%1」已存在,请换一个名字").arg(name));
  358. err->show();
  359. edit->setFocus();
  360. edit->selectAll();
  361. return;
  362. }
  363. dlg.accept();
  364. });
  365. QObject::connect(edit, &QLineEdit::textChanged, err, &QLabel::hide);
  366. QObject::connect(edit, &QLineEdit::returnPressed, ok, &QPushButton::click);
  367. edit->setFocus();
  368. if (dlg.exec() != QDialog::Accepted)
  369. return false;
  370. const QString name = edit->text().trimmed();
  371. if (name.isEmpty())
  372. return false;
  373. *out = name;
  374. return true;
  375. }
  376. } // namespace
  377. CreateTaskDialog::CreateTaskDialog(QWidget *parent)
  378. : QDialog(parent)
  379. , ui(new Ui::createTaskDialog)
  380. , m_stationTaskIds(4, QVector<int>())
  381. {
  382. ui->setupUi(this);
  383. // 这个窗口沿用旧版紧凑字体:全局基准是 11.25pt,这里回到 9pt。
  384. // QSS 里写了 font-size 的控件由 app.qss 末尾的 #createTaskDialog 覆盖规则接手。
  385. QFont dlgFont = font();
  386. dlgFont.setPointSizeF(11.25);
  387. setFont(dlgFont);
  388. if (QWidget *bar = findChild<QWidget *>(QStringLiteral("targetTempBar")))
  389. bar->setAttribute(Qt::WA_StyledBackground, true);
  390. // 参数输入框:只收数字,数字靠左显示
  391. for (int i = 0; i < kKeyCount; ++i) {
  392. QLineEdit *ed = findChild<QLineEdit *>(QStringLiteral("paramEdit_%1").arg(i));
  393. if (!ed)
  394. continue;
  395. ed->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  396. ed->setValidator(new QIntValidator(0, 99999999, ed));
  397. }
  398. // 三组算法清单:每行一个真正的 QCheckBox
  399. auto setupAlgoList = [this](QListWidget *list, QLabel *badge, QLabel *foot,
  400. QToolButton *all, QToolButton *none) {
  401. list->setSelectionMode(QAbstractItemView::NoSelection);
  402. list->setFocusPolicy(Qt::NoFocus);
  403. for (int i = 0; i < list->count(); ++i) {
  404. QListWidgetItem *it = list->item(i);
  405. const QString text = it->text();
  406. it->setText(QString());
  407. it->setFlags(Qt::ItemIsEnabled);
  408. QCheckBox *cb = new QCheckBox(text, list);
  409. cb->setProperty("role", "algoCheck");
  410. cb->setFocusPolicy(Qt::NoFocus);
  411. cb->setChecked(true);
  412. it->setSizeHint(QSize(it->sizeHint().width(), cb->sizeHint().height()));
  413. list->setItemWidget(it, cb);
  414. }
  415. auto refresh = [list, badge, foot]() {
  416. const QList<QCheckBox *> cbs = algoChecks(list);
  417. int sel = 0;
  418. for (QCheckBox *cb : cbs)
  419. if (cb->isChecked())
  420. ++sel;
  421. badge->setText(QStringLiteral("%1/%2").arg(sel).arg(cbs.size()));
  422. foot->setText(QStringLiteral("共 %1 项 · 已选 %2 项").arg(cbs.size()).arg(sel));
  423. };
  424. for (QCheckBox *cb : algoChecks(list))
  425. connect(cb, &QCheckBox::toggled, this, refresh);
  426. connect(all, &QToolButton::clicked, this, [list]() {
  427. for (QCheckBox *cb : algoChecks(list))
  428. cb->setChecked(true);
  429. });
  430. connect(none, &QToolButton::clicked, this, [list]() {
  431. for (QCheckBox *cb : algoChecks(list))
  432. cb->setChecked(false);
  433. });
  434. refresh();
  435. };
  436. setupAlgoList(ui->algoList, ui->mtBadge, ui->mtFoot, ui->mtAll, ui->mtNone);
  437. setupAlgoList(ui->amtList, ui->amtBadge, ui->amtFoot, ui->amtAll, ui->amtNone);
  438. setupAlgoList(ui->rmtList, ui->rmtBadge, ui->rmtFoot, ui->rmtAll, ui->rmtNone);
  439. // 卡头的「14/14」计数徽标按需求去掉(三组都一样),只留「全选 / 清空」。
  440. // 刷新逻辑照旧写文字,只是标签不可见,不影响勾选统计。
  441. ui->mtBadge->setVisible(false);
  442. ui->amtBadge->setVisible(false);
  443. ui->rmtBadge->setVisible(false);
  444. // 在 MT 算法卡片底部增加 ADDRLIMLO / ADDRLIMHI 区间配置行(支持快速短区间测试与自定义内存范围)
  445. if (QVBoxLayout *mtLay = qobject_cast<QVBoxLayout *>(ui->mtBox->layout())) {
  446. QHBoxLayout *rangeRow = new QHBoxLayout();
  447. rangeRow->setSpacing(6);
  448. m_mtCustomRange = new QCheckBox(QStringLiteral("限制地址"), ui->mtBox);
  449. m_mtCustomRange->setProperty("role", "paramCheck");
  450. m_mtCustomRange->setToolTip(QStringLiteral("勾选后下发 ADDRLIMLO / ADDRLIMHI(如 0x10000000 ~ 0x14000000)"));
  451. m_mtAddrLoEdit = new QLineEdit(ui->mtBox);
  452. m_mtAddrLoEdit->setProperty("role", "paramEdit");
  453. m_mtAddrLoEdit->setPlaceholderText(QStringLiteral("0x10000000"));
  454. m_mtAddrLoEdit->setMinimumWidth(96);
  455. QLabel *dash = new QLabel(QStringLiteral("~"), ui->mtBox);
  456. m_mtAddrHiEdit = new QLineEdit(ui->mtBox);
  457. m_mtAddrHiEdit->setProperty("role", "paramEdit");
  458. m_mtAddrHiEdit->setPlaceholderText(QStringLiteral("0x14000000"));
  459. m_mtAddrHiEdit->setMinimumWidth(96);
  460. auto syncRangeEnabled = [this](bool on) {
  461. if (m_mtAddrLoEdit) m_mtAddrLoEdit->setEnabled(on);
  462. if (m_mtAddrHiEdit) m_mtAddrHiEdit->setEnabled(on);
  463. };
  464. connect(m_mtCustomRange, &QCheckBox::toggled, this, syncRangeEnabled);
  465. syncRangeEnabled(false);
  466. rangeRow->addWidget(m_mtCustomRange);
  467. rangeRow->addWidget(m_mtAddrLoEdit, 1);
  468. rangeRow->addWidget(dash);
  469. rangeRow->addWidget(m_mtAddrHiEdit, 1);
  470. const int insertAt = qMax(0, mtLay->count() - 1);
  471. mtLay->insertLayout(insertAt, rangeRow);
  472. }
  473. // 适配小分辨率/高 DPI 桌面(1366x768 或 1080p @ 125%/150%):约束弹窗不超出屏幕可用区域
  474. if (QScreen *sc = (parent && parent->screen()) ? parent->screen() : QGuiApplication::primaryScreen()) {
  475. const QRect avail = sc->availableGeometry();
  476. if (avail.width() > 320 && avail.height() > 240
  477. && (width() > avail.width() - 24 || height() > avail.height() - 24)) {
  478. ui->algoList->setMinimumHeight(200);
  479. ui->amtList->setMinimumHeight(200);
  480. ui->rmtList->setMinimumHeight(140);
  481. resize(qMin(width(), qMax(900, avail.width() - 32)),
  482. qMin(height(), qMax(620, avail.height() - 32)));
  483. }
  484. }
  485. // 「创建任务」/「关闭」按钮。点创建任务只生成任务,不写文件、不关窗。
  486. QPushButton *ok = ui->buttonBox->button(QDialogButtonBox::Ok);
  487. ok->setText(QStringLiteral("创建任务"));
  488. ok->setStyleSheet(QStringLiteral(
  489. "QPushButton { color:#ffffff; background:#2f7fc1; border:1px solid #2f7fc1;"
  490. " border-radius:4px; padding:6px 20px; font-size:16px; }"
  491. "QPushButton:hover { background:#2a72ad; border-color:#2a72ad; }"
  492. "QPushButton:pressed { background:#256394; }"));
  493. QPushButton *cancel = ui->buttonBox->button(QDialogButtonBox::Cancel);
  494. cancel->setText(QStringLiteral("关闭"));
  495. cancel->setStyleSheet(QStringLiteral(
  496. "QPushButton { color:#2f7fc1; background:#ffffff; border:1px solid #cdd7e1;"
  497. " border-radius:4px; padding:6px 20px; font-size:16px; }"
  498. "QPushButton:hover { background:#eef4fa; border-color:#b9cfe2; }"
  499. "QPushButton:pressed { background:#e6f2fc; }"));
  500. connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &CreateTaskDialog::createTask);
  501. connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  502. // 「任务分配」页不需要底部的「创建任务 / 关闭」:只有「创建任务」页(index 0)显示。
  503. // 切页时同步;分配页仍可用右上角 X(或 Esc)关窗。
  504. auto syncFooter = [this](int index) {
  505. ui->buttonBox->setVisible(index == 0);
  506. };
  507. connect(ui->taskTabs, &QTabWidget::currentChanged, this, syncFooter);
  508. syncFooter(ui->taskTabs->currentIndex());
  509. ui->targetTempEdit->setValidator(new QIntValidator(1, 254, ui->targetTempEdit));
  510. // 配置页的初始值取 S1 已存配置(不再按工位分别维护一份)
  511. m_baseCfg = LocalPlans::stationConfig(0);
  512. ensureMemoryDefaults(m_baseCfg);
  513. ensureDefaultSelection(m_baseCfg, QStringLiteral("mt"), allItems(ui->algoList));
  514. ensureDefaultSelection(m_baseCfg, QStringLiteral("amt"), allItems(ui->amtList));
  515. ensureDefaultSelection(m_baseCfg, QStringLiteral("rmt"), allItems(ui->rmtList));
  516. loadUiFrom(m_baseCfg);
  517. buildAssignTab();
  518. // 从 qt-plans.json 恢复各机台已分配的任务:任务拖拽时已经落盘,
  519. // 重开软件不能让机台框显示成空的。同名任务在任务池里复用同一个 id。
  520. for (int s = 0; s < m_stationTaskIds.size(); ++s) {
  521. const QJsonArray arr = LocalPlans::stationConfig(s)
  522. .value(QStringLiteral("tasks")).toArray();
  523. for (const QJsonValue &v : arr) {
  524. const QJsonObject item = v.toObject();
  525. const QString name = item.value(QStringLiteral("name")).toString();
  526. const QJsonObject cfg = item.value(QStringLiteral("config")).toObject();
  527. if (name.isEmpty() || cfg.isEmpty())
  528. continue;
  529. int id = -1;
  530. for (const SltTask &t : m_tasks)
  531. if (t.name == name) { id = t.id; break; }
  532. if (id < 0) {
  533. SltTask t;
  534. t.id = m_nextTaskId++;
  535. t.name = name;
  536. t.cfg = cfg;
  537. m_tasks.append(t);
  538. id = t.id;
  539. }
  540. if (!m_stationTaskIds[s].contains(id))
  541. m_stationTaskIds[s].append(id);
  542. }
  543. }
  544. refreshTaskList();
  545. refreshStationBoxes();
  546. }
  547. CreateTaskDialog::~CreateTaskDialog()
  548. {
  549. delete ui;
  550. }
  551. // ---------------------------------------------------------------------------
  552. // 「任务分配」页:左侧任务池 + 右侧四个机台的任务框
  553. // ---------------------------------------------------------------------------
  554. void CreateTaskDialog::buildAssignTab()
  555. {
  556. QVBoxLayout *root = ui->assignLayout;
  557. QHBoxLayout *row = new QHBoxLayout();
  558. row->setSpacing(12);
  559. // 左:任务池
  560. QGroupBox *poolBox = new QGroupBox(QStringLiteral("已创建任务"), ui->assignTab);
  561. poolBox->setProperty("role", "algoBox");
  562. QVBoxLayout *poolLay = new QVBoxLayout(poolBox);
  563. poolLay->setContentsMargins(10, 10, 10, 10);
  564. poolLay->setSpacing(8);
  565. QHBoxLayout *poolTools = new QHBoxLayout();
  566. QLabel *poolHint = new QLabel(QStringLiteral("拖到右侧机台"), poolBox);
  567. poolHint->setProperty("role", "taskHint");
  568. QToolButton *delBtn = new QToolButton(poolBox);
  569. delBtn->setText(QStringLiteral("删除选中"));
  570. delBtn->setCursor(Qt::PointingHandCursor);
  571. delBtn->setProperty("role", "algoMini");
  572. poolTools->addWidget(poolHint);
  573. poolTools->addStretch(1);
  574. poolTools->addWidget(delBtn);
  575. poolLay->addLayout(poolTools);
  576. m_taskList = new TaskSourceList(poolBox);
  577. m_taskList->setObjectName(QStringLiteral("taskPool"));
  578. m_taskList->setProperty("role", "taskPool");
  579. m_taskList->setSelectionMode(QAbstractItemView::SingleSelection);
  580. // 不要焦点:否则选中项会被 Qt 画一圈点状虚线框,任务名看起来像被框住了
  581. m_taskList->setFocusPolicy(Qt::NoFocus);
  582. m_taskList->setDragEnabled(true);
  583. m_taskList->setDragDropMode(QAbstractItemView::DragOnly);
  584. m_taskList->setDefaultDropAction(Qt::CopyAction);
  585. m_taskList->setMinimumWidth(300);
  586. poolLay->addWidget(m_taskList, 1);
  587. connect(delBtn, &QToolButton::clicked, this, &CreateTaskDialog::deleteSelectedTask);
  588. row->addWidget(poolBox, 2);
  589. // 右:四个机台
  590. QGroupBox *stationBox = new QGroupBox(QStringLiteral("机台任务分配"), ui->assignTab);
  591. stationBox->setProperty("role", "algoBox");
  592. QGridLayout *grid = new QGridLayout(stationBox);
  593. grid->setContentsMargins(10, 10, 10, 10);
  594. grid->setSpacing(12);
  595. for (int i = 0; i < 4; ++i) {
  596. QFrame *card = new QFrame(stationBox);
  597. card->setProperty("role", "stationCard");
  598. card->setAttribute(Qt::WA_StyledBackground, true);
  599. QVBoxLayout *cardLay = new QVBoxLayout(card);
  600. cardLay->setContentsMargins(12, 10, 12, 12);
  601. cardLay->setSpacing(8);
  602. QHBoxLayout *head = new QHBoxLayout();
  603. QLabel *title = new QLabel(QStringLiteral("SLT-HT-%1").arg(i + 1, 3, 10, QLatin1Char('0')), card);
  604. title->setProperty("role", "stationTitle");
  605. // 标题右边的在线/离线标签,口径跟主界面卡片上的状态完全一致
  606. QLabel *status = new QLabel(card);
  607. status->setProperty("role", "stationStatus");
  608. m_stationStatus.append(status);
  609. QToolButton *clear = new QToolButton(card);
  610. clear->setText(QStringLiteral("清空"));
  611. clear->setCursor(Qt::PointingHandCursor);
  612. clear->setProperty("role", "algoMini");
  613. m_stationClearBtns.append(clear);
  614. head->addWidget(title);
  615. head->addWidget(status);
  616. head->addStretch(1);
  617. head->addWidget(clear);
  618. cardLay->addLayout(head);
  619. TaskDropZone *zone = new TaskDropZone(card);
  620. zone->setProperty("role", "taskDrop");
  621. zone->setAttribute(Qt::WA_StyledBackground, true);
  622. zone->setMinimumHeight(76);
  623. QVBoxLayout *zoneLay = new QVBoxLayout(zone);
  624. zoneLay->setContentsMargins(10, 8, 10, 8);
  625. zoneLay->setSpacing(4);
  626. m_stationChipLays.append(zoneLay); // 任务条由 refreshStationBoxes 动态重建
  627. connect(clear, &QToolButton::clicked, this, [this, i]() { clearStation(i); });
  628. zone->onTaskDropped = [this, i](int taskId) { assignTask(i, taskId); };
  629. zone->canDrop = [this, i]() { return m_onlineProbe ? m_onlineProbe(i) : true; };
  630. // 执行中的任务鼠标直接显示禁止图标,松手也不会被添加
  631. zone->canDropTask = [this](int taskId) { return !isTaskRunning(taskId); };
  632. cardLay->addWidget(zone);
  633. grid->addWidget(card, i / 2, i % 2);
  634. }
  635. row->addWidget(stationBox, 5);
  636. root->addLayout(row, 1);
  637. refreshStationBoxes();
  638. // 机台的在线/离线状态跟着后端走,每秒刷一次标签(后端 state 随时在变)
  639. QTimer *statusTimer = new QTimer(this);
  640. statusTimer->setInterval(1000);
  641. connect(statusTimer, &QTimer::timeout, this, &CreateTaskDialog::refreshStationStatus);
  642. statusTimer->start();
  643. refreshStationStatus();
  644. }
  645. // ---------------------------------------------------------------------------
  646. // 点「创建任务」:把配置页当前内容变成一条任务,切到第二个页签
  647. // ---------------------------------------------------------------------------
  648. void CreateTaskDialog::createTask()
  649. {
  650. // 先问任务名字:取消或留空都不建任务,也不占用编号。
  651. // 把已有任务名传进去,重名直接在输入框里红字提示,不生成第二条同名任务。
  652. QStringList existingNames;
  653. existingNames.reserve(m_tasks.size());
  654. for (const SltTask &t : m_tasks)
  655. existingNames.append(t.name);
  656. QString taskName;
  657. if (!promptTaskName(this, QStringLiteral("高温标参"), existingNames, &taskName))
  658. return;
  659. SltTask t;
  660. t.id = m_nextTaskId++;
  661. t.name = taskName;
  662. t.cfg = currentConfig();
  663. m_tasks.append(t);
  664. refreshTaskList();
  665. ui->taskTabs->setCurrentIndex(1); // 切到任务分配页,底部按钮随之隐藏
  666. }
  667. void CreateTaskDialog::refreshTaskList()
  668. {
  669. if (!m_taskList)
  670. return;
  671. m_taskList->clear();
  672. // 任务池里只显示任务名字,不铺温度、算法数等摘要
  673. for (const SltTask &t : m_tasks) {
  674. QListWidgetItem *it = new QListWidgetItem(m_taskList);
  675. it->setText(t.name);
  676. it->setSizeHint(QSize(240, 32));
  677. it->setData(Qt::UserRole, t.id);
  678. it->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
  679. it->setToolTip(QStringLiteral("拖到右侧机台的任务框里完成分配"));
  680. }
  681. }
  682. void CreateTaskDialog::deleteSelectedTask()
  683. {
  684. if (!m_taskList || !m_taskList->currentItem())
  685. return;
  686. const int id = m_taskList->currentItem()->data(Qt::UserRole).toInt();
  687. // 执行中的任务不能删:删了会让本地队列和 qt-plans.json 对不上
  688. if (isTaskRunning(id)) {
  689. showNotice(this, QStringLiteral("任务执行中"),
  690. QStringLiteral("「%1」正在执行中,不能删除。请等本轮结束后再操作。")
  691. .arg(taskNameOf(id)));
  692. return;
  693. }
  694. const QString name = m_taskList->currentItem()->text();
  695. if (!confirmRemove(this, QStringLiteral("删除任务"),
  696. QStringLiteral("确定删除任务「%1」吗?\n"
  697. "已分配到机台的这条任务也会一并移除。").arg(name)))
  698. return;
  699. for (int i = 0; i < m_tasks.size(); ++i) {
  700. if (m_tasks.at(i).id == id) {
  701. m_tasks.removeAt(i);
  702. break;
  703. }
  704. }
  705. // 已经挂到机台的任务也要从各机台列表里摘掉并重写该工位 JSON
  706. for (int s = 0; s < m_stationTaskIds.size(); ++s) {
  707. if (m_stationTaskIds[s].removeAll(id) > 0) {
  708. m_saved = saveStationTasks(s);
  709. emit configsSaved(m_saved, s, QStringLiteral("删除"));
  710. }
  711. }
  712. refreshTaskList();
  713. refreshStationBoxes();
  714. }
  715. void CreateTaskDialog::refreshStationStatus()
  716. {
  717. for (int s = 0; s < m_stationStatus.size(); ++s) {
  718. QLabel *label = m_stationStatus.at(s);
  719. const bool online = m_onlineProbe ? m_onlineProbe(s) : true;
  720. const QString text = online ? QStringLiteral("在线") : QStringLiteral("离线");
  721. const QString state = online ? QStringLiteral("online") : QStringLiteral("offline");
  722. if (label->text() == text && label->property("state").toString() == state)
  723. continue;
  724. label->setText(text);
  725. label->setProperty("state", state);
  726. label->style()->unpolish(label);
  727. label->style()->polish(label);
  728. }
  729. // 「执行中」状态随时会变(任务起跑 / 跑完),每秒同步一次「清空」按钮,
  730. // 免得按钮还停留在上一次的状态。逻辑层 clearStation() 也实时兜底拦截。
  731. for (int s = 0; s < m_stationClearBtns.size(); ++s) {
  732. QToolButton *btn = m_stationClearBtns.at(s);
  733. if (!btn)
  734. continue;
  735. const bool locked = stationHasRunningTask(s);
  736. btn->setEnabled(!locked);
  737. btn->setToolTip(locked ? QStringLiteral("有任务执行中,不能清空") : QString());
  738. }
  739. }
  740. void CreateTaskDialog::refreshStationBoxes()
  741. {
  742. for (int s = 0; s < m_stationChipLays.size(); ++s) {
  743. QVBoxLayout *lay = m_stationChipLays.at(s);
  744. QWidget *host = lay->parentWidget();
  745. QLayoutItem *child = nullptr;
  746. while ((child = lay->takeAt(0)) != nullptr) {
  747. if (QWidget *w = child->widget())
  748. delete w;
  749. delete child;
  750. }
  751. const QVector<int> &ids = m_stationTaskIds.at(s);
  752. if (ids.isEmpty()) {
  753. // 空框只留虚线框,不写「把任务拖到这里」的提示字(末尾的 addStretch 负责撑开)
  754. } else {
  755. // 按拖入顺序一条一行地列出任务名,像队列一样,不显示「已分配」这类状态字
  756. for (int n = 0; n < ids.size(); ++n) {
  757. const int id = ids.at(n);
  758. QString name;
  759. for (const SltTask &t : m_tasks)
  760. if (t.id == id) { name = t.name; break; }
  761. if (name.isEmpty())
  762. name = QStringLiteral("已删除任务");
  763. QFrame *chip = new QFrame(host);
  764. chip->setProperty("role", "taskChip");
  765. chip->setAttribute(Qt::WA_StyledBackground, true);
  766. QHBoxLayout *chipLay = new QHBoxLayout(chip);
  767. chipLay->setContentsMargins(8, 3, 4, 3);
  768. chipLay->setSpacing(6);
  769. const bool running = isTaskRunning(id);
  770. QLabel *label = new QLabel(QStringLiteral("%1. %2").arg(n + 1).arg(name), chip);
  771. label->setProperty("role", "taskName");
  772. if (running)
  773. label->setToolTip(QStringLiteral("任务执行中,暂不能移除"));
  774. QToolButton *close = new QToolButton(chip);
  775. close->setText(QStringLiteral("×"));
  776. close->setCursor(Qt::PointingHandCursor);
  777. close->setFocusPolicy(Qt::NoFocus); // 同理,点完不留焦点虚框
  778. close->setProperty("role", "algoMini");
  779. close->setEnabled(!running);
  780. close->setToolTip(running ? QStringLiteral("任务执行中,不能移除")
  781. : QStringLiteral("从该机台移除这条任务"));
  782. chipLay->addWidget(label);
  783. chipLay->addStretch(1);
  784. chipLay->addWidget(close);
  785. connect(close, &QToolButton::clicked, this,
  786. [this, s, id]() { removeTask(s, id); });
  787. lay->addWidget(chip);
  788. }
  789. }
  790. lay->addStretch(1);
  791. }
  792. refreshStationStatus(); // 重铺后立刻同步「清空」按钮的可用状态
  793. }
  794. // 把该机台当前的任务列表写进 qt-plans.json:
  795. // stations.Sn.tasks = [{name, config}, ...],一个机台可以挂多条;
  796. // 同时保留扁平的 memory_params / rmt / amt / mt(取最后一条任务),
  797. // 让详情页、stationConfig() 等旧链路照常工作。
  798. bool CreateTaskDialog::saveStationTasks(int station)
  799. {
  800. if (station < 0 || station >= m_stationTaskIds.size())
  801. return false;
  802. QJsonArray arr;
  803. QJsonObject flat;
  804. for (int id : m_stationTaskIds.at(station)) {
  805. for (const SltTask &t : m_tasks) {
  806. if (t.id != id)
  807. continue;
  808. QJsonObject item;
  809. item.insert(QStringLiteral("name"), t.name);
  810. item.insert(QStringLiteral("config"), t.cfg);
  811. arr.append(item);
  812. flat = t.cfg;
  813. break;
  814. }
  815. }
  816. QJsonObject stationCfg = LocalPlans::stationConfig(station);
  817. if (!flat.isEmpty()) {
  818. const QStringList keys = flat.keys();
  819. for (const QString &k : keys)
  820. stationCfg.insert(k, flat.value(k));
  821. }
  822. stationCfg.insert(QStringLiteral("tasks"), arr);
  823. QJsonObject one;
  824. one.insert(QStringLiteral("S%1").arg(station + 1), stationCfg);
  825. qInfo().noquote() << "[plans] 写工位" << (station + 1) << "任务数 =" << arr.size();
  826. return LocalPlans::saveStations(one);
  827. }
  828. // 拖入机台:追加一条任务(同一任务不重复挂),立刻写该工位 JSON
  829. void CreateTaskDialog::setOnlineProbe(std::function<bool(int)> probe)
  830. {
  831. m_onlineProbe = std::move(probe);
  832. }
  833. void CreateTaskDialog::setRunningProbe(std::function<bool(const QString &)> probe)
  834. {
  835. m_runningProbe = std::move(probe);
  836. }
  837. QString CreateTaskDialog::taskNameOf(int taskId) const
  838. {
  839. for (const SltTask &t : m_tasks)
  840. if (t.id == taskId)
  841. return t.name;
  842. return QString();
  843. }
  844. bool CreateTaskDialog::isTaskRunning(int taskId) const
  845. {
  846. // 任务名是唯一键(创建时已禁止重名),主窗口按「当前工位正在跑的那条任务名」判定。
  847. const QString name = taskNameOf(taskId);
  848. return !name.isEmpty() && m_runningProbe && m_runningProbe(name);
  849. }
  850. bool CreateTaskDialog::stationHasRunningTask(int station) const
  851. {
  852. if (station < 0 || station >= m_stationTaskIds.size())
  853. return false;
  854. for (int id : m_stationTaskIds.at(station))
  855. if (isTaskRunning(id))
  856. return true;
  857. return false;
  858. }
  859. void CreateTaskDialog::assignTask(int station, int taskId)
  860. {
  861. if (station < 0 || station >= m_stationTaskIds.size())
  862. return;
  863. // 执行中的任务不能再挂到任何机台。dragEnter 已挡一层,这里再兜底:
  864. // 拖拽过程中该任务可能刚好开始跑。
  865. if (isTaskRunning(taskId)) {
  866. showNotice(this, QStringLiteral("任务执行中"),
  867. QStringLiteral("「%1」正在执行中,不能添加到任务框。请等本轮结束后再操作。")
  868. .arg(taskNameOf(taskId)));
  869. return;
  870. }
  871. // 机台显示「控制台离线」时不接收任务(拖拽时已被 dragEnterEvent 拒了,这里双保险)
  872. if (m_onlineProbe && !m_onlineProbe(station))
  873. return;
  874. bool exists = false;
  875. for (const SltTask &t : m_tasks)
  876. if (t.id == taskId) { exists = true; break; }
  877. if (!exists || m_stationTaskIds.at(station).contains(taskId))
  878. return;
  879. m_stationTaskIds[station].append(taskId);
  880. m_saved = saveStationTasks(station);
  881. refreshStationBoxes();
  882. // 不再有顶部文字反馈:拖进去之后机台框里直接出现任务条,那就是反馈。
  883. // 也避免连续拖多条时被弹窗 / 提示打断。
  884. emit configsSaved(m_saved, station, QStringLiteral("分配"));
  885. }
  886. void CreateTaskDialog::removeTask(int station, int taskId)
  887. {
  888. if (station < 0 || station >= m_stationTaskIds.size())
  889. return;
  890. if (!m_stationTaskIds.at(station).contains(taskId))
  891. return;
  892. if (isTaskRunning(taskId)) {
  893. showNotice(this, QStringLiteral("任务执行中"),
  894. QStringLiteral("「%1」正在执行中,不能从任务框移除。请等本轮结束后再操作。")
  895. .arg(taskNameOf(taskId)));
  896. return;
  897. }
  898. QString name;
  899. for (const SltTask &t : m_tasks)
  900. if (t.id == taskId) { name = t.name; break; }
  901. const QString dev = QStringLiteral("SLT-HT-%1").arg(station + 1, 3, 10, QLatin1Char('0'));
  902. if (!confirmRemove(this, QStringLiteral("移除任务"),
  903. QStringLiteral("确定从 %1 移除「%2」吗?\n其它机台不受影响。")
  904. .arg(dev, name)))
  905. return;
  906. m_stationTaskIds[station].removeAll(taskId);
  907. m_saved = saveStationTasks(station);
  908. refreshStationBoxes();
  909. emit configsSaved(m_saved, station, QStringLiteral("移除"));
  910. }
  911. void CreateTaskDialog::clearStation(int station)
  912. {
  913. if (station < 0 || station >= m_stationTaskIds.size())
  914. return;
  915. if (m_stationTaskIds.at(station).isEmpty())
  916. return;
  917. // 框里只要有一条执行中的任务就整框拒绝清空,避免连正在跑的那条一起删掉
  918. if (stationHasRunningTask(station)) {
  919. showNotice(this, QStringLiteral("有任务执行中"),
  920. QStringLiteral("该机台有正在执行的任务,不能清空。请等本轮结束后再操作。"));
  921. return;
  922. }
  923. const QString dev = QStringLiteral("SLT-HT-%1").arg(station + 1, 3, 10, QLatin1Char('0'));
  924. if (!confirmRemove(this, QStringLiteral("清空任务"),
  925. QStringLiteral("确定清空 %1 任务框里的全部 %2 条任务吗?")
  926. .arg(dev).arg(m_stationTaskIds.at(station).size())))
  927. return;
  928. m_stationTaskIds[station].clear();
  929. m_saved = saveStationTasks(station);
  930. refreshStationBoxes();
  931. emit configsSaved(m_saved, station, QStringLiteral("清空"));
  932. }
  933. // ---------------------------------------------------------------------------
  934. // 配置页 ↔ JSON
  935. // ---------------------------------------------------------------------------
  936. QJsonObject CreateTaskDialog::currentConfig() const
  937. {
  938. QJsonObject cfg = m_baseCfg;
  939. const QJsonObject mem = params();
  940. cfg.insert(QStringLiteral("memory_params"), mem);
  941. QJsonObject mtObj = withSelection(cfg.value(QStringLiteral("mt")).toObject(), algorithms());
  942. mtObj.insert(QStringLiteral("ecc"), mem.value(QStringLiteral("ecc")).toBool());
  943. mtObj.insert(QStringLiteral("debuglog"), mem.value(QStringLiteral("debuglog")).toBool());
  944. if (m_mtCustomRange && m_mtCustomRange->isChecked()) {
  945. const QString lo = m_mtAddrLoEdit ? m_mtAddrLoEdit->text().trimmed() : QString();
  946. const QString hi = m_mtAddrHiEdit ? m_mtAddrHiEdit->text().trimmed() : QString();
  947. if (!lo.isEmpty() || !hi.isEmpty()) {
  948. mtObj.insert(QStringLiteral("memory_range"), QStringLiteral("custom"));
  949. if (!lo.isEmpty())
  950. mtObj.insert(QStringLiteral("addr_lim_lo"), lo);
  951. else
  952. mtObj.remove(QStringLiteral("addr_lim_lo"));
  953. if (!hi.isEmpty())
  954. mtObj.insert(QStringLiteral("addr_lim_hi"), hi);
  955. else
  956. mtObj.remove(QStringLiteral("addr_lim_hi"));
  957. } else {
  958. mtObj.insert(QStringLiteral("memory_range"), QStringLiteral("all"));
  959. mtObj.remove(QStringLiteral("addr_lim_lo"));
  960. mtObj.remove(QStringLiteral("addr_lim_hi"));
  961. }
  962. } else {
  963. mtObj.insert(QStringLiteral("memory_range"), QStringLiteral("all"));
  964. mtObj.remove(QStringLiteral("addr_lim_lo"));
  965. mtObj.remove(QStringLiteral("addr_lim_hi"));
  966. }
  967. mtObj.remove(QStringLiteral("ADDRLIMLO"));
  968. mtObj.remove(QStringLiteral("ADDRLIMHI"));
  969. mtObj.remove(QStringLiteral("addr_lo"));
  970. mtObj.remove(QStringLiteral("addr_hi"));
  971. cfg.insert(QStringLiteral("mt"), mtObj);
  972. cfg.insert(QStringLiteral("amt"),
  973. withSelection(cfg.value(QStringLiteral("amt")).toObject(), amtAlgorithms()));
  974. cfg.insert(QStringLiteral("rmt"),
  975. withSelection(cfg.value(QStringLiteral("rmt")).toObject(), rmtAlgorithms()));
  976. return cfg;
  977. }
  978. void CreateTaskDialog::loadUiFrom(const QJsonObject &cfg)
  979. {
  980. QJsonObject p = cfg.value(QStringLiteral("memory_params")).toObject();
  981. if (p.isEmpty())
  982. p = configMemoryParams();
  983. for (int i = 0; i < kKeyCount; ++i) {
  984. QLineEdit *ed = findChild<QLineEdit *>(QStringLiteral("paramEdit_%1").arg(i));
  985. if (!ed)
  986. continue;
  987. const int v = p.value(QString::fromLatin1(kKeys[i])).toInt();
  988. ed->setText(v > 0 ? QString::number(v) : QString());
  989. }
  990. ui->errBox->setChecked(p.value(QStringLiteral("errhandle")).toBool());
  991. ui->apBox->setChecked(p.value(QStringLiteral("ap")).toBool());
  992. ui->eccBox->setChecked(p.value(QStringLiteral("ecc")).toBool());
  993. ui->debuglogBox->setChecked(p.value(QStringLiteral("debuglog")).toBool());
  994. ui->targetTempEdit->setText(QString::number(p.value(QStringLiteral("target_temp")).toInt(70)));
  995. auto apply = [](QListWidget *list, const QJsonObject &sec) {
  996. if (!sec.contains(QStringLiteral("selected")))
  997. return;
  998. QStringList sel = fromArray(sec.value(QStringLiteral("selected")));
  999. for (QString &s : sel) {
  1000. if (s == QLatin1String("Adress test, walking ones, 1 CPU"))
  1001. s = QStringLiteral("Address test, walking ones, 1 CPU");
  1002. }
  1003. const QList<QCheckBox *> cbs = algoChecks(list);
  1004. // 存档里的名字一个都对不上当前清单 = 清单换过版,按全选处理,
  1005. // 避免整组显示 0/N 把该阶段静默关掉。空数组是用户主动清空,照旧全不选。
  1006. if (!sel.isEmpty()) {
  1007. bool matched = false;
  1008. for (QCheckBox *cb : cbs) {
  1009. if (sel.contains(cb->text())) {
  1010. matched = true;
  1011. break;
  1012. }
  1013. }
  1014. if (!matched) {
  1015. for (QCheckBox *cb : cbs)
  1016. cb->setChecked(true);
  1017. return;
  1018. }
  1019. }
  1020. for (QCheckBox *cb : cbs)
  1021. cb->setChecked(sel.contains(cb->text()));
  1022. };
  1023. const QJsonObject mtCfg = cfg.value(QStringLiteral("mt")).toObject();
  1024. apply(ui->algoList, mtCfg);
  1025. apply(ui->amtList, cfg.value(QStringLiteral("amt")).toObject());
  1026. apply(ui->rmtList, cfg.value(QStringLiteral("rmt")).toObject());
  1027. if (m_mtCustomRange && m_mtAddrLoEdit && m_mtAddrHiEdit) {
  1028. QString lo = mtCfg.value(QStringLiteral("addr_lim_lo")).toString().trimmed();
  1029. if (lo.isEmpty()) lo = mtCfg.value(QStringLiteral("ADDRLIMLO")).toString().trimmed();
  1030. QString hi = mtCfg.value(QStringLiteral("addr_lim_hi")).toString().trimmed();
  1031. if (hi.isEmpty()) hi = mtCfg.value(QStringLiteral("ADDRLIMHI")).toString().trimmed();
  1032. const bool custom = (mtCfg.value(QStringLiteral("memory_range")).toString() == QLatin1String("custom"))
  1033. || !lo.isEmpty() || !hi.isEmpty();
  1034. m_mtCustomRange->setChecked(custom);
  1035. m_mtAddrLoEdit->setText(lo);
  1036. m_mtAddrHiEdit->setText(hi);
  1037. }
  1038. refreshAll();
  1039. }
  1040. void CreateTaskDialog::refreshAll()
  1041. {
  1042. auto upd = [](QListWidget *list, QLabel *badge, QLabel *foot) {
  1043. const QList<QCheckBox *> cbs = algoChecks(list);
  1044. int sel = 0;
  1045. for (QCheckBox *cb : cbs)
  1046. if (cb->isChecked())
  1047. ++sel;
  1048. badge->setText(QStringLiteral("%1/%2").arg(sel).arg(cbs.size()));
  1049. foot->setText(QStringLiteral("共 %1 项 · 已选 %2 项").arg(cbs.size()).arg(sel));
  1050. };
  1051. upd(ui->algoList, ui->mtBadge, ui->mtFoot);
  1052. upd(ui->amtList, ui->amtBadge, ui->amtFoot);
  1053. upd(ui->rmtList, ui->rmtBadge, ui->rmtFoot);
  1054. }
  1055. QJsonObject CreateTaskDialog::params() const
  1056. {
  1057. // 保留后端 0.8.1.19 支持的扩展 ICT 目录参数(最多 224 项)。
  1058. // 当前表单只编辑常用快捷项,其余已从 config.json / qt-plans.json 载入的键
  1059. // 必须原样留在任务配置里,不能在创建任务时被空对象覆盖掉。
  1060. QJsonObject o = m_baseCfg.value(QStringLiteral("memory_params")).toObject();
  1061. if (o.isEmpty())
  1062. o = configMemoryParams();
  1063. for (int i = 0; i < kKeyCount; ++i) {
  1064. const QString key = QString::fromLatin1(kKeys[i]);
  1065. QLineEdit *ed = findChild<QLineEdit *>(QStringLiteral("paramEdit_%1").arg(i));
  1066. if (!ed)
  1067. continue;
  1068. const QString txt = ed->text().trimmed();
  1069. if (txt.isEmpty()) {
  1070. o.remove(key);
  1071. continue;
  1072. }
  1073. const int v = txt.toInt();
  1074. if (v > 0)
  1075. o.insert(key, v);
  1076. else
  1077. o.remove(key);
  1078. }
  1079. o.insert(QStringLiteral("errhandle"), ui->errBox->isChecked());
  1080. o.insert(QStringLiteral("ap"), ui->apBox->isChecked());
  1081. o.insert(QStringLiteral("ecc"), ui->eccBox->isChecked());
  1082. o.insert(QStringLiteral("debuglog"), ui->debuglogBox->isChecked());
  1083. const int targetTemp = ui->targetTempEdit->text().trimmed().toInt();
  1084. if (targetTemp > 0)
  1085. o.insert(QStringLiteral("target_temp"), targetTemp);
  1086. return o;
  1087. }
  1088. QStringList CreateTaskDialog::algorithms() const
  1089. {
  1090. return checkedItems(ui->algoList);
  1091. }
  1092. QStringList CreateTaskDialog::amtAlgorithms() const
  1093. {
  1094. return checkedItems(ui->amtList);
  1095. }
  1096. QStringList CreateTaskDialog::rmtAlgorithms() const
  1097. {
  1098. return checkedItems(ui->rmtList);
  1099. }