#include "MainAndSecondaryCamerasWnd.h"
#include "ui_MainAndSecondaryCamerasWnd.h"
#include <QSettings>
#include <QCloseEvent>
#include <QVBoxLayout>
#include <QTimer>
#include <QScrollBar>
#include <QDoubleValidator>
#include "CameraDistribute.h"
#include "Src/common/JMessageTip.h"


MainAndSecondaryCamerasWnd::MainAndSecondaryCamerasWnd(QWidget *parent)
    : JOriginalMainWnd(parent)
    , ui(new Ui::MainAndSecondaryCamerasWnd)
{
    ui->setupUi(this);

    QSettings settings("YourOrganization", "YourApplication");
    settings.beginGroup("LastSettings");
    m_lastRightPixmap = settings.value("LastRightPixmap").value<QPixmap>();
    lasttextList = settings.value("LasttextList").toStringList();
    lastLastRightPixmap = settings.value("LastLastRightPixmap").value<QPixmap>();
    lastLasttextList = settings.value("LastLasttextList").toStringList();
    lastLastIndex = settings.value("LastLastIndex", 0).toInt();
    lastLastGroupId = settings.value("LastLastGroupId", 0).toInt();
    settings.endGroup();


    ui->control_r->HideOrShowPage(false);
    ui->control_L->HideOrShowPage(false);
    //initFrom();
    qApp->installEventFilter(this);
}

MainAndSecondaryCamerasWnd::~MainAndSecondaryCamerasWnd()
{
    m_pMainCameraBind = nullptr;

    delete ui;
}

void MainAndSecondaryCamerasWnd::initFrom()
{
    ui->Leftlabel_Percentage->setAlignment(Qt::AlignCenter);
    ui->Rightlabel_Percentage->setAlignment(Qt::AlignCenter);

    connect(ui->LeftOperatewidget,&ImageWidget::sendDoubleClicksignal,this,&MainAndSecondaryCamerasWnd::handleDoubleClick);
    connect(ui->RightOperatewidget,&ImageWidget::sendDoubleClicksignal,this,&MainAndSecondaryCamerasWnd::handleDoubleClick);
    
    {
        // 界面刷新
        InitPage();
        //STR_TEMPLATE strFun = {};
        //strFun.pView = ui->scrollArea;
        //strFun.veSignalFun.emplace_back(std::bind(&MainAndSecondaryCamerasWnd::UpdateCameraDisplay0, this, std::placeholders::_1, std::placeholders::_2));
        //strFun.veSignalFun.emplace_back(std::bind(&MainAndSecondaryCamerasWnd::UpdateCameraDisplay1, this, std::placeholders::_1, std::placeholders::_2));
        ////strFun.veSlotFun.emplace_back([this]() { CheckIs(); });
        //strFun.veSlotFun.emplace_back(std::bind(&MainAndSecondaryCamerasWnd::CheckIs, this));

        //InitAllCamerPage(strFun, this);

        //CameraImageHandlera<MainAndSecondaryCamerasWnd, STR_TEMPLATE> A(this, strFun);

        //SwitchToConfigAAAAA<MainAndSecondaryCamerasWnd, STR_TEMPLATE>();

    }
    
    ui->LeftOperatewidget->setMouseTracking(true);
    ui->RightOperatewidget->setMouseTracking(true);

    QSettings settings2("YourOrganization", "YourApplication_lastWndType");
    int lastWndType = settings2.value("LastWndType").toInt();
    int LastGroup = settings2.value("LastGroup").toInt();
    lastIndex = lastWndType;
    lastGroupId = LastGroup;


    QTimer* timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &MainAndSecondaryCamerasWnd::checkSettings);
    timer->start(100);

    InitModulesShow();
}

void MainAndSecondaryCamerasWnd::InitPage()
{
    QWidget* viewport = ui->scrollArea->viewport();
    QWidget* container = new QWidget(viewport);
    QHBoxLayout* mainLayout = new QHBoxLayout(container);

    mainLayout->setSpacing(28);
    mainLayout->setMargin(0);

    int nSize = 0;
    if (m_pMainCameraBind != nullptr)
    {
        nSize = m_pMainCameraBind->m_vecCamera.size();
        if (nSize == 0) // 针对获取不到的情况
        {
            //nSize = 5;
            JMessageTip::Message_warning("a");
        }
        QList<int> numbers;
        for (int i = 1; i <= nSize; i++)
        {
            numbers.push_back(i);
        }

        for (int i = 0; i < numbers.size(); ++i)
        {
            int num = numbers[i];
            CameraImageHandler* manager = new CameraImageHandler(num);
            Group* widget = manager->getGroup();
            CameraConnectUpdateImageFun(i, widget);

            if (widget != nullptr)
            {
                mainLayout->addWidget(widget);
                m_currentSelectGroup.liGroup.append(widget);
            }
            if (manager->getWafer())
            {
                waferMap.insert(num, manager->getWafer());
            }
            if (manager->getWafer()) {
                waffleMap.insert(num, manager->getWaffle());
            }
            if (manager->getMaterialBox()) {
                materialBoxMap.insert(num, manager->getMaterialBox());
            }

            delete manager;
        }

        // 设置控件的最小高度和最大宽度
        int minHeight = 162;
        int maxWidth = 244;
        int num = -28;
        for (Group* w : m_currentSelectGroup.liGroup)
        {
            w->setMinimumHeight(minHeight);
            w->setMaximumWidth(maxWidth);
            num = num + 272;
        }

        container->setMinimumSize(num, 162);
        container->setLayout(mainLayout);
        ui->scrollArea->setWidget(container);
        ui->scrollArea->resize(1062, 177);


        // 只有一个相机的情况,直接绑定
        if (numbers.size() == 1)
        {
            Group* pGroup = m_currentSelectGroup.liGroup[0];
            connect(pGroup, &Group::SetUpDataImageShowSig,
                this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals0);

            ST_CURRENT_SELECT_GROUP _curr;
            _curr.isBond = true;
            _curr.pSelectGroup = pGroup;
            _curr.nCurrentCameraId = pGroup->m_nGroupId;
            _curr.nSelectLeft1 = 1;

            m_veCurrentSelectGroup.push_back(_curr);

            UpdataLightJoystickSwitchPage(pGroup);
        }
    } 
}

