blastopepage.cpp 30 KB

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