mainwindow.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "jobs.h"
  4. #include <QDebug>
  5. #include <QWidget>
  6. #include <QPushButton>
  7. #include "logger.h"
  8. #include "loadingWidget.h"
  9. // 定义 ANzI 转义序列来设置颜色
  10. #define ANSI_COLOR_GREEN "\x1B[32m"
  11. #define ANSI_COLOR_RESET "\x1B[0m"
  12. #include <exception>
  13. #include <QMessageBox>
  14. MainWindow::MainWindow(QWidget *parent)
  15. : QMainWindow(parent), ui(new Ui::MainWindow)
  16. {
  17. try
  18. {
  19. // 去除窗口边界,设置为无边框窗口
  20. this->setWindowFlags(Qt::FramelessWindowHint);
  21. this->setWindowState(Qt::WindowMaximized); // Maximizes the window
  22. ui->setupUi(this);
  23. initializeAnimate();
  24. initialMqttService();
  25. pageFactories[ui->btnNew] = new AddressFactory();
  26. pageFactories[ui->btnBlastProject] = new BlastProjectFactory();
  27. pageFactories[ui->btnEquipment] = new EquipmentFactory();
  28. pageFactories[ui->btnDet] = new DetInfoFactory();
  29. pageFactories[ui->btnBlastOper] = new BlastOperationFactory();
  30. pageFactories[ui->btnRecord] = new BlastRecordFactory();
  31. connect(ui->btnToggle, &QPushButton::clicked, this, &MainWindow::onToggleButtonClicked);
  32. for (auto *widget : left_button_station)
  33. {
  34. QPushButton *button = qobject_cast<QPushButton *>(widget);
  35. if (button)
  36. {
  37. connect(button, &QPushButton::clicked, this, [this, button]
  38. { onButtonClicked(button); });
  39. }
  40. }
  41. initDateTime();
  42. initialBtnSerial();
  43. // initialGPSSerial();
  44. ui->labLat->setText("经度: " + lat);
  45. ui->labLon->setText("维度: " + lon);
  46. connect(ui->btnClose, &QPushButton::clicked, this, &MainWindow::close);
  47. connect(this, &MainWindow::projectTitleChanged, this, &MainWindow::updateProjectTitleLabel);
  48. }
  49. catch (const std::exception &ex)
  50. {
  51. Logger::getInstance().error(QString("Application crashed: %1").arg(ex.what()));
  52. QMessageBox::critical(this, "Error", QString("Application crashed: %1").arg(ex.what()));
  53. throw; // rethrow to allow further handling if needed
  54. }
  55. catch (...)
  56. {
  57. Logger::getInstance().error("Application crashed: Unknown exception");
  58. QMessageBox::critical(this, "Error", "Application crashed: Unknown exception");
  59. throw;
  60. }
  61. }
  62. void MainWindow::updateProjectTitleLabel(const QString &newTitle)
  63. {
  64. ui->projectTitleLable->setText(newTitle);
  65. }
  66. void MainWindow::setProjectTitle(const QString &newTitle)
  67. {
  68. if (m_currentProjectTitle != newTitle)
  69. {
  70. m_currentProjectTitle = newTitle;
  71. // Emit the signal to notify listeners (like our QLabel slot)
  72. emit projectTitleChanged(m_currentProjectTitle);
  73. }
  74. }
  75. void MainWindow::initializeAnimate()
  76. {
  77. move(200, 200);
  78. animate_leftFrame = new QPropertyAnimation(ui->leftFrame, "minimumWidth");
  79. animate_leftFrame->setDuration(300);
  80. for (QObject *child : ui->left_buttonsBox->children())
  81. {
  82. if (qobject_cast<QWidget *>(child))
  83. {
  84. left_button_station.append(qobject_cast<QWidget *>(child));
  85. }
  86. }
  87. }
  88. void MainWindow::onToggleButtonClicked()
  89. {
  90. // 执行动画
  91. JOBS ::btn_animation(ui->leftFrame, animate_leftFrame);
  92. for (QWidget *b : left_button_station)
  93. {
  94. b->setProperty("spread", !b->property("spread").toBool());
  95. b->setStyleSheet(b->styleSheet());
  96. }
  97. }
  98. // 选中按钮
  99. void MainWindow::onButtonClicked(QPushButton *button)
  100. {
  101. setStyleSheets(static_cast<QPushButton *>(button));
  102. switchPage(static_cast<QPushButton *>(button));
  103. }
  104. void MainWindow::switchPage(QWidget *button)
  105. {
  106. LoadingWidget::showLoading(nullptr, "请稍等");
  107. if (pageFactories.contains(button))
  108. {
  109. PageFactory *factory = pageFactories[button];
  110. if (createdPageByButton.contains(button))
  111. {
  112. QWidget *existingPage = createdPageByButton[button];
  113. existingPage->hide();
  114. ui->stackedWidget->removeWidget(existingPage);
  115. createdPageByButton.remove(button);
  116. }
  117. QWidget *newPage = factory->createPage(this);
  118. ui->stackedWidget->addWidget(newPage);
  119. ui->stackedWidget->setCurrentWidget(newPage);
  120. setProjectTitle(qobject_cast<QPushButton *>(button)->text());
  121. createdPageByButton.insert(button, newPage);
  122. int pageCount = ui->stackedWidget->count();
  123. }
  124. LoadingWidget::hideLoading();
  125. }
  126. void MainWindow::initialMqttService()
  127. {
  128. Logger::getInstance().info("Start init Mqtt server.");
  129. MqttClient *pcMqttInit = MqttClient::getInstance();
  130. QStringList topics = {"hxgc/topic", "hxgc/companycode/pro/P"};
  131. pcMqttInit->connectToMqttBroker("114.55.233.194", 1883, "hxgc", "hxgc123456", "pcMqttInitY11", topics);
  132. connect(pcMqttInit, &MqttClient::proMessageReceived, this, &MainWindow::messageAndTopicReceived);
  133. Logger::getInstance().info("Connect Mqtt server request sent.");
  134. }
  135. void MainWindow::messageAndTopicReceived(const QByteArray &message, const QMqttTopicName &topic)
  136. {
  137. QJsonDocument jsonDoc = QJsonDocument::fromJson(message);
  138. if (!jsonDoc.isNull() && jsonDoc.isObject())
  139. {
  140. QJsonObject jsonObj = jsonDoc.object();
  141. if (jsonObj.contains("uuid") && jsonObj.contains("status"))
  142. {
  143. QJsonValue uuidValue = jsonObj["uuid"];
  144. QJsonValue statusValue = jsonObj["status"];
  145. if (statusValue.isString() && statusValue.toString() == "1")
  146. { // "1" 未注册
  147. if (uuidValue.isNull())
  148. {
  149. qDebug() << "uuid 的值为 null";
  150. }
  151. else
  152. {
  153. QString uuid = uuidValue.toString();
  154. HProjectDao dao = HProjectDao(DatabaseManager::getInstance().getDatabase());
  155. dao.updateBlastStatusByUuid(uuid, "2");
  156. }
  157. }
  158. }
  159. }
  160. }
  161. void MainWindow::setStyleSheets(QPushButton *selectedButton)
  162. {
  163. for (auto *b : left_button_station)
  164. {
  165. b->setProperty("selected", b == selectedButton);
  166. b->setStyleSheet(b->styleSheet()); // 刷新显示
  167. }
  168. }
  169. // 处理 MQTT 连接成功的槽函数
  170. void MainWindow::onMqttConnected()
  171. {
  172. m_isMqttConnected = true;
  173. Logger::getInstance().info("Mqtt connected.");
  174. }
  175. void MainWindow::initialBtnSerial()
  176. {
  177. bool success;
  178. serialTool = SerialTool::getInstance(this, &success);
  179. connect(serialTool, &SerialTool::serialPortOpened, this, &MainWindow::onSerialToolCreated);
  180. serialTool->setupSerialPort();
  181. Logger::getInstance().info("Fire buttons initialized");
  182. }
  183. void MainWindow::onSerialToolCreated()
  184. {
  185. m_btnSerialInitialized = true;
  186. serialTool->releaseInstance();
  187. qDebug() << ANSI_COLOR_GREEN << "Serial tool initialized" << ANSI_COLOR_RESET;
  188. Logger::getInstance().info("SerialTool initialized");
  189. }
  190. void MainWindow::initialGPSSerial()
  191. {
  192. Logger::getInstance().info("开始初始化GPS");
  193. SerialGPSThread *threadGPS = new SerialGPSThread(this);
  194. connect(threadGPS, &SerialGPSThread::storedGNRMCDataUpdated, this, &MainWindow::handleStoredGNRMCData);
  195. threadGPS->start();
  196. }
  197. // 槽函数,用于接收 RMCData 数据
  198. void MainWindow::handleStoredGNRMCData(const RMCData &data)
  199. {
  200. if (data.isValid)
  201. {
  202. lat = QString::number(data.latitude);
  203. lon = QString::number(data.longitude);
  204. }
  205. else
  206. {
  207. lat = "定位失败";
  208. lon = "定位失败";
  209. }
  210. ui->labLat->setText("经度: " + lat);
  211. ui->labLon->setText("纬度: " + lon);
  212. labLat = lat;
  213. labLon = lon;
  214. }
  215. void MainWindow::initDateTime()
  216. {
  217. timeThread = new TimeUpdateThread(this);
  218. connect(timeThread, &TimeUpdateThread::timeUpdated, this, &MainWindow::onTimeUpdated);
  219. timeThread->start();
  220. }
  221. void MainWindow::onTimeUpdated(const QString &timeString)
  222. {
  223. ui->dateTimeShow->setText(timeString);
  224. }
  225. MainWindow::~MainWindow()
  226. {
  227. timeThread->stop();
  228. delete ui;
  229. }
  230. void MainWindow::mousePressEvent(QMouseEvent *event)
  231. {
  232. if (event->button() == Qt::LeftButton)
  233. {
  234. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  235. event->accept();
  236. }
  237. }
  238. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  239. {
  240. if (event->buttons() & Qt::LeftButton)
  241. {
  242. move(event->globalPos() - m_dragPosition);
  243. event->accept();
  244. }
  245. }