-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrotatestackedwidget.cpp
140 lines (111 loc) · 3.5 KB
/
rotatestackedwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Animation Stacked Widget
* @brief 翻转动画 Stacked Widget
* @anchor Ho229
* @date 2020/12/12
*/
#include "rotatestackedwidget.h"
#include <QPainter>
#include <QEventLoop>
#include <QVariantAnimation>
RotateStackedWidget::RotateStackedWidget(QWidget *parent) :
QStackedWidget(parent),
m_animation(new QVariantAnimation(this))
{
m_animation->setStartValue(0);
m_animation->setEndValue(180);
m_animation->setDuration(500);
m_animation->setEasingCurve(QEasingCurve::Linear);
connect(m_animation, &QVariantAnimation::valueChanged, this,
[this](const QVariant value){
m_rotateValue = value.toInt();
this->repaint();
});
connect(m_animation, &QVariantAnimation::finished, this,
&RotateStackedWidget::on_finished);
}
RotateStackedWidget::~RotateStackedWidget()
{
}
void RotateStackedWidget::rotate(int index, bool exec)
{
if(m_animation->state() == QVariantAnimation::Running || this->currentIndex() == index)
return;
m_nextIndex = index;
m_currentWidget = this->currentWidget();
m_nextWidget = this->widget(m_nextIndex);
if(m_nextWidget == nullptr)
return;
this->updateFrame();
m_currentWidget->hide();
m_animation->start();
if(exec)
{
QEventLoop loop;
connect(m_animation, &QVariantAnimation::finished, &loop, &QEventLoop::quit);
loop.exec(QEventLoop::ExcludeUserInputEvents);
}
}
void RotateStackedWidget::setAnimationDuration(int duration)
{
m_animation->setDuration(duration);
}
int RotateStackedWidget::animationDuration()
{
return m_animation->duration();
}
void RotateStackedWidget::setAnimationEasingCurve(const QEasingCurve& easing)
{
m_animation->setEasingCurve(easing);
}
QEasingCurve RotateStackedWidget::animationEasingCurve()
{
return m_animation->easingCurve();
}
void RotateStackedWidget::on_finished()
{
m_rotateValue = 0;
m_nextWidget->show();
m_nextWidget->raise();
this->setCurrentIndex(m_nextIndex);
this->repaint();
}
void RotateStackedWidget::updateFrame()
{
m_nextWidget->setGeometry(0, 0, this->width(), this->height());
m_currentWidget->setGeometry(0, 0, this->width(), this->height());
m_currentPixmap = QPixmap(m_currentWidget->size());
m_currentWidget->render(&m_currentPixmap);
m_nextPixmap = QPixmap(m_nextWidget->size());
m_nextWidget->render(&m_nextPixmap);
}
void RotateStackedWidget::paintEvent(QPaintEvent *event)
{
if(m_animation->state() == QVariantAnimation::Running)
{
QPainter painter(this);
QTransform transform;
if(m_rotateValue > 90)
{
transform.translate(this->width() / 2, 0);
transform.rotate(m_rotateValue + 180, Qt::YAxis);
painter.setTransform(transform);
painter.drawPixmap(-1 * this->width() / 2, 0, m_nextPixmap);
}
else
{
transform.translate(this->width() / 2, 0);
transform.rotate(m_rotateValue, Qt::YAxis);
painter.setTransform(transform);
painter.drawPixmap(-1 * this->width() / 2, 0, m_currentPixmap);
}
}
else
QStackedWidget::paintEvent(event);
}
void RotateStackedWidget::resizeEvent(QResizeEvent *event)
{
if(m_animation->state() == QVariantAnimation::Running)
this->updateFrame();
QStackedWidget::resizeEvent(event);
}