void MainAndSecondaryCamerasWnd::CameraConnectUpdateImageFun(int nIndex, Group* widget)
{    
    auto BinCamerasImage = [&](void (MainAndSecondaryCamerasWnd::* pCamerasImage)(const QPixmap& imageData))
    {
        //共用函数
        //connect(widget, &Group::send_button_Signal, this, &MainAndSecondaryCamerasWnd::CheckIs);
        connect(widget, &Group::SetCurrentSelectSig, this, &MainAndSecondaryCamerasWnd::GetCurrentSelectSlots);

        // 相机图片
        connect(this, pCamerasImage, widget, &Group::UpDataImageShowSlots);
    };

    if (nIndex == 0)
    {
        BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals0);  
    }
    else if (nIndex == 1)
    {
        BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals1);
    }
    else if (nIndex == 2)
    {
        BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals2);
    }
    else if (nIndex == 3)
    {
        BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals3);
    }
    else if (nIndex == 4)
    {
        BinCamerasImage(&MainAndSecondaryCamerasWnd::UpDataImageShowSignals4);
    }
}

void MainAndSecondaryCamerasWnd::InitModulesShow()
{
    if (m_pMainCameraBind != nullptr)
    {
        DeduplicationBox(ui->axisComboBox, m_pMainCameraBind->m_vecCAxis, 0);
        DeduplicationBox(ui->axisTypeComboBox, m_pMainCameraBind->m_vecCAxis, 1);
    }
}

template<class T>
void MainAndSecondaryCamerasWnd::DeduplicationBox(QComboBox* pCom, const T& veTemp, int nIndex)
{
    QStringList items;
    for (int i = 0; i < pCom->count(); ++i)
    {
        items << pCom->itemText(i);
    }

    for (auto& a : veTemp)
    {
        QString strName;
        if (nIndex == 0)
        {
            strName = a->GetModuleType().c_str();
        }
        else if (nIndex == 1)
        {
            strName = a->GetStringAxisType().c_str();
        }
       
        bool bMa = false; // 是否匹配
        for (auto b : items)
        {
            if (b == strName)
            {
                bMa = true;
                break;
            }
        }

        if (!bMa)
        {
            pCom->addItem(strName);
        }
    }
}

