#include "cctrlgraph_chart.h"

void CCtrlGraph_Chart::initializeGL()
{
    CCtrlGraphOpenGL::initializeGL( );
}

void CCtrlGraph_Chart::resizeGL(int w, int h)
{
    CCtrlGraphOpenGL::resizeGL(w, h);
}

void CCtrlGraph_Chart::paintGL()
{
    CCtrlGraphOpenGL::paintGL();
}

CCtrlGraph_Chart::CCtrlGraph_Chart(QWidget *parent)
    : CCtrlGraphOpenGL(parent)
{
    m_iMargin_Left=30;
    m_iMargin_Right=20;
    m_iMargin_Top=30;
    m_iMargin_Bottom=90;

    m_bCapture=false;
    m_iPenWidth=0;
    m_bEnableZoom=true;
    m_bEnbleDrawText=true;
    m_bISOTROPIC=false;

    m_iZeroPos=LEFT_BOTTOM;

    m_bXVertical=true;
    m_iPrecise=0;
}

void CCtrlGraph_Chart::MInit()
{
    m_dRangeXMin=3;
    m_dRangeXMax=15;

    lUnitsX.push_back(50.0);
    lUnitsX.push_back(10.0);
    lUnitsX.push_back(1.0);

    m_dCurUnitX=1;
    m_strUnitX=QObject::trUtf8("PCS");

    m_dRangeYMin=0.5;
    m_dRangeYMax=10;
    lUnitsY.push_back(100.0);
    lUnitsY.push_back(50.0);
    lUnitsY.push_back(10.0);
    lUnitsY.push_back(2.0);
    lUnitsY.push_back(1.0);


    m_dCurUnitY=1;
    m_strUnitY=QObject::trUtf8("Date");
    CCtrlGraphOpenGL::MInit();

//    m_ptVector.push_back(Chart_Data(QPointF(1,5),QColor(Qt::red)));
//    m_ptVector.push_back(Chart_Data(QPointF(2,10),QColor(Qt::green)));
//    m_ptVector.push_back(Chart_Data(QPointF(3,8),QColor(Qt::blue)));
//    m_ptVector.push_back(Chart_Data(QPointF(4,8),QColor(Qt::blue)));
//    m_ptVector.push_back(Chart_Data(QPointF(5,8),QColor(Qt::blue)));
//    m_ptVector.push_back(Chart_Data(QPointF(6,8),QColor(Qt::blue)));
//    m_ptVector.push_back(Chart_Data(QPointF(7,8),QColor(Qt::blue)));
//    m_ptVector.push_back(Chart_Data(QPointF(8,8),QColor(Qt::blue)));
//    m_ptVector.push_back(Chart_Data(QPointF(9,8),QColor(Qt::blue)));
//    m_StartDate.setDate(2000,1,1);
}

void CCtrlGraph_Chart::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_Chart::draw_Coord(QPainter &painter)
{
    QMutexLocker locker(&m_ptVector.mutex);
    Chart_Data_Vector::iterator ii=m_ptVector.begin();

    for( ;ii!=m_ptVector.end();ii++)
    {
        QRectF rectf= DrawCoordBar(*ii,painter);
        if(m_bshowText)
        {
            QRectF rectFrame=get_rectClip();
            rectFrame.moveCenter(rectf.center());
            rectFrame.setBottom(rectf.top()-2);
            DrawCoordText(rectFrame,*ii,painter);
        }
    }
}

QRectF CCtrlGraph_Chart::DrawCoordBar(Chart_Data &data,
                                           QPainter &painter)
{
    QPointF ptTopLeft_Logic, ptBottomRight_Logic;
    QPointF ptTopLeft_Device, ptBottomRight_Device;

    ptTopLeft_Logic=QPointF(data.pt.x()-0.25,data.pt.y());
    ptBottomRight_Logic=QPointF(data.pt.x()+0.25,0);

    ptTopLeft_Device=m_trans_LP2DP.map(ptTopLeft_Logic);
    ptBottomRight_Device=m_trans_LP2DP.map(ptBottomRight_Logic);

    QRectF rectf=QRectF(ptTopLeft_Device,ptBottomRight_Device);
    if(rectf.width()<5){rectf.setWidth(5);}
    painter.fillRect(rectf,data.color);
    return rectf;
}

void CCtrlGraph_Chart::DrawCoordText(QRectF &rect,
                                          Chart_Data &data,
                                          QPainter &painter)
{
//    QColor color=QColor(255-data.color.red(),
//                      255-data.color.green(),
//                      255-data.color.blue());
    QColor color=QRgb(0);
    QPen pen(color);
    painter.setPen(pen);
    painter.drawText(rect,Qt::AlignBottom|Qt::AlignHCenter,
                     QString::number(data.pt.y(),'f',m_iPrecise));
}

QString CCtrlGraph_Chart::MakeCoordText(double dPos, bool bX)
{
    if(bX)
    {
       QDate date=m_StartDate.addDays(int(dPos));
       return date.toString("yyyy/M/d");
    }
    return CCtrlGraphOpenGL::MakeCoordText(dPos,bX);
}

void CCtrlGraph_Chart::init_rectWindow()
{
    CCtrlGraphOpenGL::init_rectWindow();
    m_bshowText=true;
}

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


