#include "cctrlmimg_opengl_cam.h"
#include "logic.h"
#include "cdlgparam_alter.h"
#include <QPainter>
CCtrlMImg_OPenGL_Cam::CCtrlMImg_OPenGL_Cam(QWidget *parent)
    :CCtrlMImg_OPenGL(parent)
{
    m_pCam=0;

	m_dOrinScale = 0.5;
	m_bIsCamera = true;

    m_ButtonMap[BTN_LIVE]=CCtrlGraphButton(trUtf8("Live"),false);
    m_ButtonMap[BTN_CLEAR]=CCtrlGraphButton(trUtf8("Clear"),false);
    m_ButtonMap[BTN_ENABLEMOVE]=CCtrlGraphButton(trUtf8("Move"),false);
    m_ButtonMap[BTN_ENABLEZOOM]=CCtrlGraphButton(trUtf8("Zoom"),false);
    m_ButtonMap[BTN_HLINEV_ADD]=CCtrlGraphButton(CCtrlGraphButton::EXPAND_H,false);
    m_ButtonMap[BTN_HLINEV_REDUCE]=CCtrlGraphButton(CCtrlGraphButton::REDUCE_H,false);
    m_ButtonMap[BTN_HLINEH_ADD]=CCtrlGraphButton(CCtrlGraphButton::EXPAND_V,true);
    m_ButtonMap[BTN_HLINEH_REDUCE]=CCtrlGraphButton(CCtrlGraphButton::REDUCE_V,true);

    m_ButtonMap[BTN_LEFT_1UM]=CCtrlGraphButton(CCtrlGraphButton::LEFT,false);
    m_ButtonMap[BTN_RIGHT_1UM]=CCtrlGraphButton(CCtrlGraphButton::RIGHT,false);

    m_ButtonMap[BTN_LEFT_STEP]=CCtrlGraphButton(CCtrlGraphButton::LEFT2,false);
    m_ButtonMap[BTN_RIGHT_STEP]=CCtrlGraphButton(CCtrlGraphButton::RIGHT2,false);

    m_ButtonMap[BTN_TOP_1UM]=CCtrlGraphButton(CCtrlGraphButton::UP,true);
    m_ButtonMap[BTN_BOTTOM_1UM]=CCtrlGraphButton(CCtrlGraphButton::DOWN,true);

    m_ButtonMap[BTN_TOP_STEP]=CCtrlGraphButton(CCtrlGraphButton::UP2,true);
    m_ButtonMap[BTN_BOTTOM_STEP]=CCtrlGraphButton(CCtrlGraphButton::DOWN2,true);

    m_ButtonMap[STR_HLINE_H]=CCtrlGraphButton(QString("0um"),true);
    m_ButtonMap[STR_HLINE_V]=CCtrlGraphButton(QString("0um"),false);

    m_ButtonMap[STR_DIS_X]=CCtrlGraphButton(QString("1um"),false);
    m_ButtonMap[STR_DIS_Y]=CCtrlGraphButton(QString("1um"),true);

	m_ButtonMap[BTN_LOCKMOVE_X]=CCtrlGraphButton(trUtf8("LockX"),false);
	m_ButtonMap[BTN_LOCKMOVE_Y]=CCtrlGraphButton(trUtf8("LockY"),false);
	m_ButtonMap[BTN_HIDESHOW]=CCtrlGraphButton(trUtf8("Hide"),false);

    m_iCaptureBtnID=BTN_RESERVE;

    m_bEnableClickZoom=false;
	m_bLockAxisX = false;
	m_bLockAxisY = false;
	m_bBtnHide = false;

	m_iStepValueX = 1;
	m_iStepValueY = 1;
	m_iStepValueV = 1;
	m_iStepValueH = 1;

    m_iJogHLine=false;
    rectPictureX=QRect(0,0,60,35);
    rectPictureY=QRect(0,0,35,60);
}

