#include "cctrlgraph_worktable.h"

CCtrlGraph_WorkTable::CCtrlGraph_WorkTable(QWidget *parent)
    :CCtrlGraphOpenGL(parent)
{
    m_iPenWidth=0;
    m_iShapeSize=16;
}

void CCtrlGraph_WorkTable::DrawString(QPainter &painter, QString& msg, Qt::GlobalColor ecolor)
{
    painter.resetTransform();
    painter.setPen(ecolor);
    QRectF rectView=get_rectClip();
    painter.drawText(rectView,Qt::AlignLeft|Qt::AlignTop,msg);
}

void CCtrlGraph_WorkTable::DrawPoint(QPointF pt,
                                     int32_t chshape,
                                     QString& chname,
                                     Qt::GlobalColor ecolor,
                                     QPainter &painter)
{
    painter.resetTransform();
    int isize=m_iShapeSize;
    pt=m_trans_LP2DP.map(pt);
    QRectF rectf(pt.x()-isize/2.0, pt.y()-isize/2.0, isize, isize);

    if(chshape==CH_CIRCLE)
    {
        QPainterPath path;
        path.addEllipse(rectf);
        painter.fillPath(path,ecolor);
    }
    else if(chshape==CH_RECT)
    {
        painter.fillRect(rectf,ecolor);
    }
    else if(CH_TRIANGLE==chshape)
    {
		QPainterPath path;
		path=QPainterPath(pt);
		path.lineTo(pt+QPointF(-isize/2.0, -isize));
		path.lineTo(pt+QPointF(isize/2.0, -isize));
		path.lineTo(pt);
		painter.fillPath(path,ecolor);
	}else if(CH_TRIANGLE_CLIP==chshape)
	{
		QPainterPath path;
		path=QPainterPath(pt);
		path.lineTo(pt+QPointF(-isize, -isize/2.0));
		path.lineTo(pt+QPointF(-isize, isize/2.0));
		path.lineTo(pt);
		painter.fillPath(path,ecolor);
	}
    else
    {
        Q_ASSERT(0);
    }
    if(!chname.isEmpty())
    {
        QRgb rgb=QColor(ecolor).rgb();
        rgb=~rgb;
        rgb|=255;
        QColor textcolor=rgb;
        QBrush bhtext(textcolor);
        painter.setPen(QPen(bhtext,0));
        if(CH_TRIANGLE!=chshape)
        {
            painter.drawText(rectf,Qt::AlignCenter,chname);
        }
        else
        {
            QRectF recttext=rect();
            QFontInfo info= painter.fontInfo();
            recttext.moveCenter(pt-QPointF(0,isize+info.pixelSize()/2));
            painter.drawText(recttext,Qt::AlignCenter,chname);
        }
    }

}

void CCtrlGraph_WorkTable::DrawCurPoint(QPointF pt, QPainter &painter)
{
    painter.resetTransform();
    int isize=m_iShapeSize+2;
    pt=m_trans_LP2DP.map(pt);
    QRectF rectf(pt.x()-isize/2.0, pt.y()-isize/2.0, isize, isize);
    painter.drawRect(rectf);
    QLine line1(pt.x()-isize,pt.y(),
                pt.x()+isize,pt.y());
    QLine line2(pt.x(),pt.y()-isize,
                pt.x(),pt.y()+isize);
    painter.setPen(Qt::black);
    painter.drawLine(line1);
    painter.drawLine(line2);
}

QPointF CCtrlGraph_WorkTable::MPT_mmToQPT_mm(const CMPointd& pt)
{
    return QPointF(pt.tX,pt.tY);
}

void CCtrlGraph_WorkTable::DrawChannel(int32_t iChid,
                                       int32_t chshape,
                                       const char *chname,
                                       Qt::GlobalColor ecolor,
                                       QPainter &painter)
{
    UNUSED(iChid);
    UNUSED(chshape);
    UNUSED(chname);
    UNUSED(ecolor);
    UNUSED(painter);
}