void MainAndSecondaryCamerasWnd::handleDoubleClick(){
    QPoint globalMousePos = QCursor::pos();  // 获取当前鼠标的全局位置
    QPoint leftWidgetLocalPos = ui->LeftOperatewidget->mapFromGlobal(globalMousePos);
    QPoint rightWidgetLocalPos = ui->RightOperatewidget->mapFromGlobal(globalMousePos);
    if (ui->LeftOperatewidget->rect().contains(leftWidgetLocalPos)){
        if (Left_currentMode == ModeImage) {
            QPixmap scaledImage = Left_currentPixmap.scaled(Left_currentPixmap.width(), Left_currentPixmap.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            ui->LeftOperatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
        } else if (Left_currentMode == ModeView && Left_currentView) {
            QTransform transform;
            transform.scale(1, 1);
            Left_currentView->setTransform(transform);
        }
        Left_scaleFactor = 1.0;
        Left_previousScaleFactor = 1.0;
        ui->Leftlabel_Percentage->setText("100%");
    }
    if(ui->RightOperatewidget->rect().contains(rightWidgetLocalPos)){
        if (Right_currentMode == ModeImage) {
            QPixmap scaledImage = Right_currentPixmap.scaled(Right_currentPixmap.width(), Right_currentPixmap.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            ui->RightOperatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
        } else if (Right_currentMode == ModeView && Right_currentView) {
            QTransform transform;
            transform.scale(1, 1);
            Right_currentView->setTransform(transform);
        }
        Right_scaleFactor = 1.0;
        Right_previousScaleFactor = 1.0;
        ui->Rightlabel_Percentage->setText("100%");
    }


}

void MainAndSecondaryCamerasWnd::checkSettings() {
    QSettings settings("YourCompany", "YourApplication_");
    int groupId = settings.value("GroupId", 0).toInt();
    int index = settings.value("Index", 0).toInt();

    if (groupId != lastGroupId || index != lastIndex) {
        loadSettings();
        lastGroupId = groupId;
        lastIndex = index;
    }
}

void MainAndSecondaryCamerasWnd::loadSettings()
{
    QSettings settings("YourCompany", "YourApplication_");
    int groupId = settings.value("GroupId", 0).toInt();
    int index = settings.value("Index", 0).toInt();

    //TODO: 这个地方目前暂停使用
    loadGroupSettings(groupId, index);
}

void MainAndSecondaryCamerasWnd::loadGroupSettings(int Id, int Index) {
    // isUpdatingSettings = true;
    QSettings settings("YourOrganization", "YourApplication");
    settings.beginGroup(QString::number(Id));
    QString imagePath1 = settings.value("ImagePath1").toString();
    int materialWndType = settings.value("MaterialWndType").toInt();
    QStringList textList = settings.value("TextList").toStringList();
    settings.endGroup();
    QSettings Last_settings("YourOrganization", "YourApplication");
    settings.beginGroup(QString::number(lastGroupId));
    QString Last_imagePath1 = settings.value("ImagePath1").toString();
    int Last_materialWndType = settings.value("MaterialWndType").toInt();
    QStringList Last_textList = settings.value("TextList").toStringList();
    settings.endGroup();
    QString lastRightImage = settings.value("LastRightImage").toString();

    QSize size_left = ui->LeftOperatewidget->size();
    QSize size_right = ui->RightOperatewidget->size();

    QPixmap newPixmap;
	QPixmap Last_newPixmap;

    if (Index == 1) {
        //newPixmap = QPixmap(imagePath1);
        clearLayout(1);
        newPixmap = QPixmap(imagePath1);
        QPixmap scaledPixmap = newPixmap.scaled(size_right, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        Right_currentMode = ModeImage;
        Right_currentPixmap = scaledPixmap;
        Right_scaleFactor = 1.0;
        Right_previousScaleFactor = 1.0;
        ui->RightOperatewidget->setPixmap(scaledPixmap);
        double percentage = Right_scaleFactor * 100;
        QString percentageStr = QString::number((int)percentage);
        ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));

        if (lastIndex == 1) {
            clearLayout(0);
            Last_newPixmap = QPixmap(Last_imagePath1);
            QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            Left_currentMode = ModeImage;
            Left_currentPixmap = scaledPixmap;
            Left_scaleFactor = 1.0;
            Left_previousScaleFactor = 1.0;
            ui->LeftOperatewidget->setPixmap(scaledPixmap);
            double percentage = Left_scaleFactor * 100;
            QString percentageStr = QString::number((int)percentage);
            ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
        }
        else if (lastIndex == 2) {

            clearLayout(0);
            if (Last_materialWndType == 1) {
                ui->LeftOperatewidget->clearPixmap();
                QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                layout->setContentsMargins(0, 0, 0, 0);
                layout->addWidget(waferMap.value(lastGroupId)->view);
                ui->LeftOperatewidget->setLayout(layout);
                Left_currentMode = ModeView;
                Left_currentView = waferMap.value(lastGroupId)->view;
                Left_scaleFactor = 1.0;
                applyScale(0);
            }
            else if (Last_materialWndType == 2) {
                ui->LeftOperatewidget->clearPixmap();
                QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                waffleMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                layout->setContentsMargins(0, 0, 0, 0);
                layout->addWidget(waffleMap.value(lastGroupId)->view);
                ui->LeftOperatewidget->setLayout(layout);
                Left_currentMode = ModeView;
                Left_currentView = waffleMap.value(lastGroupId)->view;
                Left_scaleFactor = 1.0;
                applyScale(0);
            }
            else if (Last_materialWndType == 3) {
                ui->LeftOperatewidget->clearPixmap();
                QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                materialBoxMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);
                layout->setContentsMargins(0, 0, 0, 0);
                layout->addWidget(materialBoxMap.value(lastGroupId)->view);
                ui->LeftOperatewidget->setLayout(layout);
                Left_currentMode = ModeView;
                Left_currentView = materialBoxMap.value(lastGroupId)->view;
                Left_scaleFactor = 1.0;
                applyScale(0);
            }
        }
    } else if (Index == 2) {
        clearLayout(1);
        clearLayout(0);

        if (lastIndex == 1) {
        }

        if (materialWndType == 1) {
		
            if (lastIndex == 1) {
                Last_newPixmap = QPixmap(Last_imagePath1);
                QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                Left_currentMode = ModeImage;
                Left_currentPixmap = scaledPixmap;
                Left_scaleFactor = 1.0;
                Left_previousScaleFactor = 1.0;
                ui->LeftOperatewidget->setPixmap(scaledPixmap);
                double percentage = Left_scaleFactor * 100;
                QString percentageStr = QString::number((int)percentage);
                ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
            } else if (lastIndex == 2) {
                if (Last_materialWndType == 1) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout *layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waferMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waferMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                } else if (Last_materialWndType == 2) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waffleMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waffleMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waffleMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                } else if (Last_materialWndType == 3) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    materialBoxMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);
                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(materialBoxMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = materialBoxMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                }
            }
            ui->RightOperatewidget->clearPixmap();
            QVBoxLayout *layout = new QVBoxLayout(ui->RightOperatewidget);
            waferMap.value(Id)->initFrom(ui->RightOperatewidget);

            layout->setContentsMargins(0, 0, 0, 0);
            layout->addWidget(waferMap.value(Id)->view);
            ui->RightOperatewidget->setLayout(layout);
            Right_currentMode = ModeView;
            Right_currentView = waferMap.value(Id)->view;
            Right_scaleFactor = 1.0;
            applyScale(1);
        } else if (materialWndType == 2) {
            if (lastIndex == 1) {
                Last_newPixmap = QPixmap(Last_imagePath1);
                QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                Left_currentMode = ModeImage;
                Left_currentPixmap = scaledPixmap;
                Left_scaleFactor = 1.0;
                Left_previousScaleFactor = 1.0;
                ui->LeftOperatewidget->setPixmap(scaledPixmap);
                double percentage = Left_scaleFactor * 100;
                QString percentageStr = QString::number((int)percentage);
                ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
            } else if (lastIndex == 2) {
                if (Last_materialWndType == 1) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout *layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waferMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waferMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                } else if (Last_materialWndType == 2) {
                    ui->LeftOperatewidget->clearPixmap();
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waffleMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waffleMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waffleMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                } else if (Last_materialWndType == 3) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    materialBoxMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);
                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(materialBoxMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = materialBoxMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                }
            }
            
            ui->RightOperatewidget->clearPixmap();
            QVBoxLayout* layout = new QVBoxLayout(ui->RightOperatewidget);
            waffleMap.value(Id)->initFrom(ui->RightOperatewidget);

            layout->setContentsMargins(0, 0, 0, 0);
            layout->addWidget(waffleMap.value(Id)->view);
            ui->RightOperatewidget->setLayout(layout);
            Right_currentMode = ModeView;
            Right_currentView = waffleMap.value(Id)->view;
            Right_scaleFactor = 1.0;
        } else if (materialWndType == 3) {
            if (lastIndex == 1) {
                Last_newPixmap = QPixmap(Last_imagePath1);
                QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                Left_currentMode = ModeImage;
                Left_currentPixmap = scaledPixmap;
                Left_scaleFactor = 1.0;
                Left_previousScaleFactor = 1.0;
                ui->LeftOperatewidget->setPixmap(scaledPixmap);
                double percentage = Left_scaleFactor * 100;
                QString percentageStr = QString::number((int)percentage);
                ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
            } else if (lastIndex == 2) {
                if (Last_materialWndType == 1) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout *layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waferMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waferMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                } else if (Last_materialWndType == 2) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waffleMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waffleMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waffleMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                } else if (Last_materialWndType == 3) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    materialBoxMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);
                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(materialBoxMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = materialBoxMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                }
            }
            
            ui->RightOperatewidget->clearPixmap();
            QVBoxLayout* layout = new QVBoxLayout(ui->RightOperatewidget);
            materialBoxMap.value(Id)->initFrom(ui->RightOperatewidget);
            layout->setContentsMargins(0, 0, 0, 0);
            layout->addWidget(materialBoxMap.value(Id)->view);
            ui->RightOperatewidget->setLayout(layout);
            Left_currentMode = ModeView;
            Left_currentView = materialBoxMap.value(Id)->view;
            Left_scaleFactor = 1.0;
            applyScale(0);
        }
    }

    QSettings settings2("YourOrganization", "YourApplication_lastWndType");
    settings2.setValue("LastWndType", lastIndex);
    settings2.setValue("LastGroup", lastGroupId);

    QPixmap scaledPixmap_right = newPixmap.scaled(size_right, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->RightOperatewidget->setPixmap(scaledPixmap_right);

    QPixmap scaledPixmap_left = m_lastRightPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->LeftOperatewidget->setPixmap(scaledPixmap_left);

    // 更新当前图片的成员变量
    // Left_currentPixmap = scaledPixmap_left;
    // Left_scaleFactor = 1.0;
    // Right_currentPixmap = scaledPixmap_right;
    // Right_scaleFactor = 1.0;
    // ui->Leftlabel_Percentage->setText("100%");
    // ui->Rightlabel_Percentage->setText("100%");

    // ui->LeftDataSources->clear();
    // ui->LeftDataSources->addItems(lasttextList);
    // ui->RightDataSources->clear();
    // ui->RightDataSources->addItems(textList);

    // 保存到 QSettings
    // settings.beginGroup("LastSettings");
    // settings.setValue("LastRightPixmap", lastRightPixmap);
    // settings.setValue("LasttextList", lasttextList);
    // settings.setValue("LastLastRightPixmap", lastLastRightPixmap);
    // settings.setValue("LastLasttextList", lastLasttextList);
    // settings.setValue("LastLastIndex", lastLastIndex);
    // settings.setValue("LastLastGroupId", lastLastGroupId);
    // settings.endGroup();

    if (isUpdatingSettings) {
        lastLastRightPixmap = m_lastRightPixmap;
        lastLasttextList = lasttextList;
        m_lastRightPixmap = scaledPixmap_right;
        lasttextList = textList;
        lastLastIndex = lastIndex;
        lastLastGroupId = lastGroupId;
    }
}

