#include "cmwcalilaserfoucs.h"
#include "ui_cmwcalilaserfoucs.h"
#include "common_include.h"
#include <QMessageBox>
CMWCaliLaserFoucs::CMWCaliLaserFoucs(QWidget* parent, int iLaserPathID) :
    CMWidget(parent),
    ui(new Ui::CMWCaliLaserFoucs)
{
    ui->setupUi(this);
    m_iLaserPathID = iLaserPathID;
    Q_ASSERT(m_iLaserPathID == LASERPATH_D_NARROW 
             || m_iLaserPathID == LASERPATH_D_WIDE 
             || m_iLaserPathID == LASERPATH_S_NARROW
             || m_iLaserPathID == LASERPATH_L_NARROW);
    if(m_iLaserPathID == LASERPATH_D_WIDE) {
        m_pLaserFoucs = GET_PROCESS(PROCESS_LASERFOUCSCALI_D_WIDE,
                                    SM_MAIN,
                                    CProcess_LaserFoucsCali);
    } else if(m_iLaserPathID == LASERPATH_D_NARROW) {
        m_pLaserFoucs = GET_PROCESS(PROCESS_LASERFOUCSCALI_D_NARROW,
                                    SM_MAIN,
                                    CProcess_LaserFoucsCali);
    } else if(m_iLaserPathID == LASERPATH_S_NARROW) {
        m_pLaserFoucs = GET_PROCESS(PROCESS_LASERFOUCSCALI_S_NARROW,
                                    SM_MAIN,
                                    CProcess_LaserFoucsCali);
    }
    else if (m_iLaserPathID == LASERPATH_L_NARROW)
    {
        m_pLaserFoucs = GET_PROCESS(PROCESS_LASERFOUCSCALI_L_NARROW,
                                    SM_MAIN,
                                    CProcess_LaserFoucsCali);
    } 
    Q_ASSERT(m_pLaserFoucs);
    m_iCurChannel = 0;
    m_bMarked = false;
}

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

void CMWCaliLaserFoucs::MInit()
{
    Q_ASSERT(m_pLaserFoucs);
    Variant* pparam[11] = { 0 };
    pparam[0] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_LaserFreq_ID);
    pparam[1] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_LaserEpulse_ID);
    pparam[2] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_LaserPower_ID);
    //pparam[3] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_LaserBeamWidth_ID);
    pparam[3] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_GapDis_ID);
    pparam[4] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_StepZ_ID);
    pparam[5] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_Count_ID);
    pparam[6] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_LineLength_ID);
    pparam[7] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_SpeedX_ID);
    pparam[8] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_AccRate_ID);
    pparam[9] = m_pLaserFoucs->m_Variants.get_Variant(Var_Process_LaserFoucsCali_JerkRate_ID);

    CLaserPath* pPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
    pparam[10] = pPath->m_Variants.get_Variant(Var_LaserPath_HairOffsetThres_ID);

    ui->widget->MParamInit_list(pparam, 11);
    mchildwidgets[(uintptr_t)ui->widget] = ui->widget;
    ui->label_Name->setText(_SysExt_Instance.m_mapNameList[m_iLaserPathID]);

    ui->tableWidget->horizontalHeader()->
        setSectionResizeMode(QHeaderView::Stretch);

//	ui->pushButton_HASet->setVisible(false);
    //ui->comboBox_SINDEX->setVisible(false);
    ui->lineEdit_DINDEX->setVisible(false);
    ui->label_2->setVisible(false);
    ui->label_3->setVisible(false);
    ui->label_4->setVisible(false);
    ui->label_5->setVisible(true);
}

