blastopepage.cpp 30 KB

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