#include "cmwcaliworktablefoucssingle.h"
#include "ui_cmwcaliworktablefoucssingle.h"
#include "logic.h"
CMWCaliWorkTableFoucsSingle::CMWCaliWorkTableFoucsSingle(QWidget *parent) :
    CMWidget(parent),
    ui(new Ui::CMWCaliWorkTableFoucsSingle)
{
    ui->setupUi(this);

	m_iWTID=WORKTABLE;
    m_iCHID=0;
}

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

void CMWCaliWorkTableFoucsSingle::MFresh()
{
	CWorkTable* pworkTable=GET_CHDIRECT(m_iWTID,CWorkTable);
    Q_ASSERT(pworkTable);
    CWorkTable::FOUCSPOSINFO* pFoucsInfo=pworkTable->FindFoucsPos(m_iCHID);
    Q_ASSERT(pFoucsInfo);

    ui->lineEdit_foucsPos->setText(QString::number(pFoucsInfo->dFoucsPos));
    ui->lineEdit_foucsRange->setText(QString::number(pFoucsInfo->dCaliRange));
	ui->lineEdit_FoucsSpd->setText(QString::number(pFoucsInfo->dCaliSpd));
    ui->lineEdit_FoucsSpd_Min->setText(QString::number(pFoucsInfo->dCaliSpdMin));
	ui->lineEdit_foucsRange_min->setText(QString::number(pFoucsInfo->dCaliRangeMin));
}


void CMWCaliWorkTableFoucsSingle::MApply()
{
	CWorkTable* pworkTable=GET_CHDIRECT(m_iWTID,CWorkTable);
	Q_ASSERT(pworkTable);
    CWorkTable::FOUCSPOSINFO* pFoucsInfo=pworkTable->FindFoucsPos(m_iCHID);
    Q_ASSERT(pFoucsInfo);

    pFoucsInfo->dFoucsPos=ui->lineEdit_foucsPos->text().toDouble();
    pFoucsInfo->dCaliRange=ui->lineEdit_foucsRange->text().toDouble();
	pFoucsInfo->dCaliSpd=ui->lineEdit_FoucsSpd->text().toDouble();
    pFoucsInfo->dCaliSpdMin=ui->lineEdit_FoucsSpd_Min->text().toDouble();
	pFoucsInfo->dCaliRangeMin=ui->lineEdit_foucsRange_min->text().toDouble();

}

void CMWCaliWorkTableFoucsSingle::on_pushButton_CaliFoucs_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,QObject::trUtf8("Foucs Cali Sig"),
                          QVariantList()
                          <<WORKTABLE_DRIVER<<CMWorkTableDriver::MEASTABLEFOUCSPOS
                          <<m_iCHID,0);
    m_gSystem.start_test(SM_MAIN, msgqueue);

}

void CMWCaliWorkTableFoucsSingle::on_pushButton_setcur_foucs_clicked()
{
	CMotor *pMotorZ=GET_CHDIRECT(MOTORZ,CMotor);
    Q_ASSERT(pMotorZ);

	CWorkTable* pworkTable=GET_CHDIRECT(m_iWTID,CWorkTable);
    Q_ASSERT(pworkTable);
    CWorkTable::FOUCSPOSINFO* pFoucsInfo=pworkTable->FindFoucsPos(m_iCHID);
    Q_ASSERT(pFoucsInfo);

    pFoucsInfo->dFoucsPos=pMotorZ->m_Variants.get_VData(Var_Motor_Pos_ID)
            ->toDouble();

    MFresh();
}

void CMWCaliWorkTableFoucsSingle::on_pushButton_moveto_Foucs_clicked()
{
    CWorkTable* pworkTable=GET_CHANNEL(m_iWTID,WORKTABLE_DRIVER,CWorkTable);
    Q_ASSERT(pworkTable);
    CWorkTable::FOUCSPOSINFO* pFoucsInfo=pworkTable->FindFoucsPos(m_iCHID);
    Q_ASSERT(pFoucsInfo);
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Driver_ID,QObject::trUtf8("Foucs Cali Sig"),
                          QVariantList()
                          <<MOTOR_DRIVER<<CMotorDriver::ABSMOVE
                          <<MOTORZ<<pFoucsInfo->dFoucsPos,0);
    m_gSystem.start_test(SM_MAIN, msgqueue);
}
void CMWCaliWorkTableFoucsSingle::on_pushButton_setcur_foucs_ref_clicked()
{
    CMotor *pMotorZ=GET_CHDIRECT(MOTORZ,CMotor);
    Q_ASSERT(pMotorZ);

    CWorkTable* pworkTable=GET_CHDIRECT(m_iWTID,CWorkTable);
    Q_ASSERT(pworkTable);
    CWorkTable::FOUCSPOSINFO* pFoucsInfo=pworkTable->FindFoucsPos(m_iCHID);
    Q_ASSERT(pFoucsInfo);

    CMProduct* pProduct=GET_CHDIRECT(PRODUCT,CMProduct);
    double dThickNess=pProduct->m_Variants.get_VData(Var_Product_Thick_ID)->toDouble();
    double dFilmThickNess=pProduct->m_Variants.get_VData(Var_MProduct_FilmThick_ID)->toDouble();
    pFoucsInfo->dFoucsPos=pMotorZ->m_Variants.get_VData(Var_Motor_Pos_ID)
            ->toDouble() - dThickNess - dFilmThickNess;

    MFresh();
}
