#include "guidingui.h"
#include "ui_guidingui.h"
#include <QWebEngineView>
#include <QString>
#include <QVBoxLayout>
#include <QDir>
#include <QUrl>
#include <QWebEnginePage>
#include <QMessageBox>
#include <QComboBox>
#include <QTime>
#include <QProcess>
#include <QDebug>
#include <QFileInfo>
#include <QBrush>
#include <QColor>
#include <QApplication>
#include <QDesktopWidget>

GuidingUI::GuidingUI(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::GuidingUI)
{
    ui->setupUi(this);
    
    // 设置窗口最小尺寸
    this->setMinimumSize(1200, 800);
    
    // 窗口居中显示
    this->resize(1400, 900);
    QRect screenGeometry = QApplication::desktop()->screenGeometry();
    int x = (screenGeometry.width() - this->width()) / 2;
    int y = (screenGeometry.height() - this->height()) / 2;
    this->move(x, y);
    
    // 初始化随机数生成器
    qsrand(QTime::currentTime().msec());
    
    // 设置按钮样式 - 现代化军用风格 (兼容Qt样式表)
    QString buttonStyle = "QPushButton {"
                          "  background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                          "              stop:0 rgba(45, 65, 95, 0.8), "
                          "              stop:1 rgba(25, 40, 65, 0.8));"
                          "  color: rgb(220, 230, 242);"
                          "  border: 2px solid rgba(82, 194, 242, 0.5);"
                          "  padding: 10px 18px;"
                          "  border-radius: 8px;"
                          "  font-size: 14px;"
                          "  font-weight: bold;"
                          "  text-align: left;"
                          "}"
                          "QPushButton:hover {"
                          "  background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                          "              stop:0 rgba(82, 194, 242, 0.7), "
                          "              stop:1 rgba(45, 120, 180, 0.7));"
                          "  border: 2px solid rgba(82, 194, 242, 0.9);"
                          "  color: white;"
                          "}"
                          "QPushButton:pressed {"
                          "  background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                          "              stop:0 rgba(82, 194, 242, 0.9), "
                          "              stop:1 rgba(45, 120, 180, 0.9));"
                          "  border: 2px solid rgba(82, 194, 242, 1.0);"
                          "}";
    
    // 应用样式到所有按钮
    ui->robottab->setStyleSheet(buttonStyle);
    ui->robotlocation->setStyleSheet(buttonStyle);
    ui->addrobot->setStyleSheet(buttonStyle);
    ui->mapbutton->setStyleSheet(buttonStyle);
    ui->addUAV->setStyleSheet(buttonStyle);
    ui->UAVtab->setStyleSheet(buttonStyle);
    ui->UAVview->setStyleSheet(buttonStyle);
    ui->robotView->setStyleSheet(buttonStyle);
    ui->robotMapping->setStyleSheet(buttonStyle);
    ui->smartNavigation->setStyleSheet(buttonStyle);
    ui->faceRecognition->setStyleSheet(buttonStyle);
    ui->faceTracking->setStyleSheet(buttonStyle);
    
    /* 控制地图显示 */
    MapDisplayControl(ui->mapbutton,ui->MapDisplayer,ui->gridLayout_3);
    /* 控制添加机器人 */
    AddRobotControl(ui->addrobot);
    /* 控制机器人列表 */
    RobotsInfosControl(ui->robottab);
    
    // 初始化默认机器人
    robotList.append(qMakePair(QString("ALice"), QString("192.168.0.1")));
    robotList.append(qMakePair(QString("Bob"), QString("192.168.0.2")));
    
    // 初始化人脸识别进程
    faceRecognitionProcess = new QProcess(this);
    
    // 初始化人脸跟随进程
    faceTrackingProcess = new QProcess(this);
    
    // 连接人脸识别按钮信号
    connect(ui->faceRecognition, &QPushButton::clicked, this, &GuidingUI::on_faceRecognition_clicked);
    
    // 连接人脸跟随按钮信号
    connect(ui->faceTracking, &QPushButton::clicked, this, &GuidingUI::on_faceTracking_clicked);
    
    // 连接战场探索系统新按钮的信号
    connect(ui->UAVview, &QPushButton::clicked, this, &GuidingUI::on_UAVview_clicked);
    connect(ui->robotView, &QPushButton::clicked, this, &GuidingUI::on_robotView_clicked);
    connect(ui->robotMapping, &QPushButton::clicked, this, &GuidingUI::on_robotMapping_clicked);
    connect(ui->smartNavigation, &QPushButton::clicked, this, &GuidingUI::on_smartNavigation_clicked);
    
    // 连接进程输出信号，可以在控制台显示Python脚本的输出
    connect(faceRecognitionProcess, &QProcess::readyReadStandardOutput, [this]() {
        QByteArray output = faceRecognitionProcess->readAllStandardOutput();
        qDebug() << "人脸识别输出:" << output;
    });
    
    connect(faceRecognitionProcess, &QProcess::readyReadStandardError, [this]() {
        QByteArray error = faceRecognitionProcess->readAllStandardError();
        qDebug() << "人脸识别错误:" << error;
    });
}

