#include "stable.h"
#include "cmwmanualtechnology.h"
#include "ui_cmwmanualtechnology.h"
#include <QGraphicsProxyWidget>
#include <QGraphicsItemArrow.h>
#include <QStylePainter>
#include <QPainterPath>
#include "qmymessagebox.h"
CMWManualTechnology::CMWManualTechnology(QWidget* parent)
    : CMWidget(parent)
{
    ui = new Ui::CMWManualTechnology();
    ui->setupUi(this);

    /*QImage bgImage = QImage::loa("://images/ManualTechnology.png");*/
    QPixmap pixmap;
    pixmap.load(CMakePath::make_bin_path("../assets/chamber.png"));
    //pixmap.convertFromImage(bgImage);
    //ui->lblClean->setAlignment(Qt::AlignCenter);
    /*ui->lblClean->setPixmap(pixmap);
    ui->lblCoat->setPixmap(pixmap);*/

    pixmap.load(CMakePath::make_bin_path("../assets/platform.png"));
    ui->lblPlatform->setPixmap(pixmap);
    //ui->lblClean->setPixmap(pixmap.scaled(ui->lblClean->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
}

CMWManualTechnology::~CMWManualTechnology()
{
    delete ui;
}

void CMWManualTechnology::MTimeFresh()
{
    MFresh();

    updateStatus(SM_MAIN, ui->lblStatusCutting);
    updateStatus(SM_CLEAN, ui->lblStatusClean);
    updateStatus(SM_COAT, ui->lblStatusCoat);

    CMotor* motorE = GET_CHDIRECT(MOTORE_COAT_SPINDLE, CMotor);
    CMotor* motorF = GET_CHDIRECT(MOTORF_CLEAN_SPINDLE, CMotor);
    //实际回圆角度及状态
    CProcess_Clean* processClean = GET_PROCESS(PROCESS_CLEAN, SM_CLEAN, CProcess_Clean);
    CProcess_Coat* processCoat = GET_PROCESS(PROCESS_COAT, SM_COAT, CProcess_Coat);

    double iAngleInitE = processCoat->m_Variants.get_VData(Var_Coat_Spindle_InitPos_ID)->toDouble();
    double iAngleInitF = processClean->m_Variants.get_VData(Var_Clean_Spindle_InitPos_ID)->toDouble();

    int iAngleE = (long)motorE->m_Variants.get_VData(Var_Motor_Pos_ID)->toDouble() % 360;
    int iAngleF = (long)motorF->m_Variants.get_VData(Var_Motor_Pos_ID)->toDouble() % 360;
    iAngleE -= iAngleInitE;
    iAngleF -= iAngleInitF;
    ui->labCoat_Img->setAngle(iAngleE);
    ui->labClean_Img->setAngle(iAngleF);
    CProcess_Trans* transProcess = GET_PROCESS(PROCESS_TRANS,
                                               SM_TRANS, CProcess_Trans);
    if (transProcess->m_pSlotCoat)
    {
        ui->labCoat_Img->SetWafer(true);
    }
    else
    {
        ui->labCoat_Img->SetWafer(false);
    }
    if (transProcess->m_pSlotClean)
    {
        ui->labClean_Img->SetWafer(true);
    }
    else
    {
        ui->labClean_Img->SetWafer(false);
    }


    VS_Thread* m_pState = (VS_Thread*)m_gSystem.FindSM(SM_IO)->m_pCurState;
    if (m_pState->QueueSize() < 10)
    {
        m_pState->pushback_Msg(VarMsg_Driver_ID, QObject::trUtf8("Fresh Pos"),
                               QVariantList()
                               << MOTOR_DRIVER << CMotorDriver::FRESHPOS
                               << MOTORE_COAT_SPINDLE, 0);
        m_pState->pushback_Msg(VarMsg_Driver_ID, QObject::trUtf8("Fresh Pos"),
                               QVariantList()
                               << MOTOR_DRIVER << CMotorDriver::FRESHPOS
                               << MOTORF_CLEAN_SPINDLE, 0);

    }
}

