blastopepage.cpp 31 KB

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