#include "cmwmanualload.h"
#include "ui_cmwmanualload.h"
#include "logic.h"
#include "vst_semi_main.h"
#include "vst_semi_transfer.h"
#include "vst_semi_coat.h"
#include "vst_semi_clean.h"
#include "qmymessagebox.h"
#include <QStylePainter>
CMWManualLoad::CMWManualLoad(QWidget *parent) :
    CMWidget(parent),
    ui(new Ui::CMWManualLoad)
{
    ui->setupUi(this);
}

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

void CMWManualLoad::MFresh()
{
    CMWidget::MFresh();
    ui->checkBox_LaserEmit->setChecked(m_gSystem.m_bEnbaleLaserOpen);
    ui->txtNozzleWaitTime->setText(QString::number(m_gSystem.m_iCoatPreSpayWaitTimeMs));

    //ui->btnPEGMove->setVisible(false);
    //ui->btnMultiMove->setVisible(false);
    //ui->btnMultiAxisMove->setVisible(false);
    ui->chkBoxDisableAutoHairline->setChecked(m_gSystem.m_bDisableHairlineOffsetAutoCompensation);

    //CWorker* pWorker=GET_CHDIRECT(M_WORKER,CWorker);
    //if(pWorker->m_workLevel>LEVEL_ADMINISTRATOR){
    //	bool bVisible = true;

    //}else{
    //	bool bVisible = false;
    //}

    //if(pWorker->m_workLevel==LEVEL_FACTORAY){
    //	bool bVisible = true;
    //}else{
    //	bool bVisible = false;
    //}
    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);
    } 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);
        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;
        ui->label_4->setVisible(bVisible);
        ui->label_5->setVisible(bVisible);
        ui->label_3->setVisible(bVisible);
        ui->label->setVisible(bVisible);
        ui->txtSideAStart->setVisible(bVisible);
        ui->txtSideAEnd->setVisible(bVisible);
        ui->txtSideBStart->setVisible(bVisible);
        ui->txtSideBEnd->setVisible(bVisible);
        ui->chkBoxScanWithToSensor->setVisible(bVisible);
        ui->chkBoxDisableCutLineLaserEmit->setVisible(bVisible);
        ui->chkBoxDisableAutoHairline->setVisible(bVisible);
        ui->label_2->setVisible(bVisible);
        ui->txtNozzleWaitTime->setVisible(bVisible);
        ui->pushButton_AllUnLoad->setVisible(bVisible);
        ui->pushButton_Clean_CoatTable->setVisible(bVisible);
        ui->pushButton_Clean_CleanTable->setVisible(bVisible);
    } else {
        bool bVisible = false;
        ui->label_4->setVisible(bVisible);
        ui->label_5->setVisible(bVisible);
        ui->label_3->setVisible(bVisible);
        ui->label->setVisible(bVisible);
        ui->txtSideAStart->setVisible(bVisible);
        ui->txtSideAEnd->setVisible(bVisible);
        ui->txtSideBStart->setVisible(bVisible);
        ui->txtSideBEnd->setVisible(bVisible);
        ui->chkBoxScanWithToSensor->setVisible(bVisible);
        ui->chkBoxDisableCutLineLaserEmit->setVisible(bVisible);
        ui->chkBoxDisableAutoHairline->setVisible(bVisible);
        ui->label_2->setVisible(bVisible);
        ui->txtNozzleWaitTime->setVisible(bVisible);
        ui->pushButton_AllUnLoad->setVisible(bVisible);
        ui->pushButton_Clean_CoatTable->setVisible(bVisible);
        ui->pushButton_Clean_CleanTable->setVisible(bVisible);
    }

    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->chkBoxDisableCutLineLaserEmit->setChecked(m_gSystem.m_bDisableCutlineLaser);

    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);

    FreshTransButtonStatus();
    FreshCoatButtonStatus();
    FreshCleanButtonStatus();
    FreshWorktableButtonStatus();
}

void CMWManualLoad::MTimeFresh()
{
    MFresh();
}

