blastopepage.cpp 30 KB

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