#include "progress.h"
#include "ui_progress.h"
#include "src/view/modaldialog.h"
#include <QDebug>
#define ICON_SIZE QSize(16, 16)

Progress::Progress(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Progress)
{

    QFont f;
    ui->setupUi(this);
    f = ui->labelTitle->font();
    ui->labelTitle->setFocusPolicy(Qt::NoFocus);
    f.setPixelSize(16);
    f.setBold(true);
    ui->labelTitle->setFont(f);
    ui->labelIcon->setProperty("useIconHighlightEffect", 0x8);

    f = ui->labelContent->font();
    f.setPixelSize(14);
    ui->labelContent->setFont(f);
    ui->progressBar->setFont(f);
    ui->progressBar->setValue(0);

    connect(this, SIGNAL(title(QString)), ui->labelTitle, SLOT(setText(QString)));
    connect(this, SIGNAL(setRange(int,int)), ui->progressBar, SLOT(setRange(int,int)));
    connect(this, SIGNAL(setValue(int,QIcon,QString)), this, SLOT(slotsSetValue(int,QIcon,QString)));
    connect(this, SIGNAL(addValue(int,QIcon,QString)), this, SLOT(slotsAddValue(int,QIcon,QString)));
}

void Progress::slotsSetValue(int value, QIcon icon, QString str)
{
    QCoreApplication::processEvents();
    ui->progressBar->setValue(value);
    ui->labelIcon->setPixmap(icon.pixmap(ICON_SIZE));
    ui->labelContent->setText(str);
    ui->labelContent->setToolTip(str);
    QFontMetrics metrics(ui->labelContent->font());
    if (metrics.width(str) > ui->labelContent->width())
    {
        str = QFontMetrics(ui->labelContent->font()).elidedText(str, Qt::ElideRight, ui->labelContent->width());
    }
    ui->labelContent->setText(str);
    if (ui->progressBar->value() == ui->progressBar->maximum())
    {
        MODAL->accept();
    }
}

void Progress::slotsAddValue(int value, QIcon icon, QString str)
{
    QCoreApplication::processEvents();
    value += ui->progressBar->value();
    ui->progressBar->setValue(value);
    ui->labelIcon->setPixmap(icon.pixmap(ICON_SIZE));
    ui->labelContent->setText(str);
    qDebug()<<__LINE__<<__func__<<"Progress "<<str;
    //ui->labelContent->setToolTip(str);
    if (ui->progressBar->value() == ui->progressBar->maximum())
    {
        MODAL->accept();
    }
}

void Progress::slotsUpdateIcon(QIcon icon)
{
    ui->labelIcon->setPixmap(icon.pixmap(ICON_SIZE));
}

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