void CMWManualTechnology::FreshWaferStatus()
{
    CProcess_Trans* pTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);

    if (pTrans->m_pSlotCheckBox)
    {
        ui->lblCupboardWafer->setVisible(true);
    }
    else
    {
        ui->lblCupboardWafer->setVisible(false);
    }

    if (pTrans->m_pSlotRailNear)
    {
        ui->lblRailNearWafer->setVisible(true);
        ui->lblRailNear->setVisible(false);
    }
    else
    {
        ui->lblRailNearWafer->setVisible(false);
        ui->lblRailNear->setVisible(true);
    }

    if (pTrans->m_pSlotRailFar)
    {
        ui->lblRailFarWafer->setVisible(true);
        ui->lblRailFar->setVisible(false);
    }
    else
    {
        ui->lblRailFarWafer->setVisible(false);
        ui->lblRailFar->setVisible(true);
    }

    if (pTrans->m_pSlotLoad)
    {
        ui->lblLoad->setVisible(true);
    }
    else
    {
        ui->lblLoad->setVisible(false);
    }

    if (pTrans->m_pSlotTrans)
    {
        ui->lblTransWafer->setVisible(true);
    }
    else
    {
        ui->lblTransWafer->setVisible(false);
    }

    if (pTrans->m_pSlotUnLoad)
    {
        ui->lblUnload->setVisible(true);
    }
    else
    {
        ui->lblUnload->setVisible(false);
    }

    if (pTrans->m_pSlotWT)
    {
        ui->lblCutWafer->setVisible(true);
    }
    else
    {
        ui->lblCutWafer->setVisible(false);
    }

    if (pTrans->m_pSlotCoat)
    {
        ui->lblCoatWafer->setVisible(true);
    }
    else
    {
        ui->lblCoatWafer->setVisible(false);
    }

    if (pTrans->m_pSlotClean)
    {
        ui->lblCleanWafer->setVisible(true);
    }
    else
    {
        ui->lblCleanWafer->setVisible(false);
    }


}

void CMWManualTechnology::MFresh()
{
    FreshWaferStatus();
    FreshTransButtonStatus(SM_TRANS, m_TransButtons);
    FreshTransButtonStatus(SM_MAIN, m_CutButtons);
    FreshTransButtonStatus(SM_COAT, m_CoatButtons);
    FreshTransButtonStatus(SM_CLEAN, m_CleanButtons);
    
    ui->checkBox_LaserEmit->setChecked(m_gSystem.m_bEnbaleLaserOpen);
    
    CWorker* pWorker = GET_CHDIRECT(M_WORKER, CWorker);
    if (pWorker->m_workLevel >= LEVEL_ENGINEER)
    {
        bool bVisible = true;
        ui->checkBox_CycleRun->setVisible(bVisible);
        ui->chkStopAfterFinish->setVisible(bVisible);
        ui->checkBox_LaserEmit->setVisible(bVisible);
        ui->checkBox_OnlyLocation->setVisible(bVisible);
        ui->checkBox_OnlyLeveling->setVisible(bVisible);
        ui->checkBox_OnlyA->setVisible(bVisible);
        ui->checkBox_OnlyB->setVisible(bVisible);
        ui->chkBoxSimulatorLeveling->setVisible(bVisible);
        ui->chkBoxSimulatorLocation->setVisible(bVisible);
        ui->pushButton_AllUnLoad->setVisible(bVisible);
    }
    else
    {
        bool bVisible = false;
        ui->checkBox_CycleRun->setVisible(bVisible);
        ui->chkStopAfterFinish->setVisible(bVisible);
        ui->checkBox_LaserEmit->setVisible(bVisible);
        ui->checkBox_OnlyLocation->setVisible(bVisible);
        ui->checkBox_OnlyLeveling->setVisible(bVisible);
        ui->checkBox_OnlyA->setVisible(bVisible);
        ui->checkBox_OnlyB->setVisible(bVisible);
        ui->chkBoxSimulatorLeveling->setVisible(bVisible);
        ui->chkBoxSimulatorLocation->setVisible(bVisible);
        ui->pushButton_AllUnLoad->setVisible(bVisible);
        m_gSystem.m_bEnbaleLaserOpen = true;
        m_gSystem.m_bOnlyLocation = false;
        m_gSystem.m_bOnlyASide = false;
        m_gSystem.m_bOnlyBSide = false;
        m_gSystem.m_bOnlyLeveling = false;
        m_gSystem.m_bDisableCutlineLaser = false;
    }

    if (pWorker->m_workLevel >= LEVEL_ADMINISTRATOR)
    {
        bool bVisible = true;
    }
    else
    {
        bool bVisible = false;
    }

    ui->checkBox_OnlyLocation->setChecked(m_gSystem.m_bOnlyLocation);
    ui->checkBox_OnlyLeveling->setChecked(m_gSystem.m_bOnlyLeveling);
    ui->checkBox_OnlyA->setChecked(m_gSystem.m_bOnlyASide);
    ui->checkBox_OnlyB->setChecked(m_gSystem.m_bOnlyBSide);
    ui->chkPauseAfterLocation->setChecked(m_gSystem.m_bPausedAfterLocation);
    ui->chkPauseAfterLeveling->setChecked(m_gSystem.m_bPausedAfterLeveling);
    ui->checkBox_LaserEmit->setChecked(m_gSystem.m_bEnbaleLaserOpen);

    ui->checkBox_CycleRun->setChecked(m_gSystem.m_bEnableCycle);
    ui->chkStopAfterFinish->setChecked(m_gSystem.m_bStopAfterFinish);
    ui->chkBoxSimulatorLocation->setChecked(m_gSystem.m_bSimulatorLocation);
    ui->chkBoxSimulatorLeveling->setChecked(m_gSystem.m_bSimulatorLeveling);
}