GuidingUI::~GuidingUI()
{
    // 如果进程正在运行，终止它
    if (faceRecognitionProcess->state() == QProcess::Running) {
        faceRecognitionProcess->terminate();
        faceRecognitionProcess->waitForFinished(3000);
    }
    
    // 如果人脸跟随进程正在运行，终止它
    if (faceTrackingProcess->state() == QProcess::Running) {
        faceTrackingProcess->terminate();
        faceTrackingProcess->waitForFinished(3000);
    }
    
    delete faceRecognitionProcess;
    delete faceTrackingProcess;
    delete ui;
}

void GuidingUI::MapDisplayControl(QPushButton *btnCtr,QWidget*Tar,QGridLayout*layout)
{
    // 创建堆栈部件
    QStackedWidget* stackedWidget = new QStackedWidget(this);
    
    // 创建背景板部件
    QWidget* backgroundWidget = new QWidget(this);
    QPixmap backgroundPixmap(":/image/res/image/MapBackGround.png");
    QLabel *backgroundLabel = new QLabel(backgroundWidget);
    backgroundLabel->setPixmap(backgroundPixmap);
    
    // 设置堆栈部件的组成
    backgroundLabel->setMaximumSize(1500,900); // 背景板的最大尺寸
    backgroundLabel->setMinimumSize(500,300); // 背景板的最小尺寸
    // 设置QLabel的缩放模式为拉伸填充
    backgroundLabel->setScaledContents(true);


    // 设置地图部件的尺寸
    Tar->setMaximumSize(1500,900);               // 地图部件的最大尺寸
    Tar->setMinimumSize(500,300);               // 地图部件的最小尺寸

    stackedWidget->addWidget(Tar);
    stackedWidget->addWidget(backgroundLabel);
    stackedWidget->setCurrentIndex(1);

    connect(btnCtr,&QPushButton::clicked,this,[=](){
        if(stackedWidget->currentIndex() == 0)
            stackedWidget->setCurrentIndex(1);
        else
            stackedWidget->setCurrentIndex(0);
    });

    // 将堆栈部件添加到布局当中
    layout->addWidget(stackedWidget);
    QWebEngineView *view = new QWebEngineView(ui->MapDisplayer);

    // 设置HTML文件的路径，这里使用资源文件系统
    QUrl url = QUrl("qrc:/html/res/html/map.html");
    view->load(url);

    // 连接loadFinished信号来确认页面是否加载成功
    connect(view, &QWebEngineView::loadFinished, [view]() {
        // 地图加载完成后的操作
        view->page()->runJavaScript("initMap();", [](const QVariant &result) {
            // 地图初始化完成后处理
        });
    });

    // 由于 MapDisplayer 已经在 UI 布局中，我们不需要创建新的布局
    // 直接将 QWebEngineView 添加到 MapDisplayer 中
    QVBoxLayout *layout1 = qobject_cast<QVBoxLayout*>(ui->MapDisplayer->layout());
    if (layout1) {
        layout1->addWidget(view);
    } else {
        // 如果 MapDisplayer 没有布局，则创建一个并添加 QWebEngineView
        QVBoxLayout *newLayout = new QVBoxLayout(ui->MapDisplayer);
        newLayout->addWidget(view);
        ui->MapDisplayer->setLayout(newLayout);
    }
}

