#include"splashscreen.h"
SplashScreen::SplashScreen(int nLoadingType, QWidget *parent) \
: QWidget(parent),\
m_pMainWidget(nullptr)
{
m_nLoadingType = nLoadingType;
//加载图形的类型范围为0-8
if ((m_nLoadingType < 0) ||( m_nLoadingType > 8))
{
m_nLoadingType = 0;
}
init();
}
SplashScreen::~SplashScreen()
{
delete m_pLoadTimer;
delete m_pCountTimer;
}
void SplashScreen::setWidget(QWidget *mainWidget)
{
m_pMainWidget = mainWidget;
}
QWidget *SplashScreen::getWidget()
{
return m_pMainWidget;
}
void SplashScreen::setLoadText(const QString &title)
{
if(!title.isEmpty())
{
m_pTextLabel->setText(title);
}
}
void SplashScreen::setLoadCount(int count)
{ if(m_nCount != count)
{
m_nCount = count;
}
m_pNumLabel->setText(QString::number(m_nCount));
}
void SplashScreen::start()
{
// m_pCountTimer->start();
m_pLoadTimer->start();
show();
}
void SplashScreen::init()
{
//setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
//setAttribute(Qt::WA_TranslucentBackground);
resize(300, 300);
//setAttribute(Qt::WA_DeleteOnClose);
m_pNumLabel = new QLabel(this);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(m_pNumLabel->sizePolicy().hasHeightForWidth());
m_pNumLabel->setSizePolicy(sizePolicy);
m_pNumLabel->setAlignment(Qt::AlignCenter);
m_pNumLabel->setStyleSheet(QString::fromUtf8("color: rgb(55, 55, 55);\n" "font: 75 50pt \"\346\245\267\344\275\223\";\n" "background-color: transparent;"));
m_pTextLabel = new QLabel(this);
m_pTextLabel->setVisible(false);
m_pTextLabel->setText(QString("Loading"));
m_pTextLabel->setMinimumSize(0, 40);
m_pTextLabel->setMaximumHeight(40);
m_pTextLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
m_pTextLabel->setStyleSheet(QString::fromUtf8("color: rgb(55, 55, 55);\n" "font: 75 14pt \"\346\245\267\344\275\223\";\n" "background-color: transparent;"));
m_pGridLayout = new QGridLayout(this);
m_pGridLayout->setSpacing(0);
m_pGridLayout->setContentsMargins(11, 11, 11, 11);
m_pGridLayout->setContentsMargins(70, 70, 70, 70);
m_pGridLayout->addWidget(m_pNumLabel, 1, 0, 1, 1);
m_pGridLayout->addWidget(m_pTextLabel, 2, 0, 1, 1);
setLayout(m_pGridLayout);
setStyleSheet("QLabel{ color: rgb(55, 55, 55);"
"font: 75 50pt \"\346\245\267\344\275\223\";"
"background-color: transparent;}");
m_nWidth = width();
m_nHeight = height();
m_pLoadTimer = new QTimer(this);
connect(m_pLoadTimer, SIGNAL(timeout()), SLOT(update()));
m_pLoadTimer->setInterval(5);
m_pCountTimer = new QTimer(this);
connect(m_pCountTimer, SIGNAL(timeout()), SLOT(countTimeout()));
m_pCountTimer->setInterval(1000);
m_nAngle = 0;
m_nCount = 5;
m_pNumLabel->setText(QString::number(m_nCount));
}
void SplashScreen::mousePressEvent(QMouseEvent *event)
{
m_MousePos = event->globalPos() - pos();
}
void SplashScreen::mouseMoveEvent(QMouseEvent *event)
{
move(event->globalPos() - m_MousePos);
}
void SplashScreen::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPixmap pixmap;
QString strPath = ":/Images/Loading/Loading" + QString::number(m_nLoadingType) + ".png";
pixmap.load(strPath);
//平移到中间
painter.translate(m_nWidth / 2, m_nHeight / 2);
//旋转角度
painter.rotate(m_nAngle);
//图片的旋转轨迹是一个圆圈,必须把图片圆心移到这个轨迹圆,才能拼凑出画面
painter.translate(-m_nWidth / 2, -m_nHeight / 2);
//画图片//图片的宽高和窗口宽高是一样的
painter.drawPixmap(0, 0, m_nWidth, m_nHeight, pixmap);
//角度每次转2度
m_nAngle += 2;
if (m_nAngle >= 360)
m_nAngle = 0;
}
void SplashScreen::countTimeout()
{
m_nCount--;
m_pNumLabel->setText(QString::number(m_nCount));
if (m_nCount <= 0)
{
emit finish();
if(m_pLoadTimer->isActive())
m_pLoadTimer->stop();
if(m_pCountTimer->isActive())
m_pCountTimer->stop();
close();
if(m_pMainWidget)
m_pMainWidget->show();
}
}