#include "widget.h"
#include "ui_widget.h"
#include <QFile>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
, m_isRoratingWindow(false)
{
QFile file(":/myStyle.qss/style.qss");
file.open(QFile::ReadOnly);
QString styleSheet = tr(file.readAll());
this->setStyleSheet(styleSheet);
file.close();
ui->setupUi(this);
// this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowMinimizeButtonHint);
// this->setAttribute(Qt::WA_TranslucentBackground);//这个参数目标是背景透明
// 给窗口设置rotateValue属性;
this->setProperty("rotateValue", 0);
initRotateWindow();
}
Widget::~Widget()
{
delete ui;
}
// 初始化旋转的窗口;
void Widget::initRotateWindow()
{
//m_loginWindow = new LoginWindow(this);
connect(ui->pushButton1, SIGNAL(clicked(bool)), this, SLOT(onRotateWindow()));
connect(ui->pushButton2, SIGNAL(clicked(bool)), this, SLOT(onRotateWindow()));
//connect(m_loginWindow, SIGNAL(closeWindow()), this, SLOT(close()));
//connect(m_loginWindow, SIGNAL(hideWindow()), this, SLOT(onHideWindow()));
//m_loginNetSetWindow = new LoginNetSetWindow(this);
//connect(m_loginNetSetWindow, SIGNAL(rotateWindow()), this, SLOT(onRotateWindow()));
//connect(m_loginNetSetWindow, SIGNAL(closeWindow()), this, SLOT(close()));
//connect(m_loginNetSetWindow, SIGNAL(hideWindow()), this, SLOT(onHideWindow()));
//this->addWidget(m_loginWindow);
//this->addWidget(m_loginNetSetWindow);
// 这里宽和高都增加,是因为在旋转过程中窗口宽和高都会变化;
this->setFixedSize(QSize(ui->page1->width() + 10, ui->page1->height() + 20));
}
// 开始旋转窗口;
void Widget::onRotateWindow()
{
// 如果窗口正在旋转,直接返回;
if (m_isRoratingWindow)
{
return;
}
m_isRoratingWindow = true;
m_nextPageIndex = (ui->stackedWidget->currentIndex() + 1) >= ui->stackedWidget->count() ? 0 : (ui->stackedWidget->currentIndex() + 1);
QPropertyAnimation *rotateAnimation = new QPropertyAnimation(this, "rotateValue");
// 设置旋转持续时间;
rotateAnimation->setDuration(1200);
// 设置旋转角度变化趋势;
rotateAnimation->setEasingCurve(QEasingCurve::InCubic);
// 设置旋转角度范围;
rotateAnimation->setStartValue(0);
rotateAnimation->setEndValue(180);
connect(rotateAnimation, SIGNAL(valueChanged(QVariant)), this, SLOT(repaint()));
connect(rotateAnimation, SIGNAL(finished()), this, SLOT(onRotateFinished()));
// 隐藏当前窗口,通过不同角度的绘制来达到旋转的效果,不过这里不太行;
// ui->stackedWidget->hide();
rotateAnimation->start();
}
// 旋转结束;
void Widget::onRotateFinished()
{
m_isRoratingWindow = false;
ui->stackedWidget->setCurrentWidget(ui->stackedWidget->widget(m_nextPageIndex));//widget函数返回标识是变量的控件
repaint();
}
// 绘制旋转效果;
void Widget::paintEvent(QPaintEvent* event)
{
if (m_isRoratingWindow)
{
// 小于90度时;
int rotateValue = this->property("rotateValue").toInt();
if (rotateValue <= 90)
{
QPixmap rotatePixmap(ui->stackedWidget->currentWidget()->size());
ui->stackedWidget->currentWidget()->render(&rotatePixmap);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QTransform transform;
transform.translate(width() / 2, 0);
transform.rotate(rotateValue, Qt::YAxis);
painter.setTransform(transform);
painter.drawPixmap(-1 * width() / 2, 0, rotatePixmap);
if(rotateValue = 90)
{ui->stackedWidget->currentWidget()->hide();}
}
// 大于90度时
else
{
QPixmap rotatePixmap(ui->stackedWidget->widget(m_nextPageIndex)->size());
ui->stackedWidget->widget(m_nextPageIndex)->render(&rotatePixmap);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QTransform transform;
transform.translate(width() / 2, 0);
transform.rotate(rotateValue + 180, Qt::YAxis);
painter.setTransform(transform);
painter.drawPixmap(-1 * width() / 2, 0, rotatePixmap);
}
}
else
{
return __super::paintEvent(event);
}
}
void Widget::onHideWindow()
{
showMinimized();
}
/*
void Widget::on_pushButton1_clicked()
{
onRotateWindow();
}
void Widget::on_pushButton2_clicked()
{
onRotateWindow();
}
*/