void GuidingUI::on_addrobot_clicked()
{
    QDialog *dialog = new QDialog(this);
    dialog->setWindowTitle("添加机器人");
    dialog->setStyleSheet("QDialog { background-color: rgb(34, 43, 61); color: white; }"
                         "QLabel { color: rgb(220, 230, 242); font-size: 14px; }"
                         "QLineEdit { background-color: rgb(40, 51, 70); color: white; border: 1px solid rgba(82, 194, 242, 0.5); border-radius: 4px; padding: 5px; }"
                         "QPushButton { background-color: rgba(82, 194, 242, 0.6); color: white; border: none; border-radius: 4px; padding: 8px 16px; }"
                         "QPushButton:hover { background-color: rgba(82, 194, 242, 0.8); }"
                         "QPushButton:pressed { background-color: rgba(82, 194, 242, 1.0); }");
    
    QVBoxLayout *layout = new QVBoxLayout;

    QHBoxLayout *nameLayout = new QHBoxLayout;
    QLabel *nameLabel = new QLabel("名称(customized)：", dialog);
    QLineEdit *nameEdit = new QLineEdit(dialog);
    nameLayout->addWidget(nameLabel);
    nameLayout->addWidget(nameEdit);
    layout->addLayout(nameLayout);

    QHBoxLayout *ipLayout = new QHBoxLayout;
    QLabel *ipLabel = new QLabel("IP地址：", dialog);
    QLineEdit *ipEdit = new QLineEdit(dialog);
    ipLayout->addWidget(ipLabel);
    ipLayout->addWidget(ipEdit);
    layout->addLayout(ipLayout);

    QPushButton *okButton = new QPushButton("确定", dialog);
    layout->addWidget(okButton);

    connect(okButton, &QPushButton::clicked, dialog, &QDialog::accept);
    connect(dialog, &QDialog::accepted, [=]() {
        QString name = nameEdit->text();
        QString ip = ipEdit->text();
        
        // 保存机器人信息到列表中
        if (!name.isEmpty() && !ip.isEmpty()) {
            robotList.append(qMakePair(name, ip));
            qDebug() << "添加机器人：" << name << " - " << ip;
        }
    });

    dialog->setLayout(layout);
    dialog->exec();
}

void GuidingUI::AddRobotControl(QPushButton* btnCtr)
{
    connect(btnCtr,&QPushButton::clicked,this,&GuidingUI::on_addrobot_clicked);
}


void on_specifiedrobottab_clicked()
{
    
}


void GuidingUI::on_robottab_clicked()
{
    QDialog *dialog = new QDialog(this);
    dialog->setWindowTitle("机器人列表");
    dialog->resize(600, 400);
    dialog->setStyleSheet("QDialog { background-color: rgb(34, 43, 61); color: white; }"
                         "QLabel { color: rgb(220, 230, 242); font-size: 14px; }"
                         "QTableWidget { background-color: rgb(40, 51, 70); color: white; gridline-color: rgba(82, 194, 242, 0.3); border: none; }"
                         "QTableWidget::item { padding: 5px; }"
                         "QTableWidget::item:selected { background-color: rgba(82, 194, 242, 0.5); }"
                         "QHeaderView::section { background-color: rgb(30, 40, 60); color: rgb(220, 230, 242); padding: 5px; border: none; }"
                         "QPushButton { background-color: rgba(82, 194, 242, 0.6); color: white; border: none; border-radius: 4px; padding: 8px 16px; }"
                         "QPushButton:hover { background-color: rgba(82, 194, 242, 0.8); }"
                         "QPushButton:pressed { background-color: rgba(82, 194, 242, 1.0); }");
    
    QVBoxLayout *layout = new QVBoxLayout;
    QHBoxLayout *titleLayout = new QHBoxLayout;
    QLabel *titleLabel = new QLabel("机器人列表", dialog);
    titleLabel->setStyleSheet("font-size: 18px; font-weight: bold;");
    
    titleLayout->addWidget(titleLabel);
    layout->addLayout(titleLayout);

    // 新布局当中添加列表
    QTableWidget *tableWidget = new QTableWidget(dialog);
    QVBoxLayout *robotListLayout = new QVBoxLayout;
    robotListLayout->addWidget(tableWidget);
    layout->addLayout(robotListLayout);

    // 根据实际机器人数量设置行数
    tableWidget->setRowCount(robotList.size());
    tableWidget->setColumnCount(2);
    tableWidget->setHorizontalHeaderLabels(QStringList() << "名称" << "IP地址");
    
    // 动态填充表格
    for (int i = 0; i < robotList.size(); ++i) {
        QTableWidgetItem *nameItem = new QTableWidgetItem(robotList[i].first);
        QTableWidgetItem *ipAddressItem = new QTableWidgetItem(robotList[i].second);
        nameItem->setForeground(QBrush(QColor(220, 230, 242)));
        ipAddressItem->setForeground(QBrush(QColor(220, 230, 242)));
        tableWidget->setItem(i, 0, nameItem);
        tableWidget->setItem(i, 1, ipAddressItem);
    }
    
    /* 跳转按钮 */
    QPushButton *specified_robot_btn = new QPushButton("查看详情", dialog);     // 新的按钮跟dialog是子属关系
    QHBoxLayout *specified_robot_btnLayout = new QHBoxLayout();     // 按钮的布局
    specified_robot_btnLayout->addWidget(specified_robot_btn);      // 添加按钮到按钮布局当中
    layout->addLayout(specified_robot_btnLayout);                   // 添加按钮布局到对话框布局当中
    // connect(specified_robot_btn,&QPushButton::clicked,this,&GuidingUI::on_specifiedrobottab_clicked);   // 联系槽函数

    dialog->setLayout(layout);
    dialog->exec();
}