void MainAndSecondaryCamerasWnd::UpdataGroupSettings(const QPixmap& imageData)
{
    int  Id = m_currentSelectGroup.nGroupId;
    int Index = m_currentSelectGroup.nSelectLeft1;

    // isUpdatingSettings = true;
    QSettings settings("YourOrganization", "YourApplication");
    settings.beginGroup(QString::number(Id));
    QString imagePath1 = settings.value("ImagePath1").toString();
    int materialWndType = settings.value("MaterialWndType").toInt();
    QStringList textList = settings.value("TextList").toStringList();
    settings.endGroup();
    QSettings Last_settings("YourOrganization", "YourApplication");
    settings.beginGroup(QString::number(lastGroupId));
    QString Last_imagePath1 = settings.value("ImagePath1").toString();
    int Last_materialWndType = settings.value("MaterialWndType").toInt();
    QStringList Last_textList = settings.value("TextList").toStringList();
    settings.endGroup();
    QString lastRightImage = settings.value("LastRightImage").toString();

    QSize size_left = ui->LeftOperatewidget->size();
    QSize size_right = ui->RightOperatewidget->size();

    QPixmap newPixmap;
    QPixmap Last_newPixmap;

    if (Index == 1) {

        //newPixmap = QPixmap(imagePath1);
        clearLayout(1);
        newPixmap = QPixmap(imageData);
        QPixmap scaledPixmap = newPixmap.scaled(size_right, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        Right_currentMode = ModeImage;
        Right_currentPixmap = scaledPixmap;
        Right_scaleFactor = 1.0;
        Right_previousScaleFactor = 1.0;
        ui->RightOperatewidget->setPixmap(scaledPixmap);
        double percentage = Right_scaleFactor * 100;
        QString percentageStr = QString::number((int)percentage);
        ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));

        if (lastIndex == 1) {
            clearLayout(0);
            Last_newPixmap = QPixmap(imageData);
            QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            Left_currentMode = ModeImage;
            Left_currentPixmap = scaledPixmap;
            Left_scaleFactor = 1.0;
            Left_previousScaleFactor = 1.0;
            ui->LeftOperatewidget->setPixmap(scaledPixmap);
            double percentage = Left_scaleFactor * 100;
            QString percentageStr = QString::number((int)percentage);
            ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
        }
        else if (lastIndex == 2) {
            clearLayout(0);
            if (Last_materialWndType == 1) {
                ui->LeftOperatewidget->clearPixmap();
                QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                layout->setContentsMargins(0, 0, 0, 0);
                layout->addWidget(waferMap.value(lastGroupId)->view);
                ui->LeftOperatewidget->setLayout(layout);
                Left_currentMode = ModeView;
                Left_currentView = waferMap.value(lastGroupId)->view;
                Left_scaleFactor = 1.0;
                applyScale(0);
            }
            else if (Last_materialWndType == 2) {
                WaffleWidget(0);
            }
            else if (Last_materialWndType == 3) {
                MaterialBoxWidget(0);
            }
        }
    }
    else if (Index == 2) {
        clearLayout(1);
        clearLayout(0);

        if (lastIndex == 1) {
        }

        if (materialWndType == 1) {

            if (lastIndex == 1) {
                Last_newPixmap = QPixmap(Last_imagePath1);
                QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                Left_currentMode = ModeImage;
                Left_currentPixmap = scaledPixmap;
                Left_scaleFactor = 1.0;
                Left_previousScaleFactor = 1.0;
                ui->LeftOperatewidget->setPixmap(scaledPixmap);
                double percentage = Left_scaleFactor * 100;
                QString percentageStr = QString::number((int)percentage);
                ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
            }
            else if (lastIndex == 2) 
            {
                if (Last_materialWndType == 1) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waferMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waferMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                }
                else if (Last_materialWndType == 2) {
                    WaffleWidget(0);
                }
                else if (Last_materialWndType == 3) {
                    MaterialBoxWidget(0);
                }
            }
            ui->RightOperatewidget->clearPixmap();
            QVBoxLayout* layout = new QVBoxLayout(ui->RightOperatewidget);
            waferMap.value(Id)->initFrom(ui->RightOperatewidget);

            layout->setContentsMargins(0, 0, 0, 0);
            layout->addWidget(waferMap.value(Id)->view);
            ui->RightOperatewidget->setLayout(layout);
            Right_currentMode = ModeView;
            Right_currentView = waferMap.value(Id)->view;
            Right_scaleFactor = 1.0;
            applyScale(1);
        }
        else if (materialWndType == 2) {
            if (lastIndex == 1) {
                Last_newPixmap = QPixmap(Last_imagePath1);
                QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                Left_currentMode = ModeImage;
                Left_currentPixmap = scaledPixmap;
                Left_scaleFactor = 1.0;
                Left_previousScaleFactor = 1.0;
                ui->LeftOperatewidget->setPixmap(scaledPixmap);
                double percentage = Left_scaleFactor * 100;
                QString percentageStr = QString::number((int)percentage);
                ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
            }
            else if (lastIndex == 2) {
                if (Last_materialWndType == 1) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waferMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waferMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                }
                else if (Last_materialWndType == 2) {
                    WaffleWidget(0);
                }
                else if (Last_materialWndType == 3) {
                    MaterialBoxWidget(0);
                }
            }

            WaffleWidget(1);
        }
        else if (materialWndType == 3) {
            if (lastIndex == 1) {
                Last_newPixmap = QPixmap(Last_imagePath1);
                QPixmap scaledPixmap = Last_newPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                Left_currentMode = ModeImage;
                Left_currentPixmap = scaledPixmap;
                Left_scaleFactor = 1.0;
                Left_previousScaleFactor = 1.0;
                ui->LeftOperatewidget->setPixmap(scaledPixmap);
                double percentage = Left_scaleFactor * 100;
                QString percentageStr = QString::number((int)percentage);
                ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
            }
            else if (lastIndex == 2) {
                if (Last_materialWndType == 1) {
                    ui->LeftOperatewidget->clearPixmap();
                    QVBoxLayout* layout = new QVBoxLayout(ui->LeftOperatewidget);
                    waferMap.value(lastGroupId)->initFrom(ui->LeftOperatewidget);

                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->addWidget(waferMap.value(lastGroupId)->view);
                    ui->LeftOperatewidget->setLayout(layout);
                    Left_currentMode = ModeView;
                    Left_currentView = waferMap.value(lastGroupId)->view;
                    Left_scaleFactor = 1.0;
                    applyScale(0);
                }
                else if (Last_materialWndType == 2) {
                    WaffleWidget(0);
                }
                else if (Last_materialWndType == 3) {
                    MaterialBoxWidget(0);
                }
            }

            MaterialBoxWidget(1);
        }
    }

    QSettings settings2("YourOrganization", "YourApplication_lastWndType");
    settings2.setValue("LastWndType", lastIndex);
    settings2.setValue("LastGroup", lastGroupId);

    // 新的是加载进来的
    QPixmap scaledPixmap_right = newPixmap.scaled(size_right, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->RightOperatewidget->setPixmap(scaledPixmap_right);

    // 旧的图像直接放到界面上?
    QPixmap scaledPixmap_left = m_lastRightPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->LeftOperatewidget->setPixmap(scaledPixmap_left);

    // 更新当前图片的成员变量
    // Left_currentPixmap = scaledPixmap_left;
    // Left_scaleFactor = 1.0;
    // Right_currentPixmap = scaledPixmap_right;
    // Right_scaleFactor = 1.0;
    // ui->Leftlabel_Percentage->setText("100%");
    // ui->Rightlabel_Percentage->setText("100%");

    // ui->LeftDataSources->clear();
    // ui->LeftDataSources->addItems(lasttextList);
    // ui->RightDataSources->clear();
    // ui->RightDataSources->addItems(textList);

    // 保存到 QSettings
    // settings.beginGroup("LastSettings");
    // settings.setValue("LastRightPixmap", lastRightPixmap);
    // settings.setValue("LasttextList", lasttextList);
    // settings.setValue("LastLastRightPixmap", lastLastRightPixmap);
    // settings.setValue("LastLasttextList", lastLasttextList);
    // settings.setValue("LastLastIndex", lastLastIndex);
    // settings.setValue("LastLastGroupId", lastLastGroupId);
    // settings.endGroup();

    if (isUpdatingSettings) {
        lastLastRightPixmap = m_lastRightPixmap;
        lastLasttextList = lasttextList;
        m_lastRightPixmap = scaledPixmap_right;
        lasttextList = textList;
        lastLastIndex = lastIndex;
        lastLastGroupId = lastGroupId;
    }
}

