mainwindow.cpp 8.2 KB

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