#include "cctrlparamstatic.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QMessageBox>

#include <QPainter>
CCtrlParamStatic::CCtrlParamStatic(QWidget *parent)
    :CMWidget(parent)
{

//    staticparam=new QLCDNumber(this);
// //   staticparam->setMaximumHeight(50);
//    staticparam->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
//    layout->addWidget(staticparam);
//    layout->addWidget(staticuint);
////    staticparam->setStyleSheet("color:black");
//    staticparam->display(0);
//    setLayout(layout);
    m_BKColor=Qt::transparent;
    m_iTimerInterval=0;
    setMinimumWidth(250);
}


void CCtrlParamStatic::MTimeFresh()
{
    MFresh();
}

void CCtrlParamStatic::MFresh()
{
    if(!m_pParam)return;
    QString sData;
    if(m_pParam->m_vVar.type()==QVariant::UInt) return;
    if(m_pParam->m_vVar.type()==QVariant::Double)
    {
        sData=m_pParam->m_vVar.toString();
    //    staticparam->display(m_pParam->m_vVar.toDouble());
    }
    else if(m_pParam->m_vVar.type()==QVariant::Int)
    {
        sData=m_pParam->m_vVar.toString();
    //    staticparam->display(m_pParam->m_vVar.toInt());
    }


    m_Pixmap=QPixmap(size());
    m_Pixmap.fill(m_BKColor);
    QPainter painter(&m_Pixmap);
    if(!painter.isActive()) return;
    painter.initFrom(this);

    QRect rectFrame=rect();rectFrame.adjust(2,2,-2,-2);
    QRect rect1=rectFrame;
    rect1.setRight(rectFrame.width()*0.4);

    painter.drawText(rect1,Qt::AlignVCenter|Qt::AlignRight,m_sName+":");

    QPoint pt=rect1.center();
    pt.rx()+=rectFrame.width()*0.4+2;
    rect1.moveCenter(pt);

    painter.drawRect(rect1);
    painter.drawText(rect1,Qt::AlignVCenter|Qt::AlignHCenter,sData);


    rect1=rectFrame;
    rect1.setLeft(rectFrame.width()*0.8+2);
    painter.drawText(rect1,Qt::AlignVCenter|Qt::AlignLeft,m_sUnit);

    update();
}

void CCtrlParamStatic::MApply()
{

}

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

void CCtrlParamStatic::MParamInit(Variant *pParam, QString name)
{
    m_pParam=pParam;
    m_sName=name;
    if(name.isEmpty())
    {
        m_sName=m_pParam->m_sName;
    }
    m_sName.replace(QString("_"), QString(" "));
    m_sUnit=pParam->m_sUnit;
    MFresh();
}