void MainAndSecondaryCamerasWnd::clearLayout(int flag) {
    QWidget* widget = (flag == 1) ? ui->RightOperatewidget : ui->LeftOperatewidget;
    QLayout* layout = widget->layout();

    if (layout) {
        QLayoutItem *child;
        while ((child = layout->takeAt(0)) != nullptr) {
            if (child->widget() != nullptr) {
                delete child->widget();  // 删除控件
            }
            delete child;  // 删除布局项
        }
        delete layout;  // 删除布局本身
    }
}

// 圆晶
void MainAndSecondaryCamerasWnd::WaferWidget(int flag) {
    // QWidget *operatewidget = ui->LeftOperatewidget;

    // if (flag == 1) {
    //     operatewidget = ui->RightOperatewidget;
    // }

    // QGridLayout *layout = new QGridLayout(operatewidget);
    // wafer = new Wafer(1, operatewidget);
    // wafer->initFrom(operatewidget);

    // layout->setContentsMargins(0, 0, 0, 0);
    // layout->addWidget(wafer->globalWidget);
    // operatewidget->setLayout(layout);
    // operatewidget->setFixedSize(476, 476);
}

// 华夫盒
void MainAndSecondaryCamerasWnd::WaffleWidget(int flag) {
    QWidget *operatewidget = ui->LeftOperatewidget;

    if (flag == 1) {
        operatewidget = ui->RightOperatewidget;
    }

    QVBoxLayout *layout = new QVBoxLayout(operatewidget);
    waffle = new Waffle(1, operatewidget);

    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(waffle);
    operatewidget->setLayout(layout);
    operatewidget->setFixedSize(476, 476);
}

