blastopepage.cpp 31 KB

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