mainwindow.cpp 6.5 KB

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