void CMWManualLoad::MInit()
{
    CMWidget::MInit();
    m_TransButtons[CProcess_Trans::TRANSSCANCASS] = ui->btnScanCassete;
    m_TransButtons[CProcess_Trans::kTransCassToRailNear] = ui->btnCassToRailNear;
    m_TransButtons[CProcess_Trans::kTransRailNearToCoat] = ui->btnRailNearToCoat;
    m_TransButtons[CProcess_Trans::kTransCoatToRailFar] = ui->btnCoatToRailFar;
    m_TransButtons[CProcess_Trans::kTransCoatToRailNear] = ui->btnCoatToRailNear;
    m_TransButtons[CProcess_Trans::kTransRailFarToWorktable] = ui->btnRailFarToWt;
    m_TransButtons[CProcess_Trans::kTransRailNearToWorktable] = ui->btnRailNearTOWT;
    m_TransButtons[CProcess_Trans::kTransWorktableToRailNear] = ui->btnWTToRailNear;
    m_TransButtons[CProcess_Trans::kTransRailNearToClean] = ui->btnRailNearToClean;
    m_TransButtons[CProcess_Trans::kTransCleanToRailNear] = ui->btnCleanToRailNear;
    m_TransButtons[CProcess_Trans::kTransCheckBoxToRailNear] = ui->btnCupboardToRailNear;
    m_TransButtons[CProcess_Trans::kTransRailNearToCheckBox] = ui->btnRailNearToCupboard;
    m_TransButtons[CProcess_Trans::kTransRailFarToCoat] = ui->btnRailFarToCoat;
    m_TransButtons[CProcess_Trans::kTransRailNearToFar] = ui->btnRailNearToFar;
    m_TransButtons[CProcess_Trans::kTransRailFarToRailNear] = ui->btnRailFarToNear;
    m_TransButtons[CProcess_Trans::kTransRailNearToCass] = ui->btnRailToCass;

    
    m_TransButtons[VarMsg_Transfer_UnLoadAll] = ui->pushButton_AllUnLoad;

    //涂覆操作
    m_CoatButtons[NORMAL_ACTION] = ui->pushButton_Coat;
    m_CoatButtons[CLEANTABLE_ACTION] = ui->pushButton_Clean_CoatTable;
    m_CoatButtons[CLEANCOATFLUID_ACTION] = ui->btnCleanCoatFluid;


    //正常清洗
    m_CleanButtons[NORMAL_ACTION] = ui->pushButton_Clean;
    //清洗台子
    m_CleanButtons[CLEANTABLE_ACTION] = ui->pushButton_Clean_CleanTable;
}


void CMWManualLoad::on_pushButton_Stop_clicked()
{
    m_gSystem.publish_msg(V_Msg(VarMsg_Stop_ID, QObject::trUtf8("Stop All")));
}

void CMWManualLoad::on_btnScanCassete_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Scan cassette Sig"),
                          QVariantList() << CProcess_Trans::TRANSSCANCASS);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::TRANSSCANCASS);
    //m_gSystem.start_test(SM_TRANS, msgqueue);
}

void CMWManualLoad::on_pushButton_ManualLoad_clicked()
{
    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 CMWManualLoad::on_pushButton_CassToCT_clicked()
{

    //m_gSystem.start_test(SM_TRANS, msgqueue);

}

void CMWManualLoad::on_pushButton_CTTOWT_clicked()
{

    //m_gSystem.start_test(SM_TRANS, msgqueue);

}

void CMWManualLoad::on_pushButton_WTToCT_clicked()
{

}

void CMWManualLoad::on_pushButton_CTToCass_clicked()
{

}


void CMWManualLoad::on_pushButton_Cass_Up_clicked()
{
    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 CMWManualLoad::on_pushButton_Coat_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Coat Sig"),
                          QVariantList() << NORMAL_ACTION << -1 << -1, 0);
    //m_gSystem.start_test(SM_COAT, msgqueue);
    TransBtnClicked(SM_COAT, msgqueue, NORMAL_ACTION);
}

