mainwindow.cpp 7.1 KB

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