void CMWManualTechnology::MInit()
{
    m_TransButtons[CProcess_Trans::kTransScanCass] = ui->btnScanCassete;
    m_TransButtons[CProcess_Trans::kTransCassToRailNear] = ui->btnCassToRail;
    m_TransButtons[CProcess_Trans::kTransRailNearToCass] = ui->btnRailToCassete;
    m_TransButtons[CProcess_Trans::kTransCheckBoxToRailNear] = ui->btnCupboardToRailNear;
    m_TransButtons[CProcess_Trans::kTransRailNearToCheckBox] = ui->btnRailNearToCupboard;

    m_TransButtons[CProcess_Trans::kTransRailFarToWorktable] = ui->btnRailFarToWt;
    m_TransButtons[CProcess_Trans::kTransWorktableToRailNear] = ui->btnWtToRailNear;

    m_TransButtons[CProcess_Trans::kTransRailNearToFar] = ui->btnRailNearToFar;

    m_TransButtons[CProcess_Trans::kTransCoatToRailFar] = ui->btnCoatToRailFar;
    m_TransButtons[CProcess_Trans::kTransRailFarToCoat] = ui->btnRailFarToCoat;

    m_TransButtons[CProcess_Trans::kTransRailNearToClean] = ui->btnRailToClean;
    m_TransButtons[CProcess_Trans::kTransCleanToRailNear] = ui->btnCleanToRail;
    m_TransButtons[CProcess_Trans::kTransRailFarToRailNear] = ui->btnRailFarToNear;

	m_TransButtons[VarMsg_Transfer_UnLoadAll] = ui->pushButton_AllUnLoad;
    for (auto iter = m_TransButtons.begin(); iter != m_TransButtons.end(); iter++)
    {
        int iKey = iter.key();
        QPushButton* pBtn = iter.value();

        QObject::connect(pBtn, &QPushButton::clicked, this, [=]() {
            OnTransBtnClicked(iKey, pBtn);
        });
    }

    m_TransButtons[CProcess_Trans::kTransCheckboxToClean] = ui->btnCupboardToClean;
    m_TransButtons[CProcess_Trans::kTransCheckboxToCoat] = ui->btnCupboardToCoat;
    m_TransButtons[CProcess_Trans::kTransCleanToCheckbox] = ui->btnCleanToCupboard;
    m_TransButtons[CProcess_Trans::kTransCoatToCheckbox] = ui->btnCoatToCupboard;
    m_TransButtons[CProcess_Trans::kTransCheckboxToWt] = ui->btnCupboardToWt;
    m_TransButtons[CProcess_Trans::kTransWtToCheckbox] = ui->btnWtToCupboard;
    m_TransButtons[CProcess_Trans::kTransCoatToWt] = ui->btnCoatToWt;
    m_TransButtons[CProcess_Trans::kTransWtToClean] = ui->btnWtToClean;

    QMap<int, QPushButton*> compButtons;
    compButtons[CProcess_Trans::kTransCheckboxToClean] = ui->btnCupboardToClean;
    compButtons[CProcess_Trans::kTransCheckboxToCoat] = ui->btnCupboardToCoat;
    compButtons[CProcess_Trans::kTransCleanToCheckbox] = ui->btnCleanToCupboard;
    compButtons[CProcess_Trans::kTransCoatToCheckbox] = ui->btnCoatToCupboard;
    compButtons[CProcess_Trans::kTransCheckboxToWt] = ui->btnCupboardToWt;
    compButtons[CProcess_Trans::kTransWtToCheckbox] = ui->btnWtToCupboard;
    compButtons[CProcess_Trans::kTransCoatToWt] = ui->btnCoatToWt;
    compButtons[CProcess_Trans::kTransWtToClean] = ui->btnWtToClean;
    for (auto iter = compButtons.begin(); iter != compButtons.end(); iter++)
    {
        int iKey = iter.key();
        QPushButton* pBtn = iter.value();

        QObject::connect(pBtn, &QPushButton::clicked, this, [=]()
        {
            OnTransCompClicked(iKey, pBtn);
        });
    }

    //涂覆操作
    m_CoatButtons[NORMAL_ACTION] = ui->btnStartCoat;
    for (auto iter = m_CoatButtons.begin(); iter != m_CoatButtons.end(); iter++)
    {
        int iKey = iter.key();
        QPushButton* pBtn = iter.value();

        QObject::connect(pBtn, &QPushButton::clicked, this, [=]()
        {
            OnCoatBtnClicked(iKey, pBtn);
        });
    }

    //正常清洗
    m_CleanButtons[NORMAL_ACTION] = ui->btnStartClean;
    for (auto iter = m_CleanButtons.begin(); iter != m_CleanButtons.end(); iter++)
    {
        int iKey = iter.key();
        QPushButton* pBtn = iter.value();

        QObject::connect(pBtn, &QPushButton::clicked, this, [=]()
        {
            OnCleanBtnClicked(iKey, pBtn);
        });
    }

    m_CutButtons[VarMsg_StartSemi_ID] = ui->btnStartCut;
    for (auto iter = m_CutButtons.begin(); iter != m_CutButtons.end(); iter++)
    {
        int iKey = iter.key();
        QPushButton* pBtn = iter.value();

        QObject::connect(pBtn, &QPushButton::clicked, this, [=]()
        {
            OnCutBtnClicked(iKey, pBtn);
        });
    }

    QObject::connect(ui->btnStopClean, &QPushButton::clicked, this, [=]()
    {
        OnStopBtnClicked(SM_CLEAN);
    });

    QObject::connect(ui->btnStopCoat, &QPushButton::clicked, this, [=]()
    {
        OnStopBtnClicked(SM_COAT);
    });
    QObject::connect(ui->btnStopCut, &QPushButton::clicked, this, [=]()
    {
        OnStopBtnClicked(SM_MAIN);
    });
}