void CMWCaliLaserFoucs::MFresh()
{
    CMWorkTable* pWorkTable = GET_CHDIRECT(WORKTABLE,
                                           CMWorkTable);
    Q_ASSERT(pWorkTable);
    CWorkTable::FOUCSPOSINFO* pInfo =
        pWorkTable->FindFoucsPos(m_iLaserPathID);
    Q_ASSERT(pInfo);
    ui->lineEdit_FoucsPos->setText(QString::number(pInfo->dFoucsPos,
                                                   'f', 3));
    MFreshPos();
    MFreshCombo();
    CMWidget::MFresh();

//	ui->pushButton_HASet->setVisible(false);
    //ui->comboBox_SINDEX->setVisible(false);
    ui->lineEdit_DINDEX->setVisible(false);
    ui->label_2->setVisible(false);
    ui->label_3->setVisible(false);
    ui->label_4->setVisible(false);
    ui->label_5->setVisible(true);

    CLaserPath* pLaserPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
    Q_ASSERT(pLaserPath);
    double dOffsetY = 0;
    pLaserPath->GetOffset(dOffsetY);
    ui->txtLaserOffsetY->setText(QString::number(dOffsetY, 'f', 5));

    CWorker* pWorker = GET_CHDIRECT(M_WORKER, CWorker);
    if(pWorker->m_workLevel >= LEVEL_EQUIPMENT) {
        ui->pushButton_FoucsSearchUp->setVisible(true);
        ui->pushButton_FoucsSearchDown->setVisible(true);
        ui->txtLaserOffsetY->setEnabled(true);
        ui->pushButton_SetFoucs->setEnabled(true);
        ui->lineEdit_FoucsPos->setEnabled(true);
        ui->btnCalibrationHairLine->setEnabled(true);
        ui->pushButton_CurPos->setEnabled(true);
    } else {
        ui->pushButton_FoucsSearchUp->setVisible(false);
        ui->pushButton_FoucsSearchDown->setVisible(false);
        ui->txtLaserOffsetY->setEnabled(false);
        ui->pushButton_SetFoucs->setEnabled(false);
        ui->lineEdit_FoucsPos->setEnabled(false);
        ui->btnCalibrationHairLine->setEnabled(false);
        ui->pushButton_CurPos->setEnabled(false);
    }
}

void CMWCaliLaserFoucs::MApply()
{
    CMWorkTable* pWorkTable = GET_CHDIRECT(WORKTABLE,
                                           CMWorkTable);
    Q_ASSERT(pWorkTable);
    CWorkTable::FOUCSPOSINFO* pInfo =
        pWorkTable->FindFoucsPos(m_iLaserPathID);
    Q_ASSERT(pInfo);
    pInfo->dFoucsPos = ui->lineEdit_FoucsPos->text().toDouble();

    CMWidget::MApply();
}

void CMWCaliLaserFoucs::MTimeFresh()
{
//    if(m_iCurChannel!=m_gSystem.m_iCurChID)
//    {
//        m_iCurChannel=m_gSystem.m_iCurChID;
//        ui->pushButton_HASet->setEnabled(m_iCurChannel==CAM6X);
//    }
}

void CMWCaliLaserFoucs::MFreshPos()
{
    int isize = m_VectorFoucsPt.size();
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(isize);
    for(int i = 0; i < isize; i++) {
        FOUCSPT& pt = m_VectorFoucsPt[i];
        QTableWidgetItem* pItem =
            new QTableWidgetItem(QString::number(pt.dZ, 'f', 3));
        ui->tableWidget->setItem(i, 0, pItem);
        pItem = new QTableWidgetItem(QString::number(pt.dX, 'f', 3));
        ui->tableWidget->setItem(i, 1, pItem);
        pItem = new QTableWidgetItem(QString::number(pt.dY, 'f', 3));
        ui->tableWidget->setItem(i, 2, pItem);
    }

}

void CMWCaliLaserFoucs::MFreshCombo()
{

    if(m_iLaserPathID == LASERPATH_D_WIDE || m_iLaserPathID == LASERPATH_L_NARROW) {
        QString s = ui->comboBox_SINDEX->currentText();
        CLaserPath* pLaserPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
        Q_ASSERT(pLaserPath);
        ui->comboBox_SINDEX->clear();
        LPATHBEAMPOSMAP::iterator ii = pLaserPath->m_mBeamPos.begin();
        for(; ii != pLaserPath->m_mBeamPos.end(); ii++) {
            double sindex = ii.key() / 1000.0;
            ui->comboBox_SINDEX->addItem
            (QString::number(sindex, 'f', 3), sindex);
        }
        ui->comboBox_SINDEX->setCurrentText(s);
    } 
    else if(m_iLaserPathID == LASERPATH_D_NARROW 
            || m_iLaserPathID == LASERPATH_S_NARROW)
    {
        QString s = ui->comboBox_SINDEX->currentText();
        CLaserPath* pLaserPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
        Q_ASSERT(pLaserPath);
        ui->comboBox_SINDEX->clear();
        auto ii = pLaserPath->m_vDIndexParam.begin();
        for(; ii != pLaserPath->m_vDIndexParam.end(); ii++) {
            double sindex = ii->first / 1000.0;
            ui->comboBox_SINDEX->addItem
            (QString::number(sindex, 'f', 3), sindex);
        }
        ui->comboBox_SINDEX->setCurrentText(s);
        //ui->comboBox_SINDEX->setEnabled(false);
    }
}