void GuidingUI::RobotsInfosControl(QPushButton* btnCtr)
{
    connect(btnCtr,&QPushButton::clicked,this,&GuidingUI::on_robottab_clicked);
}

// 添加无人机按钮槽函数
void GuidingUI::on_addUAV_clicked()
{
    QDialog *dialog = new QDialog(this);
    QVBoxLayout *layout = new QVBoxLayout;

    QHBoxLayout *nameLayout = new QHBoxLayout;
    QLabel *nameLabel = new QLabel("名称：", dialog);
    QLineEdit *nameEdit = new QLineEdit(dialog);
    nameLayout->addWidget(nameLabel);
    nameLayout->addWidget(nameEdit);
    layout->addLayout(nameLayout);

    QHBoxLayout *ipLayout = new QHBoxLayout;
    QLabel *ipLabel = new QLabel("IP地址：", dialog);
    QLineEdit *ipEdit = new QLineEdit(dialog);
    ipLayout->addWidget(ipLabel);
    ipLayout->addWidget(ipEdit);
    layout->addLayout(ipLayout);

    QPushButton *okButton = new QPushButton("确定", dialog);
    layout->addWidget(okButton);

    connect(okButton, &QPushButton::clicked, dialog, &QDialog::accept);
    connect(dialog, &QDialog::accepted, [=]() {
        QString name = nameEdit->text();
        QString ip = ipEdit->text();
        
        // 保存无人机信息到列表中
        if (!name.isEmpty() && !ip.isEmpty()) {
            uavList.append(qMakePair(name, ip));
            qDebug() << "添加无人机：" << name << " - " << ip;
        }
    });

    dialog->setLayout(layout);
    dialog->exec();
}

// 无人机列表显示按钮槽函数
void GuidingUI::on_UAVtab_clicked()
{
    QDialog *dialog = new QDialog(this);
    QVBoxLayout *layout = new QVBoxLayout;
    QHBoxLayout *titleLayout = new QHBoxLayout;
    QLabel *titleLabel = new QLabel("无人机列表", dialog);
    
    titleLayout->addWidget(titleLabel);
    layout->addLayout(titleLayout);

    // 添加列表
    QTableWidget *tableWidget = new QTableWidget(dialog);
    QVBoxLayout *uavListLayout = new QVBoxLayout;
    uavListLayout->addWidget(tableWidget);
    layout->addLayout(uavListLayout);

    // 根据实际无人机数量设置行数
    tableWidget->setRowCount(uavList.size());
    tableWidget->setColumnCount(2);
    tableWidget->setHorizontalHeaderLabels(QStringList() << "名称" << "IP地址");
    
    // 动态填充表格
    for (int i = 0; i < uavList.size(); ++i) {
        QTableWidgetItem *nameItem = new QTableWidgetItem(uavList[i].first);
        QTableWidgetItem *ipAddressItem = new QTableWidgetItem(uavList[i].second);
        tableWidget->setItem(i, 0, nameItem);
        tableWidget->setItem(i, 1, ipAddressItem);
    }
    
    /* 跳转按钮 */
    QPushButton *specified_uav_btn = new QPushButton("查看详情", dialog);
    QHBoxLayout *specified_uav_btnLayout = new QHBoxLayout();
    specified_uav_btnLayout->addWidget(specified_uav_btn);
    layout->addLayout(specified_uav_btnLayout);

    dialog->setLayout(layout);
    dialog->exec();
}

