blastopepage.cpp 32 KB

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