void CCtrlMImg_OPenGL_Cam::BtnClick(int iBtnID)
{
	if (m_bBtnHide
		&& iBtnID != BTN_HIDESHOW)
	{
		return;
	}

    switch(iBtnID)
    {
    case BTN_LIVE:  m_pCam->m_bTransImage=!m_pCam->m_bTransImage;break;
    case BTN_CLEAR: m_pCam->m_imageContainer.clearShape(-1);
                    m_pCam->m_imageContainer.clearString();
                    break;
    case BTN_ENABLEMOVE:m_pCam->m_bCamMove=!m_pCam->m_bCamMove;
        if(m_pCam->m_bCamMove)
        {
            m_pCam->m_bTransImage=true;
        }
        break;
    case BTN_ENABLEZOOM:
        if(m_bFit)
        {
            ZoomOrginal();
			m_bEnableMouseZoom = true;
        }
        else
        {
            ZoomFit();
			m_bEnableMouseZoom = false;
        }
        m_ButtonMap[iBtnID].m_bChecked=!m_bFit;
        break;
    case BTN_HLINEV_ADD:    on_pushButton_HLineV_add_clicked();break;
    case BTN_HLINEV_REDUCE:  on_pushButton_HLineV_reduce_clicked();break;
    case BTN_HLINEH_ADD:    on_pushButton_HLineH_add_clicked();break;
    case BTN_HLINEH_REDUCE:  on_pushButton_HLineH_reduce_clicked();break;

    case BTN_LEFT_1UM:  on_pushButton_Left1um_clicked();break;
    case BTN_RIGHT_1UM: on_pushButton_Right1um_clicked();break;

    case BTN_LEFT_STEP:  on_pushButton_LeftStep_clicked();break;
    case BTN_RIGHT_STEP: on_pushButton_RightStep_clicked();break;

    case BTN_TOP_1UM: on_pushButton_Up1um_clicked();break;
    case BTN_BOTTOM_1UM: on_pushButton_Down1um_clicked();break;

    case BTN_TOP_STEP: on_pushButton_UpStep_clicked();break;
    case BTN_BOTTOM_STEP: on_pushButton_DownStep_clicked();break;

    case STR_HLINE_H: MoveHLine(false,0);break;
    case STR_HLINE_V: MoveHLine(true,0);break;

	case BTN_LOCKMOVE_X:m_bLockAxisX=!m_bLockAxisX;break;
	case BTN_LOCKMOVE_Y:m_bLockAxisY=!m_bLockAxisY;break;
	case BTN_HIDESHOW:m_bBtnHide=!m_bBtnHide;break;
    }
}

void CCtrlMImg_OPenGL_Cam::BtnPress(int iBtnID)
{
    switch(iBtnID)
    {
    case BTN_HLINEV_ADD:    on_pushButton_HLineV_add_pressed();break;
    case BTN_HLINEV_REDUCE:  on_pushButton_HLineV_reduce_pressed();break;
    case BTN_HLINEH_ADD:    on_pushButton_HLineH_add_pressed();break;
    case BTN_HLINEH_REDUCE:  on_pushButton_HLineH_reduce_pressed();break;

    case BTN_LEFT_1UM:  on_pushButton_Left1um_pressed();break;
    case BTN_RIGHT_1UM: on_pushButton_Right1um_pressed();break;

    case BTN_LEFT_STEP:  on_pushButton_LeftStep_pressed();break;
    case BTN_RIGHT_STEP: on_pushButton_RightStep_pressed();break;

    case BTN_TOP_1UM: on_pushButton_Up1um_pressed();break;
    case BTN_BOTTOM_1UM: on_pushButton_Down1um_pressed();break;

    case BTN_TOP_STEP: on_pushButton_UpStep_pressed();break;
    case BTN_BOTTOM_STEP: on_pushButton_DownStep_pressed();break;

	case STR_DIS_X: on_pushButton_DisX_pressed();break;
	case STR_DIS_Y: on_pushButton_DisY_pressed();break;
	case STR_HLINE_H: on_pushButton_LineH_pressed();break;
	case STR_HLINE_V: on_pushButton_LineV_pressed();break;
    }
}