// 机器人位置显示按钮槽函数
void GuidingUI::on_robotlocation_clicked()
{
    // 如果存在机器人位置数据，可从数据库获取
    QMessageBox::information(this, "机器人位置", "此功能正在开发中，暂时无法使用");
}

// 战场探索系统新增的槽函数
void GuidingUI::on_UAVview_clicked()
{
    QMessageBox::information(this, "无人机视角", "无人机视角功能正在开发中，暂时无法使用");
}

void GuidingUI::on_robotView_clicked()
{
    QMessageBox::information(this, "机器狗视角", "机器狗视角功能正在开发中，暂时无法使用");
}

void GuidingUI::on_robotMapping_clicked()
{
    QMessageBox::information(this, "机器狗建图", "机器狗建图功能正在开发中，暂时无法使用");
}

void GuidingUI::on_smartNavigation_clicked()
{
    QMessageBox::information(this, "智能导航", "智能导航功能正在开发中，暂时无法使用");
}

// 人脸识别按钮点击处理函数
void GuidingUI::on_faceRecognition_clicked()
{
    // 设置QMessageBox样式
    QString messageBoxStyle = "QMessageBox {"
                             "  background-color: rgb(34, 43, 61);"
                             "  color: white;"
                             "}"
                             "QMessageBox QLabel {"
                             "  color: rgb(220, 230, 242);"
                             "  font-size: 14px;"
                             "}"
                             "QMessageBox QPushButton {"
                             "  background-color: rgba(82, 194, 242, 0.6);"
                             "  color: white;"
                             "  border: none;"
                             "  border-radius: 4px;"
                             "  padding: 8px 16px;"
                             "  min-width: 80px;"
                             "}"
                             "QMessageBox QPushButton:hover {"
                             "  background-color: rgba(82, 194, 242, 0.8);"
                             "}"
                             "QMessageBox QPushButton:pressed {"
                             "  background-color: rgba(82, 194, 242, 1.0);"
                             "}";
    
    // 检查进程是否已经在运行
    if (faceRecognitionProcess->state() == QProcess::Running) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("人脸识别程序已经在运行！");
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    // 设置Python解释器路径和脚本路径
    QString pythonPath = "/home/hzk/Enjoy/src/face-recognition-cv2-master/venv/bin/python";
    QString scriptPath = "/home/hzk/Enjoy/src/face-recognition-cv2-master/main.py";
    
    // 检查文件是否存在
    QFileInfo pythonFileInfo(pythonPath);
    QFileInfo scriptFileInfo(scriptPath);
    
    if (!pythonFileInfo.exists()) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("找不到Python解释器: " + pythonPath);
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    if (!scriptFileInfo.exists()) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("找不到人脸识别脚本: " + scriptPath);
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    // 设置工作目录
    faceRecognitionProcess->setWorkingDirectory("/home/hzk/Enjoy/src/face-recognition-cv2-master");
    
    // 启动Python脚本
    QStringList arguments;
    arguments << scriptPath;
    
    faceRecognitionProcess->start(pythonPath, arguments);
    
    // 等待进程启动
    if (!faceRecognitionProcess->waitForStarted(3000)) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("无法启动人脸识别程序: " + faceRecognitionProcess->errorString());
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    QMessageBox msgBox;
    msgBox.setWindowTitle("提示");
    msgBox.setText("人脸识别程序已启动！");
    msgBox.setStyleSheet(messageBoxStyle);
    msgBox.exec();
    
    // 连接进程结束信号
    connect(faceRecognitionProcess, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
            [this, messageBoxStyle](int exitCode, QProcess::ExitStatus exitStatus) {
        if (exitStatus == QProcess::NormalExit) {
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("人脸识别程序已正常结束，退出代码: " + QString::number(exitCode));
            msgBox.setStyleSheet(messageBoxStyle);
            msgBox.exec();
        } else {
            QMessageBox msgBox;
            msgBox.setWindowTitle("警告");
            msgBox.setText("人脸识别程序异常结束！");
            msgBox.setStyleSheet(messageBoxStyle);
            msgBox.exec();
        }
    });
}