// 料盒
void MainAndSecondaryCamerasWnd::MaterialBoxWidget(int flag) {
    QWidget *operatewidget = ui->LeftOperatewidget;

    if (flag == 1) {
        operatewidget = ui->RightOperatewidget;
    }

    QVBoxLayout *layout = new QVBoxLayout(operatewidget);
    materialbox = new MaterialBox(1, operatewidget);

    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(materialbox);
    operatewidget->setLayout(layout);
    operatewidget->setFixedSize(476, 476);
}

void MainAndSecondaryCamerasWnd::InitMainCameraBind(CameraBind* pCameraBind)
{
    m_pMainCameraBind = pCameraBind;
    initFrom();

    m_nTimeShowPos = startTimer(600);
    ResetIdleTimer(true);
}

void MainAndSecondaryCamerasWnd::UpdateCameraDisplay0(int iCameraId, JVision::ImageInfo imageData)
{
    // TODO: 这里需要改动
    QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
    ui->LeftOperatewidget->setGeometry(ui->LeftOperatewidget->pos().x(),
        ui->LeftOperatewidget->pos().y(),
        ui->LeftOperatewidget->width(),
        ui->LeftOperatewidget->height());
    //ui->LeftOperatewidget->setPixmap(QPixmap::fromImage(image));
    emit UpDataImageShowSignals0(QPixmap::fromImage(image));
}

void MainAndSecondaryCamerasWnd::UpdateCameraDisplay1(int iCameraId, JVision::ImageInfo imageData)
{
    QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
    ui->RightOperatewidget->setGeometry(ui->RightOperatewidget->pos().x(),
        ui->RightOperatewidget->pos().y(),
        ui->RightOperatewidget->width(),
        ui->RightOperatewidget->height());
    //ui->RightOperatewidget->setPixmap(QPixmap::fromImage(image));
    emit UpDataImageShowSignals1(QPixmap::fromImage(image));
}

void MainAndSecondaryCamerasWnd::UpdateCameraDisplay2(int iCameraId, JVision::ImageInfo imageData)
{

}

void MainAndSecondaryCamerasWnd::UpdateCameraDisplay3(int iCameraId, JVision::ImageInfo imageData)
{

}

void MainAndSecondaryCamerasWnd::UpdateCameraDisplay4(int iCameraId, JVision::ImageInfo imageData)
{
    QImage image(imageData.data, imageData.width, imageData.height, QImage::Format_Indexed8);
    ui->RightOperatewidget->setGeometry(ui->RightOperatewidget->pos().x(),
        ui->RightOperatewidget->pos().y(),
        ui->RightOperatewidget->width(),
        ui->RightOperatewidget->height());
    //ui->RightOperatewidget->setPixmap(QPixmap::fromImage(image));
    emit UpDataImageShowSignals4(QPixmap::fromImage(image));
}

// 更新缩放比例
void MainAndSecondaryCamerasWnd::updateScale(double newScaleFactor, int flag) {
    if (flag == 0) {
        if (newScaleFactor >= 1.0) {
            Left_scaleFactor = newScaleFactor;
        } else {
            Left_scaleFactor = 1.0; // 最小缩放比例为 1.0
        }
    } else if (flag == 1) {
        if (newScaleFactor >= 1.0) {
            Right_scaleFactor = newScaleFactor;
        } else {
            Right_scaleFactor = 1.0; // 最小缩放比例为 1.0
        }
    }

    applyScale(flag); // 应用缩放
}