void CMWManualLoad::on_pushButton_Clean_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Clean Sig"),
                          QVariantList() << NORMAL_ACTION << -1 << -1, 0);
    //m_gSystem.start_test(SM_CLEAN, msgqueue);
    TransBtnClicked(SM_CLEAN, msgqueue, NORMAL_ACTION);
}

void CMWManualLoad::on_pushButton_Clean_CoatTable_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Clean coattable Sig"),
                          QVariantList() << CLEANTABLE_ACTION << -1 << -1, 0);
    //m_gSystem.start_test(SM_COAT, msgqueue);
    TransBtnClicked(SM_COAT, msgqueue, CLEANTABLE_ACTION);
}

void CMWManualLoad::on_pushButton_Clean_CleanTable_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Clean coattable Sig"),
                          QVariantList() << CLEANTABLE_ACTION << -1 << -1, 0);
    //m_gSystem.start_test(SM_CLEAN, msgqueue);
    TransBtnClicked(SM_CLEAN, msgqueue, CLEANTABLE_ACTION);
}

void CMWManualLoad::on_pushButton_Single_Auto_clicked()
{
    //   VST_Semi_Main* pSemiState = (VST_Semi_Main*)(m_gSystem.FindSM(SM_MAIN)->m_pState_Semi);
       //if(pSemiState->m_iCurOperator == VarMsg_StartSemi_ID){
       //	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::Yes);
       //	msgBoxAsk.setButtonText(QMessageBox::Yes, tr("Continue"));
       //	msgBoxAsk.setButtonText(QMessageBox::Abort, tr("Abort"));
       //	msgBoxAsk.setButtonText(QMessageBox::Cancel, tr("Cancel"));
       //	int ret = msgBoxAsk.exec();
       //	if(ret == QMessageBox::Abort){
       //		m_gSystem.StopSemi(SM_MAIN);
       //	}else if(ret == QMessageBox::)
       //}

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Single Cut Sig"),
                          QVariantList() << VarMsg_StartSemi_ID);
    //m_gSystem.start_test(SM_MAIN, msgqueue);
    TransBtnClicked(SM_MAIN, msgqueue, VarMsg_StartSemi_ID);

}

void CMWManualLoad::on_pushButton_Pause_clicked()
{
    m_gSystem.publish_msg(V_Msg(VarMsg_Pause_ID,
                                QObject::trUtf8("Pause Sig")));
}

void CMWManualLoad::on_pushButton_AllUnLoad_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("UnLoad All Sig"),
                          QVariantList() << VarMsg_Transfer_UnLoadAll);
    //m_gSystem.start_test(SM_TRANS, msgqueue);
    TransBtnClicked(SM_TRANS, msgqueue, VarMsg_Transfer_UnLoadAll);

}


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

void CMWManualLoad::on_checkBox_CycleRun_clicked(bool checked)
{
    m_gSystem.m_bEnableCycle = checked;
}


void CMWManualLoad::on_btnPEGMove_clicked()
{
    CMotorDriver_ACS* pAcsMotorDriver = GET_CARD(MOTOR_DRIVER, CMotorDriver_ACS);
    Q_ASSERT(pAcsMotorDriver);

    double pegpos[2] = { 400, 480 };

    pAcsMotorDriver->PEGMove_BuffGate(MOTORX, OUT_LASERGATE, 360, 500, pegpos, 2, false);

    pegpos[0] = 480;
    pegpos[1] = 400;
    pAcsMotorDriver->PEGMove_BuffGate(MOTORX, OUT_LASERGATE, 500, 360, pegpos, 2, false);
}

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

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

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

void CMWManualLoad::on_txtSideAStart_editingFinished()
{
    m_gSystem.m_iAsideStart = ui->txtSideAStart->text().toInt() - 1;
}

void CMWManualLoad::on_txtSideAEnd_editingFinished()
{
    m_gSystem.m_iAsideEnd = ui->txtSideAEnd->text().toInt() - 1;
}

void CMWManualLoad::on_txtSideBStart_editingFinished()
{
    m_gSystem.m_iBsideStart = ui->txtSideBStart->text().toInt() - 1;
}