void CMWCaliLaserFoucs::resizeEvent(QResizeEvent* event)
{
    CMWidget::resizeEvent(event);
}

void CMWCaliLaserFoucs::DrawSingle(int iIndex, V_MsgQueue& msgqueue)
{
    if(OUTOFRANGE(iIndex, 0, m_VectorFoucsPt.size())) {
        return;
    }
    FOUCSPT& pt = m_VectorFoucsPt[iIndex];
    DrawSingle(CMPointd(pt.dX, pt.dY), pt.dZ, msgqueue);
}

//打一个标记点
void CMWCaliLaserFoucs::DrawSingle(CMPointd WPDP, double dZPos, V_MsgQueue& msgqueue)
{

    CMProduct* pProduct = GET_CHDIRECT(PRODUCT,
                                       CMProduct);
    Q_ASSERT(pProduct);
    double dThickNess = pProduct->m_Variants.get_VData(Var_Product_Thick_ID)->toDouble();
    double dFilmThickNess = pProduct->m_Variants.get_VData(Var_MProduct_FilmThick_ID)->toDouble();
    dZPos = dZPos + dThickNess + dFilmThickNess;

    double dDIndex = ui->lineEdit_DINDEX->text().toDouble();
    double dSIndex = ui->comboBox_SINDEX->currentText().toDouble();
    bool bcross = ui->radioButton_Cross->isChecked();

    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Laser Foucs Mark"),
                          QVariantList()
                          << m_pLaserFoucs->m_iID
                          << CProcess_LaserFoucsCali::LASER_MARK
                          << WPDP.tX << WPDP.tY << dZPos
                          << dDIndex << dSIndex << bcross, 0);

    m_InitWPDP = WPDP;
    m_bMarked = true;
}

void CMWCaliLaserFoucs::on_pushButton_LaserMark_clicked()
{
    CMWorkTableDriver* pWorkTableDriver = GET_CARD(WORKTABLE_DRIVER,
                                                   CMWorkTableDriver);
    Q_ASSERT(pWorkTableDriver);
    CMPointd wt;
    pWorkTableDriver->CurAxisToWt(_SysExt_Instance.m_iCurChID,
                                  wt, true);

    CMWorkTable* pWorkTable = GET_CHDIRECT(WORKTABLE, CMWorkTable);
    Q_ASSERT(pWorkTable);
    CWorkTable::FOUCSPOSINFO* pInfo = pWorkTable->FindFoucsPos(m_iLaserPathID);
    Q_ASSERT(pInfo);
    double dFoucsPos = pInfo->dFoucsPos;
    double dZOffset = ui->txtZOffset->text().toDouble();
    V_MsgQueue msgqueue;
    DrawSingle(wt, dFoucsPos + dZOffset, msgqueue);

    CMPointd axis;
    pWorkTableDriver->WtToAxis(LASERPATH_D_NARROW, wt, axis);
    _SysExt_Instance.absmove_xy_msg(axis.tX, axis.tY, msgqueue);

    _SysExt_Instance.changeChannel_msg(CAM6X, true, msgqueue);

    //移动到定位点
    pWorkTableDriver->WtToAxis(CAM6X, wt, axis);
    _SysExt_Instance.absmove_xy_msg(axis.tX, axis.tY, msgqueue);

    _SysExt_Instance.start_test(SM_MAIN, msgqueue);
}

void CMWCaliLaserFoucs::on_tableWidget_cellClicked(int row, int column)
{
    UNUSED(column);
    m_iSelRow = row;
    ui->label_CurFoucs->
        setText(ui->tableWidget->item(row, 0)->text());
    CMWorkTableDriver* pWorkTableDriver = GET_CARD(WORKTABLE_DRIVER,
                                                   CMWorkTableDriver);
    Q_ASSERT(pWorkTableDriver);
    CMPointd wt, axis;
    wt.tX = ui->tableWidget->item(row, 1)->text().toDouble();
    wt.tY = ui->tableWidget->item(row, 2)->text().toDouble();
    pWorkTableDriver->WtToAxis(_SysExt_Instance.m_iCurChID,
                               wt, axis);

    V_MsgQueue msgqueue;
    _SysExt_Instance.absmove_xy_msg(axis.tX, axis.tY, msgqueue);
    _SysExt_Instance.start_test(SM_MAIN, msgqueue, false);

    m_InitWPDP = wt;
}

