blastopepage.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #include "blastopepage.h"
  2. #include <QFont>
  3. #include <QProcessEnvironment>
  4. #include <QWebEngineSettings>
  5. #include "../components/custommessagebox.h"
  6. #include "../components/loadingwidget.h"
  7. #include "../registryManager/registrymanager.h"
  8. #include "../utils/global.h"
  9. #include "../utils/logger.h"
  10. #include "countdownwidget.h"
  11. #include "ui_blastopepage.h"
  12. const int ColIndexBlastStatus = 6;
  13. const int ColIndexProgressBar = 7;
  14. const int ColIndexOpBtn = 8;
  15. const int ColIndexBlasterDev = 1;
  16. BlastOpePage::BlastOpePage(QWidget *parent)
  17. : QWidget(parent),
  18. ui(new Ui::BlastOpePage),
  19. dao(DatabaseManager::getInstance().getDatabase()),
  20. m_faceVerification(nullptr) {
  21. QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
  22. bool isAllSafetyInspectorConfirmed = true;
  23. const QJsonObject data = backendAPIManager::getSafetyInspect(QDate::currentDate().toString("yyyy-MM-dd"));
  24. for (const QJsonValue &value : data["safetyInspectors"].toArray()) {
  25. if (value.isObject()) {
  26. if (value.toObject()["confirmTime"].toString() == "") {
  27. isAllSafetyInspectorConfirmed = false;
  28. break;
  29. }
  30. }
  31. }
  32. if (!isAllSafetyInspectorConfirmed) {
  33. bool isContinue =
  34. CustomMessageBox::showConfirmDialog("警告", "安全检查没有全部确认,是否继续?", "继续", "取消", this);
  35. if (!isContinue) {
  36. return;
  37. }
  38. }
  39. if (isFaceVerificationEnabled) {
  40. initFaceVerification();
  41. } else {
  42. ui->setupUi(this);
  43. initPagination();
  44. }
  45. }
  46. void BlastOpePage::showCountDownWidget(QString uuid, const QString &topic, const QString &message) {
  47. CountdownWidget *countdownWidget = new CountdownWidget(this);
  48. countdownWidget->resize(200, 200);
  49. int x = (this->width() - countdownWidget->width()) / 2;
  50. int y = (this->height() - countdownWidget->height()) / 2;
  51. countdownWidget->move(x, y);
  52. countdownWidget->show();
  53. firingWidget *widget = firingWidgetByUuid.value(uuid);
  54. if (widget) {
  55. connect(
  56. countdownWidget, &CountdownWidget::countdownFinished, widget,
  57. [widget, topic, message, countdownWidget]() { widget->onCountdownFinished(topic, message); },
  58. Qt::SingleShotConnection);
  59. }
  60. }
  61. void BlastOpePage::initFaceVerification() {
  62. m_faceVerification = new FaceVerification(this);
  63. connect(m_faceVerification, &FaceVerification::verificationSuccessful, this,
  64. &BlastOpePage::onFaceVerificationSuccess);
  65. connect(m_faceVerification, &FaceVerification::verificationFailed, this, &BlastOpePage::onFaceVerificationFailed);
  66. m_faceVerification->initFaceVerification();
  67. }
  68. void BlastOpePage::onFaceVerificationSuccess() {
  69. ui->setupUi(this);
  70. initPagination();
  71. }
  72. void BlastOpePage::onFaceVerificationFailed(const QString &message) {
  73. // Handle face verification failure
  74. // The FaceVerification class already shows appropriate error messages
  75. Q_UNUSED(message)
  76. }
  77. BlastOpePage::~BlastOpePage() {
  78. delete ui;
  79. if (m_faceVerification) {
  80. delete m_faceVerification;
  81. }
  82. }
  83. void BlastOpePage::initPagination() {
  84. // // pageWidget = new PageWidget;
  85. // //connect(pageWidget, &PageWidget::currentPageChanged, this, &BlastOpePage::PageChanged);
  86. // connect(pageWidget->getComboBox(), QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  87. // &BlastOpePage::onComboBoxIndexChanged);
  88. // ui->verticalLayout_4->addWidget(pageWidget);
  89. m_pageSize = 100;
  90. m_currentPage = 1;
  91. RefreshData();
  92. }
  93. void BlastOpePage::RefreshData() { loadDataAndDrawTable(m_currentPage, m_pageSize); }
  94. void BlastOpePage::loadDataAndDrawTable(int currentPage, int pageSize) {
  95. LoadingWidget::showLoading(this, "正在加载数据...");
  96. QJsonObject extParams{{"blastStatus", QJsonArray({"2"})}};
  97. QJsonObject result = backendAPIManager::getHProjects(currentPage, pageSize, extParams);
  98. QList<QSharedPointer<HProject>> projectList =
  99. dao.getHProjectsFromJsonArray(result["data"].toObject()["list"].toArray());
  100. QMap<QString, QList<QSharedPointer<HProject>>> groupedProjects;
  101. for (const QSharedPointer<HProject> &project : projectList) {
  102. QString key = project->getPcSn();
  103. if (!groupedProjects.contains(key)) {
  104. groupedProjects[key] = QList<QSharedPointer<HProject>>();
  105. }
  106. groupedProjects[key].append(project);
  107. }
  108. totalCount = result["data"].toObject()["count"].toInt();
  109. // pageWidget->setMaxPage(ceil(static_cast<double>(totalCount) / pageSize));
  110. // Clear previous data
  111. pcSnToFirstRowMap.clear();
  112. progressBars.clear();
  113. model = new QStandardItemModel(this);
  114. headers = {
  115. {"选择", ""},
  116. {"井下起爆器", "pcSn"},
  117. {"工程名称", "name"},
  118. {"井下地址", "addressPath"},
  119. {"雷管数量", "detSum"},
  120. {"炸药量(kg)", "blastCount"},
  121. {"起爆状态", "blastStatus"},
  122. {"进度", ""},
  123. {"操作", ""},
  124. };
  125. int headerCount = headers.size();
  126. QStringList headerLabels;
  127. QMap<int, QString> propMap;
  128. for (int i = 0; i < headers.size(); ++i) {
  129. headerLabels << headers[i].label;
  130. propMap[i] = headers[i].prop;
  131. }
  132. model->setHorizontalHeaderLabels(headerLabels);
  133. // Create rows with sub-rows for each project, grouped by pcSn
  134. QStringList pcSnKeys = groupedProjects.keys();
  135. int currentRow = 0;
  136. for (const QString &pcSn : pcSnKeys) {
  137. QList<QSharedPointer<HProject>> projectsForPcSn = groupedProjects[pcSn];
  138. pcSnToFirstRowMap[pcSn] = currentRow; // Store the first row index for this pcSn
  139. // Calculate aggregated blast status for this pcSn group
  140. QString combinedBlastStatus = "";
  141. QSet<QString> statusSet;
  142. for (const QSharedPointer<HProject> &project : projectsForPcSn) {
  143. QMetaProperty statusProp =
  144. project->metaObject()->property(project->metaObject()->indexOfProperty("blastStatus"));
  145. QString status = statusProp.read(project.data()).toString();
  146. statusSet.insert(status);
  147. }
  148. // Determine the combined status based on all projects in the group
  149. if (statusSet.size() == 1) {
  150. // All projects have the same status
  151. combinedBlastStatus = statusSet.values().first();
  152. } else {
  153. // Mixed statuses - use the most advanced status
  154. if (statusSet.contains(BlastStatus::Blasted)) {
  155. combinedBlastStatus = BlastStatus::Blasted;
  156. } else {
  157. combinedBlastStatus = BlastStatus::Registered;
  158. }
  159. }
  160. // Store all UUIDs for this pcSn group in the first row
  161. QStandardItem *uuidItem = new QStandardItem();
  162. QStringList uuids;
  163. for (const QSharedPointer<HProject> &project : projectsForPcSn) {
  164. uuids << project->getUuid();
  165. }
  166. uuidItem->setData(uuids.join(","), Qt::UserRole);
  167. model->setItem(currentRow, headerCount, uuidItem);
  168. // Create a sub-row for each project in this pcSn group
  169. for (int subIndex = 0; subIndex < projectsForPcSn.size(); ++subIndex) {
  170. HProject &project = *projectsForPcSn[subIndex].data();
  171. for (int col = 0; col < headerCount; ++col) {
  172. QString prop = propMap[col];
  173. QStandardItem *item = nullptr;
  174. if (col == 0) {
  175. // Only show checkbox on the first row of each pcSn group
  176. if (subIndex == 0) {
  177. item = new QStandardItem();
  178. item->setCheckable(true);
  179. item->setCheckState(Qt::Unchecked);
  180. item->setText("");
  181. } else {
  182. item = new QStandardItem("");
  183. }
  184. } else if (prop == "pcSn") {
  185. // Only show pcSn on the first row of each group
  186. if (subIndex == 0) {
  187. item = new QStandardItem(pcSn);
  188. } else {
  189. item = new QStandardItem("");
  190. }
  191. } else if (prop == "blastStatus") {
  192. // Only show aggregated blast status on the first row of each group
  193. if (subIndex == 0) {
  194. QString statusText;
  195. if (combinedBlastStatus == BlastStatus::Registered) {
  196. statusText = "待起爆";
  197. item = new QStandardItem(statusText);
  198. item->setForeground(QColor("#f3a3k'k1"));
  199. } else if (combinedBlastStatus == BlastStatus::Blasted) {
  200. statusText = "起 爆 完 成";
  201. item = new QStandardItem(statusText);
  202. item->setForeground(QColor("#90d543"));
  203. } else {
  204. item = new QStandardItem(combinedBlastStatus);
  205. }
  206. } else {
  207. item = new QStandardItem("");
  208. }
  209. } else if (col == ColIndexProgressBar || col == ColIndexOpBtn) {
  210. // Progress and operation columns will be handled separately with widgets
  211. // Only on the first row of each group
  212. if (subIndex == 0) {
  213. item = new QStandardItem("");
  214. } else {
  215. item = new QStandardItem("");
  216. }
  217. } else if (!prop.isEmpty()) {
  218. // Show individual project data for other columns
  219. QMetaProperty metaProp =
  220. project.metaObject()->property(project.metaObject()->indexOfProperty(prop.toUtf8()));
  221. QVariant value = metaProp.read(&project);
  222. item = new QStandardItem(value.toString());
  223. }
  224. if (item) {
  225. item->setTextAlignment(Qt::AlignCenter);
  226. model->setItem(currentRow, col, item);
  227. }
  228. }
  229. currentRow++;
  230. }
  231. }
  232. ui->tableView->setModel(model);
  233. connectionItem =
  234. QObject::connect(model, &QStandardItemModel::itemChanged, this, &BlastOpePage::onItemCheckboxChanged);
  235. ui->tableView->setColumnWidth(0, 30);
  236. ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); // 禁止编辑
  237. for (int i = 1; i < headerCount; ++i) {
  238. if (i == 8) { // Column: 进度
  239. ui->tableView->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Custom);
  240. ui->tableView->setColumnWidth(i, 180);
  241. } else {
  242. ui->tableView->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
  243. }
  244. }
  245. ui->tableView->setColumnHidden(headerCount, true);
  246. ui->tableView->setAlternatingRowColors(true);
  247. ui->tableView->verticalHeader()->setDefaultSectionSize(50);
  248. // Create progress bars and operation buttons only for the first row of each pcSn group
  249. for (const QString &pcSn : pcSnKeys) {
  250. QList<QSharedPointer<HProject>> projectsForPcSn = groupedProjects[pcSn];
  251. int firstRowIndex = pcSnToFirstRowMap[pcSn];
  252. int groupRowSpan = projectsForPcSn.size();
  253. // 初始化progressBars for the first row of this pcSn group
  254. int progressCol = headers.size() - 2;
  255. QProgressBar *progressBar1 = new QProgressBar(ui->tableView);
  256. QProgressBar *progressBar2 = new QProgressBar(ui->tableView);
  257. QProgressBar *progressBar3 = new QProgressBar(ui->tableView);
  258. QHBoxLayout *progressBarLayout = new QHBoxLayout;
  259. progressBars.append(ProgressBarTriple(progressBar1, progressBar2, progressBar3));
  260. for (QProgressBar *pb : {progressBar1, progressBar2, progressBar3}) {
  261. pb->setRange(0, 100);
  262. pb->setValue(0);
  263. pb->setAlignment(Qt::AlignCenter);
  264. pb->setStyleSheet(
  265. "QProgressBar { border: 1px solid grey; border-radius: 5px; background-color: #EEEEEE; "
  266. "height: 10px; }"
  267. "QProgressBar::chunk { background-color: #05B8CC; width: 2px; margin: 0.5px; border - "
  268. "radius: 10px; }");
  269. pb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  270. progressBarLayout->addWidget(pb);
  271. }
  272. progressBarLayout->setAlignment(Qt::AlignCenter);
  273. progressBarLayout->setContentsMargins(0, 0, 0, 0);
  274. progressBarLayout->setSpacing(0);
  275. QWidget *progressBarContainer = new QWidget(ui->tableView);
  276. progressBarContainer->setLayout(progressBarLayout);
  277. QModelIndex progressIndex = model->index(firstRowIndex, progressCol);
  278. if (progressIndex.isValid()) {
  279. ui->tableView->setIndexWidget(progressIndex, progressBarContainer);
  280. // Set row span to cover all sub-rows for this pcSn
  281. ui->tableView->setSpan(firstRowIndex, progressCol, groupRowSpan, 1);
  282. }
  283. // 创建操作按钮 for the first row of this pcSn group
  284. int operationCol = headers.size() - 1;
  285. QWidget *widget = new QWidget(ui->tableView);
  286. QPushButton *button = new QPushButton(widget);
  287. button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  288. button->setStyleSheet("QPushButton:disabled { background-color: #d3d3d3; color: gray; }");
  289. QModelIndex statusIndex = model->index(firstRowIndex, ColIndexBlastStatus);
  290. if (statusIndex.isValid()) {
  291. QString blastStatus = model->data(statusIndex).toString();
  292. if (blastStatus == "待起爆") {
  293. button->setText(startBlastButtonTxt);
  294. button->setEnabled(true);
  295. }
  296. }
  297. QHBoxLayout *layout = new QHBoxLayout(widget);
  298. layout->addWidget(button);
  299. layout->setAlignment(Qt::AlignCenter);
  300. layout->setContentsMargins(0, 0, 0, 0);
  301. widget->setLayout(layout);
  302. QModelIndex index = model->index(firstRowIndex, operationCol);
  303. if (index.isValid()) {
  304. ui->tableView->setIndexWidget(index, widget);
  305. // Set row span to cover all sub-rows for this pcSn
  306. ui->tableView->setSpan(firstRowIndex, operationCol, groupRowSpan, 1);
  307. connect(button, &QPushButton::clicked,
  308. [this, firstRowIndex, button]() { handleSingleBlastButtonClicked(firstRowIndex, button); });
  309. }
  310. // Also set span for the pcSn column to merge cells
  311. ui->tableView->setSpan(firstRowIndex, ColIndexBlasterDev, groupRowSpan, 1);
  312. // Set span for the checkbox column
  313. ui->tableView->setSpan(firstRowIndex, 0, groupRowSpan, 1);
  314. // Set span for the blast status column
  315. ui->tableView->setSpan(firstRowIndex, ColIndexBlastStatus, groupRowSpan, 1);
  316. }
  317. LoadingWidget::hideLoading();
  318. }
  319. // 切换页数
  320. void BlastOpePage::PageChanged(int page) {
  321. m_currentPage = page;
  322. loadDataAndDrawTable(m_currentPage, m_pageSize);
  323. }
  324. void BlastOpePage::onComboBoxIndexChanged(int index) {
  325. // QVariant variant = pageWidget->getComboBox()->itemData(index);
  326. // int value = variant.toInt();
  327. // m_pageSize = value;
  328. // m_currentPage = 1;
  329. // loadDataAndDrawTable(m_currentPage, m_pageSize);
  330. }
  331. void BlastOpePage::updateProgressBar(int firingStage, int row) {
  332. // Find the progress bar index for this row's pcSn group
  333. QString pcSn;
  334. QStandardItem *devItem = model->item(row, ColIndexBlasterDev);
  335. if (devItem) {
  336. pcSn = devItem->text();
  337. if (pcSn.isEmpty()) {
  338. // This might be a sub-row, need to find the parent row with pcSn
  339. for (int i = row - 1; i >= 0; i--) {
  340. QStandardItem *parentDevItem = model->item(i, ColIndexBlasterDev);
  341. if (parentDevItem && !parentDevItem->text().isEmpty()) {
  342. pcSn = parentDevItem->text();
  343. break;
  344. }
  345. }
  346. }
  347. }
  348. // Find the progress bar index for this pcSn
  349. QStringList pcSnKeys = QStringList(pcSnToFirstRowMap.keys());
  350. int progressBarIndex = pcSnKeys.indexOf(pcSn);
  351. if (progressBarIndex >= 0 && progressBarIndex < progressBars.size()) {
  352. QProgressBar *progressBar1 = progressBars[progressBarIndex].bar1;
  353. QProgressBar *progressBar2 = progressBars[progressBarIndex].bar2;
  354. QProgressBar *progressBar3 = progressBars[progressBarIndex].bar3;
  355. // reset all progress bars
  356. for (QProgressBar *pb : {progressBar1, progressBar2, progressBar3}) {
  357. pb->setRange(0, 100); // 设置范围为0到100
  358. pb->setValue(0);
  359. }
  360. switch (firingStage) {
  361. case FiringStages::QuickTesting:
  362. // 起爆检测状态
  363. progressBar1->setRange(0, 0); // 设置范围为0到100
  364. progressBar1->setValue(0);
  365. break;
  366. case FiringStages::QuickTestFinished:
  367. // 起爆检测完成
  368. progressBar1->setValue(100);
  369. break;
  370. case FiringStages::NetCharging:
  371. // 充电开始
  372. progressBar1->setValue(100);
  373. progressBar2->setRange(0, 0); // 设置范围为0到100
  374. progressBar2->setValue(0);
  375. break;
  376. case FiringStages::NetChargingFinished:
  377. // 充电完成
  378. progressBar1->setValue(100);
  379. progressBar2->setValue(100);
  380. break;
  381. case FiringStages::Blasting:
  382. progressBar1->setValue(100);
  383. progressBar2->setValue(100);
  384. progressBar3->setRange(0, 0); // 设置范围为0到100
  385. progressBar3->setValue(0);
  386. break;
  387. case FiringStages::BlastFinished:
  388. // 充电完成
  389. progressBar1->setValue(100);
  390. progressBar2->setValue(100);
  391. progressBar3->setValue(100);
  392. break;
  393. default:
  394. break;
  395. }
  396. }
  397. }
  398. // update tableView's fields about firing stage
  399. void BlastOpePage::onFiringStageUpdated(int stage, int row) {
  400. // update blast status item
  401. QModelIndex firingStatusItem = model->index(row, ColIndexBlastStatus);
  402. if (firingStatusItem.isValid()) {
  403. QColor customColor;
  404. QFont boldFont;
  405. boldFont.setBold(true);
  406. qDebug() << "opage::onFiringStageUpdated: stage = " << stage << ", row = " << row;
  407. switch (stage) {
  408. case FiringStages::Starting:
  409. model->setData(firingStatusItem, "待起爆");
  410. customColor = QColor("#44035b");
  411. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  412. model->setData(firingStatusItem, boldFont, Qt::FontRole);
  413. break;
  414. case FiringStages::QuickTesting:
  415. model->setData(firingStatusItem, "起爆检测中...");
  416. customColor = QColor("#44035b");
  417. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  418. model->setData(firingStatusItem, boldFont, Qt::FontRole);
  419. break;
  420. case FiringStages::QuickTestFinished:
  421. model->setData(firingStatusItem, "检测完成");
  422. customColor = QColor("#404185");
  423. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  424. break;
  425. case FiringStages::NetCharging:
  426. model->setData(firingStatusItem, "充电中 ...");
  427. customColor = QColor("#31688e");
  428. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  429. break;
  430. case FiringStages::NetChargingFinished:
  431. model->setData(firingStatusItem, "充电完成");
  432. customColor = QColor("#1f918d");
  433. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  434. break;
  435. case FiringStages::Blasting:
  436. model->setData(firingStatusItem, "起爆中...");
  437. customColor = QColor("#38b775");
  438. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  439. break;
  440. case FiringStages::BlastFinished:
  441. model->setData(firingStatusItem, "起爆完成");
  442. customColor = QColor("#90d543");
  443. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  444. break;
  445. case FiringStages::PendingTriggerButtonClick:
  446. model->setData(firingStatusItem, "按下双键起爆...");
  447. customColor = QColor("#8e620");
  448. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  449. break;
  450. case FiringStages::CancelConfirmed:
  451. model->setData(firingStatusItem, "已取消");
  452. customColor = QColor("#893c3c");
  453. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  454. break;
  455. case FiringStages::ForceCanceled:
  456. model->setData(firingStatusItem, "已强制取消");
  457. customColor = QColor("#893c3c");
  458. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  459. break;
  460. default:
  461. break;
  462. }
  463. }
  464. updateProgressBar(stage, row);
  465. handleUpdateOpButton(stage, row);
  466. }
  467. void BlastOpePage::handleSingleBlastButtonClicked(int row, QPushButton *button) {
  468. QStandardItem *devItem = model->item(row, ColIndexBlasterDev);
  469. QString blasterDevPcSn;
  470. if (devItem) {
  471. blasterDevPcSn = devItem->text();
  472. }
  473. // Get all UUIDs for this pcSn group
  474. QStandardItem *uuidItem = model->item(row, headers.size());
  475. QStringList uuids;
  476. if (uuidItem) {
  477. QString uuidData = uuidItem->data(Qt::UserRole).toString();
  478. uuids = uuidData.split(",", Qt::SkipEmptyParts);
  479. }
  480. if (button->text() == startBlastButtonTxt) {
  481. button->setDisabled(true);
  482. QTimer::singleShot(5000, [button]() {
  483. if (button->isEnabled()) {
  484. return;
  485. }
  486. button->setEnabled(true);
  487. });
  488. button->setMinimumWidth(80);
  489. button->setText(stopBlastButtonTxt);
  490. // Create firing widget for the pcSn group (use pcSn as identifier)
  491. // Use the first row index for this pcSn group for progress updates
  492. firingWidget *widget = new firingWidget(row, false, blasterDevPcSn);
  493. connect(widget, &firingWidget::updatefiringStage, this, &BlastOpePage::onFiringStageUpdated);
  494. connect(widget, &firingWidget::startCountdown, this, &BlastOpePage::showCountDownWidget);
  495. connect(widget, &firingWidget::updateProjectStatus, this, &BlastOpePage::handlerUpdateProjectStatus);
  496. connect(widget, &firingWidget::closeFiring, this, &BlastOpePage::destroyFiringWidget);
  497. if (isShowTriggeringWidget) {
  498. widget->show();
  499. }
  500. widget->setAttribute(Qt::WA_DeleteOnClose);
  501. firingWidgetByUuid.insert(blasterDevPcSn, widget);
  502. widget->startBlasting();
  503. } else if (button->text() == stopBlastButtonTxt) {
  504. bool isContinue = CustomMessageBox::showConfirmDialog("提示", "是否取消爆破?", "继续", "取消", this);
  505. if (!isContinue) {
  506. return;
  507. }
  508. firingWidget *widget = firingWidgetByUuid.value(blasterDevPcSn);
  509. if (widget) {
  510. widget->sendCancelFiringMsg();
  511. }
  512. } else {
  513. Logger::getInstance().error(
  514. QString("handleSingleBlastButtonClicked: unknown button text %1").arg(button->text()));
  515. return;
  516. }
  517. }
  518. void BlastOpePage::handleUpdateOpButton(int stage, int row) {
  519. QModelIndex index = model->index(row, ColIndexOpBtn);
  520. if (!index.isValid()) {
  521. Logger::getInstance().error(QString("can not get updateOpBtnStage index %1").arg(row));
  522. return;
  523. }
  524. QWidget *widget = ui->tableView->indexWidget(index);
  525. QPushButton *button = widget->findChild<QPushButton *>();
  526. if (!button) {
  527. Logger::getInstance().error(QString("can not find button in row %1").arg(row));
  528. return;
  529. }
  530. switch (stage) {
  531. case FiringStages::CancelConfirmed:
  532. button->setDisabled(true);
  533. button->setText(reStartButtonTxt);
  534. break;
  535. case FiringStages::Starting:
  536. button->setDisabled(true);
  537. break;
  538. case FiringStages::QuickTesting:
  539. button->setEnabled(true);
  540. button->setText(stopBlastButtonTxt);
  541. break;
  542. case FiringStages::QuickTestFinished:
  543. button->setEnabled(true);
  544. button->setText(stopBlastButtonTxt);
  545. break;
  546. case FiringStages::NetCharging:
  547. button->setEnabled(true);
  548. button->setText(stopBlastButtonTxt);
  549. break;
  550. case FiringStages::NetChargingFinished:
  551. button->setEnabled(true);
  552. button->setText(stopBlastButtonTxt);
  553. break;
  554. case FiringStages::PendingTriggerButtonClick:
  555. button->setEnabled(true);
  556. button->setText(stopBlastButtonTxt);
  557. break;
  558. case FiringStages::Blasting:
  559. button->setDisabled(true);
  560. button->setText("...");
  561. break;
  562. case FiringStages::BlastFinished:
  563. button->setDisabled(true);
  564. button->setText("");
  565. // 使用样式表设置图标居中
  566. button->setStyleSheet(
  567. "QPushButton {"
  568. " padding: 0px;"
  569. " border: none;"
  570. " background-color: transparent;"
  571. "}");
  572. button->setIcon(QIcon(":/icons/icons/svg/blast.svg"));
  573. button->setIconSize(QSize(32, 32));
  574. break;
  575. case FiringStages::ForceCanceled:
  576. button->setDisabled(true);
  577. button->setText(reStartButtonTxt);
  578. break;
  579. default:
  580. button->setEnabled(true);
  581. break;
  582. }
  583. }
  584. void BlastOpePage::handlerUpdateProjectStatus(QString uuid, const QString &newStatus) {
  585. // dao.updateBlastStatusByUuid(uuid, newStatus);
  586. QJsonObject extParams;
  587. qDebug() << "handlerUpdateProjectStatus: uuid = " << uuid << ", newStatus = " << newStatus;
  588. // extParams.insert("blastStatus", QJsonArray({newStatus}));
  589. }
  590. void BlastOpePage::destroyFiringWidget(const QString &uuid, int row) {
  591. firingWidget *widget = firingWidgetByUuid.value(uuid);
  592. if (widget) {
  593. widget->close(); // 关闭窗口
  594. widget->deleteLater(); // 释放内存
  595. widget->disconnect(); // 断开信号连接
  596. firingWidgetByUuid.remove(uuid); // 从映射中移除
  597. }
  598. }
  599. // 槽函数,当 item 状态改变时触发
  600. void BlastOpePage::onItemCheckboxChanged(QStandardItem *item) {
  601. if (item->column() == 0) { // 仅处理第一列的勾选状态改变
  602. if (item->checkState() == Qt::Checked) {
  603. // Find the first row for this pcSn group that contains the UUID data
  604. int currentRow = item->row();
  605. QStandardItem *uuidItem = model->item(currentRow, headers.size());
  606. if (uuidItem) {
  607. QString uuidData = uuidItem->data(Qt::UserRole).toString();
  608. QStringList uuids = uuidData.split(",", Qt::SkipEmptyParts);
  609. if (!uuids.isEmpty()) {
  610. // Get the pcSn for this group
  611. QStandardItem *pcSnItem = model->item(currentRow, ColIndexBlasterDev);
  612. if (pcSnItem) {
  613. QString pcSn = pcSnItem->text();
  614. uuidMap[currentRow] = pcSn; // Store pcSn using the first row of the group
  615. }
  616. }
  617. }
  618. } else if (item->checkState() == Qt::Unchecked) {
  619. uuidMap.remove(item->row());
  620. }
  621. }
  622. }
  623. void BlastOpePage::on_btnSelect_clicked() {
  624. // 禁用表格第一列的选项
  625. for (int row = 0; row < model->rowCount(); ++row) {
  626. QStandardItem *item = model->item(row, 0);
  627. if (item) {
  628. Qt::ItemFlags flags = item->flags();
  629. flags &= ~Qt::ItemIsEnabled;
  630. item->setFlags(flags);
  631. }
  632. }
  633. for (auto it = uuidMap.begin(); it != uuidMap.end(); ++it) {
  634. int row = it.key();
  635. QString pcSn = it.value(); // This is now pcSn instead of uuid
  636. firingWidget *widgetSelect = new firingWidget(row, true, pcSn);
  637. QModelIndex index = model->index(row, ColIndexOpBtn); // Use ColIndexOpBtn instead of ColIndexBlasterDev
  638. if (index.isValid()) {
  639. QWidget *widgetButton = ui->tableView->indexWidget(index);
  640. if (widgetButton) {
  641. QPushButton *button = widgetButton->findChild<QPushButton *>();
  642. if (button) {
  643. button->setText(stopBlastButtonTxt);
  644. button->setDisabled(true);
  645. }
  646. }
  647. }
  648. // 信号连接
  649. connect(widgetSelect, &firingWidget::updatefiringStage, this, &BlastOpePage::onFiringStageUpdated);
  650. connect(widgetSelect, &firingWidget::batchFiringSignal, this, &BlastOpePage::setBatchBlastTrigger);
  651. connect(widgetSelect, &firingWidget::updateProjectStatus, this, &BlastOpePage::handlerUpdateProjectStatus);
  652. connect(widgetSelect, &firingWidget::closeFiring, this, &BlastOpePage::destroyBatchFiringWidget);
  653. widgetSelect->setAttribute(Qt::WA_DeleteOnClose);
  654. uuidWidgetSMap.insert(pcSn, widgetSelect);
  655. widgetSelect->startBlasting();
  656. if (isShowTriggeringWidget) {
  657. widgetSelect->show();
  658. }
  659. }
  660. }
  661. // 完成充电
  662. void BlastOpePage::setBatchBlastTrigger(QString pcSn) {
  663. selectedUuids.insert(pcSn); // Now storing pcSn instead of UUID
  664. bool isSame = checkUuidsSame();
  665. if (isSame) {
  666. bool successSelect;
  667. serialTool = SerialTool::getInstance(nullptr, &successSelect);
  668. connect(serialTool, &SerialTool::triggerButtonPressed, this, &BlastOpePage::showCountDownForBatchBlast,
  669. Qt::SingleShotConnection);
  670. if (serialTool) {
  671. QByteArray data = "\r\nENABLE_BUTTON\r\n";
  672. bool success = serialTool->sendData(data);
  673. if (success) {
  674. Logger::getInstance().info("enable batch trigger button message send.");
  675. } else {
  676. Logger::getInstance().warn("enable batch trigger button message send failed.");
  677. }
  678. connect(
  679. serialTool, &SerialTool::triggerButtonEnabled, this,
  680. [this]() {
  681. for (const auto &row : uuidMap.keys()) {
  682. onFiringStageUpdated(FiringStages::PendingTriggerButtonClick, row);
  683. }
  684. },
  685. Qt::SingleShotConnection);
  686. } else {
  687. qDebug() << "serialTool Not fond.";
  688. QMessageBox::critical(nullptr, "错误", "trigger button devices not found");
  689. }
  690. } else {
  691. Logger::getInstance().error(
  692. QString("The pcSns in selectedUuids and uuidMap are different. pcSn: %1").arg(pcSn));
  693. }
  694. }
  695. // 检查 selectedUuids 和 uuidMap 中的 pcSn 是否相同
  696. bool BlastOpePage::checkUuidsSame() {
  697. QSet<QString> mapPcSns;
  698. for (const auto &value : uuidMap) {
  699. mapPcSns.insert(value);
  700. }
  701. return selectedUuids == mapPcSns;
  702. }
  703. void BlastOpePage::showCountDownForBatchBlast() {
  704. QByteArray data = "\r\nDISABLE_BUTTON\r\n";
  705. bool success = serialTool->sendData(data);
  706. if (success) {
  707. qDebug() << "Data sent successfully";
  708. } else {
  709. qDebug() << "Failed to send data";
  710. }
  711. serialTool->releaseInstance();
  712. CountdownWidget *countdownWidgetSelect = new CountdownWidget(this);
  713. countdownWidgetSelect->resize(200, 200);
  714. int x = (this->width() - countdownWidgetSelect->width()) / 2;
  715. int y = (this->height() - countdownWidgetSelect->height()) / 2;
  716. countdownWidgetSelect->move(x, y);
  717. countdownWidgetSelect->show();
  718. connect(countdownWidgetSelect, &CountdownWidget::countdownFinished, this, &BlastOpePage::triggerBatchFiringBlast,
  719. Qt::SingleShotConnection);
  720. }
  721. void BlastOpePage::triggerBatchFiringBlast() {
  722. for (auto it = uuidWidgetSMap.begin(); it != uuidWidgetSMap.end(); ++it) {
  723. QString pcSn = it.key();
  724. firingWidget *widget = it.value();
  725. QString topic = "hxgc/" + pcSn + "/BlastTask";
  726. QString message = "起爆";
  727. widget->onCountdownFinished(topic, message);
  728. }
  729. }
  730. void BlastOpePage::destroyBatchFiringWidget(const QString &pcSn, int row) {
  731. firingWidget *widget = uuidWidgetSMap.value(pcSn);
  732. if (widget) {
  733. widget->close();
  734. widget->deleteLater();
  735. uuidWidgetSMap.remove(pcSn);
  736. }
  737. // reset the table's row by row index
  738. for (int row = 0; row < model->rowCount(); ++row) {
  739. // 恢复 checkbox 可选
  740. QStandardItem *item = model->item(row, 0);
  741. if (item) {
  742. Qt::ItemFlags flags = item->flags();
  743. flags |= Qt::ItemIsEnabled;
  744. item->setFlags(flags);
  745. }
  746. }
  747. }