#include "cctrlpowerindicate.h"
#include <QPainter>
#include <QStylePainter>
#include "base.h"
CCtrlPowerIndicate::CCtrlPowerIndicate(QWidget *parent)
    : CMWidget(parent)
{
    m_dPowerRange=10;
    m_dPowerCur=0;
    m_dPowerMax=0;
    m_BKColor=Qt::transparent;
}

void CCtrlPowerIndicate::paintEvent(QPaintEvent *pEvent)
{
    UNUSED(pEvent);

    QStylePainter painter(this);
    painter.drawPixmap(0,0,m_Pixmap);


}

void CCtrlPowerIndicate::refreshPixmap()
{
    if(!isVisible()) return;
    m_Pixmap=QPixmap(size());
    m_Pixmap.fill(m_BKColor);

    QPainter painter(&m_Pixmap);
    painter.initFrom(this);


    Q_ASSERT(!DEQUAL0(m_dPowerRange));
    QRect rectFrame=rect();
    int iwidth= rectFrame.width();

	int iwidthMax=iwidth;
	int iwidthCur=m_dPowerCur/m_dPowerMax*iwidth;

    /*int iwidthMax=m_dPowerMax/m_dPowerRange*iwidth;
    int iwidthCur=m_dPowerCur/m_dPowerRange*iwidth;*/

    QRect rectCur=rectFrame;
    rectCur.setWidth(iwidthCur);

    painter.fillRect(rectCur,Qt::green);

    QPoint pt1=QPoint(iwidthMax,0);
    QPoint pt2=QPoint(iwidthMax,rectFrame.height());

    QString str=QString::fromUtf8("%1/%2W")
            .arg(m_dPowerCur,0,'f',1)
            .arg(m_dPowerMax,0,'f',1);
    painter.drawText(rectFrame,Qt::AlignCenter,str);

    painter.setPen(QPen(Qt::blue,3));
    //painter.drawLine(pt1,pt2);


    update();
}

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

