#include "q_fader_widget.h"

#include <QtGui/QPainter>

QFaderWidget::QFaderWidget(QWidget *parent)
: QWidget(parent)
{
    if (parent)
        startColor = parent->palette().window().color();  
    else
        startColor = Qt::white;
    currentAlpha = 0;
    fadeTimes = 1000;  
    timer = new QTimer(this);  
    connect(timer, SIGNAL(timeout()),this, SLOT(update()));  
    setAttribute(Qt::WA_DeleteOnClose); 
    if (parent)
    {
        resize(parent->size());
    }
}

QFaderWidget::~QFaderWidget()
{

}

void QFaderWidget::paintEvent( QPaintEvent * ev )
{
    QPainter painter(this);  
    QColor currentColor = startColor;  
    currentColor.setAlpha(currentAlpha);  
    painter.fillRect(rect(), currentColor);  

    currentAlpha -= 255 * timer->interval() / fadeTimes;  
    if (currentAlpha <= 0)   
    {  
        timer->stop();  
        close();  
    } 
}

void QFaderWidget::Start()
{
    currentAlpha = 255;  
    timer->start(100);  
    show();
}
