blastopepage.cpp 31 KB

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