#include "cctrlheightsensor.h"
#include <QStylePainter>
CCtrlHeightSensor::CCtrlHeightSensor(QWidget *parent)
	: CMWidget(parent)
{
	m_dVolumeRange = 2000;
	m_dCurVolume = 0;
	m_dVolumeWarning = 0;
	m_dVolumeAlarm = 0;
	m_BKColor=Qt::transparent;
}

CCtrlHeightSensor::~CCtrlHeightSensor()
{

}

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

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

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


    QRect rectFrame=rect();
    int iwidth= rectFrame.width();

	int iwidthMax=iwidth;
	int iwidthCur=std::min<int>(m_dCurVolume/m_dVolumeRange*iwidth, iwidth);

    QRect rectCur=rectFrame;
    rectCur.setWidth(iwidthCur);
	QColor bkcolor = Qt::green;
	if(m_dCurVolume < m_dVolumeAlarm){
		bkcolor = Qt::red;
	}
	else if(m_dCurVolume< m_dVolumeWarning){
		bkcolor = Qt::yellow;
	}
    painter.fillRect(rectCur,bkcolor);

	QString str=QString::fromUtf8("%1 ml")
		.arg(m_dCurVolume,0,'f',1);
	painter.drawText(rectFrame,Qt::AlignCenter,str);

	int iwidthAlarm = m_dVolumeAlarm/m_dVolumeRange*iwidthMax;
	int iwidthWarning = m_dVolumeWarning/m_dVolumeRange*iwidthMax;

	{
		QPoint pt1=QPoint(iwidthAlarm,0);
		QPoint pt2=QPoint(iwidthAlarm,rectFrame.height());
		painter.setPen(QPen(Qt::red,3));
		painter.drawLine(pt1,pt2);
	}

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

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


    update();
}

void CCtrlHeightSensor::paintEvent(QPaintEvent *)
{
	QStylePainter painter(this);
	painter.drawPixmap(0,0,m_Pixmap);
}
