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

    m_bkColor=Qt::green;
    m_timer_id=-1;
    m_bOn=true;
    strText=tr("No message");
    m_iMsgType=NOTIFY_MSG_TYPE;
    m_iCount=0;
    ui->textEdit->setText(strText);
}

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

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

void CDlgMsg::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 CDlgMsg::resizeEvent(QResizeEvent *)
{
    refreshPixmap();
}

void CDlgMsg::SetMsg(QString &s, int iErrType)
{
	QString tittle = "Info:";
	if(iErrType == ERR_MSG_TYPE){
		tittle = "Err:";
	}else if(iErrType == WARN_MSG_TYPE){
		tittle = "Warn:";
	}
	if(isVisible()){
		ui->textEdit->append(tittle + s);
		if(m_iMsgType == ERR_MSG_TYPE || iErrType == ERR_MSG_TYPE){
			m_iMsgType = ERR_MSG_TYPE;
		}else if(m_iMsgType == WARN_MSG_TYPE || iErrType == WARN_MSG_TYPE){
			m_iMsgType=WARN_MSG_TYPE;
		}else{
			m_iMsgType=iErrType;
		}
	}else{
		ui->textEdit->setText(tittle + s);
		m_iMsgType=iErrType;
	}
    switch(m_iMsgType)
    {
        case ERR_MSG_TYPE:
			setWindowTitle(tr("Error"));
			m_gSystem.SetIndicatorLight(Qt::red,m_bOn);
			m_bkColor=Qt::red;
			break;
        case NOTIFY_MSG_TYPE:
			m_bkColor=Qt::green;
			setWindowTitle(tr("Notify"));
			break;
		case WARN_MSG_TYPE:
			m_bkColor=Qt::yellow;
			setWindowTitle(tr("Information"));
			break;
    default:
        break;
    }
	if(isVisible()) {
	refreshPixmap();
	//return;
	}
    show();
    m_bOn=false;
	/*if(iErrType!=NOTIFY_MSG_TYPE)
	m_gSystem.SetIndicatorBuzz(m_bOn);*/
	m_gSystem.SetIndicatorBuzz(m_bOn);

    if(m_timer_id>0)
    {
        m_iCount=0;
        killTimer(m_timer_id);m_timer_id=-1;
    }
    m_timer_id=startTimer(200);
}
void CDlgMsg::showEvent(QShowEvent * )
{
    refreshPixmap();
}

void CDlgMsg::hideEvent(QHideEvent *)
{
    on_pushButton_buzz_off_clicked();
}

void CDlgMsg::timerEvent(QTimerEvent *event)
{
    if(event->timerId()==m_timer_id)
    {
        m_iCount++;
        m_bOn=!m_bOn;
        if(ERR_MSG_TYPE==m_iMsgType)
        {
            m_gSystem.SetIndicatorLight(Qt::red,m_bOn);
        }
        else
        {
            m_gSystem.SetIndicatorLight(Qt::red,false);
        }

		//仅信息，绿色
		/*if(NOTIFY_MSG_TYPE!=m_iMsgType){
		m_gSystem.SetIndicatorBuzz(m_bOn);
		}*/

		m_gSystem.SetIndicatorBuzz(m_bOn);

		//提示信息，红色
        if((WARN_MSG_TYPE==m_iMsgType|| NOTIFY_MSG_TYPE == m_iMsgType)
			&&m_iCount>5)
        {
            m_iCount=0;
            killTimer(m_timer_id);m_timer_id=-1;
        }
    }
}

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

void CDlgMsg::on_pushButton_buzz_off_clicked()
{
    if(m_timer_id!=-1)
    {
        m_iCount=0;
        killTimer( m_timer_id);m_timer_id=-1;
    }
    m_gSystem.SetIndicatorLight(Qt::red,false);
    m_gSystem.SetIndicatorBuzz(false);
    m_bOn=false;
}
