#include "cctrlgraph_coord.h"

CCtrlGraph_Coord::CCtrlGraph_Coord(QWidget *parent)
    : CCtrlGraphOpenGL(parent)
{
    m_iPenWidth=0;
    m_bEnbleDrawText=true;
    m_bISOTROPIC=false;
    m_iZeroPos=LEFT_BOTTOM;
    m_bshowText=true;
    m_iShapeSize=4;
}

void CCtrlGraph_Coord::MInit()
{
    m_dRangeXMin=-10;
    m_dRangeXMax=60;
    
    //lUnitsX.push_back(5.0);
    //lUnitsX.push_back(2.0);
    lUnitsX.push_back(10);
    lUnitsX.push_back(20);
    lUnitsX.push_back(30);
    lUnitsX.push_back(40);
    lUnitsX.push_back(50);
    //m_dCurUnitX=0.1;

	m_dCurUnitX=1;
    m_strUnitX="度";

    m_dRangeYMin=0;
    m_dRangeYMax=13;

    //lUnitsY.push_back(50.0);
    //lUnitsY.push_back(20.0);
    //lUnitsY.push_back(10.0);
    //lUnitsY.push_back(5.0);
    lUnitsY.push_back(2.0);
    lUnitsY.push_back(1.0);
    m_dCurUnitY=1;
    m_strUnitY="W";
    m_strUnitX = u8"°";
    CCtrlGraphOpenGL::MInit();
}

void CCtrlGraph_Coord::refreshPixmap(bool bupdate)
{
    if(!isVisible()) return;
    makeCurrent();
    m_Pixmap=QPixmap(size());
    m_Pixmap.fill(m_BKColor);
    QPainter painter(&m_Pixmap);
    painter.initFrom(this);
    if(!painter.isActive())return;
    draw_Background(painter);
    QRectF rectView=get_rectClip();
    painter.setClipRect(rectView);
    draw_Coord(painter);
    if(bupdate)
    {
        update();
    }
}

void CCtrlGraph_Coord::draw_Coord(QPainter &painter)
{
    QMutexLocker locker(&m_ptVector.mutex);
    int isize=m_ptVector.size();

    int iIndexMax=0;
    double dXMax=0;
    for(int i=0;i<isize;i++)
    {
        CMPointd &pt=m_ptVector[i];
        if(dXMax<pt.tX)
        {
            dXMax=pt.tX;
            iIndexMax=i;
        }
        DrawCoordPoint(pt,Qt::green,painter);
        if(m_bshowText||i==isize-1)
        {
            DrawCoordText(pt,Qt::darkCyan,painter);
        }
    }
    if(isize)
    {
        DrawCoordText(m_ptVector[iIndexMax],Qt::darkCyan,painter);
    }
}

void CCtrlGraph_Coord::DrawCoordPoint(CMPointd pt,
                                      Qt::GlobalColor ecolor,
                                      QPainter &painter)
{
    painter.resetTransform();
    int isize=m_iShapeSize;
    QPointF pt1=m_trans_LP2DP.map(QPointF(pt.tX,pt.tY));
    QRectF rectf(pt1.x()-isize/2.0,
                 pt1.y()-isize/2.0,
                 isize, isize);
    QPainterPath path;
    path.addRect(rectf);
    painter.fillPath(path,ecolor);
}

void CCtrlGraph_Coord::DrawCoordText(CMPointd pt,
                                     Qt::GlobalColor ecolor,
                                     QPainter &painter)
{
    painter.resetTransform();
    int isize=m_iShapeSize;
    QPointF pt1=m_trans_LP2DP.map(QPointF(pt.tX,pt.tY));
    pt1.rx()+=isize;
    QString str;
    str=QString::fromUtf8("(%1,%2)").arg(pt.tX,0,'f',3)
            .arg(pt.tY,0,'f',3);
    QPen pen(ecolor,m_iPenWidth);
    painter.setPen(pen);
    QRectF rectView=get_rectClip();
    QRectF rect(pt1,rectView.bottomRight());
    painter.drawText(rect,Qt::AlignTop|Qt::AlignLeft,str);
}

void CCtrlGraph_Coord::DrawString(QPainter &painter, Qt::GlobalColor ecolor)
{
    painter.resetTransform();
    painter.setPen(ecolor);
    QRectF rectView=get_rectClip();
    QMutexLocker locker(&m_mutex);
    painter.drawText(rectView,Qt::AlignLeft|Qt::AlignTop,m_String);
}

void CCtrlGraph_Coord::init_rectWindow()
{
    CCtrlGraphOpenGL::init_rectWindow();
    m_bshowText=false;
}

void CCtrlGraph_Coord::SetString(QString &str)
{
    QMutexLocker locker(&m_mutex);
    m_String=str;
}

void CCtrlGraph_Coord::updaterubberBandRect()
{
    CCtrlGraphOpenGL::updaterubberBandRect();
    m_bshowText=true;
}


