#include "update.h"
#include <QLabel>
#include <QMovie>
#include <QLayout>
#include <QMouseEvent>
#include <QCoreApplication>
#include "UpdateThread.h"
Update::Update(QWidget *parent) : QDialog(parent)
{
int pixmapWidth = QPixmap("Images/roating_1.png").width(); //239;
int pixmapHeight = QPixmap("Images/roating_1.png").height(); //237;
int textLabelWidth = 120;
QLabel* roatingLabel_1 = new QLabel(this);
roatingLabel_1->setPixmap(QPixmap("Images/roating_1.png"));
roatingLabel_1->setAttribute(Qt::WA_TranslucentBackground,true);
QLabel* roatingLabel_2 = new QLabel(this);
roatingLabel_2->setFixedSize(pixmapWidth,pixmapHeight);
roatingLabel_2->setAttribute(Qt::WA_TranslucentBackground,true);
m_ProcessMovie = new QMovie("Images/roating_2.gif");
m_ProcessMovie->setCacheMode(QMovie::CacheAll);
roatingLabel_2->setMovie(m_ProcessMovie);
QLabel* roatingLabel_3 = new QLabel(this);
roatingLabel_3->setFixedSize(pixmapWidth,pixmapHeight);
roatingLabel_3->setAttribute(Qt::WA_TranslucentBackground,true);
QMovie* roatingMovie = new QMovie("Images/roating_3.gif");
roatingLabel_3->setMovie(roatingMovie);
roatingMovie->start();
m_NoteText = QString::fromLocal8Bit("正在更新,请稍后...\n已更新文件:\n");
m_UpdatingTextLabel = new QLabel(m_NoteText,this);
m_UpdatingTextLabel->setFixedWidth(textLabelWidth);
m_UpdatingTextLabel->setAttribute(Qt::WA_TranslucentBackground,true);
m_UpdatingTextLabel->setAlignment(Qt::AlignCenter);
QGridLayout* mainLayout = new QGridLayout;
mainLayout->addWidget(roatingLabel_1,0,0,Qt::AlignCenter);
mainLayout->addWidget(roatingLabel_2,0,0,Qt::AlignCenter);
mainLayout->addWidget(roatingLabel_3,0,0,Qt::AlignCenter);
mainLayout->addWidget(m_UpdatingTextLabel,0,0,Qt::AlignCenter);
this->setLayout(mainLayout);
this->setWindowFlags(Qt::FramelessWindowHint|Qt::Window);
this->setAttribute(Qt::WA_TranslucentBackground,true);
m_UpdateFileCount = 0;
m_UpdateingFileNumber = 0;
UpdateThread* thread = new UpdateThread(this);
thread->start();
}
Update::~Update()
{
}
void Update::mousePressEvent(QMouseEvent * event)
{
if(event->button()==Qt::LeftButton)
m_LastPoint = event->globalPos();
QDialog::mousePressEvent(event);
}
void Update::mouseMoveEvent(QMouseEvent * event)
{
int dx = event->globalX() - m_LastPoint.x();
int dy = event->globalY() - m_LastPoint.y();
m_LastPoint = event->globalPos();
move(x()+dx, y()+dy);
QDialog::mouseMoveEvent(event);
}
void Update::UpdateSlot(int fileCount)
{
if(fileCount>0)
{
m_UpdateFileCount = fileCount;
m_ProcessMovie->start();
m_ProcessMovie->setPaused(true);
}
}
void Update::UpdateSlot(const QString& fileName)
{
m_UpdateingFileNumber++;
int percent = m_UpdateingFileNumber*m_ProcessMovie->frameCount()/m_UpdateFileCount;
if(percent>=m_ProcessMovie->frameCount())
{
m_ProcessMovie->jumpToFrame(m_ProcessMovie->frameCount()-1);
m_UpdatingTextLabel->setText(m_NoteText+fileName);
qApp->processEvents();
m_UpdatingTextLabel->setText(QString::fromLocal8Bit("更新成功!"));
qApp->processEvents();
QThread::msleep(1000);
this->close();
return ;
}
else
{
m_ProcessMovie->jumpToFrame(percent);
m_UpdatingTextLabel->setText(m_NoteText+fileName);
}
}
#include "updatethread.h"
#include <QTimer>
#include "Update.h"
UpdateThread::UpdateThread(Update* ui,QObject *parent) : QThread(parent)
{
m_FileNameList << "Name1.txt"
<< "format.bat"
<< "earth.jpg"
<< "tree1.bat"
<< "lock1.sh"
<< "china1.png" ;
connect(this,SIGNAL(UpdateSignal(int)),ui,SLOT(UpdateSlot(int)));
connect(this,SIGNAL(UpdateSignal(const QString&)),ui,SLOT(UpdateSlot(const QString&)));
emit UpdateSignal(m_FileNameList.count());
}
UpdateThread::~UpdateThread()
{
}
void UpdateThread::run()
{
static bool bStart = false;
if(!bStart)
{
QTimer* timer = new QTimer;
connect(timer,SIGNAL(timeout()),SLOT(UpdateSlot()));
timer->start(500);
bStart = true;
}
this->exec();
}
void UpdateThread::UpdateSlot()
{
if(m_FileNameList.count()>0)
{
emit UpdateSignal(m_FileNameList[0]);
m_FileNameList.pop_front();
}
}
评论