//Hair Line Setting
void CMWCaliLaserFoucs::on_pushButton_HASet_clicked()
{
    if(_SysExt_Instance.m_iCurChID != CAM6X) {
        _SysExt_Instance.m_SysMsg.SetMsg(tr("Hairline set only in Cam6X"));
        return;
    }

    if (QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"),
                                                  QObject::tr("Are you sure to set hairline")))
    {
        return;
    }

    if(false == m_bMarked) {
        _SysExt_Instance.m_SysMsg.SetMsg(tr("Mark first"));
        return;
    }

    CMWorkTableDriver* pWorkTableDriver = GET_CARD(WORKTABLE_DRIVER,
                                                   CMWorkTableDriver);
    Q_ASSERT(pWorkTableDriver);

    CMPointd wt;
    pWorkTableDriver->CurAxisToWt(_SysExt_Instance.m_iCurChID, wt, true);

    //直接校正坐标系里面的值
//    if(DEQUAL0(dIndex)&&DEQUAL0(sIndex))
//    {
//        CMPointd Axis1,Axis2;
//        pWorkTableDriver->WtToAxis(m_iLaserPathID,
//                                   m_InitWPDP,Axis1);
//        pWorkTableDriver->WtToAxis(CAM6X,wt,Axis2);
//        pWorkTableDriver->CaliCoord(m_iLaserPathID,Axis1,Axis2);
//    }
//    else
//    {

    CLaserPath* pLaserPath = GET_CHDIRECT(m_iLaserPathID, CLaserPath);
    Q_ASSERT(pLaserPath);
    double dOffsetY = m_InitWPDP.tY - wt.tY;
    double dAbsOffset = 0;
#ifdef __USE_MOTOR_PATH__
    double dIndex = ui->lineEdit_DINDEX->text().toDouble();
    double sIndex = ui->comboBox_SINDEX->currentText().toDouble();
    pLaserPath->GetOffset(dIndex, sIndex, dAbsOffset);
#else
    pLaserPath->GetOffset(dAbsOffset);
#endif
    dAbsOffset += dOffsetY;
    double dHairOffsetThres =
        pLaserPath->m_Variants.get_VData(Var_LaserPath_HairOffsetThres_ID)->toDouble();

    if(fabs(dAbsOffset) > dHairOffsetThres) {
        QMessageBox::StandardButton buttonID =
            QMessageBox::critical(this, "HairLine",
                                  tr("Hairline Offset:%1mm."
                                     "max then threshold:%2mm."
                                     "press yes to continue.")
                                  .arg(dAbsOffset, 0, 'f', 3)
                                  .arg(dHairOffsetThres, 0, 'f', 3)
                                  , QMessageBox::Yes | QMessageBox::No);
        if(buttonID == QMessageBox::No) {
            return;
        }
    }
#ifdef __USE_MOTOR_PATH__
    pLaserPath->SetOffset(dIndex, sIndex, dAbsOffset);
#else
    pLaserPath->SetOffset(dAbsOffset);
#endif
//    }
    m_InitWPDP = wt;

    pLaserPath->GetOffset(dOffsetY);
    ui->txtLaserOffsetY->setText(QString::number(dOffsetY));
}

void CMWCaliLaserFoucs::on_pushButton_FoucsSearch_clicked()
{
    if (QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"),
                                                  QObject::tr("Are you sure to focus search")))
    {
        return;
    }

    MApply();
    //Y向偏移
    //CalcPosListY();
    CalcuPosList();
    int iSize = m_VectorFoucsPt.size();
    //批量打点
    V_MsgQueue msgqueue;
    for(int i = 0; i < iSize; i++) {
        DrawSingle(i, msgqueue);
    }
    _SysExt_Instance.changeChannel_msg(CAM6X, true, msgqueue);
    _SysExt_Instance.start_test(SM_MAIN, msgqueue);

}