void CMWManualLoad::on_txtSideBEnd_editingFinished()
{
    m_gSystem.m_iBsideEnd = ui->txtSideBEnd->text().toInt() - 1;
}

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

void CMWManualLoad::on_txtNozzleWaitTime_editingFinished()
{
    int waitTime = ui->txtNozzleWaitTime->text().toInt();
    if (m_gSystem.m_iCoatPreSpayWaitTimeMs != waitTime && waitTime >= 0)
        m_gSystem.m_iCoatPreSpayWaitTimeMs = waitTime;
}

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

void CMWManualLoad::on_btnStopSingleCut_clicked()
{
    m_gSystem.StopSemi(SM_MAIN);
}

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

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

void CMWManualLoad::on_chkBoxDisableCutLineLaserEmit_clicked(bool checked)
{
    m_gSystem.m_bDisableCutlineLaser = checked;
}

void CMWManualLoad::on_chkBoxDisableAutoHairline_clicked(bool checked)
{
    m_gSystem.m_bDisableHairlineOffsetAutoCompensation = checked;
}

void CMWManualLoad::on_chkBoxScanWithToSensor_clicked(bool checked)
{
    m_gSystem.m_bScanTwoSensor = checked;
}

void CMWManualLoad::on_chkBoxSimulatorLeveling_clicked(bool checked)
{
    m_gSystem.m_bSimulatorLeveling = checked;
}

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

void CMWManualLoad::on_btnCleanCoatFluid_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID,
                          QObject::trUtf8("Clean coattable coat fulid sig"),
                          QVariantList() << CLEANCOATFLUID_ACTION << -1 << -1, 0);
    //m_gSystem.start_test(SM_COAT, msgqueue);
    TransBtnClicked(SM_COAT, msgqueue, CLEANCOATFLUID_ACTION);
}

void CMWManualLoad::on_btnCupboardToRailNear_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Checkbox to Rail(Near)  Sig"),
                          QVariantList() << CProcess_Trans::kTransCheckBoxToRailNear);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransCheckBoxToRailNear);


}

void CMWManualLoad::on_btnRailNearToCupboard_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail(Near) to Checkbox Sight"),
                          QVariantList() << CProcess_Trans::kTransRailNearToCheckBox);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailNearToCheckBox);

}

void CMWManualLoad::on_btnRailNearTOWT_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail Near to WT Sig"),
                          QVariantList() << CProcess_Trans::kTransRailNearToWorktable);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailNearToWorktable);
}

void CMWManualLoad::on_btnRailNearToFar_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail(Near) to Rail(Far) Sig"),
                          QVariantList() << CProcess_Trans::kTransRailNearToFar);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailNearToFar);
}

void CMWManualLoad::TransBtnClicked(int smID, const V_MsgQueue &msgqueue, int operateId)
{
    if (smID != SM_TRANS && smID != SM_MAIN && smID != SM_COAT&& smID != SM_CLEAN) {
        return;
    }
    int ret = AskButtonOperte(smID, operateId);
    if (ret == QMessageBox::Abort) {
        m_gSystem.StopSemi(smID);
    } else if (ret == QMessageBox::Yes) {
        m_gSystem.start_test(smID, msgqueue);
    } else {
        return;
    }
}

void CMWManualLoad::FreshTransButtonStatus()
{
    QMutexLocker l(&m_gSystem.FindSM(SM_TRANS)->m_SMMutex);
    VST_Semi_Transfer* pSemiState = (VST_Semi_Transfer*)(m_gSystem.FindSM(SM_TRANS)->m_pState_Semi);
    //VS_Thread* state = m_gSystem.FindSM(SM_TRANS)->m_pCurState;
    int iType = m_gSystem.FindSM(SM_TRANS)->m_pCurState->m_iStateType;
    bool bSemiRun = false;
    if (iType == VS_Thread::State_Semi || iType == VS_Thread::State_Work_Semi
        || m_gSystem.FindSM(SM_TRANS)->m_pHistoryState_Semi) {
        //半自动运行中
        bSemiRun = true;
    }
    l.unlock();

    int iCurOperator = pSemiState->m_iCurOperator;
    for (auto iter = m_TransButtons.cbegin(); iter != m_TransButtons.cend(); iter++) {
        if ((iter).key() == iCurOperator) {
            (iter).value()->setEnabled(true);
            (iter).value()->setChecked(true);
        } else {
            if (bSemiRun) {
                (iter).value()->setEnabled(false);
                (iter).value()->setChecked(false);
            } else {
                (iter).value()->setEnabled(true);
                (iter).value()->setChecked(false);
            }
        }
    }
}

