blastopepage.cpp 30 KB

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