void CCtrlMImg_OPenGL_Cam::BtnRelease(int iBtnID)
{
    switch(iBtnID)
    {
    case BTN_HLINEV_ADD:    on_pushButton_HLineV_add_released();break;
    case BTN_HLINEV_REDUCE:  on_pushButton_HLineV_reduce_released();break;
    case BTN_HLINEH_ADD:    on_pushButton_HLineH_add_released();break;
    case BTN_HLINEH_REDUCE:  on_pushButton_HLineH_reduce_released();break;

    case BTN_LEFT_1UM:  on_pushButton_Left1um_released();break;
    case BTN_RIGHT_1UM: on_pushButton_Right1um_released();break;

    case BTN_LEFT_STEP:  on_pushButton_LeftStep_released();break;
    case BTN_RIGHT_STEP: on_pushButton_RightStep_released();break;

    case BTN_TOP_1UM: on_pushButton_Up1um_released();break;
    case BTN_BOTTOM_1UM: on_pushButton_Down1um_released();break;

    case BTN_TOP_STEP: on_pushButton_UpStep_released();break;
    case BTN_BOTTOM_STEP: on_pushButton_DownStep_released();break;
    }
}

void CCtrlMImg_OPenGL_Cam::mousePressEvent(QMouseEvent *pEvent)
{
    m_iCaptureBtnID=BtnHitIn(pEvent->pos());
    if(m_iCaptureBtnID)
    {
		if(m_iCaptureBtnID != STR_DIS_X 
			&& m_iCaptureBtnID != STR_DIS_Y
			&& m_iCaptureBtnID != STR_HLINE_H 
			&& m_iCaptureBtnID != STR_HLINE_V)
		{
			m_ButtonMap[m_iCaptureBtnID].m_iState=CCtrlGraphButton::PRESS;
		}
        BtnPress(m_iCaptureBtnID);
        return;
    }
    CCtrlMImg_OPenGL::mousePressEvent(pEvent);
}

void CCtrlMImg_OPenGL_Cam::mouseReleaseEvent(QMouseEvent *pEvent)
{
    if(pEvent->button()==Qt::LeftButton)
    {
        if(m_iCaptureBtnID)
        {
            BtnClick(m_iCaptureBtnID);
            BtnRelease(m_iCaptureBtnID);
            m_ButtonMap[m_iCaptureBtnID].m_iState=CCtrlGraphButton::RELEASE;
            m_iCaptureBtnID=BTN_RESERVE;
            return;
        }
        if(!m_pContainer)return;
        QPointF pos(pEvent->pos());
		//pos = rotate_point(-90, pos);
        QPointF pos1 =  m_trans_DP2LP.map(pos);
        int32_t pixelx=pos1.x();
        int32_t pixely=pos1.y();
        if(pixelx<0||pixelx>=m_pContainer->m_Image.m_width) return;
        if(pixely<0||pixely>=m_pContainer->m_Image.m_height) return;
        m_pContainer->clearString();
        m_pContainer->addString(QObject::trUtf8("pixel:x=%1,y=%2.")
                                .arg(pixelx).arg(pixely));


        CMPointd posref= m_pCam->DPTOLP(CMPointd(pixelx,pixely));
#if FILPCAM_XY
        CMPointd ptConveterd(posref.tY, posref.tX);
        ptConveterd.tY = -ptConveterd.tY;

        posref = ptConveterd;
#endif
        m_pContainer->addString(QObject::trUtf8("real:x=%1mm,y=%2mm.")
                                .arg(posref.tX,0,'f',3)
                                .arg(posref.tY,0,'f',3));

        int gray = m_pContainer->m_Image.m_pRawImage[pixelx+pixely*m_pContainer->m_Image.m_width];

        m_pContainer->addString(QObject::trUtf8("Gray:%1")
                                .arg(gray)
                                .arg(posref.tY,0,'f',3));
        if(m_pCam->m_bCamMove)
        {
            V_MsgQueue msgqueue;
			if (m_bLockAxisX) posref.tX = 0;
			if (m_bLockAxisY) posref.tY = 0;
            //HACK:相对运动时，轴的方向与操作面板的一致性需要调整
            //m_gSystem.relmove_xy_msg(-posref.tX, -posref.tY,msgqueue);
            m_gSystem.relmove_xy_msg(-posref.tX, -posref.tY, msgqueue);
            m_gSystem.start_manual(SM_MAIN, msgqueue,false);
        }
    }
    CCtrlMImg_OPenGL::mouseReleaseEvent(pEvent);
}