void CMWManualLoad::FreshCoatButtonStatus()
{
    VSMT_Base* vsmThread = static_cast<VSMT_Base*>(m_gSystem.FindSM(SM_COAT));
    bool bCanStartSemi = vsmThread->IsCanDoSemi();

    bool bSemiRun = false;
    if (vsmThread->IsSemiPaused() || vsmThread->IsSemiRunning()) {
        //半自动运行中
        bSemiRun = true;
    }


    QMutexLocker l(&vsmThread->m_SMMutex);
    VST_Semi_Coat* pSemiState = (VST_Semi_Coat*)(vsmThread->m_pState_Semi);
    int iCurOperator = pSemiState->m_iCurOperator;
    l.unlock();

    for (auto iter = m_CoatButtons.cbegin(); iter != m_CoatButtons.cend(); iter++) {
        if ((iter).key() == iCurOperator) {
            (iter).value()->setEnabled(true);
            (iter).value()->setChecked(true);
        } else {
            if (bSemiRun) {
                (iter).value()->setEnabled(false);
                (iter).value()->setChecked(false);
            } else {
                if (bCanStartSemi) {
                    (iter).value()->setEnabled(true);
                    (iter).value()->setChecked(false);
                } else {
                    (iter).value()->setEnabled(false);
                    (iter).value()->setChecked(false);
                }
            }
        }
    }
}

void CMWManualLoad::FreshCleanButtonStatus()
{
    QMutexLocker l(&m_gSystem.FindSM(SM_CLEAN)->m_SMMutex);
    VST_Semi_Clean* pSemiState = (VST_Semi_Clean*)(m_gSystem.FindSM(SM_CLEAN)->m_pState_Semi);

    //VS_Thread* state = m_gSystem.FindSM(SM_CLEAN)->m_pCurState;
    int iType = m_gSystem.FindSM(SM_CLEAN)->m_pCurState->m_iStateType;
    bool bSemiRun = false;
    if (iType == VS_Thread::State_Semi || iType == VS_Thread::State_Work_Semi
        || m_gSystem.FindSM(SM_CLEAN)->m_pHistoryState_Semi) {
        //半自动运行中
        bSemiRun = true;
    }
    l.unlock();

    int iCurOperator = pSemiState->m_iCurOperator;
    for (auto iter = m_CleanButtons.cbegin(); iter != m_CleanButtons.cend(); iter++) {
        if ((iter).key() == iCurOperator) {
            (iter).value()->setEnabled(true);
            (iter).value()->setChecked(true);
        } else {
            if (bSemiRun) {
                (iter).value()->setEnabled(false);
                (iter).value()->setChecked(false);
            } else {
                (iter).value()->setEnabled(true);
                (iter).value()->setChecked(false);
            }
        }
    }
}

