#include "mainwindow.h"
#include <QPainter>
#include <QBitmap>
#include <QMouseEvent>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_timer(new QTimer(this))
    , i_conts(0)
    , is_init(false)

{
     setAttribute(Qt::WA_TranslucentBackground);   //去除毛边
     this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint  | Qt::FramelessWindowHint);
     initSpirite();
     initConnect();
     is_init = true;
}

MainWindow::~MainWindow()
{

}

void MainWindow::initSpirite()
{
    for(int i = 0 ; i < 15 ; i++){
        QString strPath = ":/spirite/"+QString::number(i+1)+".png";
         QPixmap tmppix ;
         tmppix.load(strPath);
         m_vecPixmap.push_back(tmppix);
    }
}

void MainWindow::initConnect()
{
    connect(m_timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
    m_timer->setInterval(50);
    m_timer->start();
}

void MainWindow::handleTimeout()
{
    i_conts++;
    i_conts = i_conts%15;
    update();
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    if( is_init == true)
    {
        QString strPath =  ":/photo/win/"+QString::number(i_conts+1)+".png";
       // i_conts = 1;
        QPixmap pix(m_vecPixmap.at(i_conts));
        resize(pix.size());
        clearMask();
        setMask(QBitmap(pix.mask()));                 //设置透明
        painter.drawPixmap(rect(),pix,QRect());
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{

    if(event->button() == Qt::LeftButton)
    {
        startPoint = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }

}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos() - startPoint);
        event->accept();
    }
}
