#include "cctrlgraph_pi.h"
#include "def_global_gui.h"
#include <QStylePainter>
CCtrlGraph_PI::CCtrlGraph_PI(QWidget *parent)
    : QOpenGLWidget(parent)
{
    m_dTotal=0;
    m_BKColor=0xececec;
    m_sName=QObject::trUtf8("PI Chart");
    m_pPrecise=1;
}

void CCtrlGraph_PI::initializeGL()
{
    makeCurrent();
    initializeOpenGLFunctions(); //初始化OPenGL功能函数
    glClearColor(0.93f, 0.93f, 0.93f, 1.0f);

}

void CCtrlGraph_PI::resizeGL(int w, int h)
{
    UNUSED(w);
    UNUSED(h);
    refreshPixmap();
}

void CCtrlGraph_PI::paintGL()
{
    makeCurrent();
    QStylePainter painter(this);
    painter.drawPixmap(0,0,m_Pixmap);
}

void CCtrlGraph_PI::MInit()
{
    m_dataList.push_back(PI_Graph_Data("Pause",10,QColor(Qt::red),"Hour"));
    m_dataList.push_back(PI_Graph_Data("Idle",100,QColor(Qt::green),"Hour"));
    m_dataList.push_back(PI_Graph_Data("Work",100,QColor(Qt::blue),"Hour"));
    m_dataList.push_back(PI_Graph_Data("Error",10,QColor(Qt::yellow),"Hour"));


}

void CCtrlGraph_PI::MTimeFresh()
{
    refreshPixmap();
}

void CCtrlGraph_PI::MFresh()
{
    refreshPixmap();
}

void CCtrlGraph_PI::showEvent(QShowEvent *pEvent)
{
    UNUSED(pEvent);
}

void CCtrlGraph_PI::refreshPixmap(bool bupdate)
{
    if(!isVisible()) return;
    makeCurrent();
    QSize msize=size();
    m_Pixmap=QPixmap(msize);
    m_Pixmap.fill(m_BKColor);
    QPainter painter(&m_Pixmap);
    painter.initFrom(this);
    if(!painter.isActive())return;
    painter.drawText(rect(),Qt::AlignTop|Qt::AlignLeft,m_sName);
    m_dCurAngle=0;
    m_dTotal=0;
    LPI_Graph_Data::iterator ii=m_dataList.begin();
    for(;ii!=m_dataList.end();ii++)
    {
        m_dTotal+=ii->dData;
    }
    ii=m_dataList.begin();
    for(;ii!=m_dataList.end();ii++)
    {
        DrawPI(painter, *ii);
    }
    DrawDataText(painter);
    if(bupdate)
    {
        update();
    }
}

void CCtrlGraph_PI::SetString(QStringList &extraString)
{
    QMutexLocker locker(&m_mutex);
    m_ExtraString=extraString;
}

QRectF CCtrlGraph_PI::Get_Graph_rect()
{
    QRectF rectf=rect();
    double dRight=rectf.left()+ rectf.width()/2;
    rectf.setRight(dRight);
    rectf.adjust(+2,+2,-2,-2);

    return rectf;

}

QRectF CCtrlGraph_PI::Get_PI_rect()
{
    QRectF rectf=Get_Graph_rect();
    double dWidth=rectf.width();
    double dHeight=rectf.height();
    rectf.adjust(dWidth/4,dHeight/4,-dWidth/4,-dHeight/4);
    dWidth=rectf.width();
    dHeight=rectf.height();
    if(dWidth>dHeight)
    {
        double dOffset=(dWidth-dHeight)/2;
        rectf.adjust(+dOffset,+0,-dOffset,-0);
    }
    else
    {
        double dOffset=(dHeight-dWidth)/2;
        rectf.adjust(+0,+dOffset,-0,-dOffset);
    }
    return rectf;
}

QRectF CCtrlGraph_PI::Get_Text_rect()
{
    QRectF rectf=rect();
    double dLeft=rectf.left()+ rectf.width()/2;
    rectf.setLeft(dLeft);
    rectf.adjust(+2,+2,-2,-2);
//    QRectF rectf1=Get_PI_rect();
//    rectf.setTop(rectf1.top());
//    rectf.setBottom(rectf1.bottom());
    return rectf;

}

