blastopepage.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. #include "blastopepage.h"
  2. #include "ui_blastopepage.h"
  3. #include "countdownwidget.h"
  4. #include <QFont>
  5. #include "logger.h"
  6. #include <QProcessEnvironment>
  7. #include "loadingwidget.h"
  8. #include "global.h"
  9. BlastOpePage::BlastOpePage(QWidget *parent) : QWidget(parent),
  10. ui(new Ui::BlastOpePage),
  11. dao(DatabaseManager::getInstance().getDatabase())
  12. {
  13. InitFace();
  14. // ui->setupUi(this);
  15. // initPagination();
  16. }
  17. void BlastOpePage::showDownWidget(QString uuid, const QString &topic, const QString &message)
  18. {
  19. CountdownWidget *countdownWidget = new CountdownWidget(this);
  20. countdownWidget->resize(200, 200);
  21. int x = (this->width() - countdownWidget->width()) / 2;
  22. int y = (this->height() - countdownWidget->height()) / 2;
  23. countdownWidget->move(x, y);
  24. countdownWidget->show();
  25. firingWidget *widget = uuidWidgetMap.value(uuid);
  26. if (widget)
  27. {
  28. connect(countdownWidget, &CountdownWidget::countdownFinished, widget, [widget, topic, message, countdownWidget]()
  29. { widget->onCountdownFinished(topic, message); }, Qt::SingleShotConnection);
  30. }
  31. }
  32. void BlastOpePage::InitFace()
  33. {
  34. Logger::getInstance().info("start init face verification");
  35. LoadingWidget::showLoading(nullptr, "请求创建人脸识别...");
  36. layout = new QVBoxLayout(this);
  37. view = new QWebEngineView(this);
  38. view->setAttribute(Qt::WA_OpaquePaintEvent);
  39. QWebEnginePage *page = view->page();
  40. QJsonObject metaInfo = getMetaInfo();
  41. QObject::connect(page, &QWebEnginePage::featurePermissionRequested, [this, page](const QUrl &securityOrigin, QWebEnginePage::Feature feature)
  42. { handleFeaturePermission(page, securityOrigin, feature); });
  43. Logger::getInstance().info("FaceVerification: connect");
  44. QUrl postUrl(g_url.resolved(QUrl("h-face-verify/pc")));
  45. QJsonObject response = sendPostRequest(postUrl, metaInfo);
  46. QString certifyUrl;
  47. if (response.contains("data") && response["data"].isObject())
  48. {
  49. LoadingWidget::showLoading(this, "正在打开人脸识别界面...");
  50. QJsonObject dataObject = response["data"].toObject();
  51. if (dataObject.contains("ResultObject") && dataObject["ResultObject"].isObject())
  52. {
  53. QJsonObject resultObject = dataObject["ResultObject"].toObject();
  54. if (resultObject.contains("CertifyId") && resultObject["CertifyId"].isString())
  55. {
  56. certifyId = resultObject["CertifyId"].toString();
  57. }
  58. if (resultObject.contains("CertifyUrl") && resultObject["CertifyUrl"].isString())
  59. {
  60. certifyUrl = resultObject["CertifyUrl"].toString();
  61. }
  62. }
  63. }
  64. if (!certifyUrl.isEmpty())
  65. {
  66. view->load(QUrl(certifyUrl));
  67. layout->addWidget(view);
  68. layout->setStretchFactor(view, 1);
  69. QObject::connect(page, &QWebEnginePage::urlChanged, this, &BlastOpePage::onUrlChanged);
  70. }
  71. else
  72. {
  73. QMessageBox::information(nullptr, "提示", "人脸识别请求失败");
  74. qDebug() << "Failed to get certifyUrl." << response;
  75. Logger::getInstance().error("FaceVerificationInit: Failed to get certifyUrl");
  76. }
  77. Logger::getInstance().info("FaceVerificationInit: successfully");
  78. LoadingWidget::hideLoading();
  79. }
  80. void BlastOpePage::closeWebViewAndRestoreUI()
  81. {
  82. if (view)
  83. {
  84. layout->removeWidget(view);
  85. delete view;
  86. view = nullptr;
  87. }
  88. if (layout)
  89. {
  90. delete layout;
  91. layout = nullptr;
  92. }
  93. }
  94. // 槽函数:处理 URL 改变事件
  95. void BlastOpePage::onUrlChanged(const QUrl &newUrl)
  96. {
  97. LoadingWidget::showLoading(this, "跳转验证页...");
  98. if (newUrl.scheme() == "https" && newUrl.host() == "www.baidu.com")
  99. {
  100. closeWebViewAndRestoreUI();
  101. QNetworkAccessManager manager;
  102. QUrl requestUrl(g_url.resolved(QUrl(QString("h-face-verify/certifyId/%1").arg(certifyId))));
  103. QNetworkRequest request(requestUrl);
  104. QNetworkReply *reply = manager.get(request);
  105. QEventLoop loop;
  106. QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
  107. loop.exec();
  108. if (reply->error() == QNetworkReply::NoError)
  109. {
  110. QByteArray responseData = reply->readAll();
  111. QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
  112. if (!jsonDoc.isNull() && jsonDoc.isObject())
  113. {
  114. QJsonObject rootObj = jsonDoc.object();
  115. QJsonObject dataObj = rootObj["data"].toObject();
  116. QString message = dataObj["Message"].toString();
  117. QJsonObject resultObj = dataObj["ResultObject"].toObject();
  118. if (resultObj.isEmpty())
  119. {
  120. Logger::getInstance().error(QString("获取认证初始化数据失败. message: %1.").arg(message));
  121. int ret = QMessageBox::information(nullptr, "认证失败", message + " ,请重新认证!");
  122. if (ret == QMessageBox::Ok)
  123. {
  124. InitFace();
  125. }
  126. }
  127. else
  128. {
  129. QString passed = resultObj["Passed"].toString();
  130. if (passed == "T")
  131. {
  132. ui->setupUi(this);
  133. initPagination();
  134. Logger::getInstance().info(QString("进入认证界面"));
  135. LoadingWidget::hideLoading();
  136. }
  137. else if (passed == "F")
  138. {
  139. int ret = QMessageBox::critical(nullptr, "提示", "操作失败,请重新认证!");
  140. if (ret == QMessageBox::Ok)
  141. {
  142. InitFace();
  143. }
  144. }
  145. }
  146. }
  147. }
  148. else
  149. {
  150. qDebug() << "Request failed:" << reply->errorString();
  151. Logger::getInstance().error(QString("InitFaseVerification request failed. error message: %1").arg(reply->errorString()));
  152. }
  153. reply->deleteLater();
  154. LoadingWidget::hideLoading();
  155. }
  156. LoadingWidget::hideLoading();
  157. }
  158. BlastOpePage::~BlastOpePage()
  159. {
  160. delete ui;
  161. if (view)
  162. {
  163. delete view;
  164. }
  165. if (layout)
  166. {
  167. delete layout;
  168. }
  169. }
  170. QJsonObject BlastOpePage::sendPostRequest(const QUrl &url, const QJsonObject &data)
  171. {
  172. QNetworkAccessManager manager;
  173. QNetworkRequest request(url);
  174. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
  175. QJsonDocument doc(data);
  176. QByteArray postData = doc.toJson();
  177. QNetworkReply *reply = manager.post(request, postData);
  178. QEventLoop loop;
  179. QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
  180. loop.exec();
  181. QJsonObject responseJson;
  182. if (reply->error() == QNetworkReply::NoError)
  183. {
  184. QByteArray responseData = reply->readAll();
  185. QJsonDocument responseDoc = QJsonDocument::fromJson(responseData);
  186. if (!responseDoc.isNull() && responseDoc.isObject())
  187. {
  188. responseJson = responseDoc.object();
  189. }
  190. }
  191. else
  192. {
  193. qDebug() << "Error fetching content: " << reply->errorString();
  194. }
  195. reply->deleteLater();
  196. return responseJson;
  197. }
  198. void BlastOpePage::handleFeaturePermission(QWebEnginePage *page, const QUrl &securityOrigin, QWebEnginePage::Feature feature)
  199. {
  200. if (feature == QWebEnginePage::MediaAudioCapture ||
  201. feature == QWebEnginePage::MediaAudioVideoCapture ||
  202. feature == QWebEnginePage::MediaVideoCapture)
  203. {
  204. page->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
  205. }
  206. else
  207. {
  208. page->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
  209. }
  210. }
  211. QJsonObject BlastOpePage::getMetaInfo()
  212. {
  213. QJsonObject metaInfo;
  214. // TODO: 获取登录用户信息
  215. QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); // 获取系统环境变量
  216. QString certName = env.value("certName", ""); // 第二个参数为默认值
  217. QString certNo = env.value("certNo", "");
  218. metaInfo["certName"] = certName;
  219. metaInfo["certNo"] = certNo;
  220. qDebug() << certName << certNo << "<certInfo";
  221. return metaInfo;
  222. }
  223. void BlastOpePage::initPagination()
  224. {
  225. pageWidget = new PageWidget;
  226. connect(pageWidget, &PageWidget::currentPageChanged, this, &BlastOpePage::PageChanged);
  227. connect(pageWidget->getComboBox(), QOverload<int>::of(&QComboBox::currentIndexChanged), this, &BlastOpePage::onComboBoxIndexChanged);
  228. ui->verticalLayout_4->addWidget(pageWidget);
  229. ;
  230. pageSize = 10;
  231. currentPage = 1;
  232. RefreshData();
  233. }
  234. void BlastOpePage::RefreshData()
  235. {
  236. loadDataFromSource(currentPage, pageSize);
  237. }
  238. void BlastOpePage::loadDataFromSource(int currentPage, int pageSize)
  239. {
  240. PaginatedHProjectResult result = dao.getAllHProjectsByOpera(currentPage, pageSize);
  241. QList<QSharedPointer<HProject>> projectList = result.projects;
  242. totalCount = result.totalCount;
  243. pageWidget->setMaxPage(ceil(static_cast<double>(totalCount) / pageSize));
  244. model = new QStandardItemModel(this);
  245. headers = {
  246. {"选择", ""}, // 新增选择列
  247. {"工程名称", "name"},
  248. {"操作员", "operatorName"},
  249. {"爆破员", "blasterName"},
  250. {"井下地址", "addressUuid"},
  251. {"雷管数量", "detSum"},
  252. {"起爆器数量", "blastCount"},
  253. {"起爆状态", "blastStatus"},
  254. {"进度", ""},
  255. {"操作", ""},
  256. };
  257. int headerCount = headers.size();
  258. QStringList headerLabels;
  259. QMap<int, QString> propMap;
  260. for (int i = 0; i < headers.size(); ++i)
  261. {
  262. headerLabels << headers[i].label;
  263. propMap[i] = headers[i].prop;
  264. }
  265. model->setHorizontalHeaderLabels(headerLabels);
  266. for (int row = 0; row < projectList.size(); ++row)
  267. {
  268. HProject &HProject = *projectList.at(row).data();
  269. QStandardItem *uuidItem = new QStandardItem();
  270. uuidItem->setData(HProject.getUuid(), Qt::UserRole);
  271. model->setItem(row, headerCount, uuidItem);
  272. for (int col = 0; col < headerCount; ++col)
  273. {
  274. QString prop = propMap[col];
  275. QStandardItem *item = nullptr;
  276. if (col == 0)
  277. {
  278. item = new QStandardItem();
  279. item->setCheckable(true);
  280. item->setCheckState(Qt::Unchecked);
  281. item->setText("");
  282. }
  283. if (!prop.isEmpty())
  284. {
  285. QMetaProperty metaProp = HProject.metaObject()->property(HProject.metaObject()->indexOfProperty(prop.toUtf8()));
  286. QVariant value = metaProp.read(&HProject);
  287. if (prop == "blastStatus")
  288. {
  289. QString statusText;
  290. if (value.toString() == "1")
  291. {
  292. statusText = "未 注 册";
  293. item = new QStandardItem(statusText);
  294. item->setForeground(QColor("#e7c66b"));
  295. }
  296. else if (value.toString() == "2")
  297. {
  298. statusText = "待 起 爆";
  299. item = new QStandardItem(statusText);
  300. item->setForeground(QColor("#f3a361"));
  301. }
  302. else if (value.toString() == "3")
  303. {
  304. statusText = "起 爆 完 成";
  305. item = new QStandardItem(statusText);
  306. item->setForeground(QColor("#90d543"));
  307. }
  308. else
  309. {
  310. item = new QStandardItem(value.toString());
  311. }
  312. }
  313. else
  314. {
  315. item = new QStandardItem(value.toString());
  316. }
  317. }
  318. if (item)
  319. {
  320. item->setTextAlignment(Qt::AlignCenter); // 设置文本居中对齐
  321. model->setItem(row, col, item);
  322. }
  323. }
  324. }
  325. ui->tableView->setModel(model);
  326. connectionItem = QObject::connect(model, &QStandardItemModel::itemChanged, this, &BlastOpePage::onItemChanged);
  327. ui->tableView->setColumnWidth(0, 30);
  328. for (int i = 1; i < headerCount; ++i)
  329. {
  330. if (i == 8)
  331. { // Column: 进度
  332. ui->tableView->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Custom);
  333. ui->tableView->setColumnWidth(i, 180);
  334. }
  335. else
  336. {
  337. ui->tableView->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
  338. }
  339. }
  340. ui->tableView->setColumnHidden(headerCount, true);
  341. ui->tableView->setAlternatingRowColors(true);
  342. ui->tableView->verticalHeader()->setDefaultSectionSize(50);
  343. for (int row = 0; row < projectList.size(); ++row)
  344. {
  345. int progressCol = headers.size() - 2; //
  346. QProgressBar *progressBar1 = new QProgressBar(ui->tableView);
  347. progressBar1->setRange(0, 100); // 设置范围为0到100
  348. progressBar1->setValue(0);
  349. progressBar1->setAlignment(Qt::AlignCenter);
  350. progressBar1->setStyleSheet("QProgressBar { border: 1px solid grey; border-radius: 5px; background-color: #EEEEEE; height: 10px; }"
  351. "QProgressBar::chunk { background-color: #05B8CC; width: 2px; margin: 0.5px; border - radius: 10px; }");
  352. progressBar1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  353. QProgressBar *progressBar2 = new QProgressBar(ui->tableView);
  354. progressBar2->setRange(0, 100); // 设置范围为0到100
  355. progressBar2->setValue(0);
  356. progressBar2->setAlignment(Qt::AlignCenter);
  357. progressBar2->setStyleSheet("QProgressBar { border: 1px solid grey; border-radius: 5px; background-color: #EEEEEE; height: 10px; }"
  358. "QProgressBar::chunk { background-color: #05B8CC; width: 2px; margin: 0.5px; border - radius: 10px; }");
  359. progressBar2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  360. QProgressBar *progressBar3 = new QProgressBar(ui->tableView);
  361. progressBar3->setRange(0, 100); // 设置范围为0到100
  362. progressBar3->setValue(0);
  363. progressBar3->setAlignment(Qt::AlignCenter);
  364. progressBar3->setStyleSheet("QProgressBar { border: 1px solid grey; border-radius: 5px; background-color: #EEEEEE; height: 10px; }"
  365. "QProgressBar::chunk { background-color: #05B8CC; width: 2px; margin: 0.5px; border - radius: 10px; }");
  366. progressBar3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  367. progressBars.append(ProgressBarTriple(progressBar1, progressBar2, progressBar3));
  368. QHBoxLayout *progressBarLayout = new QHBoxLayout;
  369. progressBarLayout->addWidget(progressBar1);
  370. progressBarLayout->addWidget(progressBar2);
  371. progressBarLayout->addWidget(progressBar3);
  372. progressBarLayout->setAlignment(Qt::AlignCenter);
  373. progressBarLayout->setContentsMargins(0, 0, 0, 0);
  374. progressBarLayout->setSpacing(0); // 设置进度条之间的间距为0
  375. QWidget *progressBarContainer = new QWidget(ui->tableView);
  376. progressBarContainer->setLayout(progressBarLayout);
  377. QModelIndex progressIndex = model->index(row, progressCol);
  378. if (progressIndex.isValid())
  379. {
  380. ui->tableView->setIndexWidget(progressIndex, progressBarContainer);
  381. }
  382. int col = headers.size() - 1;
  383. // 创建一个按钮
  384. QWidget *widget = new QWidget(ui->tableView);
  385. QPushButton *button = new QPushButton(widget);
  386. button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  387. QModelIndex statusIndex = model->index(row, propMap.key("blastStatus"));
  388. if (statusIndex.isValid())
  389. {
  390. QString blastStatus = model->data(statusIndex).toString();
  391. if (blastStatus == "起 爆 完 成")
  392. {
  393. // 为按钮添加图标,这里假设图标文件名为 icon.png,你需要根据实际情况修改
  394. QIcon icon(":/icons/icons/svg/blast.svg");
  395. button->setIcon(icon);
  396. button->setIconSize(QSize(32, 32)); // 设置图标大小
  397. button->setStyleSheet("QPushButton {"
  398. " padding: 0px;"
  399. " border: none;"
  400. " background-color: transparent;"
  401. "}");
  402. }
  403. else
  404. {
  405. button->setText("开启起爆流程");
  406. }
  407. }
  408. QHBoxLayout *layout = new QHBoxLayout(widget);
  409. layout->addWidget(button);
  410. layout->setAlignment(Qt::AlignCenter);
  411. layout->setContentsMargins(0, 0, 0, 0);
  412. widget->setLayout(layout);
  413. QModelIndex index = model->index(row, col);
  414. if (index.isValid())
  415. {
  416. ui->tableView->setIndexWidget(index, widget);
  417. connect(button, &QPushButton::clicked, [this, row, button]()
  418. { handleButtonClick(row, button); });
  419. }
  420. }
  421. }
  422. // 切换页数
  423. void BlastOpePage::PageChanged(int page)
  424. {
  425. currentPage = page;
  426. loadDataFromSource(currentPage, pageSize);
  427. }
  428. void BlastOpePage::onComboBoxIndexChanged(int index)
  429. {
  430. QVariant variant = pageWidget->getComboBox()->itemData(index);
  431. int value = variant.toInt();
  432. pageSize = value;
  433. currentPage = 1;
  434. loadDataFromSource(currentPage, pageSize);
  435. }
  436. void BlastOpePage::updateProgressBar(int value, int row)
  437. {
  438. if (!progressBars.isEmpty())
  439. {
  440. QProgressBar *progressBar1 = progressBars[row].bar1;
  441. QProgressBar *progressBar2 = progressBars[row].bar2;
  442. QProgressBar *progressBar3 = progressBars[row].bar3;
  443. switch (value)
  444. {
  445. case 1:
  446. // 检查状态
  447. progressBar1->setRange(0, 0); // 设置范围为0到100
  448. progressBar1->setValue(0);
  449. break;
  450. case 2:
  451. // 检查完成
  452. progressBar1->setRange(0, 100); // 设置范围为0到100
  453. progressBar1->setValue(100);
  454. break;
  455. case 3:
  456. // 充电开始
  457. progressBar2->setRange(0, 0); // 设置范围为0到100
  458. progressBar2->setValue(0);
  459. break;
  460. case 4:
  461. // 充电完成
  462. progressBar2->setRange(0, 100); // 设置范围为0到100
  463. progressBar2->setValue(100);
  464. break;
  465. case 5:
  466. // 起爆中
  467. progressBar3->setRange(0, 0); // 设置范围为0到100
  468. progressBar3->setValue(0);
  469. break;
  470. case 6:
  471. // 充电完成
  472. progressBar3->setRange(0, 100); // 设置范围为0到100
  473. progressBar3->setValue(100);
  474. break;
  475. case 0:
  476. progressBar1->setRange(0, 100);
  477. progressBar1->setValue(0);
  478. progressBar2->setRange(0, 100);
  479. progressBar2->setValue(0);
  480. progressBar3->setRange(0, 100);
  481. progressBar3->setValue(0);
  482. break;
  483. default:
  484. break;
  485. }
  486. }
  487. }
  488. void BlastOpePage::onUpdateBlastStatus(int status, int row)
  489. {
  490. QModelIndex index = model->index(row, 7);
  491. if (index.isValid())
  492. {
  493. QColor customColor;
  494. QFont boldFont;
  495. boldFont.setBold(true);
  496. switch (status)
  497. {
  498. case 1:
  499. model->setData(index, "组 网 中 ...");
  500. customColor = QColor("#44035b");
  501. model->setData(index, customColor, Qt::ForegroundRole);
  502. model->setData(index, boldFont, Qt::FontRole);
  503. break;
  504. case 2:
  505. model->setData(index, "组 网 完 成");
  506. customColor = QColor("#404185");
  507. model->setData(index, customColor, Qt::ForegroundRole);
  508. break;
  509. case 3:
  510. model->setData(index, "充 电 中 ...");
  511. customColor = QColor("#31688e");
  512. model->setData(index, customColor, Qt::ForegroundRole);
  513. break;
  514. case 4:
  515. model->setData(index, "充 电 完 成");
  516. customColor = QColor("#1f918d");
  517. model->setData(index, customColor, Qt::ForegroundRole);
  518. break;
  519. case 5:
  520. model->setData(index, "起 爆 中 ...");
  521. customColor = QColor("#38b775");
  522. model->setData(index, customColor, Qt::ForegroundRole);
  523. break;
  524. case 6:
  525. model->setData(index, "起 爆 完 成");
  526. customColor = QColor("#90d543");
  527. model->setData(index, customColor, Qt::ForegroundRole);
  528. break;
  529. case 0:
  530. model->setData(index, "已 注 册");
  531. customColor = QColor("#8e620");
  532. model->setData(index, customColor, Qt::ForegroundRole);
  533. break;
  534. case 10:
  535. model->setData(index, "按 下 双 建 起 爆 ...");
  536. customColor = QColor("#8e620");
  537. model->setData(index, customColor, Qt::ForegroundRole);
  538. break;
  539. default:
  540. break;
  541. }
  542. }
  543. }
  544. void BlastOpePage::handleButtonClick(int row, QPushButton *button)
  545. {
  546. QStandardItem *uuidItem = model->item(row, 10);
  547. QString uuid;
  548. if (uuidItem)
  549. {
  550. QVariant uuidVariant = uuidItem->data(Qt::UserRole);
  551. if (uuidVariant.isValid())
  552. {
  553. uuid = uuidVariant.toString();
  554. }
  555. }
  556. if (button->text() == "开启起爆流程")
  557. {
  558. button->setText("取消起爆流程");
  559. firingWidget *widget = new firingWidget(row, false, uuid);
  560. connect(widget, &firingWidget::progressChanged, this, &BlastOpePage::updateProgressBar);
  561. connect(widget, &firingWidget::updateBlastStatus, this, &BlastOpePage::onUpdateBlastStatus);
  562. connect(widget, &firingWidget::updateButton, this, &BlastOpePage::changeButByMqtt);
  563. connect(widget, &firingWidget::countdown, this, &BlastOpePage::showDownWidget);
  564. connect(widget, &firingWidget::updateProjectStatus, this, &BlastOpePage::updateProject);
  565. connect(widget, &firingWidget::closeFiring, this, &BlastOpePage::destroyFiringWidget);
  566. widget->show();
  567. widget->setAttribute(Qt::WA_DeleteOnClose);
  568. uuidWidgetMap.insert(uuid, widget);
  569. }
  570. else if (button->text() == "取消起爆流程")
  571. {
  572. firingWidget *widget = uuidWidgetMap.value(uuid);
  573. if (widget)
  574. {
  575. widget->cancelBlasting();
  576. }
  577. }
  578. }
  579. void BlastOpePage::changeButByMqtt(int status, int row)
  580. {
  581. qDebug() << "statusButton:" << status;
  582. QModelIndex index = model->index(row, 8);
  583. if (index.isValid())
  584. {
  585. QWidget *widget = ui->tableView->indexWidget(index);
  586. if (widget)
  587. {
  588. QPushButton *button = widget->findChild<QPushButton *>();
  589. if (button)
  590. {
  591. // 使用样式表设置图标居中
  592. button->setStyleSheet("QPushButton {"
  593. " padding: 0px;"
  594. " border: none;"
  595. " background-color: transparent;"
  596. "}");
  597. // 添加图标,假设图标文件名为 blast.svg,并且该文件存在于项目资源中
  598. QIcon icon(":/icons/icons/svg/blast.svg");
  599. button->setText("");
  600. button->setIcon(icon);
  601. button->setIconSize(QSize(32, 32));
  602. }
  603. }
  604. }
  605. }
  606. void BlastOpePage::updateProject(QString uuid)
  607. {
  608. dao.updateBlastStatusByUuid(uuid, "3");
  609. }
  610. void BlastOpePage::destroyFiringWidget(const QString &uuid)
  611. {
  612. firingWidget *widget = uuidWidgetMap.value(uuid);
  613. if (widget)
  614. {
  615. widget->close(); // 关闭窗口
  616. widget->deleteLater(); // 释放内存
  617. uuidWidgetMap.remove(uuid); // 从映射中移除
  618. }
  619. }
  620. // 槽函数,当 item 状态改变时触发
  621. void BlastOpePage::onItemChanged(QStandardItem *item)
  622. {
  623. if (item->column() == 0)
  624. { // 仅处理第一列的勾选状态改变
  625. if (item->checkState() == Qt::Checked)
  626. {
  627. QStandardItem *uuidItem = model->item(item->row(), 10);
  628. if (uuidItem)
  629. {
  630. QVariant uuidVariant = uuidItem->data(Qt::UserRole);
  631. if (uuidVariant.isValid())
  632. {
  633. QString uuid = uuidVariant.toString();
  634. uuidMap[item->row()] = uuid;
  635. }
  636. }
  637. }
  638. else if (item->checkState() == Qt::Unchecked)
  639. {
  640. QStandardItem *uuidItem = model->item(item->row(), 10);
  641. if (uuidItem)
  642. {
  643. // 从 item 中获取 uuid 数据
  644. QVariant uuidVariant = uuidItem->data(Qt::UserRole);
  645. if (uuidVariant.isValid())
  646. {
  647. QString uuid = uuidVariant.toString();
  648. // 从数组中移除该 uuid
  649. uuidMap.remove(item->row());
  650. }
  651. }
  652. }
  653. }
  654. }
  655. void BlastOpePage::on_btnSelect_clicked()
  656. {
  657. // 禁用表格第一列的选项
  658. for (int row = 0; row < model->rowCount(); ++row)
  659. {
  660. QStandardItem *item = model->item(row, 0);
  661. if (item)
  662. {
  663. Qt::ItemFlags flags = item->flags();
  664. flags &= ~Qt::ItemIsEnabled;
  665. item->setFlags(flags);
  666. }
  667. }
  668. for (auto it = uuidMap.begin(); it != uuidMap.end(); ++it)
  669. {
  670. int row = it.key();
  671. QString uuid = it.value();
  672. firingWidget *widgetSelect = new firingWidget(row, true, uuid);
  673. QModelIndex index = model->index(row, 9);
  674. if (index.isValid())
  675. {
  676. QWidget *widgetButton = ui->tableView->indexWidget(index);
  677. if (widgetButton)
  678. {
  679. QPushButton *button = widgetButton->findChild<QPushButton *>();
  680. button->setText("取消起爆流程");
  681. }
  682. }
  683. // 信号连接
  684. connect(widgetSelect, &firingWidget::progressChanged, this, &BlastOpePage::updateProgressBar);
  685. connect(widgetSelect, &firingWidget::updateBlastStatus, this, &BlastOpePage::onUpdateBlastStatus);
  686. connect(widgetSelect, &firingWidget::selectSignal, this, &BlastOpePage::handleSelect);
  687. connect(widgetSelect, &firingWidget::updateButton, this, &BlastOpePage::changeButByMqtt);
  688. connect(widgetSelect, &firingWidget::updateProjectStatus, this, &BlastOpePage::updateProject);
  689. connect(widgetSelect, &firingWidget::closeFiring, this, &BlastOpePage::destroyFiringWidgetSelect);
  690. widgetSelect->show();
  691. widgetSelect->setAttribute(Qt::WA_DeleteOnClose);
  692. uuidWidgetSMap.insert(uuid, widgetSelect);
  693. }
  694. }
  695. // 完成充电
  696. void BlastOpePage::handleSelect(QString uuid)
  697. {
  698. selectedUuids.insert(uuid);
  699. bool isSame = checkUuidsSame();
  700. if (isSame)
  701. {
  702. bool successSelect;
  703. serialTool = SerialTool::getInstance(nullptr, &successSelect);
  704. connect(serialTool, &SerialTool::buttonPressedReceived, this, &BlastOpePage::showDownWidgetSelect, Qt::SingleShotConnection);
  705. if (serialTool)
  706. {
  707. QByteArray data = "\r\nENABLE_BUTTON\r\n";
  708. bool success = serialTool->sendData(data);
  709. if (success)
  710. {
  711. Logger::getInstance().info("blast triggered.");
  712. }
  713. else
  714. {
  715. Logger::getInstance().warn("blast trigger failed.");
  716. }
  717. connect(serialTool, &SerialTool::enableButtonReceived, this, [this]()
  718. {
  719. for (const auto& row : uuidMap.keys()) {
  720. qDebug() << "Key:" << row;
  721. onUpdateBlastStatus(10,row);
  722. } }, Qt::SingleShotConnection);
  723. // TODO: receive blast record
  724. }
  725. else
  726. {
  727. qDebug() << "serialTool Not fond.";
  728. QMessageBox::critical(nullptr, "错误", "trigger button devices not found");
  729. }
  730. }
  731. else
  732. {
  733. qDebug() << "The uuids in selectedUuids and uuidMap are different.";
  734. }
  735. }
  736. // 检查 selectedUuids 和 uuidMap 中的 uuid 是否相同
  737. bool BlastOpePage::checkUuidsSame()
  738. {
  739. QSet<QString> mapUuids;
  740. for (const auto &value : uuidMap)
  741. {
  742. qDebug() << "value" << value;
  743. mapUuids.insert(value);
  744. }
  745. return selectedUuids == mapUuids;
  746. }
  747. void BlastOpePage::showDownWidgetSelect()
  748. {
  749. QByteArray data = "\r\nDISABLE_BUTTON\r\n";
  750. bool success = serialTool->sendData(data);
  751. if (success)
  752. {
  753. qDebug() << "Data sent successfully";
  754. }
  755. else
  756. {
  757. qDebug() << "Failed to send data";
  758. }
  759. serialTool->releaseInstance();
  760. CountdownWidget *countdownWidgetSelect = new CountdownWidget(this);
  761. countdownWidgetSelect->resize(200, 200);
  762. int x = (this->width() - countdownWidgetSelect->width()) / 2;
  763. int y = (this->height() - countdownWidgetSelect->height()) / 2;
  764. countdownWidgetSelect->move(x, y);
  765. countdownWidgetSelect->show();
  766. connect(countdownWidgetSelect, &CountdownWidget::countdownFinished, this, &BlastOpePage::selectBlasting, Qt::SingleShotConnection);
  767. }
  768. void BlastOpePage::selectBlasting()
  769. {
  770. for (auto it = uuidWidgetSMap.begin(); it != uuidWidgetSMap.end(); ++it)
  771. {
  772. QString uuid = it.key();
  773. firingWidget *widget = it.value();
  774. QString topic = "hxgc/" + uuid + "/P";
  775. QString message = "起爆";
  776. widget->onCountdownFinished(topic, message);
  777. }
  778. }
  779. void BlastOpePage::destroyFiringWidgetSelect(const QString &uuid)
  780. {
  781. firingWidget *widget = uuidWidgetSMap.value(uuid);
  782. if (widget)
  783. {
  784. widget->close();
  785. widget->deleteLater();
  786. uuidWidgetSMap.remove(uuid);
  787. }
  788. for (int row = 0; row < model->rowCount(); ++row)
  789. {
  790. QStandardItem *item = model->item(row, 0);
  791. if (item)
  792. {
  793. Qt::ItemFlags flags = item->flags();
  794. flags &= ~Qt::ItemIsEnabled; // 去除 ItemIsEnabled 标志位
  795. item->setFlags(flags);
  796. }
  797. }
  798. }