// 应用缩放
void MainAndSecondaryCamerasWnd::applyScale(int flag) {
    if (flag == 0) {
        if (Left_currentMode == ModeImage) {
            // 图片模式:缩放图片
            int newWidth = Left_currentPixmap.width() * Left_scaleFactor;
            int newHeight = Left_currentPixmap.height() * Left_scaleFactor;

            QPixmap scaledImage = Left_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            ui->LeftOperatewidget->setPixmapAndPoint(scaledImage, Left_previousScaleFactor, Left_scaleFactor, mousePos);
            Left_previousScaleFactor = Left_scaleFactor;
        } else if (Left_currentMode == ModeView && Left_currentView) {
            // View 模式:缩放 view
            QTransform transform;
            transform.scale(Left_scaleFactor, Left_scaleFactor);
            Left_currentView->setTransform(transform);
        }

        // 更新百分比显示
        double percentage = Left_scaleFactor * 100;
        QString percentageStr = QString::number((int)percentage);
        ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
    } else if (flag == 1) {
        if (Right_currentMode == ModeImage) {
            // 图片模式:缩放图片
            int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
            int newHeight = Right_currentPixmap.height() * Right_scaleFactor;

            QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            ui->RightOperatewidget->setPixmapAndPoint(scaledImage, Right_previousScaleFactor, Right_scaleFactor, mousePos);
            Right_previousScaleFactor = Right_scaleFactor;
        } else if (Right_currentMode == ModeView && Right_currentView) {
            // View 模式:缩放 view
            QTransform transform;
            transform.scale(Right_scaleFactor, Right_scaleFactor);
            Right_currentView->setTransform(transform);
        }

        // 更新百分比显示
        double percentage = Right_scaleFactor * 100;
        QString percentageStr = QString::number((int)percentage);
        ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
    }
}

void MainAndSecondaryCamerasWnd::HideLayout(QHBoxLayout* layout, bool isHide)
{
    for (int i = 0; i < layout->count(); ++i)
    {
        QWidget* widget = layout->itemAt(i)->widget();
        if (widget)
        {
            if (isHide)
            {
                widget->hide();
            }
            else
            {
                widget->show();
            }

        }
    }
}

void MainAndSecondaryCamerasWnd::UpdataLightJoystickSwitchPage(Group* pGroup)
{
    auto Fun = [&](LightJoystickSwitchPage* p, bool bShow)
    {
        p->UpdatemPageGroup(pGroup);
        p->InitMainCameraBind(m_pMainCameraBind);
        p->HideOrShowPage(bShow);
    };

    for (int i = 0; i < m_veCurrentSelectGroup.size(); i++)
    {
        if (i == 0)
        {
            bool bShow = false;
            
            if (m_veCurrentSelectGroup[0].nSelectLeft1 == 1)
            {
                bShow = true;
            }
            Fun(ui->control_L, bShow);

        }
        else if (i == 1)
        {
            bool bShow = false;
            if (m_veCurrentSelectGroup[1].nSelectLeft1 == 1)
            {
                bShow = true;
            }
            Fun(ui->control_L, bShow);

        }
    }


}

void MainAndSecondaryCamerasWnd::timerEvent(QTimerEvent* event)
{
    int nID = event->timerId();
}

void MainAndSecondaryCamerasWnd::on_LeftZoomUpButton_clicked()
{
    mousePos = ui->LeftOperatewidget->geometry().center();
    updateScale(Left_scaleFactor * 1.1, 0);

}


void MainAndSecondaryCamerasWnd::on_RightZoomUpButton_clicked()
{
    mousePos = ui->RightOperatewidget->geometry().center();
    updateScale(Right_scaleFactor * 1.1, 1);

}


void MainAndSecondaryCamerasWnd::on_LeftZoomOutButton_clicked()
{
    mousePos = ui->LeftOperatewidget->geometry().center();
    updateScale(Left_scaleFactor * 0.9, 0);

}


void MainAndSecondaryCamerasWnd::on_RightZoomOutButton_clicked()
{
    mousePos = ui->RightOperatewidget->geometry().center();
    updateScale(Right_scaleFactor * 0.9, 1);

}

void MainAndSecondaryCamerasWnd::ResetIdleTimer(bool bStart /*= false*/)
{
    if (bStart)
    {
        if (isActiveWindow())
        {
            m_idleTimer = startTimer(3000);
        }
    }
    else
    {
        killTimer(m_idleTimer);
        m_idleTimer = -1;
    }
}

