blastopepage.cpp 32 KB

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