blastopepage.cpp 30 KB

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