void CCtrlMImg_OPenGL_Cam::MTimeFresh()
{
	int iSMID= SM_CAMGRAB6X;
	switch(m_pCam->m_iID){
	case CAM2X:
		iSMID = SM_CAMGRAB2X;
		break;
	case CAM6X:
		iSMID = SM_CAMGRAB6X;
		break;
#ifdef __USE_THIRD_CAMERA__
	case CAMKerf:
		iSMID = SM_CAMGRAB4X;
		break;
#endif
	default:
		break;
	}

    V_State* m_pState=m_gSystem.FindSM(iSMID)->GetIdleState();
    if(m_pState->m_VarMsgQueue.m_iMsgCnt<4)
    {
        m_pState->pushback_Msg(VarMsg_Driver_ID,QObject::trUtf8("Grab Image"),
                               QVariantList()
                               <<CAM_DRIVER<<CCamDriver::GRAB
                               <<m_pCam->m_iID,0);

    }
    CCtrlMImg_OPenGL::MTimeFresh();
}

void CCtrlMImg_OPenGL_Cam::refreshPixmap()
{
    if(!isVisible()) return;
	//CPreciseTime timer;
	//timer.StartTime();
    makeCurrent();
    m_Pixmap=QPixmap(size());
    m_Pixmap.fill(m_BKColor);
    if(!m_pContainer){return;}

    int32_t  iImgWidth=m_pContainer->m_Image.m_width;
    int32_t  iImgHeight=m_pContainer->m_Image.m_height;

    QPainter painter(&m_Pixmap);
    painter.initFrom(this);
    if(!painter.isActive())return;
    if(!iImgWidth||!iImgHeight)
    {
        draw_container_name( painter,true);
        return;
    }

    adjust_imgviewrect();
    painter.setClipRect(get_clipRect());

    draw_container_image(painter);
    draw_container_shapes( painter);
    draw_container_strings( painter);

    DrawButton(painter);
    draw_container_name( painter,false);


    QRectF rectClip=m_rectImageView.intersected(get_clipRect());
    painter.setPen(QPen(Qt::green,0));
    painter.drawText(rectClip,
                     Qt::AlignBottom|Qt::AlignRight,
                     QObject::trUtf8("FPS:%1")
                     .arg(m_pCam->m_iFPS));//.arg(m_pCam->m_dTimeFrame,0,'f',3)
	//MqDebug() << timer.GetTime() << "S";
    update();
}

