blastopepage.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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]() { widget->onCountdownFinished(topic, message); },
  31. Qt::SingleShotConnection);
  32. }
  33. }
  34. void BlastOpePage::InitFace() {
  35. Logger::getInstance().info("start init face verification");
  36. LoadingWidget::showLoading(this, "请求创建人脸识别...");
  37. layout = new QVBoxLayout(this);
  38. // TODO: relase the qwebengineview when not successfully verified
  39. view = new QWebEngineView(this);
  40. view->setAttribute(Qt::WA_OpaquePaintEvent);
  41. QWebEnginePage *page = view->page();
  42. QJsonObject metaInfo = getMetaInfo();
  43. if (metaInfo["certName"] == "" || metaInfo["certNo"] == "") {
  44. QMessageBox::information(nullptr, "获取用户信息错误", "未获得用户的身份证信息,请联系管理员");
  45. return;
  46. }
  47. QObject::connect(page, &QWebEnginePage::featurePermissionRequested,
  48. [this, page](const QUrl &securityOrigin, QWebEnginePage::Feature feature) {
  49. handleFeaturePermission(page, securityOrigin, feature);
  50. });
  51. Logger::getInstance().info("FaceVerification: connect");
  52. QUrl postUrl(apiBackendUrl.resolved(QUrl("h-face-verify/pc")));
  53. QJsonObject response = sendPostRequest(postUrl, metaInfo);
  54. QString certifyUrl;
  55. if (response["code"] != 200) {
  56. Logger::getInstance().error(
  57. QString("创建人脸识别请求服务器返回错误: userName: %1. response: %2")
  58. .arg(metaInfo["certName"].toString(), QString::fromUtf8(QJsonDocument(response).toJson())));
  59. QMessageBox::critical(nullptr, "错误", "无法创建人脸识别,请确认后台录入的身份信息正确");
  60. return;
  61. }
  62. if (response.contains("data") && response["data"].isObject()) {
  63. LoadingWidget::showLoading(this, "人脸识别请求已创建...");
  64. QJsonObject dataObject = response["data"].toObject();
  65. if (dataObject.contains("ResultObject") && dataObject["ResultObject"].isObject()) {
  66. QJsonObject resultObject = dataObject["ResultObject"].toObject();
  67. if (resultObject.contains("CertifyId") && resultObject["CertifyId"].isString()) {
  68. certifyId = resultObject["CertifyId"].toString();
  69. }
  70. if (resultObject.contains("CertifyUrl") && resultObject["CertifyUrl"].isString()) {
  71. certifyUrl = resultObject["CertifyUrl"].toString();
  72. }
  73. }
  74. }
  75. if (!certifyUrl.isEmpty()) {
  76. view->load(QUrl(certifyUrl));
  77. layout->addWidget(view);
  78. layout->setStretchFactor(view, 1);
  79. QObject::connect(page, &QWebEnginePage::urlChanged, this, &BlastOpePage::onUrlChanged);
  80. } else {
  81. QMessageBox::information(nullptr, "提示", "人脸识别请求失败");
  82. Logger::getInstance().error("FaceVerificationInit: Failed to get certifyUrl");
  83. LoadingWidget::hideLoading();
  84. }
  85. }
  86. void BlastOpePage::closeWebViewAndRestoreUI() {
  87. if (view) {
  88. layout->removeWidget(view);
  89. delete view;
  90. view = nullptr;
  91. }
  92. if (layout) {
  93. delete layout;
  94. layout = nullptr;
  95. }
  96. }
  97. // 槽函数:处理 URL 改变事件
  98. void BlastOpePage::onUrlChanged(const QUrl &newUrl) {
  99. LoadingWidget::showLoading(this, "查询验证结果...");
  100. if (newUrl.host() == "www.integrateblaster.com") {
  101. closeWebViewAndRestoreUI();
  102. QNetworkAccessManager manager;
  103. QUrl requestUrl(apiBackendUrl.resolved(QUrl(QString("h-face-verify/certifyId/%1").arg(certifyId))));
  104. QNetworkRequest request(requestUrl);
  105. QNetworkReply *reply = manager.get(request);
  106. QEventLoop loop;
  107. QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
  108. loop.exec();
  109. if (reply->error() == QNetworkReply::NoError) {
  110. QByteArray responseData = reply->readAll();
  111. QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
  112. if (!jsonDoc.isNull() && jsonDoc.isObject()) {
  113. QJsonObject rootObj = jsonDoc.object();
  114. QJsonObject dataObj = rootObj["data"].toObject();
  115. QString message = dataObj["Message"].toString();
  116. QJsonObject resultObj = dataObj["ResultObject"].toObject();
  117. if (resultObj.isEmpty()) {
  118. Logger::getInstance().error(QString("获取认证初始化数据失败. message: %1.").arg(message));
  119. int ret = QMessageBox::information(nullptr, "认证失败", message + " ,请重新认证!");
  120. if (ret == QMessageBox::Ok) {
  121. InitFace();
  122. }
  123. } else {
  124. QString passed = resultObj["Passed"].toString();
  125. if (passed == "T") {
  126. ui->setupUi(this);
  127. initPagination();
  128. Logger::getInstance().info(QString("进入认证界面"));
  129. LoadingWidget::hideLoading();
  130. } else if (passed == "F") {
  131. int ret = QMessageBox::critical(nullptr, "提示", "操作失败,请重新认证!");
  132. if (ret == QMessageBox::Ok) {
  133. InitFace();
  134. }
  135. }
  136. }
  137. }
  138. } else {
  139. qDebug() << "Request failed:" << reply->errorString();
  140. Logger::getInstance().error(
  141. QString("InitFaseVerification request failed. error message: %1").arg(reply->errorString()));
  142. }
  143. reply->deleteLater();
  144. LoadingWidget::hideLoading();
  145. }
  146. LoadingWidget::hideLoading();
  147. }
  148. BlastOpePage::~BlastOpePage() {
  149. delete ui;
  150. if (view) {
  151. delete view;
  152. }
  153. if (layout) {
  154. delete layout;
  155. }
  156. }
  157. QJsonObject BlastOpePage::sendPostRequest(const QUrl &url, const QJsonObject &data) {
  158. QNetworkAccessManager manager;
  159. QNetworkRequest request(url);
  160. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
  161. QJsonDocument doc(data);
  162. QByteArray postData = doc.toJson();
  163. QNetworkReply *reply = manager.post(request, postData);
  164. QEventLoop loop;
  165. QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
  166. loop.exec();
  167. QJsonObject responseJson;
  168. if (reply->error() == QNetworkReply::NoError) {
  169. QByteArray responseData = reply->readAll();
  170. QJsonDocument responseDoc = QJsonDocument::fromJson(responseData);
  171. if (!responseDoc.isNull() && responseDoc.isObject()) {
  172. responseJson = responseDoc.object();
  173. }
  174. } else {
  175. qDebug() << "Error fetching content: " << reply->errorString();
  176. }
  177. reply->deleteLater();
  178. return responseJson;
  179. }
  180. void BlastOpePage::handleFeaturePermission(QWebEnginePage *page, const QUrl &securityOrigin,
  181. QWebEnginePage::Feature feature) {
  182. if (feature == QWebEnginePage::MediaAudioCapture || feature == QWebEnginePage::MediaAudioVideoCapture ||
  183. feature == QWebEnginePage::MediaVideoCapture) {
  184. page->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
  185. } else {
  186. page->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
  187. }
  188. }
  189. QJsonObject BlastOpePage::getMetaInfo() {
  190. QJsonObject metaInfo;
  191. QString certName;
  192. QString certNo;
  193. QMap<QString, QString> userInfo = RegistryManager::instance()->getCurentLoginUser();
  194. certName = userInfo.value("certName", "");
  195. certNo = userInfo.value("identity", "");
  196. metaInfo["certName"] = certName;
  197. metaInfo["certNo"] = certNo;
  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"}, {"操作员", "operatorName"}, {"爆破员", "blasterName"}, {"井下地址", "addressPath"},
  221. {"雷管数量", "detSum"}, {"起爆器数量", "blastCount"}, {"起爆状态", "blastStatus"}, {"进度", ""},
  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 =
  248. HProject.metaObject()->property(HProject.metaObject()->indexOfProperty(prop.toUtf8()));
  249. QVariant value = metaProp.read(&HProject);
  250. if (prop == "blastStatus") {
  251. QString statusText;
  252. if (value.toString() == BlastStatus::Created) {
  253. statusText = "未 注 册";
  254. item = new QStandardItem(statusText);
  255. item->setForeground(QColor("#e7c66b"));
  256. } else if (value.toString() == BlastStatus::Registered) {
  257. statusText = "待安全确认";
  258. item = new QStandardItem(statusText);
  259. item->setForeground(QColor("#f3a361"));
  260. } else if (value.toString() == BlastStatus::SafeChecked) {
  261. statusText = "待起爆";
  262. item = new QStandardItem(statusText);
  263. item->setForeground(QColor("#f3a3k'k1"));
  264. } else if (value.toString() == BlastStatus::Blasted) {
  265. statusText = "起 爆 完 成";
  266. item = new QStandardItem(statusText);
  267. item->setForeground(QColor("#90d543"));
  268. } else {
  269. item = new QStandardItem(value.toString());
  270. }
  271. } else {
  272. item = new QStandardItem(value.toString());
  273. }
  274. }
  275. if (item) {
  276. item->setTextAlignment(Qt::AlignCenter); // 设置文本居中对齐
  277. model->setItem(row, col, item);
  278. }
  279. }
  280. }
  281. ui->tableView->setModel(model);
  282. connectionItem =
  283. QObject::connect(model, &QStandardItemModel::itemChanged, this, &BlastOpePage::onItemCheckboxChanged);
  284. ui->tableView->setColumnWidth(0, 30);
  285. for (int i = 1; i < headerCount; ++i) {
  286. if (i == 8) { // Column: 进度
  287. ui->tableView->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Custom);
  288. ui->tableView->setColumnWidth(i, 180);
  289. } else {
  290. ui->tableView->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
  291. }
  292. }
  293. ui->tableView->setColumnHidden(headerCount, true);
  294. ui->tableView->setAlternatingRowColors(true);
  295. ui->tableView->verticalHeader()->setDefaultSectionSize(50);
  296. for (int row = 0; row < projectList.size(); ++row) {
  297. int progressCol = headers.size() - 2; //
  298. QProgressBar *progressBar1 = new QProgressBar(ui->tableView);
  299. progressBar1->setRange(0, 100); // 设置范围为0到100
  300. progressBar1->setValue(0);
  301. progressBar1->setAlignment(Qt::AlignCenter);
  302. progressBar1->setStyleSheet(
  303. "QProgressBar { border: 1px solid grey; border-radius: 5px; background-color: #EEEEEE; "
  304. "height: 10px; }"
  305. "QProgressBar::chunk { background-color: #05B8CC; width: 2px; margin: 0.5px; border - "
  306. "radius: 10px; }");
  307. progressBar1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  308. QProgressBar *progressBar2 = new QProgressBar(ui->tableView);
  309. progressBar2->setRange(0, 100); // 设置范围为0到100
  310. progressBar2->setValue(0);
  311. progressBar2->setAlignment(Qt::AlignCenter);
  312. progressBar2->setStyleSheet(
  313. "QProgressBar { border: 1px solid grey; border-radius: 5px; background-color: #EEEEEE; "
  314. "height: 10px; }"
  315. "QProgressBar::chunk { background-color: #05B8CC; width: 2px; margin: 0.5px; border - "
  316. "radius: 10px; }");
  317. progressBar2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  318. QProgressBar *progressBar3 = new QProgressBar(ui->tableView);
  319. progressBar3->setRange(0, 100); // 设置范围为0到100
  320. progressBar3->setValue(0);
  321. progressBar3->setAlignment(Qt::AlignCenter);
  322. progressBar3->setStyleSheet(
  323. "QProgressBar { border: 1px solid grey; border-radius: 5px; background-color: #EEEEEE; "
  324. "height: 10px; }"
  325. "QProgressBar::chunk { background-color: #05B8CC; width: 2px; margin: 0.5px; border - "
  326. "radius: 10px; }");
  327. progressBar3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  328. progressBars.append(ProgressBarTriple(progressBar1, progressBar2, progressBar3));
  329. QHBoxLayout *progressBarLayout = new QHBoxLayout;
  330. progressBarLayout->addWidget(progressBar1);
  331. progressBarLayout->addWidget(progressBar2);
  332. progressBarLayout->addWidget(progressBar3);
  333. progressBarLayout->setAlignment(Qt::AlignCenter);
  334. progressBarLayout->setContentsMargins(0, 0, 0, 0);
  335. progressBarLayout->setSpacing(0); // 设置进度条之间的间距为0
  336. QWidget *progressBarContainer = new QWidget(ui->tableView);
  337. progressBarContainer->setLayout(progressBarLayout);
  338. QModelIndex progressIndex = model->index(row, progressCol);
  339. if (progressIndex.isValid()) {
  340. ui->tableView->setIndexWidget(progressIndex, progressBarContainer);
  341. }
  342. int col = headers.size() - 1;
  343. // 创建一个按钮
  344. QWidget *widget = new QWidget(ui->tableView);
  345. QPushButton *button = new QPushButton(widget);
  346. button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  347. QModelIndex statusIndex = model->index(row, propMap.key("blastStatus"));
  348. if (statusIndex.isValid()) {
  349. QString blastStatus = model->data(statusIndex).toString();
  350. if (blastStatus == "待起爆") {
  351. button->setText(startBlastButtonTxt);
  352. button->setEnabled(true);
  353. }
  354. }
  355. QHBoxLayout *layout = new QHBoxLayout(widget);
  356. layout->addWidget(button);
  357. layout->setAlignment(Qt::AlignCenter);
  358. layout->setContentsMargins(0, 0, 0, 0);
  359. widget->setLayout(layout);
  360. QModelIndex index = model->index(row, col);
  361. if (index.isValid()) {
  362. ui->tableView->setIndexWidget(index, widget);
  363. connect(button, &QPushButton::clicked,
  364. [this, row, button]() { handleSingleBlastButtonClick(row, button); });
  365. }
  366. }
  367. }
  368. // 切换页数
  369. void BlastOpePage::PageChanged(int page) {
  370. currentPage = page;
  371. loadDataFromSource(currentPage, pageSize);
  372. }
  373. void BlastOpePage::onComboBoxIndexChanged(int index) {
  374. QVariant variant = pageWidget->getComboBox()->itemData(index);
  375. int value = variant.toInt();
  376. pageSize = value;
  377. currentPage = 1;
  378. loadDataFromSource(currentPage, pageSize);
  379. }
  380. void BlastOpePage::updateProgressBar(int firingStage, int row) {
  381. if (!progressBars.isEmpty()) {
  382. QProgressBar *progressBar1 = progressBars[row].bar1;
  383. QProgressBar *progressBar2 = progressBars[row].bar2;
  384. QProgressBar *progressBar3 = progressBars[row].bar3;
  385. switch (firingStage) {
  386. case FiringStages::QuickTesting:
  387. // 组网状态
  388. progressBar1->setRange(0, 0); // 设置范围为0到100
  389. progressBar1->setValue(0);
  390. break;
  391. case FiringStages::QuickTestFinished:
  392. // 组网完成
  393. progressBar1->setRange(0, 100); // 设置范围为0到100
  394. progressBar1->setValue(100);
  395. break;
  396. case FiringStages::NetCharging:
  397. // 充电开始
  398. progressBar2->setRange(0, 0); // 设置范围为0到100
  399. progressBar2->setValue(0);
  400. break;
  401. case FiringStages::NetChargingFinished:
  402. // 充电完成
  403. progressBar2->setRange(0, 100); // 设置范围为0到100
  404. progressBar2->setValue(100);
  405. break;
  406. case FiringStages::Blasting:
  407. // 起爆中
  408. progressBar3->setRange(0, 0); // 设置范围为0到100
  409. progressBar3->setValue(0);
  410. break;
  411. case FiringStages::BlastFinished:
  412. // 充电完成
  413. progressBar3->setRange(0, 100); // 设置范围为0到100
  414. progressBar3->setValue(100);
  415. break;
  416. case FiringStages::NotStarted:
  417. progressBar1->setRange(0, 100);
  418. progressBar1->setValue(0);
  419. progressBar2->setRange(0, 100);
  420. progressBar2->setValue(0);
  421. progressBar3->setRange(0, 100);
  422. progressBar3->setValue(0);
  423. break;
  424. default:
  425. break;
  426. }
  427. }
  428. }
  429. void BlastOpePage::onFiringStageUpdated(int stage, int row) {
  430. QModelIndex firingStatusItem = model->index(row, 7);
  431. if (firingStatusItem.isValid()) {
  432. QColor customColor;
  433. QFont boldFont;
  434. boldFont.setBold(true);
  435. switch (stage) {
  436. case FiringStages::NotStarted:
  437. model->setData(firingStatusItem, "待起爆");
  438. customColor = QColor("#44035b");
  439. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  440. model->setData(firingStatusItem, boldFont, Qt::FontRole);
  441. break;
  442. case FiringStages::QuickTesting:
  443. model->setData(firingStatusItem, "组 网 中 ...");
  444. customColor = QColor("#44035b");
  445. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  446. model->setData(firingStatusItem, boldFont, Qt::FontRole);
  447. break;
  448. case FiringStages::QuickTestFinished:
  449. model->setData(firingStatusItem, "组 网 完 成");
  450. customColor = QColor("#404185");
  451. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  452. break;
  453. case FiringStages::NetCharging:
  454. model->setData(firingStatusItem, "充 电 中 ...");
  455. customColor = QColor("#31688e");
  456. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  457. break;
  458. case FiringStages::NetChargingFinished:
  459. model->setData(firingStatusItem, "充 电 完 成");
  460. customColor = QColor("#1f918d");
  461. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  462. break;
  463. case FiringStages::Blasting:
  464. model->setData(firingStatusItem, "起 爆 中 ...");
  465. customColor = QColor("#38b775");
  466. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  467. updateOpBtn(6, row); // 更新按钮
  468. break;
  469. case FiringStages::BlastFinished:
  470. model->setData(firingStatusItem, "起 爆 完 成");
  471. customColor = QColor("#90d543");
  472. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  473. break;
  474. case FiringStages::PendingTrigger:
  475. model->setData(firingStatusItem, "按 下 双 建 起 爆 ...");
  476. customColor = QColor("#8e620");
  477. model->setData(firingStatusItem, customColor, Qt::ForegroundRole);
  478. break;
  479. default:
  480. break;
  481. }
  482. }
  483. }
  484. void BlastOpePage::handleSingleBlastButtonClick(int row, QPushButton *button) {
  485. QStandardItem *uuidItem = model->item(row, 10);
  486. QString uuid;
  487. if (uuidItem) {
  488. QVariant uuidVariant = uuidItem->data(Qt::UserRole);
  489. if (uuidVariant.isValid()) {
  490. uuid = uuidVariant.toString();
  491. }
  492. }
  493. if (button->text() == startBlastButtonTxt) {
  494. button->setMinimumWidth(80);
  495. button->setText(stopBlastButtonTxt);
  496. firingWidget *widget = new firingWidget(row, false, uuid);
  497. connect(widget, &firingWidget::updateBlastStatus, this, &BlastOpePage::updateProgressBar);
  498. connect(widget, &firingWidget::updateBlastStatus, this, &BlastOpePage::onFiringStageUpdated);
  499. connect(widget, &firingWidget::updateButton, this, &BlastOpePage::updateOpBtn);
  500. connect(widget, &firingWidget::countdown, this, &BlastOpePage::showCountDownWidget);
  501. connect(widget, &firingWidget::updateProjectStatus, this, &BlastOpePage::handlerUpdateProjectStatus);
  502. connect(widget, &firingWidget::closeFiring, this, &BlastOpePage::destroyFiringWidget);
  503. if (isShowTriggeringWidget) {
  504. widget->show();
  505. }
  506. widget->setAttribute(Qt::WA_DeleteOnClose);
  507. uuidWidgetMap.insert(uuid, widget);
  508. } else if (button->text() == stopBlastButtonTxt) {
  509. firingWidget *widget = uuidWidgetMap.value(uuid);
  510. if (widget) {
  511. widget->cancelBlasting();
  512. }
  513. }
  514. }
  515. void BlastOpePage::updateOpBtn(int status, int row) {
  516. qDebug() << "statusButton:" << status;
  517. QModelIndex index = model->index(row, 8);
  518. if (index.isValid()) {
  519. QWidget *widget = ui->tableView->indexWidget(index);
  520. if (widget) {
  521. QPushButton *button = widget->findChild<QPushButton *>();
  522. if (button) {
  523. // 使用样式表设置图标居中
  524. button->setStyleSheet(
  525. "QPushButton {"
  526. " padding: 0px;"
  527. " border: none;"
  528. " background-color: transparent;"
  529. "}");
  530. // 添加图标,假设图标文件名为 blast.svg,并且该文件存在于项目资源中
  531. QIcon icon(":/icons/icons/svg/blast.svg");
  532. button->setText("");
  533. button->setIcon(icon);
  534. button->setIconSize(QSize(32, 32));
  535. }
  536. }
  537. }
  538. }
  539. void BlastOpePage::handlerUpdateProjectStatus(QString uuid, const QString &newStatus) {
  540. dao.updateBlastStatusByUuid(uuid, newStatus);
  541. }
  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::onItemCheckboxChanged(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::updateBlastStatus, this, &BlastOpePage::updateProgressBar);
  600. connect(widgetSelect, &firingWidget::updateBlastStatus, this, &BlastOpePage::onFiringStageUpdated);
  601. connect(widgetSelect, &firingWidget::selectSignal, this, &BlastOpePage::setBatchBlastTrigger);
  602. connect(widgetSelect, &firingWidget::updateButton, this, &BlastOpePage::updateOpBtn);
  603. connect(widgetSelect, &firingWidget::updateProjectStatus, this, &BlastOpePage::handlerUpdateProjectStatus);
  604. connect(widgetSelect, &firingWidget::closeFiring, this, &BlastOpePage::destroyBatchFiringWidget);
  605. widgetSelect->show();
  606. widgetSelect->setAttribute(Qt::WA_DeleteOnClose);
  607. uuidWidgetSMap.insert(uuid, widgetSelect);
  608. if (isShowTriggeringWidget) {
  609. widgetSelect->show();
  610. }
  611. }
  612. }
  613. // 完成充电
  614. void BlastOpePage::setBatchBlastTrigger(QString uuid) {
  615. selectedUuids.insert(uuid);
  616. bool isSame = checkUuidsSame();
  617. if (isSame) {
  618. bool successSelect;
  619. serialTool = SerialTool::getInstance(nullptr, &successSelect);
  620. connect(serialTool, &SerialTool::buttonPressedReceived, this, &BlastOpePage::showCountDownForBatchBlast,
  621. Qt::SingleShotConnection);
  622. if (serialTool) {
  623. QByteArray data = "\r\nENABLE_BUTTON\r\n";
  624. bool success = serialTool->sendData(data);
  625. if (success) {
  626. Logger::getInstance().info("blast triggered.");
  627. } else {
  628. Logger::getInstance().warn("blast trigger failed.");
  629. }
  630. connect(
  631. serialTool, &SerialTool::enableButtonReceived, this,
  632. [this]() {
  633. for (const auto &row : uuidMap.keys()) {
  634. qDebug() << "Key:" << row;
  635. onFiringStageUpdated(10, row);
  636. }
  637. },
  638. Qt::SingleShotConnection);
  639. } else {
  640. qDebug() << "serialTool Not fond.";
  641. QMessageBox::critical(nullptr, "错误", "trigger button devices not found");
  642. }
  643. } else {
  644. Logger::getInstance().error(
  645. QString("The uuids in selectedUuids and uuidMap are different. uuid: %1").arg(uuid));
  646. }
  647. }
  648. // 检查 selectedUuids 和 uuidMap 中的 uuid 是否相同
  649. bool BlastOpePage::checkUuidsSame() {
  650. QSet<QString> mapUuids;
  651. for (const auto &value : uuidMap) {
  652. mapUuids.insert(value);
  653. }
  654. return selectedUuids == mapUuids;
  655. }
  656. void BlastOpePage::showCountDownForBatchBlast() {
  657. QByteArray data = "\r\nDISABLE_BUTTON\r\n";
  658. bool success = serialTool->sendData(data);
  659. if (success) {
  660. qDebug() << "Data sent successfully";
  661. } else {
  662. qDebug() << "Failed to send data";
  663. }
  664. serialTool->releaseInstance();
  665. CountdownWidget *countdownWidgetSelect = new CountdownWidget(this);
  666. countdownWidgetSelect->resize(200, 200);
  667. int x = (this->width() - countdownWidgetSelect->width()) / 2;
  668. int y = (this->height() - countdownWidgetSelect->height()) / 2;
  669. countdownWidgetSelect->move(x, y);
  670. countdownWidgetSelect->show();
  671. connect(countdownWidgetSelect, &CountdownWidget::countdownFinished, this, &BlastOpePage::triggerBatchFiringBlast,
  672. Qt::SingleShotConnection);
  673. }
  674. void BlastOpePage::triggerBatchFiringBlast() {
  675. for (auto it = uuidWidgetSMap.begin(); it != uuidWidgetSMap.end(); ++it) {
  676. QString uuid = it.key();
  677. firingWidget *widget = it.value();
  678. QString topic = "hxgc/" + uuid + "/P";
  679. QString message = "起爆";
  680. widget->onCountdownFinished(topic, message);
  681. }
  682. }
  683. void BlastOpePage::destroyBatchFiringWidget(const QString &uuid) {
  684. firingWidget *widget = uuidWidgetSMap.value(uuid);
  685. if (widget) {
  686. widget->close();
  687. widget->deleteLater();
  688. uuidWidgetSMap.remove(uuid);
  689. }
  690. for (int row = 0; row < model->rowCount(); ++row) {
  691. QStandardItem *item = model->item(row, 0);
  692. if (item) {
  693. Qt::ItemFlags flags = item->flags();
  694. flags &= ~Qt::ItemIsEnabled; // 去除 ItemIsEnabled 标志位
  695. item->setFlags(flags);
  696. }
  697. }
  698. }