void CMWCaliLaserFoucs::CalcuPosList()
{
    CMWorkTableDriver* pWorkTableDriver = GET_CARD(WORKTABLE_DRIVER,
                                                   CMWorkTableDriver);
    Q_ASSERT(pWorkTableDriver);

    CMWorkTable* pWorkTable = GET_CHDIRECT(WORKTABLE, CMWorkTable);
    Q_ASSERT(pWorkTable);
    CWorkTable::FOUCSPOSINFO* pInfo = pWorkTable->FindFoucsPos(m_iLaserPathID);
    Q_ASSERT(pInfo);
    double dFoucsPos = pInfo->dFoucsPos;
    CMPointd WPDP;
    double dZ = dFoucsPos;
    pWorkTableDriver->CurAxisToWt(_SysExt_Instance.m_iCurChID, WPDP, true);
    int iCount = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_Count_ID)
        ->toInt();
    double dCrossDis = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_GapDis_ID)
        ->toDouble();
    WPDP.tX -= iCount / 2 * dCrossDis;
    WPDP.tY += iCount / 2 * dCrossDis;
    double dStep = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_StepZ_ID)
        ->toDouble();
    m_VectorFoucsPt.clear();
    //向上偏移
    FOUCSPT foucsPT;
    //为0时表示当前
    for(int i = iCount; i >= 0; i--) {
        foucsPT.dX = WPDP.tX - dCrossDis * 0;
        foucsPT.dY = WPDP.tY + dCrossDis * i;
        foucsPT.dZ = dZ + dStep * i;
        m_VectorFoucsPt.push_back(foucsPT);
    }

    //向下偏移
    for(int i = 1; i <= iCount; i++) {
        foucsPT.dX = WPDP.tX + dCrossDis * 0;
        foucsPT.dY = WPDP.tY - dCrossDis * i;
        foucsPT.dZ = dZ - dStep * i;
        m_VectorFoucsPt.push_back(foucsPT);
    }
    MFreshPos();
}


void CMWCaliLaserFoucs::CalcPosListY()
{
    CMWorkTableDriver* pWorkTableDriver = GET_CARD(WORKTABLE_DRIVER,
                                                   CMWorkTableDriver);
    Q_ASSERT(pWorkTableDriver);

    CMWorkTable* pWorkTable = GET_CHDIRECT(WORKTABLE, CMWorkTable);
    Q_ASSERT(pWorkTable);
    CWorkTable::FOUCSPOSINFO* pInfo = pWorkTable->FindFoucsPos(m_iLaserPathID);
    Q_ASSERT(pInfo);
    double dFoucsPos = pInfo->dFoucsPos;
    CMPointd WPDP;
    double dZ = dFoucsPos;
    pWorkTableDriver->CurAxisToWt(_SysExt_Instance.m_iCurChID, WPDP, true);
    int iCount = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_Count_ID)
        ->toInt();
    double dCrossDis = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_GapDis_ID)
        ->toDouble();

    WPDP.tX -= iCount / 2 * dCrossDis;
    WPDP.tY += iCount / 2 * dCrossDis;

    double dStep = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_StepZ_ID)
        ->toDouble();
    m_VectorFoucsPt.clear();
    //向上偏移
    FOUCSPT foucsPT;
    //为0时表示当前
    //Z 向上为正
    for(int i = iCount; i >= 0; i--) {
        foucsPT.dX = WPDP.tX + dCrossDis * i;
        foucsPT.dY = WPDP.tY - dCrossDis * 0;
        foucsPT.dZ = dZ + dStep * i;
        m_VectorFoucsPt.push_back(foucsPT);
    }

    //向下偏移
    for(int i = 1; i <= iCount; i++) {
        foucsPT.dX = WPDP.tX - dCrossDis * i;
        foucsPT.dY = WPDP.tY + dCrossDis * 0;
        foucsPT.dZ = dZ - dStep * i;
        m_VectorFoucsPt.push_back(foucsPT);
    }
    MFreshPos();
}

void CMWCaliLaserFoucs::CalcuPosListUp(bool bUp)
{
    CMWorkTableDriver* pWorkTableDriver = GET_CARD(WORKTABLE_DRIVER,
                                                   CMWorkTableDriver);
    Q_ASSERT(pWorkTableDriver);

    CMWorkTable* pWorkTable = GET_CHDIRECT(WORKTABLE, CMWorkTable);
    Q_ASSERT(pWorkTable);
    CWorkTable::FOUCSPOSINFO* pInfo = pWorkTable->FindFoucsPos(m_iLaserPathID);
    Q_ASSERT(pInfo);
    double dFoucsPos = pInfo->dFoucsPos;
    CMPointd WPDP;
    double dZ = dFoucsPos;
    pWorkTableDriver->CurAxisToWt(_SysExt_Instance.m_iCurChID, WPDP, true);
    int iCount = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_Count_ID)
        ->toInt();
    double dCrossDis = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_GapDis_ID)
        ->toDouble();
    WPDP.tX -= iCount / 2 * dCrossDis;
    WPDP.tY += iCount / 2 * dCrossDis;
    double dStep = m_pLaserFoucs->m_Variants.get_VData(Var_Process_LaserFoucsCali_StepZ_ID)
        ->toDouble();
    m_VectorFoucsPt.clear();
    //向上偏移
    FOUCSPT foucsPT;
    if(true == bUp) {
        //为0时表示当前
        for(int i = iCount; i >= 0; i--) {
            foucsPT.dX = WPDP.tX - dCrossDis * 0;
            foucsPT.dY = WPDP.tY + dCrossDis * i;
            foucsPT.dZ = dZ - dStep * i;
            m_VectorFoucsPt.push_back(foucsPT);
        }
    } else {

        //向下偏移
        for(int i = 0; i <= iCount; i++) {
            foucsPT.dX = WPDP.tX + dCrossDis * 0;
            foucsPT.dY = WPDP.tY - dCrossDis * i;
            foucsPT.dZ = dZ + dStep * i;
            m_VectorFoucsPt.push_back(foucsPT);
        }
    }
    MFreshPos();
}


