#include "cctrlgraph_worktable_z.h"
#include <QStylePainter>
#include "logic.h"
#include "gui.h"
CCtrlGraph_WorkTable_Z::CCtrlGraph_WorkTable_Z(QWidget *parent)
    : CCtrlGraph_WorkTable(parent)
{
    m_iPenWidth=0;
    m_bISOTROPIC=false;
    m_iZeroPos=BOTTOM_CENTER;
}

void CCtrlGraph_WorkTable_Z::MInit()
{

    m_dRangeXMin=0.5;
    m_dRangeXMax=350;
    lUnitsX.push_back(50.0);
    m_dCurUnitX=0.1;
    m_strUnitX="mm";
    m_dRangeYMin=0.1;
    m_dRangeYMax=2;
    lUnitsY.push_back(0.5);
    m_dCurUnitY=0.1;
    m_strUnitY="mm";

    CMWorkTable* pWorkTable=GET_CHDIRECT(WORKTABLE,CMWorkTable);

    int iwidth= pWorkTable->m_Variants.get_VData(Var_WorkTable_Width_ID)
            ->toDouble();

    m_dRangeXMax=iwidth;
    if(m_dRangeXMax<350)m_dRangeXMax=350;
    CCtrlGraph_WorkTable::MInit();
}


void CCtrlGraph_WorkTable_Z::refreshPixmap(bool bupdate)
{
    if(!isVisible()) return;
    makeCurrent();
    m_Pixmap=QPixmap(size());
    m_Pixmap.fill(m_BKColor);
    QPainter painter(&m_Pixmap);
    painter.initFrom(this);
 //   painter.fillRect(rect(),m_BKColor);

    if(!painter.isActive())return;
    draw_Background(painter);
    QRectF rectView=get_rectClip();
    painter.setClipRect(rectView);
    DrawMsg( painter);

    DrawCellThick(painter);
    DrawFilm(painter);
    DrawChannel(CAM2X,CH_TRIANGLE,m_gSystem.m_mapNameList[CAM2X],Qt::red,painter);
    DrawChannel(CAM6X,CH_TRIANGLE,m_gSystem.m_mapNameList[CAM6X],Qt::red,painter);
#ifdef __USE_THIRD_CAMERA__
    DrawChannel(CAMKerf,CH_TRIANGLE,m_gSystem.m_mapNameList[CAMKerf],Qt::red,painter);
#endif
#ifdef __USE_D_NARROW__
    DrawChannel(LASERPATH_D_NARROW,CH_TRIANGLE,m_gSystem.m_mapNameList[LASERPATH_D_NARROW],Qt::red,painter);
#endif
#ifdef __USE_D_WIDE__
    DrawChannel(LASERPATH_D_WIDE,CH_TRIANGLE,m_gSystem.m_mapNameList[LASERPATH_D_WIDE],Qt::red,painter);
#endif

    if(bupdate)
    {
        update();
    }
}

void CCtrlGraph_WorkTable_Z::DrawCellThick(QPainter &painter)
{
    painter.resetTransform();

    QPen pen(Qt::blue);
    painter.setPen(pen);
    CMProduct* pProduct=GET_CHDIRECT(PRODUCT,CMProduct);
    Q_ASSERT(pProduct);

    QRectF rectf;
    QPointF center;
    double dWidth=pProduct->m_Variants.get_VData(Var_Product_Diameter_ID)
            ->toDouble();
    double dHeight= pProduct->m_Variants.get_VData(Var_Product_Thick_ID)
            ->toDouble();
    double dfilmthick= pProduct->m_Variants.get_VData(Var_MProduct_FilmThick_ID)
            ->toDouble();
    center.ry()=dHeight/2+dfilmthick;

    center.rx()=pProduct->m_RealCenter.tX;
    int iCurSide=m_gSystem.m_iCurSideID;
    if(iCurSide==SIDE_A||
       iCurSide==SIDE_B)
    {
        CProductSide* pSide=GET_CHDIRECT(iCurSide, CProductSide);
        center.rx()=pSide->m_realCenter.tX;
        QMutexLocker locker(&pSide->m_CutLineVector.mutex);
        CUTLINE* pLine=pSide->GetCutLine(m_gSystem.m_iCurLine);
        if(pLine)
        {
            dWidth=pLine->m_WPPair.ptPostive.tX-pLine->m_WPPair.ptNegative.tX;
            dWidth=fabs(dWidth);
        }
    }
    rectf.setWidth(dWidth);
    rectf.setHeight(dHeight);
    rectf.moveCenter(center);
    rectf=m_trans_LP2DP.mapRect(rectf);
    QBrush brush(Qt::darkGreen,Qt::BDiagPattern);
    painter.fillRect(rectf,brush);
    painter.drawRect(rectf);
}

