#include "stable.h"
#include "cmwalarmprocess.h"

CMWAlarmProcess::CMWAlarmProcess(QWidget *parent)
	: CMWidget(parent)
{
	ui.setupUi(this);
}

CMWAlarmProcess::~CMWAlarmProcess()
{

}


void CMWAlarmProcess::MFresh()
{
	if(m_gSystem.m_iCurChID!=CAM6X)
	{
		showShieldWindow(true,
			QObject::trUtf8("Switch to cam.6X"));
		return;
	}
	if(m_gSystem.m_iCurSideID!=SIDE_A&&
		m_gSystem.m_iCurSideID!=SIDE_B)
	{
		showShieldWindow(true,
			QObject::trUtf8("Switch to any side."));
		return;
	}

	showShieldWindow(false);

    CProcess_Kerf* processKerf =  GET_PROCESS(PROCESS_KERFCHECK, SM_MAIN, CProcess_Kerf);
    ui.txtKerfPower->setText(QString::number(processKerf->m_dKerfPower));
    ui.txtKerfWidth->setText(QString::number(processKerf->m_dKerfWidth));
}

void CMWAlarmProcess::GetStepMoveDis(double& dX, double& dY)
{
	//    m_pCam->GetRealSize(dX,dY);
	//    if(dX>1) dX=int(dX);else dX=1;//int(dX*10)/10.0;
	//    if(dY>1) dY=int(dY);else dY=1;//int(dY*10)/10.0;
	CProductSide* pSideA=GET_CHDIRECT(SIDE_A,
		CProductSide);
	double dStepA= pSideA->m_Variants.get_VData(Var_ProductSide_SideStep)
		->toDouble();

	CProductSide* pSideB=GET_CHDIRECT(SIDE_B,
		CProductSide);
	double dStepB= pSideB->m_Variants.get_VData(Var_ProductSide_SideStep)
		->toDouble();
	if(m_gSystem.m_iCurSideID==SIDE_B)
	{
		dX=dStepA;dY=dStepB;
	}
	else
	{
		dX=dStepB;dY=dStepA;
	}
}

void CMWAlarmProcess::MotorStepMove(bool bX, bool bStep, int iDirect)
{
    double dX=0,dY=0;
    GetStepMoveDis(dX,dY);
    //iDirect=iDirect>0?1:-1;
    V_MsgQueue msgqueue;
    if(bStep)
    {
		dX*=iDirect;
		dY*=iDirect;
		if(bX)
		{
			m_gSystem.relmove_msg(dX,MOTORX,msgqueue);
		}
		else
		{
			m_gSystem.relmove_msg(dY,MOTORY,msgqueue);
		}

	}
	else
	{
		dX=iDirect*0.001;
		dY=iDirect*0.001;
		if(bX)
		{
			m_gSystem.relmove_msg(dX,MOTORX,msgqueue);
		}
		else
		{
			m_gSystem.relmove_msg(dY,MOTORY,msgqueue);
		}
	}

    m_gSystem.start_manual(SM_MAIN, msgqueue);
}

void CMWAlarmProcess::on_btnStepLeft_clicked()
{
    MotorStepMove(true, true, 1);
}

void CMWAlarmProcess::on_btnStepRight_clicked()
{
    MotorStepMove(true, true, -1);
}

void CMWAlarmProcess::on_btnLeft1um_clicked()
{
     MotorStepMove(true, false, 1);
}

void CMWAlarmProcess::on_btnRight1um_clicked()
{
     MotorStepMove(true, false, -1);
}

void CMWAlarmProcess::on_btnStepUp_clicked()
{
     MotorStepMove(false, true, -1);
}

void CMWAlarmProcess::on_btnStepDown_clicked()
{
    MotorStepMove(false, true , 1);
}

void CMWAlarmProcess::on_btnUp1um_clicked()
{
    MotorStepMove(false, false, -1);
}

void CMWAlarmProcess::on_btnDown1um_clicked()
{
    MotorStepMove(false, false, 1);
}

void CMWAlarmProcess::on_btnHairlineReduce_clicked()
{
    CCam* pCam=GET_CHDIRECT(CAM6X,CCam);
    pCam->m_iHairLenH-=5;
    pCam->SetHairLineToImageContainer(0, true);
}