// 人脸跟随按钮点击处理函数
void GuidingUI::on_faceTracking_clicked()
{
    // 设置QMessageBox样式
    QString messageBoxStyle = "QMessageBox {"
                             "  background-color: rgb(34, 43, 61);"
                             "  color: white;"
                             "}"
                             "QMessageBox QLabel {"
                             "  color: rgb(220, 230, 242);"
                             "  font-size: 14px;"
                             "}"
                             "QMessageBox QPushButton {"
                             "  background-color: rgba(82, 194, 242, 0.6);"
                             "  color: white;"
                             "  border: none;"
                             "  border-radius: 4px;"
                             "  padding: 8px 16px;"
                             "  min-width: 80px;"
                             "}"
                             "QMessageBox QPushButton:hover {"
                             "  background-color: rgba(82, 194, 242, 0.8);"
                             "}"
                             "QMessageBox QPushButton:pressed {"
                             "  background-color: rgba(82, 194, 242, 1.0);"
                             "}";
    
    // 检查进程是否已经在运行
    if (faceTrackingProcess->state() == QProcess::Running) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("人脸跟随程序已经在运行！");
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    // 设置Python解释器路径和脚本路径
    QString pythonPath = "/home/hzk/Enjoy/src/face-recognition-cv2-master/venv/bin/python";
    QString scriptPath = "/home/hzk/Enjoy/src/yolov3_sort/main.py";
    
    // 检查文件是否存在
    QFileInfo pythonFileInfo(pythonPath);
    QFileInfo scriptFileInfo(scriptPath);
    
    if (!pythonFileInfo.exists()) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("找不到Python解释器: " + pythonPath);
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    if (!scriptFileInfo.exists()) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("找不到人脸跟随脚本: " + scriptPath);
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    // 设置工作目录
    faceTrackingProcess->setWorkingDirectory("/home/hzk/Enjoy/src/yolov3_sort");
    
    // 启动Python脚本
    QStringList arguments;
    arguments << scriptPath;
    
    faceTrackingProcess->start(pythonPath, arguments);
    
    // 等待进程启动
    if (!faceTrackingProcess->waitForStarted(3000)) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("无法启动人脸跟随程序: " + faceTrackingProcess->errorString());
        msgBox.setStyleSheet(messageBoxStyle);
        msgBox.exec();
        return;
    }
    
    QMessageBox msgBox;
    msgBox.setWindowTitle("提示");
    msgBox.setText("人脸跟随程序已启动！按'q'键可退出跟随窗口");
    msgBox.setStyleSheet(messageBoxStyle);
    msgBox.exec();
    
    // 连接进程输出信号，可以在控制台显示Python脚本的输出
    connect(faceTrackingProcess, &QProcess::readyReadStandardOutput, [this]() {
        QByteArray output = faceTrackingProcess->readAllStandardOutput();
        qDebug() << "人脸跟随输出:" << output;
    });
    
    connect(faceTrackingProcess, &QProcess::readyReadStandardError, [this]() {
        QByteArray error = faceTrackingProcess->readAllStandardError();
        qDebug() << "人脸跟随错误:" << error;
    });
    
    // 连接进程结束信号
    connect(faceTrackingProcess, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
            [this, messageBoxStyle](int exitCode, QProcess::ExitStatus exitStatus) {
        if (exitStatus == QProcess::NormalExit) {
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("人脸跟随程序已正常结束，退出代码: " + QString::number(exitCode));
            msgBox.setStyleSheet(messageBoxStyle);
            msgBox.exec();
        } else {
            QMessageBox msgBox;
            msgBox.setWindowTitle("警告");
            msgBox.setText("人脸跟随程序异常结束！");
            msgBox.setStyleSheet(messageBoxStyle);
            msgBox.exec();
        }
    });
}