void CMWManualTechnology::OnTransBtnClicked(int iSemiMsgId, QPushButton* pBtn)
{
    CProcess_Trans* pTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, pTrans->GetMsgName(iSemiMsgId),
                          QVariantList() << iSemiMsgId);

    int ret = AskButtonOperte(SM_TRANS, iSemiMsgId);
    if (ret == QMessageBox::Abort)
    {
        m_gSystem.StopSemi(SM_TRANS);
    }
    else if (ret == QMessageBox::Yes)
    {
        m_gSystem.start_test(SM_TRANS, msgqueue);
    }
    else
    {
        return;
    }
}

void CMWManualTechnology::OnTransCompClicked(int iSemiMsgId, QPushButton* pBtn)
{
    CProcess_Trans* pTrans = GET_PROCESS(PROCESS_TRANS, SM_TRANS, CProcess_Trans);
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, pTrans->GetMsgName(iSemiMsgId),
                          QVariantList() << VarMsg_Transfer_Component << iSemiMsgId);

    int ret = AskButtonOperte(SM_TRANS, iSemiMsgId);
    if (ret == QMessageBox::Abort)
    {
        m_gSystem.StopSemi(SM_TRANS);
    }
    else if (ret == QMessageBox::Yes)
    {
        m_gSystem.start_test(SM_TRANS, msgqueue);
    }
    else
    {
        return;
    }
}