void MainAndSecondaryCamerasWnd::GetCurrentSelectSlots(int groupId, int nIndex)
{
    if (m_pMainCameraBind == nullptr)
    {
        return;
    }

    // 绑定操作,用来初始化
    ST_CURRENT_SELECT_GROUP _curr;
    _curr.isBond = true;
    m_currentSelectGroup.nGroupId = groupId;
    //2. 开始干活
    int nOnClickGroupId = groupId - 1; // 这里new 有些是0,有些是1 暂时选择
    if (m_pMainCameraBind->m_vecCamera.size() > nOnClickGroupId)
    {
        _curr.nCurrentCameraId = m_pMainCameraBind->m_vecCamera[nOnClickGroupId].iCameraId;
    }
    else
    {
        _curr.nCurrentCameraId = 0;
    }

    _curr.nSelectLeft1 = nIndex; // 是否选中窗口

    Group* pGroup = m_currentSelectGroup.liGroup[nOnClickGroupId];
    _curr.pSelectGroup = pGroup;

    // 1.判断当前是否存储
    if (m_veCurrentSelectGroup.size() >= 2)
    {
        if (pGroup == m_veCurrentSelectGroup[1].pSelectGroup && 
            m_veCurrentSelectGroup[1].nSelectLeft1== nIndex)
        {
            return;
        }
        // 解绑操作,删除第0个 ,, 主要目前为了实现 相机切换
        disconnect(m_veCurrentSelectGroup[0].pSelectGroup, &Group::SetUpDataImageShowSig,
            this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals0);
        disconnect(m_veCurrentSelectGroup[0].pSelectGroup, &Group::SetUpDataImageShowSig,
            this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals4);
        disconnect(m_veCurrentSelectGroup[1].pSelectGroup, &Group::SetUpDataImageShowSig,
            this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals0);
        disconnect(m_veCurrentSelectGroup[1].pSelectGroup, &Group::SetUpDataImageShowSig,
            this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals4);


        m_veCurrentSelectGroup.erase(m_veCurrentSelectGroup.begin() + 0);// 0个

        if (m_veCurrentSelectGroup[0].nSelectLeft1 == 1)
        {
            connect(m_veCurrentSelectGroup[0].pSelectGroup, &Group::SetUpDataImageShowSig,
                this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals0);

            // 在绑定
            connect(pGroup, &Group::SetUpDataImageShowSig,
                this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals4);
        }
        else
        {
            // 直接复制图片?
            if (_curr.nSelectLeft1 == 1)
            {
                connect(m_veCurrentSelectGroup[0].pSelectGroup, &Group::SetUpDataImageShowSig,
                    this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals4);
            }
            else
            {
                loadGroupSettings(groupId, nIndex);
            }
        }
        
        m_veCurrentSelectGroup.push_back(_curr);         
    }
    else
    {
        if (m_veCurrentSelectGroup[0].pSelectGroup != _curr.pSelectGroup)
        {
            if (m_veCurrentSelectGroup.size() == 1)
            {
                connect(pGroup, &Group::SetUpDataImageShowSig,
                    this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals4);
            }
            else
            {
                connect(pGroup, &Group::SetUpDataImageShowSig,
                    this, &MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals0);
            }
            m_veCurrentSelectGroup.push_back(_curr);
        }
        
    }

    UpdataLightJoystickSwitchPage(pGroup);
}

void MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals0(const QPixmap& imageData)
{
    //lastIndex = 1;
    QSize size_left = ui->LeftOperatewidget->size();
    QPixmap scaledPixmap_left = m_lastRightPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->LeftOperatewidget->setPixmap(scaledPixmap_left);
    m_lastRightPixmap = imageData;
    //UpdataGroupSettings(imageData);
}

void MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals1(const QPixmap& imageData)
{
   // lastIndex = 2;
    UpdataGroupSettings(imageData);
}

void MainAndSecondaryCamerasWnd::GetUpDataImageShowSignals4(const QPixmap& imageData)
{
    QSize size_left = ui->RightOperatewidget->size();
    QPixmap scaledPixmap_left = imageData.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->RightOperatewidget->setPixmap(scaledPixmap_left);
    //m_lastRightPixmap = imageData;
}

void MainAndSecondaryCamerasWnd::wheelEvent(QWheelEvent *event)
{
    if (ui->LeftOperatewidget->rect().contains(ui->LeftOperatewidget->mapFromGlobal(event->globalPos()))) {
        mousePos = ui->LeftOperatewidget->mapFromGlobal(event->globalPos());
        if (event->angleDelta().y() > 0) {
            updateScale(Left_scaleFactor * 1.1, 0); // 放大
        } else {
            updateScale(Left_scaleFactor * 0.9, 0); // 缩小
        }
    }

    if (ui->RightOperatewidget->rect().contains(ui->RightOperatewidget->mapFromGlobal(event->globalPos()))) {
        mousePos = ui->RightOperatewidget->mapFromGlobal(event->globalPos());
        if (event->angleDelta().y() > 0) {
            updateScale(Right_scaleFactor * 1.1, 1); // 放大
        } else {
            updateScale(Right_scaleFactor * 0.9, 1); // 缩小
        }
    }
    if (ui->scrollArea->rect().contains(ui->scrollArea->mapFromGlobal(event->globalPos()))) {
        // 获取 QScrollArea 的横向滚动条
        QScrollBar *scrollBar = ui->scrollArea->horizontalScrollBar();
        // 根据滚轮滚动方向改变滚动条的值
        if (event->angleDelta().y() > 0) {
            scrollBar->setValue(scrollBar->value() - 50);
        } else {
            scrollBar->setValue(scrollBar->value() + 50);
        }
    }
    
	QMainWindow::wheelEvent(event);
}

bool MainAndSecondaryCamerasWnd::eventFilter(QObject* obj, QEvent* event)
{
    // 检测用户活动事件
    switch (event->type()) 
    {
    case QEvent::KeyPress:
    case QEvent::MouseMove:
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonDblClick:
    case QEvent::Wheel:
        ResetIdleTimer();
        ResetIdleTimer(true);
        break;
    default:
        break;
    }

    return QMainWindow::eventFilter(obj, event);
}

void MainAndSecondaryCamerasWnd::showEvent(QShowEvent *event) {
    isUpdatingSettings = false;
    QMainWindow::showEvent(event);

    //loadSettings();
    isUpdatingSettings = false;
}

void MainAndSecondaryCamerasWnd::hideEvent(QHideEvent *event) {
    isUpdatingSettings = false;
    QMainWindow::hideEvent(event);
}

void MainAndSecondaryCamerasWnd::CheckIs()
{
    /*QSettings settings("YourCompany", "YourApplication_Button");
    int groupId = settings.value("GroupId_button", 0).toInt();

    if (groupId == lastGroupId) {
        if (lastIndex == 1 ) {
            if(isShow == true){
                isShow = false;
            } else {
                isShow = true;
            }
        }
        if (lastLastIndex == 1 && groupId == lastLastGroupId) {
            if(isShow_L == true){
                isShow_L = false;
            } else {
                isShow_L = true;
            }
        }
        showAndHide();
        showAndHide_L();

    } else if (groupId == lastLastGroupId) {
        if (lastLastIndex == 1) {
            if(isShow_L == true){
                isShow_L = false;
            } else {
                isShow_L = true;
            }
        }
        showAndHide_L();
    }*/
}