void CCtrlMImg_OPenGL_Cam::timerEvent(QTimerEvent *pEvent)
{
    if(m_iJogTimerID==pEvent->timerId())
    {
        m_bJogMove=true;
        MotorStepMove(m_bX, true,m_iButtonType, m_iDirect);
        killTimer(m_iJogTimerID);
    }
    if(m_iJogHLineTimerID==pEvent->timerId())
    {
        m_iJogHLine=true;
        MoveHLine(m_bVHLine,m_iDirectHLine);
    }
    CCtrlMImg_OPenGL::timerEvent(pEvent);
}
void CCtrlMImg_OPenGL_Cam::DrawButton(QPainter &painter)
{
    painter.resetTransform();
    QRect rectFrame=rect();
    painter.setClipRect(rectFrame);

	//隐藏全部按钮
	if (m_bBtnHide)
	{
		GRAPHBTNMAP::iterator it=m_ButtonMap.find(BTN_HIDESHOW);
		if (it != m_ButtonMap.end())
		{
			it->m_bChecked=!m_bBtnHide;
			it->DrawButton(painter);
		}
		return;
	}

    GRAPHBTNMAP::iterator ii=m_ButtonMap.begin();
    for(;ii!=m_ButtonMap.end();ii++)
    {
        if(ii.key()==BTN_LIVE)
        {
            ii->m_bChecked=m_pCam->m_bTransImage;
        }
        if(ii.key()==BTN_ENABLEMOVE)
        {
            ii->m_bChecked=m_pCam->m_bCamMove;
        }
        if(ii.key()==BTN_ENABLEZOOM)
        {
            ii->m_bChecked=!m_bFit;
        }

        if(ii.key()==STR_HLINE_H)
        {
            ii->m_str=QString::fromUtf8("%1um").arg(m_pCam->m_iHairLenH);
        }
        if(ii.key()==STR_HLINE_V)
        {
            ii->m_str=QString::fromUtf8("%1um").arg(m_pCam->m_iHairLenV);
        }

        if(ii.key()==STR_DIS_X)
        {
            if(m_iCaptureBtnID!=BTN_LEFT_STEP&&
               m_iCaptureBtnID!=BTN_RIGHT_STEP)
            {
                 ii->m_str=QString("%1um").arg(m_iStepValueX);
            }
            else
            {
                double dX=0,dY=0;
                GetStepMoveDis(dX,dY);
                ii->m_str=QString::fromUtf8("%1MM").arg(dX);
            }
        }
        if(ii.key()==STR_DIS_Y)
        {
            if(m_iCaptureBtnID!=BTN_TOP_STEP&&
               m_iCaptureBtnID!=BTN_BOTTOM_STEP)
            {
                 ii->m_str=QString("%1um").arg(m_iStepValueY);
            }
            else
            {
                double dX=0,dY=0;
                GetStepMoveDis(dX,dY);
                ii->m_str=QString::fromUtf8("%1MM").arg(dY);
            }
        }

		if(ii.key()==BTN_LOCKMOVE_X)
		{
			ii->m_bChecked=m_bLockAxisX;
		}
		if(ii.key()==BTN_LOCKMOVE_Y)
		{
			ii->m_bChecked=m_bLockAxisY;
		}
		if(ii.key()==BTN_HIDESHOW)
		{
			ii->m_bChecked=m_bBtnHide;
		}

        ii->DrawButton(painter);
    }
}