void CMWManualTechnology::OnCoatBtnClicked(int iSemiMsgId, QPushButton* pBtn)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Coat Sig"),
                          QVariantList() << NORMAL_ACTION << -1 << -1, 0);

    int ret = AskButtonOperte(SM_COAT, iSemiMsgId);
    if (ret == QMessageBox::Abort)
    {
        m_gSystem.StopSemi(SM_COAT);
    }
    else if (ret == QMessageBox::Yes)
    {
        m_gSystem.start_test(SM_COAT, msgqueue);
    }
    else
    {
        return;
    }
}

void CMWManualTechnology::OnCleanBtnClicked(int iSemiMsgId, QPushButton* pBtn)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Clean Sig"),
                          QVariantList() << NORMAL_ACTION << -1 << -1, 0);

    int ret = AskButtonOperte(SM_CLEAN, iSemiMsgId);
    if (ret == QMessageBox::Abort)
    {
        m_gSystem.StopSemi(SM_CLEAN);
    }
    else if (ret == QMessageBox::Yes)
    {
        m_gSystem.start_test(SM_CLEAN, msgqueue);
    }
    else
    {
        return;
    }
}

void CMWManualTechnology::OnCutBtnClicked(int iSemiMsgId, QPushButton* pBtn)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Single Cut Sig"),
                          QVariantList() << VarMsg_StartSemi_ID);

    int ret = AskButtonOperte(SM_MAIN, iSemiMsgId);
    if (ret == QMessageBox::Abort)
    {
        m_gSystem.StopSemi(SM_MAIN);
    }
    else if (ret == QMessageBox::Yes)
    {
        m_gSystem.start_test(SM_MAIN, msgqueue);
    }
    else
    {
        return;
    }
}

//横线上下间距
int const iLineSpace = 30;
//点上下间距
int const iVecSpace = 10;

void CMWManualTechnology::paintEvent(QPaintEvent* event)
{
    QRect rect_normalized = QRect(0, 0, 10, 10);
    QStylePainter painter(this);

    painter.setRenderHint(QPainter::HighQualityAntialiasing);


    QPen pen(palette().mid().color(), 4);
    painter.setPen(pen);


    QPainterPath line;
    
    int iPosX = ui->gpCassete->geometry().bottomRight().x() + 4;
    int iPosY = ui->gpCassete->geometry().bottomLeft().y() / 2 + ui->gpCassete->geometry().topLeft().y() / 2 + 1;
    line.moveTo(iPosX, iPosY);

    //检查盒到近轨道
    iPosX += (ui->lblRailNear->mapTo(this, QPoint(0, 0)).x() - iPosX) / 2;
    line.lineTo(iPosX, iPosY);
    int iPoxTemp = iPosX;
    
    iPosY = ui->lblRailNear->mapTo(this, QPoint(0, 0)).y() + ui->lblRailNear->height() / 3 * 2;
    line.lineTo(iPosX, iPosY);

    iPosX = ui->lblRailNear->mapTo(this, QPoint(0, 0)).x() ;
    iPosX -= iVecSpace;
    line.lineTo(iPosX, iPosY);

    /*iPosY += iLineSpace - 4;
    line.lineTo(iPosX, iPosY);*/

    //近轨道到平台
    iPosX = ui->lblRailNear->mapTo(this, QPoint(0, 0)).x() - iVecSpace;
    iPosY = ui->lblRailNear->mapTo(this, QPoint(0, 0)).y() + ui->lblRailNear->height() / 3;
    line.moveTo(iPosX, iPosY);

    iPosX -= iVecSpace;
    line.lineTo(iPosX, iPosY);

    iPosY = ui->lblPlatform->mapTo(this, QPoint(0, 0)).y() + ui->lblPlatform->height() + 2*iVecSpace;
    line.lineTo(iPosX, iPosY);

    iPosX = ui->lblPlatform->mapTo(this, QPoint(0, 0)).x() + ui->lblPlatform->width() / 2;
    line.lineTo(iPosX, iPosY);

    iPosY = ui->lblPlatform->mapTo(this, QPoint(0, 0)).y() + ui->lblPlatform->height() + iVecSpace;
    line.lineTo(iPosX, iPosY);


    //远轨道平台
    iPosX = ui->lblRailFar->mapTo(this, QPoint(0, 0)).x() - iVecSpace;
    iPosY = ui->lblRailFar->mapTo(this, QPoint(0, 0)).y() + ui->lblRailFar->height() / 3 - iVecSpace;
    line.moveTo(iPosX, iPosY);

    iPosX = ui->lblPlatform->mapTo(this, QPoint(ui->lblPlatform->width()/2, 0)).x();
    line.lineTo(iPosX, iPosY);

    iPosY = ui->lblPlatform->mapTo(this, QPoint(0, 0)).y() - iVecSpace;
    line.lineTo(iPosX, iPosY);

    //近轨道到远轨道
    iPosX = ui->lblRailNear->mapTo(this, QPoint(0, 0)).x() + ui->lblRailNear->width() / 4 * 3 ;
    iPosY = ui->lblRailNear->mapTo(this, QPoint(0, 0)).y() - iVecSpace;
    line.moveTo(iPosX, iPosY);

    iPosY = ui->lblRailFar->mapTo(this, QPoint(0, 0)).y() + ui->lblRailFar->height() + iVecSpace;
    line.lineTo(iPosX, iPosY);

    painter.drawPath(line);

    CMWidget::paintEvent(event);
}



