#include "mainwindow.h" #include "ui_mainwindow.h" #include "jobs.h" #include // 定义 ANzI 转义序列来设置颜色 #define ANSI_COLOR_GREEN "\x1B[32m" #define ANSI_COLOR_RESET "\x1B[0m" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { // 去除窗口边界,设置为无边框窗口 this->setWindowFlags(Qt::FramelessWindowHint); ui->setupUi(this); initializeAnimate(); // initialMqttService(); pageFactories[ui->btnNew] = new AddressFactory(); pageFactories[ui->btnBlastProject] = new BlastProjectFactory(); pageFactories[ui->btnEquipment] = new EquipmentFactory(); pageFactories[ui->btnDet] = new DetInfoFactory(); pageFactories[ui->btnBlastOper] = new BlastOperationFactory(); pageFactories[ui->btnRecord] = new BlastRecordFactory(); connect(ui->btnToggle, &QPushButton::clicked, this, &MainWindow::onToggleButtonClicked); for (auto *widget : left_button_station) { QPushButton *button = qobject_cast(widget); if (button) { connect(button, &QPushButton::clicked, this, [this, button]{ onButtonClicked(button); }); } } initDateTime(); // initialBtnSerial(); // initialGPSSerial(); ui->labLat->setText("经度: "+lat); ui->labLon->setText("维度: "+lon); connect(ui->btnClose, &QPushButton::clicked, this, &MainWindow::close); } void MainWindow::initializeAnimate() { move(200, 200); animate_leftFrame = new QPropertyAnimation(ui->leftFrame, "minimumWidth"); animate_leftFrame->setDuration(300); for (QObject *child : ui->left_buttonsBox->children()) { if (qobject_cast(child)) { left_button_station.append(qobject_cast(child)); } } } void MainWindow::onToggleButtonClicked() { // 执行动画 JOBS ::btn_animation(ui->leftFrame, animate_leftFrame); for (QWidget *b : left_button_station) { b->setProperty("spread", !b->property("spread").toBool()); b->setStyleSheet(b->styleSheet()); } } // 选中按钮 void MainWindow::onButtonClicked(QPushButton *button) { setStyleSheets(static_cast(button)); switchPage(static_cast(button)); } void MainWindow::switchPage(QWidget *button) { if (pageFactories.contains(button)) { PageFactory* factory = pageFactories[button]; if (buttonToPage.contains(button)) { QWidget* existingPage = buttonToPage[button]; ui->stackedWidget->removeWidget(existingPage); delete existingPage; buttonToPage.remove(button); } QWidget* newPage = factory->createPage(this); ui->stackedWidget->addWidget(newPage); ui->stackedWidget->setCurrentWidget(newPage); buttonToPage.insert(button, newPage); int pageCount = ui->stackedWidget->count(); } } void MainWindow::initialMqttService() { MqttClient *pcMqttInit = MqttClient::getInstance(); QStringList topics = {"hxgc/topic","hxgc/companycode/pro/P"}; pcMqttInit->connectToMqttBroker("114.55.233.194", 1883, "hxgc", "hxgc123456", "pcMqttInit", topics); connect(pcMqttInit, &MqttClient::proMessageReceived, this, &MainWindow::messageAndTopicReceived); } void MainWindow::messageAndTopicReceived(const QByteArray &message, const QMqttTopicName &topic){ QJsonDocument jsonDoc = QJsonDocument::fromJson(message); if (!jsonDoc.isNull() && jsonDoc.isObject()) { QJsonObject jsonObj = jsonDoc.object(); if (jsonObj.contains("uuid")&& jsonObj.contains("status")) { QJsonValue uuidValue = jsonObj["uuid"]; QJsonValue statusValue = jsonObj["status"]; if (statusValue.isString() && statusValue.toString() == "1") { if (uuidValue.isNull()) { qDebug() << "uuid 的值为 null"; } else { QString uuid = uuidValue.toString(); HProjectDao dao = HProjectDao(DatabaseManager::getInstance().getDatabase()); dao.updateBlastStatusByUuid(uuid,"2"); } } } } } void MainWindow::setStyleSheets(QPushButton *selectedButton) { for (auto *b : left_button_station) { b->setProperty("selected", b == selectedButton); b->setStyleSheet(b->styleSheet()); // 刷新显示 } } // 处理 MQTT 连接成功的槽函数 void MainWindow::onMqttConnected() { m_isMqttConnected = true; qDebug() << "MQTT 连接成功"; } void MainWindow::initialBtnSerial() { bool success; serialTool = SerialTool::getInstance(this,&success); connect(serialTool, &SerialTool::serialPortOpened, this, &MainWindow::onSerialToolCreated); serialTool->setupSerialPort(); } void MainWindow::onSerialToolCreated() { m_btnSerialInitialized = true; serialTool->releaseInstance(); qDebug() << ANSI_COLOR_GREEN << "Serial tool initialized" << ANSI_COLOR_RESET; } void MainWindow::initialGPSSerial(){ SerialGPSThread* threadGPS = new SerialGPSThread(this); connect(threadGPS, &SerialGPSThread::storedGNRMCDataUpdated, this,&MainWindow::handleStoredGNRMCData); threadGPS->start(); } // 槽函数,用于接收 RMCData 数据 void MainWindow::handleStoredGNRMCData(const RMCData &data) { if(data.isValid){ lat = QString::number(data.latitude); lon = QString::number(data.longitude); }else{ lat = "定位失败"; lon = "定位失败"; } ui->labLat->setText("经度: "+lat); ui->labLon->setText("纬度: "+lon); labLat = lat; labLon = lon; } void MainWindow::initDateTime(){ timeThread = new TimeUpdateThread(this); connect(timeThread, &TimeUpdateThread::timeUpdated, this, &MainWindow::onTimeUpdated); timeThread->start(); } void MainWindow::onTimeUpdated(const QString &timeString) { ui->dateTimeShow->setText(timeString); } MainWindow::~MainWindow() { timeThread->stop(); delete ui; } void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragPosition = event->globalPos() - frameGeometry().topLeft(); event->accept(); } } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { move(event->globalPos() - m_dragPosition); event->accept(); } }