void CCtrlMImg_OPenGL_Cam::ResizeButtonPos()
{
    QRectF rectFrame=get_clipRect();
    int iGap=10;
    int iEdge=5;
    GRAPHBTNMAP::iterator ii=m_ButtonMap.begin();
    int iStartX=iEdge;
    int iStartY=iEdge;

	//按钮布局中心位置 8排按钮
	int height = rectFrame.height() / 16;
	iStartY += (rectFrame.height() - (height) * 16 - iGap * 8) / 2;
	rectPictureX.setHeight(height);

    QRect rectLeftBtn(0,0,65,height);

    for(;ii!=m_ButtonMap.end();ii++)
    {
            if(ii->m_bBtnDirectY)
				ii->m_rect=rectPictureY;
            else 
				ii->m_rect=rectPictureX;
        int iWidth=ii->m_rect.width();
        int iHeight=ii->m_rect.height();
        switch(ii.key())
        {
        case BTN_LIVE:
            iStartY+=iHeight*2+iGap;
            ii->m_rect=rectLeftBtn;
            ii->m_rect.moveTo(iStartX,iStartY);iStartY+=iHeight*2+iGap; break;
        case BTN_CLEAR:
            ii->m_rect=rectLeftBtn;
            ii->m_rect.moveTo(iStartX,iStartY);iStartY+=iHeight*2+iGap; break;
        case BTN_ENABLEMOVE:
            ii->m_rect=rectLeftBtn;
            ii->m_rect.moveTo(iStartX,iStartY);iStartY+=iHeight*2+iGap; break;
        case BTN_ENABLEZOOM:
            ii->m_rect=rectLeftBtn;
            ii->m_rect.moveTo(iStartX,iStartY);iStartY+=iHeight*2+iGap; break;
		case BTN_LOCKMOVE_X:
			ii->m_rect=rectLeftBtn;
			ii->m_rect.moveTo(iStartX,iStartY);iStartY+=iHeight*2+iGap; break;
		case BTN_LOCKMOVE_Y:
			ii->m_rect=rectLeftBtn;
			ii->m_rect.moveTo(iStartX,iStartY);iStartY+=iHeight*2+iGap; break;

        case BTN_HLINEV_ADD:    ii->m_rect.moveTo(rectFrame.center().x()+(iWidth*0.5+iGap),
                                                  rectFrame.bottom()-(iHeight*2+iEdge+iGap));break;
        case BTN_HLINEV_REDUCE: ii->m_rect.moveTo(rectFrame.center().x()-(iWidth*1.5+iGap),
                                                  rectFrame.bottom()-(iHeight*2+iEdge+iGap));break;
        case BTN_HLINEH_ADD: ii->m_rect.moveTo(rectFrame.right()-(iWidth*2+iEdge+iGap),
                                               rectFrame.center().y()-(iHeight*1.5+iGap));break;
        case BTN_HLINEH_REDUCE: ii->m_rect.moveTo(rectFrame.right()-(iWidth*2+iEdge+iGap),
                                                  rectFrame.center().y()+(iHeight*0.5+iGap));break;

        case BTN_LEFT_1UM: ii->m_rect.moveTo(rectFrame.center().x()-(iWidth*1.5+iGap),
                                             rectFrame.bottom()-(iHeight+iEdge));break;
        case BTN_RIGHT_1UM:ii->m_rect.moveTo(rectFrame.center().x()+(iWidth*0.5+iGap),
                                             rectFrame.bottom()-(iHeight+iEdge));break;

		case BTN_HIDESHOW: ii->m_rect.moveTo(iStartX,
								rectFrame.bottom()-(iHeight+iEdge));break;
        case BTN_LEFT_STEP: ii->m_rect.moveTo(rectFrame.center().x()-(iWidth*3.5+iGap*2),
                                              rectFrame.bottom()-(iHeight+iEdge));break;
        case BTN_RIGHT_STEP:ii->m_rect.moveTo(rectFrame.center().x()+(iWidth*2.5+iGap*2),
                                              rectFrame.bottom()-(iHeight+iEdge));break;

        case BTN_TOP_1UM:ii->m_rect.moveTo(rectFrame.right()-(iWidth+iEdge),
                                           rectFrame.center().y()-(iHeight*1.5+iGap));break;
        case BTN_BOTTOM_1UM:ii->m_rect.moveTo(rectFrame.right()-(iWidth+iEdge),
                                              rectFrame.center().y()+(iHeight*0.5+iGap));break;

        case BTN_TOP_STEP:ii->m_rect.moveTo(rectFrame.right()-(iWidth+iEdge),
                                            rectFrame.center().y()-(iHeight*3.5+iGap*2));break;
        case BTN_BOTTOM_STEP:ii->m_rect.moveTo(rectFrame.right()-(iWidth+iEdge),
                                               rectFrame.center().y()+(iHeight*2.5+iGap*2));break;

        case STR_HLINE_V:
            ii->m_rect.moveTo(rectFrame.center().x()-iWidth/2,
                              rectFrame.bottom()-(iHeight*2+iEdge+iGap));
            break;
        case STR_HLINE_H:
            ii->m_rect.moveTo(rectFrame.right()-(iWidth*2+iEdge+iGap),
                              rectFrame.center().y()-iHeight/2);
            break;

        case STR_DIS_X:
            ii->m_rect.moveTo(rectFrame.center().x()-iWidth/2,
                     rectFrame.bottom()-(iHeight+iEdge));
            break;
        case STR_DIS_Y:
            ii->m_rect.moveTo(rectFrame.right()-(iWidth+iEdge),
                     rectFrame.center().y()-(iHeight/2));
            break;
        }
    }
}

void CCtrlMImg_OPenGL_Cam::ZoomFit()
{
    CCtrlMImg_OPenGL::ZoomFit();
    ResizeButtonPos();
}

void CCtrlMImg_OPenGL_Cam::ZoomOrginal()
{
    CCtrlMImg_OPenGL::ZoomOrginal();
    ResizeButtonPos();
}