int CMWManualTechnology::AskButtonOperte(int smID, int operateID)
{
    int curOperateID = -1;
    VSM_Thread* vsmThread = m_gSystem.FindSM(smID);
    switch (smID)
    {
        case SM_TRANS:
            curOperateID = ((VST_Semi*)(vsmThread->m_pState_Semi))->m_iCurOperator;
            break;
        case SM_MAIN:
            curOperateID = ((VST_Semi*)(vsmThread->m_pState_Semi))->m_iCurOperator;
            break;
        case SM_COAT:
            curOperateID = ((VST_Semi*)(vsmThread->m_pState_Semi))->m_iCurOperator;
            break;
        case SM_CLEAN:
            curOperateID = ((VST_Semi*)(vsmThread->m_pState_Semi))->m_iCurOperator;
            break;
        default:
            return QMessageBox::Yes;
    }

    if (curOperateID == operateID)
    {
        QMyMessageBox msgBoxAsk(QMessageBox::Question, tr("Confirm information"),
                                tr("Operate not finish\n\nSelect what you want to do?"),
                                QMessageBox::Yes | QMessageBox::Abort | QMessageBox::Cancel);
        //msgBoxAsk.setStandardButtons();
        msgBoxAsk.setDefaultButton(QMessageBox::Cancel);
        msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Continue"));
        msgBoxAsk.setButtonText(QMessageBox::Abort, tr("Abort"));
        msgBoxAsk.setButtonText(QMessageBox::Cancel, tr("Cancel"));
        int ret = msgBoxAsk.exec();
        return ret;
    }

    return QMessageBox::Yes;
}

void CMWManualTechnology::FreshTransButtonStatus(int smId, const QMap<int, QPushButton*>& btnList)
{
    //检查半自动是否在运行
    uint32_t iStatus;
    m_gSystem.FindSM(smId)->GetStatus(iStatus);
    
    bool bEnabled = true;
    bool bSemiRun = false;
    if (iStatus & EStatus::kAuto_Paused || iStatus & EStatus::kAuto_Runing || iStatus & EStatus::kAutoWaitTask)
    {
        bEnabled = false;
    }
    else
    {
        if(iStatus & EStatus::kSemi_Running || iStatus & EStatus::kSemi_Paused)
        {
            bEnabled = false;
            bSemiRun = true;
        }
        else
        {
            bEnabled = true;
        }
    }

    VST_Semi* pSemiState = m_gSystem.FindSM(smId)->m_pState_Semi;
    int iCurOperator = pSemiState->m_iCurOperator;

    bool bBtnEnabled = false;
    bool bBtnChecked = false;
    for (auto iter = btnList.cbegin(); iter != btnList.cend(); iter++)
    {
        bBtnEnabled = bEnabled;
        bBtnChecked = false;
        //检查运行状态
        QPushButton* pBtn = (iter).value();
        if ((iter).key() == iCurOperator)
        {
            if (bSemiRun)
            {
                bBtnEnabled = true;
                bBtnChecked = true;
                
            }
        }
        
        //检查可执行状态
        if (bBtnEnabled == true)
        {

        }

        pBtn->setEnabled(bBtnEnabled);
        pBtn->setChecked(bBtnChecked);
    }
}

