blastopepage.cpp 31 KB

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