#include "cstatuslog.h"
#include "ui_cstatuslog.h"
#include "logic.h"
#include "gui.h"
#include <QTextFormat>
#include <QScrollBar>
#include <thread>
CStatusLog::CStatusLog(QWidget *parent) :
    CMWidget(parent),
    ui(new Ui::CStatusLog)
{
    ui->setupUi(this);
    m_pLayout=new QGridLayout(this);
    m_pLayout->setMargin(2);
    m_pLayout->setSpacing(2);
    m_iTimerInterval=30;

    ui->txtLogInfo->setMaximumHeight(16000);
    ui->txtLogInfo->document()->setMaximumBlockCount(300);
}

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

void CStatusLog::MInit()
{
//    CCtrlSensorAnalog* pCtrlSensor;
//    pCtrlSensor=new CCtrlSensorAnalog(this,AIN_MAINPRESS);
//    m_pLayout->addWidget(pCtrlSensor,0,0);
//    mchildwidgets[int(pCtrlSensor)]=pCtrlSensor;
}

void CStatusLog::MTimeFresh()
{
    CMWidget::MTimeFresh();
}


void CStatusLog::OnNotifyLogMessage(const QString& info, int type)
{
    QTextCharFormat fmt;
    if (type == 0)
        fmt.setForeground(QBrush(Qt::gray));
    else if (type == 1)
        fmt.setForeground(QBrush(Qt::darkYellow));
    else {
        fmt.setForeground(QBrush(Qt::red));
    }

    /*std::stringstream s;
    s << std::this_thread::get_id();
    QString x = QString("%1 thread id:%2 ").arg(info, s.str().c_str());*/
    ui->txtLogInfo->mergeCurrentCharFormat(fmt);
    ui->txtLogInfo->appendPlainText(info);

    QScrollBar* scrollbar = ui->txtLogInfo->verticalScrollBar();
    if (scrollbar) {
        scrollbar->setSliderPosition(scrollbar->maximum());
    }
}