int CCtrlMImg_OPenGL_Cam::BtnHitIn(QPoint &pt)
{
    GRAPHBTNMAP::iterator ii=m_ButtonMap.begin();
    for(;ii!=m_ButtonMap.end();ii++)
    {
        if(ii->m_rect.contains(pt))
        {
            return ii.key();
        }
    }

    return 0;
}

void CCtrlMImg_OPenGL_Cam::resizeEvent(QResizeEvent *e)
{
    UNUSED(e);
    ResizeButtonPos();
    CCtrlMImg_OPenGL::resizeEvent(e);
}

void CCtrlMImg_OPenGL_Cam::showEvent(QShowEvent *pEvent)
{
    //m_pCam->m_bCamMove=false;
        UNUSED(pEvent);
        ResizeButtonPos();

}


void CCtrlMImg_OPenGL_Cam::MotorStepMove(bool bX, bool bJOG,
                               int iButtonType, int iDirect)
{
    //if(!m_pCam->m_bCamMove) return;
    double dX=0,dY=0;
    GetStepMoveDis(dX,dY);
    //iDirect=iDirect>0?1:-1;
    V_MsgQueue msgqueue;
    if(!bJOG)
    {
        if(iButtonType==STEPMOVE_BTN)
        {
            dX*=iDirect;
            dY*=iDirect;
            if(bX)
            {
                m_gSystem.relmove_msg(dX,MOTORX,msgqueue);
            }
            else
            {
                m_gSystem.relmove_msg(dY,MOTORY,msgqueue);
                /*if(m_gSystem.m_iCurSideID==SIDE_A||m_gSystem.m_iCurSideID==SIDE_B)
                {
                    CProcess_Level* pLevel=GET_PROCESS(PROCESS_LEVELING,
                                                       SM_MAIN,
                                                       CProcess_Level);
                    pLevel->m_AlignPtBase.Pt.tY+=-dY;
                }*/
            }

        }
        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);
            }
        }
    }
    else
    {
        if(iButtonType==STEPMOVE_BTN)
        {
            double dSpdX=dX;
            double dSpdY=dY;
            if(bX)
            {
                m_gSystem.jogmove_msg(dSpdX,MOTORX,msgqueue);
            }
            else
            {
                m_gSystem.jogmove_msg(dSpdY,MOTORY,msgqueue);
            }
        }
        else
        {
            double dSpdX=iDirect*0.001;
            double dSpdY=iDirect*0.001;
            if(bX)
            {
                m_gSystem.jogmove_msg(dSpdX,MOTORX,msgqueue);
            }
            else
            {
                m_gSystem.jogmove_msg(dSpdY,MOTORY,msgqueue);
            }
        }
    }
    m_gSystem.start_manual(SM_MAIN, msgqueue, false);
}

void CCtrlMImg_OPenGL_Cam::MotorStop()
{
    if(!m_pCam->m_bCamMove) return;
    V_MsgQueue msgqueue;
    m_gSystem.stopmove_msg(MOTORX,msgqueue);
    m_gSystem.stopmove_msg(MOTORY,msgqueue);
    m_gSystem.start_manual(SM_MAIN, msgqueue);
}

void CCtrlMImg_OPenGL_Cam::StartJogTimer(bool bX, int iButtonType, int iDirect)
{
    if(!m_pCam->m_bCamMove) return;
    m_bX=bX;
    m_iButtonType=iButtonType;
    m_iDirect=iDirect;
    m_iJogTimerID=startTimer(500);
}

void CCtrlMImg_OPenGL_Cam::StopTimer()
{
    if(!m_pCam->m_bCamMove) return;
    m_bJogMove=false;
    killTimer(m_iJogTimerID);
    MotorStop();
}