void CMWCaliLaserFoucs::on_pushButton_CurPos_clicked()
{
    CMotorDriver* pDriver = GET_CARD(MOTOR_DRIVER, CMotorDriver);
    Q_ASSERT(pDriver);

    double dZPos = pDriver->get_posdata(MOTORZ, true);

    CMProduct* pProduct = GET_CHDIRECT(PRODUCT, CMProduct);
    Q_ASSERT(pProduct);

    //片厚及膜厚
    double dThickNess = pProduct->m_Variants.get_VData(Var_Product_Thick_ID)->toDouble();
    double dFilmThickNess = pProduct->m_Variants.get_VData(Var_MProduct_FilmThick_ID)->toDouble();

    dZPos = dZPos + dThickNess + dFilmThickNess;
    ui->lineEdit_FoucsPos->setText(QString::number(dZPos, 'f', 3));

}

void CMWCaliLaserFoucs::on_pushButton_SetFoucs_clicked()
{
    ui->lineEdit_FoucsPos->setText(ui->label_CurFoucs->text());
}

void CMWCaliLaserFoucs::on_pushButton_FoucsSearchUp_clicked()
{
    MApply();
    CalcuPosListUp(true);
    int iSize = m_VectorFoucsPt.size();
    V_MsgQueue msgqueue;
    for(int i = 0; i < iSize; i++) {
        DrawSingle(i, msgqueue);
    }
    _SysExt_Instance.changeChannel_msg(CAM6X, true, msgqueue);
    _SysExt_Instance.start_test(SM_MAIN, msgqueue);
}

void CMWCaliLaserFoucs::on_pushButton_FoucsSearchDown_clicked()
{
    MApply();
    CalcuPosListUp(false);
    int iSize = m_VectorFoucsPt.size();
    V_MsgQueue msgqueue;
    for(int i = 0; i < iSize; i++) {
        DrawSingle(i, msgqueue);
    }
    _SysExt_Instance.changeChannel_msg(CAM6X, true, msgqueue);
    _SysExt_Instance.start_test(SM_MAIN, msgqueue);
}

void CMWCaliLaserFoucs::on_btnCalibrationHairLine_clicked()
{
    if(_SysExt_Instance.m_iCurChID != CAM6X) {
        _SysExt_Instance.m_SysMsg.SetMsg(tr("Hairline set only in Cam6X"));
        return;
    }

    if (QMessageBox::Yes != QMessageBox::question(this, QObject::tr("Confirm"),
                                                  QObject::tr("Are you sure to calibration laser to new position")))
    {
        return;
    }

    if(false == m_bMarked) {
        _SysExt_Instance.m_SysMsg.SetMsg(tr("Mark first"));
        return;
    }

    CMWorkTableDriver* pWorkTableDriver = GET_CARD(WORKTABLE_DRIVER,
                                                   CMWorkTableDriver);
    Q_ASSERT(pWorkTableDriver);
//    double dIndex=ui->lineEdit_DINDEX->text().toDouble();
//    double sIndex=ui->comboBox_SINDEX->currentText().toDouble();
    CMPointd wt;
    pWorkTableDriver->CurAxisToWt(_SysExt_Instance.m_iCurChID, wt, true);

    //直接校正坐标系里面的值,移动后校正
    CMPointd Axis1, Axis2;
    pWorkTableDriver->WtToAxis(m_iLaserPathID, m_InitWPDP, Axis1);
    pWorkTableDriver->WtToAxis(CAM6X, wt, Axis2);
    pWorkTableDriver->CaliCoord(m_iLaserPathID, Axis1, Axis2);

    m_InitWPDP = wt;
}