void CMWManualLoad::FreshWorktableButtonStatus()
{
    QMutexLocker l(&m_gSystem.FindSM(SM_MAIN)->m_SMMutex);
    VST_Semi_Main* pSemiState = (VST_Semi_Main*)(m_gSystem.FindSM(SM_MAIN)->m_pState_Semi);
    int iType = m_gSystem.FindSM(SM_MAIN)->m_pCurState->m_iStateType;
    bool bSemiRun = false;
    if (iType == VS_Thread::State_Semi || iType == VS_Thread::State_Work_Semi
        || m_gSystem.FindSM(SM_MAIN)->m_pHistoryState_Semi) {
        //半自动运行中
        bSemiRun = true;
    }
    l.unlock();

    int iCurOperator = pSemiState->m_iCurOperator;

    if (bSemiRun) {
        if (iCurOperator == VarMsg_StartSemi_ID) {
            ui->pushButton_Single_Auto->setChecked(true);
            ui->pushButton_Single_Auto->setEnabled(true);
        } else {
            ui->pushButton_Single_Auto->setChecked(false);
            ui->pushButton_Single_Auto->setEnabled(false);
        }
    } else {
        ui->pushButton_Single_Auto->setChecked(false);
        ui->pushButton_Single_Auto->setEnabled(true);
    }

    //int iType = m_gSystem.FindSM(SM_MAIN)->m_pCurState->m_iStateType;
    //if(iType == VS_Thread::State_Semi || iType == VS_Thread::State_Work_Semi
    //	|| m_gSystem.FindSM(SM_MAIN)->m_pHistoryState_Semi)
    //{
    //	int iCurOperator = pSemiState->m_iCurOperator;
    //	//半自动运行中
    //	ui->pushButton_Single_Auto->setChecked(true);
    //}else{
    //	ui->pushButton_Single_Auto->setChecked(false);
    //}

}

int CMWManualLoad::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_Main*)(vsmThread->m_pState_Semi))->m_iCurOperator;
        break;
    case SM_COAT:
        curOperateID = ((VST_Semi_Coat*)(vsmThread->m_pState_Semi))->m_iCurOperator;
        break;
    case SM_CLEAN:
        curOperateID = ((VST_Semi_Clean*)(vsmThread->m_pState_Semi))->m_iCurOperator;
        break;
    default:
        return QMessageBox::Yes;
    }*/
    curOperateID = vsmThread->m_pState_Semi->m_iCurOperator;
    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 CMWManualLoad::on_btnCassToRailNear_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Cass to Rail  Sig"),
                          QVariantList() << CProcess_Trans::kTransCassToRailNear);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransCassToRailNear);
}

void CMWManualLoad::on_btnRailFarToWt_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail to WT Sig"),
                          QVariantList() << CProcess_Trans::kTransRailFarToWorktable);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailFarToWorktable);
}

void CMWManualLoad::on_btnRailNearToClean_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail to CT Sig"),
                          QVariantList() << CProcess_Trans::kTransRailNearToClean);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailNearToClean);
}

void CMWManualLoad::on_btnCleanToRailNear_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Clean to Rail Sig"),
                          QVariantList() << CProcess_Trans::kTransCleanToRailNear);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransCleanToRailNear);
}

void CMWManualLoad::on_btnRailNearToCoat_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Clean to Rail Sig"),
                          QVariantList() << CProcess_Trans::kTransRailNearToCoat);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailNearToCoat);
}

void CMWManualLoad::on_btnRailFarToCoat_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail Far to Coat Sig"),
                          QVariantList() << CProcess_Trans::kTransRailFarToCoat);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailFarToCoat);
}

void CMWManualLoad::on_btnRailFarToNear_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail(Far) to Rail(Near) Sig"),
                          QVariantList() << CProcess_Trans::kTransRailFarToRailNear);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailFarToRailNear);
}

void CMWManualLoad::on_btnCoatToRailFar_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Coat to Rail Far Sig"),
                          QVariantList() << CProcess_Trans::kTransCoatToRailFar);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransCoatToRailFar);
}

void CMWManualLoad::on_btnCoatToRailNear_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Coat to Rail Near Sig"),
                          QVariantList() << CProcess_Trans::kTransCoatToRailNear);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransCoatToRailNear);
}

void CMWManualLoad::on_btnWTToRailNear_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Wt to Rail Sig"),
                          QVariantList() << CProcess_Trans::kTransWorktableToRailNear);

    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransWorktableToRailNear);
}

void CMWManualLoad::on_btnRailToCass_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_StartSemi_ID, QObject::trUtf8("Rail to Cass Sig"),
                          QVariantList() << CProcess_Trans::kTransRailNearToCass);
    TransBtnClicked(SM_TRANS, msgqueue, CProcess_Trans::kTransRailNearToCass);
}