void CCtrlMImg_OPenGL_Cam::on_pushButton_LeftStep_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(true, false ,STEPMOVE_BTN, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_RightStep_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(true, false ,STEPMOVE_BTN, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Left1um_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(true, false ,UMMOVE_BTN, m_iStepValueX);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Right1um_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(true, false ,UMMOVE_BTN, -m_iStepValueX);
}


void CCtrlMImg_OPenGL_Cam::on_pushButton_LeftStep_pressed()
{
//    StartJogTimer(true, STEPMOVE_BTN, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_RightStep_pressed()
{
//    StartJogTimer(true, STEPMOVE_BTN, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Left1um_pressed()
{
//    StartJogTimer(true, UMMOVE_BTN, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Right1um_pressed()
{
//    StartJogTimer(true, UMMOVE_BTN, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_LeftStep_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_RightStep_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Left1um_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Right1um_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_UpStep_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(false, false ,STEPMOVE_BTN, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_DownStep_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(false, false ,STEPMOVE_BTN, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Up1um_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(false, false ,UMMOVE_BTN, -m_iStepValueY);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Down1um_clicked()
{
    if(m_bJogMove==true) return;
    MotorStepMove(false, false ,UMMOVE_BTN, m_iStepValueY);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_UpStep_pressed()
{
//    StartJogTimer(false, STEPMOVE_BTN, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_DownStep_pressed()
{
//    StartJogTimer(false, STEPMOVE_BTN, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Up1um_pressed()
{
//    StartJogTimer(false, UMMOVE_BTN, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Down1um_pressed()
{
//    StartJogTimer(false, UMMOVE_BTN, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_DisX_pressed()
{
	CDlgParam_Alter dlg(m_iStepValueX, this);
	if (dlg.exec())
	{
		m_iStepValueX = dlg.getValue().toDouble();
	}
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_DisY_pressed()
{
	CDlgParam_Alter dlg(m_iStepValueY, this);
	if (dlg.exec())
	{
		m_iStepValueY = dlg.getValue().toDouble();
	}
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_LineV_pressed()
{
	CDlgParam_Alter dlg(m_pCam->m_iHairLenV, this);
	if (dlg.exec())
	{
		m_pCam->m_iHairLenV = dlg.getValue().toDouble();
		m_pCam->SetHairLineToImageContainer(0,false);
	}
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_LineH_pressed()
{
	CDlgParam_Alter dlg(m_pCam->m_iHairLenH, this);
	if (dlg.exec())
	{
		m_pCam->m_iHairLenH = dlg.getValue().toDouble();
		m_pCam->SetHairLineToImageContainer(0,true);
	}
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_UpStep_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Up1um_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_Down1um_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_DownStep_released()
{
//    StopTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineV_add_clicked()
{
    if(m_iJogHLine)return;
    MoveHLine(true,1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineH_add_clicked()
{
    if(m_iJogHLine)return;
    MoveHLine(false,1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineV_add_pressed()
{
    StartHLineTimer(true, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineH_add_pressed()
{
    StartHLineTimer(false, 1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineV_add_released()
{
    StopHLineTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineH_add_released()
{
    StopHLineTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineV_reduce_clicked()
{
    if(m_iJogHLine)return;
    MoveHLine(true,-1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineH_reduce_clicked()
{
    if(m_iJogHLine)return;
    MoveHLine(false,-1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineV_reduce_pressed()
{
    StartHLineTimer(true, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineH_reduce_pressed()
{
    StartHLineTimer(false, -1);
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineV_reduce_released()
{
    StopHLineTimer();
}

void CCtrlMImg_OPenGL_Cam::on_pushButton_HLineH_reduce_released()
{
    StopHLineTimer();
}

void CCtrlMImg_OPenGL_Cam::MoveHLine(bool bV, int iStep)
{
    if(!bV)
    {
        m_pCam->m_iHairLenH+=iStep;
    }
    else
    {
        m_pCam->m_iHairLenV+=iStep;
    }
    m_pCam->SetHairLineToImageContainer(0,!bV);
}

void CCtrlMImg_OPenGL_Cam::StartHLineTimer(bool bV, int iDirect)
{
    m_bVHLine=bV;
    m_iDirectHLine=iDirect;
    m_iJogHLineTimerID=startTimer(10);
}

void CCtrlMImg_OPenGL_Cam::StopHLineTimer()
{
    m_iJogHLine=false;
    killTimer(m_iJogHLineTimerID);
}

void CCtrlMImg_OPenGL_Cam::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;
    }
}