void CCtrlGraph_WorkTable_Z::DrawFilm(QPainter &painter)
{
    painter.resetTransform();

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


    QRectF rectf;
    QPointF center;
    double dWidth= m_rectWindow.m_width;
    double dHeight= pProduct->m_Variants.get_VData(Var_MProduct_FilmThick_ID)
            ->toDouble();
    center.ry()=dHeight/2;
    center.rx()=0;

    rectf.setWidth(dWidth);
    rectf.setHeight(dHeight);
    rectf.moveCenter(center);
    rectf=m_trans_LP2DP.mapRect(rectf);
    QBrush brush(Qt::darkBlue);
    painter.fillRect(rectf,brush);
}

void CCtrlGraph_WorkTable_Z::DrawMsg(QPainter &painter)
{
    CMProduct* pProduct=GET_CHDIRECT(PRODUCT,CMProduct);
    double dThick=pProduct->m_Variants.get_VData(Var_Product_Thick_ID)
            ->toDouble();
    double dFilmThick=pProduct->m_Variants.get_VData(Var_MProduct_FilmThick_ID)
            ->toDouble();
    QString str1=tr("Cell Thick:%1mm.\n"
          "Film Thick:%2mm.\n")
            .arg(dThick,0,'f',3)
          .arg(dFilmThick,0,'f',3) ;
    DrawString(painter, str1);
}

void CCtrlGraph_WorkTable_Z::DrawChannel(int32_t iChid, int32_t chshape,
                                         QString& chname,
                                         Qt::GlobalColor ecolor,
                                         QPainter &painter)
{

    CMWorkTableDriver* pWorktableDriver=GET_CARD(WORKTABLE_DRIVER,
                                                 CMWorkTableDriver);
    Q_ASSERT(pWorktableDriver);
    CMPointd wt;
    pWorktableDriver->CurAxisToWt(iChid,wt,false);
    CMotor* pMotor=GET_CHDIRECT(MOTORZ,CMotor);
    double dZPos= pMotor->m_Variants.get_VData(Var_Motor_Pos_ID)
            ->toDouble();
    double dDeFoucs= pWorktableDriver->ZPosToDefoucsDis(iChid,dZPos);
    wt.tY=dDeFoucs;
    DrawPoint( MPT_mmToQPT_mm(wt),   chshape,chname,   ecolor, painter);

}

void CCtrlGraph_WorkTable_Z::mousePressEvent(QMouseEvent *pEvent)
{
    CCtrlGraphOpenGL::mousePressEvent(pEvent);
}

void CCtrlGraph_WorkTable_Z::MTimeFresh()
{
    if(!isVisible()) return;
    CCtrlGraph_WorkTable::MTimeFresh();
    VS_Thread* m_pState=(VS_Thread*)m_gSystem.FindSM(SM_IO)->GetIdleState();
    if(m_pState->QueueSize()<100)
    {
    m_pState->pushback_Msg(VarMsg_Driver_ID,QObject::trUtf8("Fresh Pos"),
                         QVariantList()
                         <<MOTOR_DRIVER<<CMotorDriver::FRESHPOS
                         <<MOTORX,0);
    m_pState->pushback_Msg(VarMsg_Driver_ID,QObject::trUtf8("Fresh Pos"),
                         QVariantList()
                         <<MOTOR_DRIVER<<CMotorDriver::FRESHPOS
                         <<MOTORZ,0);
    }
}

