#include "CDlgPowerMsg.h"
#include "ui_CDlgPowerMsg.h"
#include <QStylePainter>
#include <QPainter>
#include "logic.h"
CDlgPowerMsg::CDlgPowerMsg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CDlgPowerMsg)
{
    ui->setupUi(this);

    m_bkColor=Qt::yellow;
    m_timer_id=-1;
    m_bOn=true;
    m_iCount=0;
    ui->textEdit->setText("");
}

CDlgPowerMsg::~CDlgPowerMsg()
{
    delete ui;
}

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

void CDlgPowerMsg::refreshPixmap()
{
    m_Pixmap=QPixmap(size());
    QPainter painter(&m_Pixmap);
    painter.initFrom(this);
    QRect rect1=rect();
    QLinearGradient gradient(0,0,20,20);
    gradient.setSpread(QLinearGradient::RepeatSpread);

    gradient.setColorAt(0,Qt::GlobalColor (m_bkColor));
    gradient.setColorAt(0.499,Qt::GlobalColor (m_bkColor));
    gradient.setColorAt(0.5,Qt::white);
    gradient.setColorAt(1,Qt::white);
    QBrush bh(gradient);
    painter.fillRect(rect1,bh);
    rect1.adjust(20,20,-20,-20);
    painter.fillRect(rect1,QColor(128,128,128));
    update();
}

void CDlgPowerMsg::resizeEvent(QResizeEvent *)
{
    refreshPixmap();
}

void CDlgPowerMsg::showEvent(QShowEvent * )
{
    refreshPixmap();

    if (m_timer_id < 0) {
        m_timer_id = startTimer(200);
    }
}

void CDlgPowerMsg::timerEvent(QTimerEvent *event)
{
    if(event->timerId()==m_timer_id)
    {
        if (!isVisible()) {
            return;
        }

        CPowerMeterDriver_lp* pPowerMeterDriver = GET_CARD(POWERMETERDRIVER, CPowerMeterDriver_lp);
        Q_ASSERT(pPowerMeterDriver);
        CPowerMeter* pPowerMeterHead = GET_CHDIRECT(POWERMETERHEAD,
                                                    CPowerMeter);
        Q_ASSERT(pPowerMeterHead);

        ui->textEdit->setText(pPowerMeterDriver->m_sCheckPowerResult);
        ui->textEdit->append("\n" + pPowerMeterDriver->m_sCurCheckInfo);
        ui->lblPower->setText(QString::number(pPowerMeterHead->m_Variants.get_VData(Var_PowerMeter_MeasResult_ID)->toDouble()));
    }
}

void CDlgPowerMsg::on_pushButton_2_clicked()
{
    setHidden(true);
}