blastopepage.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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]() { handleRowOperateButtonClick(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 value, 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 (value) {
  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::onUpdateBlastStatus(int status, 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 (status) {
  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::handleRowOperateButtonClick(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, &BlastOpePage::onUpdateBlastStatus);
  514. connect(widget, &firingWidget::updateButton, this, &BlastOpePage::updateOpBtn);
  515. connect(widget, &firingWidget::countdown, this, &BlastOpePage::showCountDownWidget);
  516. connect(widget, &firingWidget::updateProjectStatus, this, &BlastOpePage::updateProject);
  517. connect(widget, &firingWidget::closeFiring, this, &BlastOpePage::destroyFiringWidget);
  518. if (isShowTriggeringWidget) {
  519. widget->show();
  520. }
  521. widget->setAttribute(Qt::WA_DeleteOnClose);
  522. uuidWidgetMap.insert(uuid, widget);
  523. } else if (button->text() == stopBlastButtonTxt) {
  524. firingWidget *widget = uuidWidgetMap.value(uuid);
  525. if (widget) {
  526. widget->cancelBlasting();
  527. }
  528. }
  529. }
  530. void BlastOpePage::updateOpBtn(int status, int row) {
  531. qDebug() << "statusButton:" << status;
  532. QModelIndex index = model->index(row, 8);
  533. if (index.isValid()) {
  534. QWidget *widget = ui->tableView->indexWidget(index);
  535. if (widget) {
  536. QPushButton *button = widget->findChild<QPushButton *>();
  537. if (button) {
  538. // 使用样式表设置图标居中
  539. button->setStyleSheet(
  540. "QPushButton {"
  541. " padding: 0px;"
  542. " border: none;"
  543. " background-color: transparent;"
  544. "}");
  545. // 添加图标,假设图标文件名为 blast.svg,并且该文件存在于项目资源中
  546. QIcon icon(":/icons/icons/svg/blast.svg");
  547. button->setText("");
  548. button->setIcon(icon);
  549. button->setIconSize(QSize(32, 32));
  550. }
  551. }
  552. }
  553. }
  554. void BlastOpePage::updateProject(QString uuid) { dao.updateBlastStatusByUuid(uuid, "3"); }
  555. void BlastOpePage::destroyFiringWidget(const QString &uuid) {
  556. firingWidget *widget = uuidWidgetMap.value(uuid);
  557. if (widget) {
  558. widget->close(); // 关闭窗口
  559. widget->deleteLater(); // 释放内存
  560. uuidWidgetMap.remove(uuid); // 从映射中移除
  561. }
  562. }
  563. // 槽函数,当 item 状态改变时触发
  564. void BlastOpePage::onItemCheckboxChanged(QStandardItem *item) {
  565. if (item->column() == 0) { // 仅处理第一列的勾选状态改变
  566. if (item->checkState() == Qt::Checked) {
  567. QStandardItem *uuidItem = model->item(item->row(), 10);
  568. if (uuidItem) {
  569. QVariant uuidVariant = uuidItem->data(Qt::UserRole);
  570. if (uuidVariant.isValid()) {
  571. QString uuid = uuidVariant.toString();
  572. uuidMap[item->row()] = uuid;
  573. }
  574. }
  575. } else if (item->checkState() == Qt::Unchecked) {
  576. QStandardItem *uuidItem = model->item(item->row(), 10);
  577. if (uuidItem) {
  578. // 从 item 中获取 uuid 数据
  579. QVariant uuidVariant = uuidItem->data(Qt::UserRole);
  580. if (uuidVariant.isValid()) {
  581. QString uuid = uuidVariant.toString();
  582. // 从数组中移除该 uuid
  583. uuidMap.remove(item->row());
  584. }
  585. }
  586. }
  587. }
  588. }
  589. void BlastOpePage::on_btnSelect_clicked() {
  590. // 禁用表格第一列的选项
  591. for (int row = 0; row < model->rowCount(); ++row) {
  592. QStandardItem *item = model->item(row, 0);
  593. if (item) {
  594. Qt::ItemFlags flags = item->flags();
  595. flags &= ~Qt::ItemIsEnabled;
  596. item->setFlags(flags);
  597. }
  598. }
  599. for (auto it = uuidMap.begin(); it != uuidMap.end(); ++it) {
  600. int row = it.key();
  601. QString uuid = it.value();
  602. firingWidget *widgetSelect = new firingWidget(row, true, uuid);
  603. QModelIndex index = model->index(row, 9);
  604. if (index.isValid()) {
  605. QWidget *widgetButton = ui->tableView->indexWidget(index);
  606. if (widgetButton) {
  607. QPushButton *button = widgetButton->findChild<QPushButton *>();
  608. button->setText("取消起爆流程");
  609. }
  610. }
  611. // 信号连接
  612. connect(widgetSelect, &firingWidget::progressChanged, this,
  613. &BlastOpePage::updateProgressBar);
  614. connect(widgetSelect, &firingWidget::updateBlastStatus, this,
  615. &BlastOpePage::onUpdateBlastStatus);
  616. connect(widgetSelect, &firingWidget::selectSignal, this, &BlastOpePage::handleSelect);
  617. connect(widgetSelect, &firingWidget::updateButton, this, &BlastOpePage::updateOpBtn);
  618. connect(widgetSelect, &firingWidget::updateProjectStatus, this,
  619. &BlastOpePage::updateProject);
  620. connect(widgetSelect, &firingWidget::closeFiring, this,
  621. &BlastOpePage::destroyFiringWidgetSelect);
  622. widgetSelect->show();
  623. widgetSelect->setAttribute(Qt::WA_DeleteOnClose);
  624. uuidWidgetSMap.insert(uuid, widgetSelect);
  625. if (isShowTriggeringWidget) {
  626. widgetSelect->show();
  627. }
  628. }
  629. }
  630. // 完成充电
  631. void BlastOpePage::handleSelect(QString uuid) {
  632. selectedUuids.insert(uuid);
  633. bool isSame = checkUuidsSame();
  634. if (isSame) {
  635. bool successSelect;
  636. serialTool = SerialTool::getInstance(nullptr, &successSelect);
  637. connect(serialTool, &SerialTool::buttonPressedReceived, this,
  638. &BlastOpePage::showDownWidgetSelect, Qt::SingleShotConnection);
  639. if (serialTool) {
  640. QByteArray data = "\r\nENABLE_BUTTON\r\n";
  641. bool success = serialTool->sendData(data);
  642. if (success) {
  643. Logger::getInstance().info("blast triggered.");
  644. } else {
  645. Logger::getInstance().warn("blast trigger failed.");
  646. }
  647. connect(
  648. serialTool, &SerialTool::enableButtonReceived, this,
  649. [this]() {
  650. for (const auto &row : uuidMap.keys()) {
  651. qDebug() << "Key:" << row;
  652. onUpdateBlastStatus(10, row);
  653. }
  654. },
  655. Qt::SingleShotConnection);
  656. } else {
  657. qDebug() << "serialTool Not fond.";
  658. QMessageBox::critical(nullptr, "错误", "trigger button devices not found");
  659. }
  660. } else {
  661. Logger::getInstance().error(
  662. QString("The uuids in selectedUuids and uuidMap are different. uuid: %1").arg(uuid));
  663. }
  664. }
  665. // 检查 selectedUuids 和 uuidMap 中的 uuid 是否相同
  666. bool BlastOpePage::checkUuidsSame() {
  667. QSet<QString> mapUuids;
  668. for (const auto &value : uuidMap) {
  669. qDebug() << "value" << value;
  670. mapUuids.insert(value);
  671. }
  672. return selectedUuids == mapUuids;
  673. }
  674. void BlastOpePage::showDownWidgetSelect() {
  675. QByteArray data = "\r\nDISABLE_BUTTON\r\n";
  676. bool success = serialTool->sendData(data);
  677. if (success) {
  678. qDebug() << "Data sent successfully";
  679. } else {
  680. qDebug() << "Failed to send data";
  681. }
  682. serialTool->releaseInstance();
  683. CountdownWidget *countdownWidgetSelect = new CountdownWidget(this);
  684. countdownWidgetSelect->resize(200, 200);
  685. int x = (this->width() - countdownWidgetSelect->width()) / 2;
  686. int y = (this->height() - countdownWidgetSelect->height()) / 2;
  687. countdownWidgetSelect->move(x, y);
  688. countdownWidgetSelect->show();
  689. connect(countdownWidgetSelect, &CountdownWidget::countdownFinished, this,
  690. &BlastOpePage::triggerBlastSelected, Qt::SingleShotConnection);
  691. }
  692. void BlastOpePage::triggerBlastSelected() {
  693. for (auto it = uuidWidgetSMap.begin(); it != uuidWidgetSMap.end(); ++it) {
  694. QString uuid = it.key();
  695. firingWidget *widget = it.value();
  696. QString topic = "hxgc/" + uuid + "/P";
  697. QString message = "起爆";
  698. widget->onCountdownFinished(topic, message);
  699. }
  700. }
  701. void BlastOpePage::destroyFiringWidgetSelect(const QString &uuid) {
  702. firingWidget *widget = uuidWidgetSMap.value(uuid);
  703. if (widget) {
  704. widget->close();
  705. widget->deleteLater();
  706. uuidWidgetSMap.remove(uuid);
  707. }
  708. for (int row = 0; row < model->rowCount(); ++row) {
  709. QStandardItem *item = model->item(row, 0);
  710. if (item) {
  711. Qt::ItemFlags flags = item->flags();
  712. flags &= ~Qt::ItemIsEnabled; // 去除 ItemIsEnabled 标志位
  713. item->setFlags(flags);
  714. }
  715. }
  716. }