mainwindow.cpp 8.3 KB

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