void CMWManualTechnology::OnStopBtnClicked(int smID)
{
    m_gSystem.StopSemi(smID);
}

void CMWManualTechnology::on_checkBox_LaserEmit_clicked(bool checked)
{
    m_gSystem.m_bEnbaleLaserOpen = checked;
}


void CMWManualTechnology::on_checkBox_OnlyLeveling_clicked(bool checked)
{
    m_gSystem.m_bOnlyLeveling = checked;
}

void CMWManualTechnology::on_checkBox_OnlyLocation_clicked(bool checked)
{
    m_gSystem.m_bOnlyLocation = checked;
}

void CMWManualTechnology::on_checkBox_OnlyA_clicked(bool checked)
{
    m_gSystem.m_bOnlyASide = checked;
}

void CMWManualTechnology::on_checkBox_OnlyB_clicked(bool checked)
{
    m_gSystem.m_bOnlyBSide = checked;
}

void CMWManualTechnology::on_chkPauseAfterLeveling_clicked(bool checked)
{
    m_gSystem.m_bPausedAfterLeveling = checked;
}

void CMWManualTechnology::on_chkPauseAfterLocation_clicked(bool checked)
{
    m_gSystem.m_bPausedAfterLocation = checked;
}

void CMWManualTechnology::on_chkStopAfterFinish_toggled(bool checked)
{
    m_gSystem.m_bStopAfterFinish = checked;
}

void CMWManualTechnology::on_checkBox_CycleRun_clicked(bool checked)
{
    m_gSystem.m_bEnableCycle = checked;
}
void CMWManualTechnology::on_chkBoxSimulatorLeveling_clicked(bool checked)
{
    m_gSystem.m_bSimulatorLeveling = checked;
}

void CMWManualTechnology::on_chkBoxSimulatorLocation_clicked(bool checked)
{
    m_gSystem.m_bSimulatorLocation = checked;
}


void CMWManualTechnology::on_btnStopAll_clicked(bool checked)
{
    m_gSystem.publish_msg(V_Msg(VarMsg_Stop_ID, QObject::trUtf8("Stop All")));
}

void CMWManualTechnology::on_btnManualLoad_clicked(bool checked)
{
    V_MsgQueue msgqueue;

    msgqueue.pushback_Msg(VarMsg_Driver_ID, QObject::trUtf8("Manual load Sig"),
                          QVariantList()
                          << WORKTABLE_DRIVER << CMWorkTableDriver::MANUALLOADPOS, 0);

    msgqueue.pushback_Msg(VarMsg_System_ChangeSide,
                          QObject::trUtf8("Change Side Load"),
                          QVariantList() << -1, 0);

    m_gSystem.start_test(SM_MAIN, msgqueue);

}

void CMWManualTechnology::on_btnCassUp_clicked(bool checked)
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Cass up Sig"),
                          QVariantList()
                          << PROCESS_SCAN << CProcess_Scan::CASSUP << CASSETTE, 0);
    m_gSystem.start_test(SM_TRANS, msgqueue);
}

void CMWManualTechnology::updateStatus(int iVsmId, QLabel* pLabel)
{
    VSM_Thread* pSMMain = m_gSystem.FindSM(iVsmId);
    uint32_t iStatusMain = 0;
    pSMMain->GetStatus(iStatusMain);
    if (iStatusMain & EStatus::kIDLE)
    {
        pLabel->setStyleSheet("border:1px;background-color:rgb(2,2,2);color:white");
    }

    if (iStatusMain & EStatus::kSemi_Paused || iStatusMain & EStatus::kAuto_Paused)
    {
        pLabel->setStyleSheet("border:1px;background-color:yellow;color:black");
    }

    if (iStatusMain & EStatus::kSemi_Running || iStatusMain & EStatus::kAuto_Runing)
    {
        pLabel->setStyleSheet("border:1px;background-color:rgb(0,255,0);color:black");
    }

    if (iStatusMain & EStatus::kAutoWaitTask)
    {
        pLabel->setStyleSheet("border:1px;background-color:rgb(255,255,197);color:black");
    }
}