mainwindow.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "jobs.h"
  4. #include <QDebug>
  5. // 定义 ANzI 转义序列来设置颜色
  6. #define ANSI_COLOR_GREEN "\x1B[32m"
  7. #define ANSI_COLOR_RESET "\x1B[0m"
  8. MainWindow::MainWindow(QWidget *parent)
  9. : QMainWindow(parent)
  10. , ui(new Ui::MainWindow)
  11. {
  12. // 去除窗口边界,设置为无边框窗口
  13. this->setWindowFlags(Qt::FramelessWindowHint);
  14. ui->setupUi(this);
  15. initializeAnimate();
  16. // initialMqttService();
  17. pageFactories[ui->btnNew] = new AddressFactory();
  18. pageFactories[ui->btnBlastProject] = new BlastProjectFactory();
  19. pageFactories[ui->btnEquipment] = new EquipmentFactory();
  20. pageFactories[ui->btnDet] = new DetInfoFactory();
  21. pageFactories[ui->btnBlastOper] = new BlastOperationFactory();
  22. pageFactories[ui->btnRecord] = new BlastRecordFactory();
  23. connect(ui->btnToggle, &QPushButton::clicked, this, &MainWindow::onToggleButtonClicked);
  24. for (auto *widget : left_button_station) {
  25. QPushButton *button = qobject_cast<QPushButton*>(widget);
  26. if (button) {
  27. connect(button, &QPushButton::clicked, this, [this, button]{
  28. onButtonClicked(button);
  29. });
  30. }
  31. }
  32. initDateTime();
  33. // initialBtnSerial();
  34. // initialGPSSerial();
  35. ui->labLat->setText("经度: "+lat);
  36. ui->labLon->setText("维度: "+lon);
  37. connect(ui->btnClose, &QPushButton::clicked, this, &MainWindow::close);
  38. }
  39. void MainWindow::initializeAnimate() {
  40. move(200, 200);
  41. animate_leftFrame = new QPropertyAnimation(ui->leftFrame, "minimumWidth");
  42. animate_leftFrame->setDuration(300);
  43. for (QObject *child : ui->left_buttonsBox->children()) {
  44. if (qobject_cast<QWidget*>(child)) {
  45. left_button_station.append(qobject_cast<QWidget*>(child));
  46. }
  47. }
  48. }
  49. void MainWindow::onToggleButtonClicked() {
  50. // 执行动画
  51. JOBS ::btn_animation(ui->leftFrame, animate_leftFrame);
  52. for (QWidget *b : left_button_station) {
  53. b->setProperty("spread", !b->property("spread").toBool());
  54. b->setStyleSheet(b->styleSheet());
  55. }
  56. }
  57. // 选中按钮
  58. void MainWindow::onButtonClicked(QPushButton *button)
  59. {
  60. setStyleSheets(static_cast<QPushButton *>(button));
  61. switchPage(static_cast<QPushButton *>(button));
  62. }
  63. void MainWindow::switchPage(QWidget *button) {
  64. if (pageFactories.contains(button)) {
  65. PageFactory* factory = pageFactories[button];
  66. if (buttonToPage.contains(button)) {
  67. QWidget* existingPage = buttonToPage[button];
  68. ui->stackedWidget->removeWidget(existingPage);
  69. delete existingPage;
  70. buttonToPage.remove(button);
  71. }
  72. QWidget* newPage = factory->createPage(this);
  73. ui->stackedWidget->addWidget(newPage);
  74. ui->stackedWidget->setCurrentWidget(newPage);
  75. buttonToPage.insert(button, newPage);
  76. int pageCount = ui->stackedWidget->count();
  77. }
  78. }
  79. void MainWindow::initialMqttService()
  80. {
  81. MqttClient *pcMqttInit = MqttClient::getInstance();
  82. QStringList topics = {"hxgc/topic","hxgc/companycode/pro/P"};
  83. pcMqttInit->connectToMqttBroker("114.55.233.194", 1883, "hxgc", "hxgc123456", "pcMqttInit", topics);
  84. connect(pcMqttInit, &MqttClient::proMessageReceived, this, &MainWindow::messageAndTopicReceived);
  85. }
  86. void MainWindow::messageAndTopicReceived(const QByteArray &message, const QMqttTopicName &topic){
  87. QJsonDocument jsonDoc = QJsonDocument::fromJson(message);
  88. if (!jsonDoc.isNull() && jsonDoc.isObject()) {
  89. QJsonObject jsonObj = jsonDoc.object();
  90. if (jsonObj.contains("uuid")&& jsonObj.contains("status")) {
  91. QJsonValue uuidValue = jsonObj["uuid"];
  92. QJsonValue statusValue = jsonObj["status"];
  93. if (statusValue.isString() && statusValue.toString() == "1") {
  94. if (uuidValue.isNull()) {
  95. qDebug() << "uuid 的值为 null";
  96. } else {
  97. QString uuid = uuidValue.toString();
  98. HProjectDao dao = HProjectDao(DatabaseManager::getInstance().getDatabase());
  99. dao.updateBlastStatusByUuid(uuid,"2");
  100. }
  101. }
  102. }
  103. }
  104. }
  105. void MainWindow::setStyleSheets(QPushButton *selectedButton)
  106. {
  107. for (auto *b : left_button_station) {
  108. b->setProperty("selected", b == selectedButton);
  109. b->setStyleSheet(b->styleSheet()); // 刷新显示
  110. }
  111. }
  112. // 处理 MQTT 连接成功的槽函数
  113. void MainWindow::onMqttConnected()
  114. {
  115. m_isMqttConnected = true;
  116. qDebug() << "MQTT 连接成功";
  117. }
  118. void MainWindow::initialBtnSerial()
  119. { bool success;
  120. serialTool = SerialTool::getInstance(this,&success);
  121. connect(serialTool, &SerialTool::serialPortOpened, this, &MainWindow::onSerialToolCreated);
  122. serialTool->setupSerialPort();
  123. }
  124. void MainWindow::onSerialToolCreated()
  125. {
  126. m_btnSerialInitialized = true;
  127. serialTool->releaseInstance();
  128. qDebug() << ANSI_COLOR_GREEN << "Serial tool initialized" << ANSI_COLOR_RESET;
  129. }
  130. void MainWindow::initialGPSSerial(){
  131. SerialGPSThread* threadGPS = new SerialGPSThread(this);
  132. connect(threadGPS, &SerialGPSThread::storedGNRMCDataUpdated, this,&MainWindow::handleStoredGNRMCData);
  133. threadGPS->start();
  134. }
  135. // 槽函数,用于接收 RMCData 数据
  136. void MainWindow::handleStoredGNRMCData(const RMCData &data)
  137. {
  138. if(data.isValid){
  139. lat = QString::number(data.latitude);
  140. lon = QString::number(data.longitude);
  141. }else{
  142. lat = "定位失败";
  143. lon = "定位失败";
  144. }
  145. ui->labLat->setText("经度: "+lat);
  146. ui->labLon->setText("纬度: "+lon);
  147. labLat = lat;
  148. labLon = lon;
  149. }
  150. void MainWindow::initDateTime(){
  151. timeThread = new TimeUpdateThread(this);
  152. connect(timeThread, &TimeUpdateThread::timeUpdated, this, &MainWindow::onTimeUpdated);
  153. timeThread->start();
  154. }
  155. void MainWindow::onTimeUpdated(const QString &timeString)
  156. {
  157. ui->dateTimeShow->setText(timeString);
  158. }
  159. MainWindow::~MainWindow()
  160. {
  161. timeThread->stop();
  162. delete ui;
  163. }
  164. void MainWindow::mousePressEvent(QMouseEvent *event)
  165. {
  166. if (event->button() == Qt::LeftButton) {
  167. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  168. event->accept();
  169. }
  170. }
  171. void MainWindow::mouseMoveEvent(QMouseEvent *event)
  172. {
  173. if (event->buttons() & Qt::LeftButton) {
  174. move(event->globalPos() - m_dragPosition);
  175. event->accept();
  176. }
  177. }