#define LINELENGTH  46
void CCtrlGraph_PI::DrawPI(QPainter& painter,PI_Graph_Data &data)
{
    double dStartAngle=m_dCurAngle;
    if(DEQUAL0(m_dTotal))
    {
    //    qDebug()<<"CCtrlGraphPIOpenGL Total is 0";
        return;
    }
    float fPercent=data.dData/m_dTotal;
    double dSpanAngle=fPercent*360;
    m_dCurAngle=dStartAngle+dSpanAngle;
    if(m_dCurAngle>360)
    {
//        qDebug()<<"CCtrlGraphPIOpenGL Angle To large:\nSpan:"
//               <<dSpanAngle<<"CurAngle:"<<m_dCurAngle;
        dSpanAngle-=m_dCurAngle-360;
        m_dCurAngle=360;
    }
    QRectF rectf=Get_PI_rect();
    QBrush bh(data.color);
    painter.setBrush(bh);
    painter.drawPie(rectf,dStartAngle*16,dSpanAngle*16);

    double dHalfAngle=dStartAngle+dSpanAngle/2;
    QPointF center=rectf.center();
    double dRadiusIn=rectf.width()/2*0.5;
    double dRadiusOut=rectf.width()/2*1.2;
    double dStartY=-sin(RAD(dHalfAngle))*dRadiusIn+center.y();
    double dStartX=cos(RAD(dHalfAngle))*dRadiusIn+center.x();
    double dEndY=-sin(RAD(dHalfAngle))*dRadiusOut+center.y();
    double dEndX=cos(RAD(dHalfAngle))*dRadiusOut+center.x();

    QPen pen(QBrush(QRgb(0)),0);
    painter.setPen(pen);
    painter.drawLine(dStartX,dStartY,dEndX,dEndY);
    QRectF rectGraph=Get_Graph_rect();
    QString str= QString::fromUtf8("%1%").arg(int(fPercent*100));
    if(dHalfAngle>90&&dHalfAngle<270)
    {
        double dEndY1=dEndY;
        double dEndX1=dEndX-LINELENGTH;
        painter.drawLine(dEndX,dEndY,dEndX1,dEndY1);
        QRectF rectText=QRectF(rectGraph.topLeft(),
                               QPointF(dEndX,dEndY-2));
        painter.drawText(rectText,Qt::AlignBottom|Qt::AlignRight,str);
    }
    else
    {
        double dEndY1=dEndY;
        double dEndX1=dEndX+LINELENGTH;
        painter.drawLine(dEndX,dEndY,dEndX1,dEndY1);
        QRectF rectText=QRectF(QPointF(dEndX,rectGraph.top()),
                               QPointF(dEndX1,dEndY1-2));
        painter.drawText(rectText,Qt::AlignBottom|Qt::AlignLeft,str);
    }

}
#define LINEHEIGHT  20
void CCtrlGraph_PI::DrawDataText(QPainter &painter)
{
    QRectF rect = Get_Text_rect();
    QRectF rect1=rect;
    rect1.setTop(rect.top()+rect.height()-LINEHEIGHT);
    rect1.setWidth(LINEHEIGHT);
    LPI_Graph_Data::iterator ii=m_dataList.begin();
    if(DEQUAL0(m_dTotal)){m_dTotal=1;}
    for(;ii!=m_dataList.end();ii++)
    {
        painter.fillRect(rect1,QBrush(ii->color));
        QString str;
        str=QString::fromUtf8("%1:%4%:%2%3").arg(ii->name)
                .arg(ii->dData,0,'f',m_pPrecise).arg(ii->unit)
                .arg(ii->dData/m_dTotal*100,0,'f',m_pPrecise);
        QRectF rect2=rect1;
        rect2.adjust(rect1.width()+5,0,rect1.width()+15,0);
        rect2.setWidth(rect.width());
        painter.drawText(rect2,Qt::AlignVCenter|Qt::AlignLeft,str);
        QPointF center=rect1.center();
        center.ry()-=LINEHEIGHT+2;
        rect1.moveCenter(center);
    }
    QMutexLocker locker(&m_mutex);
    QString str=m_ExtraString.join('\n');

    painter.drawText(rect,Qt::AlignTop|Qt::AlignLeft,str);

}

