#include "q_debugview.h"
#include <QTime>

QDebugview::QDebugview(QWidget *parent)
    : QWidget(parent)
{
    ui_debugview_.setupUi(this);

    ui_debugview_.lblDPCLoading->hide();
    QObject::connect(ui_debugview_.btnDeadPixel,SIGNAL(clicked()),this,SLOT(OnDeadPixelClick()));
}

QDebugview::~QDebugview()
{

}

void QDebugview::closeEvent( QCloseEvent *event )
{
    this->hide();
    event->ignore();
}

void QDebugview::OnDeadPixelClick()
{
    NsstChannel nsst_channel;
    nsst_channel.dev_id = id_;
    nsst_channel.channel = 0;
    NSSTDeadPixelCorrection(&nsst_channel);
    loading_movie_ = new QMovie(":/multi_ipnc_demo/Resources/loading13.gif");
    ui_debugview_.lblDPCLoading->setMovie(loading_movie_);
    ui_debugview_.lblDPCLoading->setScaledContents(true);
    ui_debugview_.lblDPCLoading->show();
    loading_movie_->start();
    ui_debugview_.btnDeadPixel->setEnabled(false);
    ShowStatus(STATUS_LOG_WARNING,"Start dead pixel correction...");
}

void QDebugview::OnDeadPixelResult( int result )
{
    ui_debugview_.btnDeadPixel->setEnabled(true);
    loading_movie_->stop();
    ui_debugview_.lblDPCLoading->setMovie(NULL);
    ui_debugview_.lblDPCLoading->hide();
    QString text;
    if (result == NSST_SUCCESS)
    {
        text.sprintf("Correct dead pixel success!");
        ShowStatus(STATUS_LOG_NORMAL,text);
    }
    else
    {
        text.sprintf("Correct dead pixel failed! result = %d",result);
        ShowStatus(STATUS_LOG_ERROR,text);
    }
}

void QDebugview::ShowStatus( STATUS_LOG_LEVEL level,const QString& qstr )
{
    QTime now;
    switch (level)
    {
    case STATUS_LOG_NORMAL:
        ui_debugview_.textLog->append(QString("<font color=black>%1 : %2</font>").arg(now.currentTime().toString()).arg(qstr));
        break;
    case STATUS_LOG_WARNING:
        ui_debugview_.textLog->append(QString("<font color=blue>%1 : %2</font>").arg(now.currentTime().toString()).arg(qstr));
        break;
    case STATUS_LOG_ERROR:
        ui_debugview_.textLog->append(QString("<font color=red>%1 : %2</font>").arg(now.currentTime().toString()).arg(qstr));
        break;
    }
}