void CMWAlarmProcess::on_btnExpansion_clicked()
{
    CCam* pCam=GET_CHDIRECT(CAM6X,CCam);
    pCam->m_iHairLenH+=5;
    pCam->SetHairLineToImageContainer(0, true);
}

void CMWAlarmProcess::on_btnManualSetHairline_clicked()
{
    if(m_gSystem.m_iCurSideID!=SIDE_A && m_gSystem.m_iCurSideID!=SIDE_B)
    {
        m_gSystem.m_SysMsg.SetMsg
            (QObject::trUtf8("Currently not on any machining surface"));
        return;
    }

    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Set Kerf Pos Manual Sig"),
                          QVariantList()
                          <<PROCESS_KERFCHECK
                          <<CProcess_Kerf::SETKERFPOSMANUAL <<m_gSystem.m_iCurSideID
                          <<0/*m_gSystem.m_iLayer*/,0);

    m_gSystem.start_manual(SM_MAIN, msgqueue,true);
}

void CMWAlarmProcess::on_btnManualSetBaseOffset_clicked()
{
    if(m_gSystem.m_iCurSideID!=SIDE_A && m_gSystem.m_iCurSideID!=SIDE_B)
    {
        m_gSystem.m_SysMsg.SetMsg
            (QObject::trUtf8("Currently not on any machining surface"));
        return;
    }

    if(m_gSystem.m_iCurSideID!=SIDE_A && m_gSystem.m_iCurSideID!=SIDE_B)
    {
        CProductSide* pSide=GET_CHDIRECT(m_gSystem.m_iCurSideID,CProductSide);
        int iSizeLine =pSide->m_CutLineVector.size();
        if(iSizeLine<=0){
            m_gSystem.m_SysMsg.SetMsg
                (QObject::trUtf8("The current status does not support this operation"));
        }
        return;
    }

	CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING,SM_MAIN,CProcess_Level);
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
                          QObject::trUtf8("Set Pt Manual Sig"),
                          QVariantList()
                          <<PROCESS_LEVELING<<CProcess_Level::SETPT_MANUAL
                          <<m_gSystem.m_iCurSideID<<CAM6X<<CProcess_Level::TYPE_PTBASE
                          <<pLevel->m_iCurLineId,0);
	
    m_gSystem.start_manual(SM_MAIN, msgqueue,true);
}

void CMWAlarmProcess::on_btnPauseCut_clicked()
{
    m_gSystem.FindSM(SM_MAIN)->pushfront_Msg(VarMsg_Pause_ID,
                                       QObject::trUtf8("Pause Sig"));
}

void CMWAlarmProcess::on_txtKerfPower_editingFinished()
{
    CProcess_Kerf* processKerf =  GET_PROCESS(PROCESS_KERFCHECK, SM_MAIN, CProcess_Kerf);
	processKerf->m_dKerfPower = ui.txtKerfPower->text().toDouble();
}

void CMWAlarmProcess::on_txtKerfWidth_editingFinished()
{
	CProcess_Kerf* processKerf =  GET_PROCESS(PROCESS_KERFCHECK, SM_MAIN, CProcess_Kerf);
	processKerf->m_dKerfWidth = ui.txtKerfWidth->text().toDouble();
}

void CMWAlarmProcess::on_btnToKerf_clicked()
{
    V_MsgQueue msgqueue;
    msgqueue.pushback_Msg(VarMsg_Process_ID,
        QObject::trUtf8("Move Kerf P"),
        QVariantList()
		<<PROCESS_LASERKERF
		<<CProcess_LaserKerf::MOVEKERFPOS,0);

    m_gSystem.start_manual(SM_MAIN, msgqueue,true);
}

void CMWAlarmProcess::on_btnCCW_clicked()
{
    V_MsgQueue msgqueue;
    m_gSystem.relmove_msg(-0.001,MOTORW,msgqueue);
    m_gSystem.start_manual(SM_MAIN, msgqueue);
}

void CMWAlarmProcess::on_btnStepCW_clicked()
{
    V_MsgQueue msgqueue;
    m_gSystem.relmove_msg(0.001,MOTORW,msgqueue);
    m_gSystem.start_manual(SM_MAIN, msgqueue);
}
