-
Notifications
You must be signed in to change notification settings - Fork 7
/
translationstackedwidget.cpp
160 lines (127 loc) · 4.07 KB
/
translationstackedwidget.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Animation Stacked Widget
* @brief 平移动画 Stacked Widget
* @anchor Ho229
* @date 2020/12/12
*/
#include "translationstackedwidget.h"
#include <QPainter>
#include <QEventLoop>
#include <QVariantAnimation>
TranslationStackedWidget::TranslationStackedWidget(QWidget *parent) :
QStackedWidget(parent),
m_animation(new QVariantAnimation(this))
{
m_animation->setDuration(500);
m_animation->setEasingCurve(QEasingCurve::OutQuint);
connect(m_animation, &QVariantAnimation::finished, this,
&TranslationStackedWidget::on_finished);
connect(m_animation, &QVariantAnimation::valueChanged, this,
[this](const QVariant value){
m_animationX = value.toInt();
this->repaint();
});
}
TranslationStackedWidget::~TranslationStackedWidget()
{
}
void TranslationStackedWidget::moveToIndex(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(index);
if(m_nextWidget == nullptr)
return;
this->updateFrame();
this->updateAnimation();
m_currentWidget->hide();
m_animation->start();
if(exec)
{
QEventLoop loop;
connect(m_animation, &QVariantAnimation::finished, &loop, &QEventLoop::quit);
loop.exec(QEventLoop::ExcludeUserInputEvents);
}
}
void TranslationStackedWidget::setAnimationDuration(int duration)
{
m_animation->setDuration(duration);
}
int TranslationStackedWidget::animationDuration()
{
return m_animation->duration();
}
void TranslationStackedWidget::setAnimationEasingCurve(const QEasingCurve &easing)
{
m_animation->setEasingCurve(easing);
}
QEasingCurve TranslationStackedWidget::animationEasingCurve()
{
return m_animation->easingCurve();
}
void TranslationStackedWidget::on_finished()
{
m_nextWidget->show();
m_nextWidget->raise();
this->setCurrentIndex(m_nextIndex);
this->repaint();
}
QPixmap TranslationStackedWidget::mergePixmap(const QPixmap &leftPixmap, const QPixmap &rightPixmap)
{
QPixmap newPixmap(leftPixmap.width() + rightPixmap.width(),
qMax(leftPixmap.height(), rightPixmap.height()));
QPainter painter(&newPixmap);
painter.drawPixmap(QPoint(0, 0), leftPixmap);
painter.drawPixmap(QPoint(leftPixmap.width(), 0), rightPixmap);
return newPixmap;
}
void TranslationStackedWidget::updateFrame()
{
m_nextWidget->setGeometry(0, 0, this->width(), this->height());
m_currentWidget->setGeometry(0, 0, this->width(), this->height());
m_nextWidget->setGeometry(0, 0, this->width(), this->height());
QPixmap currentPixmap(m_currentWidget->size());
m_currentWidget->render(¤tPixmap);
QPixmap nextPixmap(m_nextWidget->size());
m_nextWidget->render(&nextPixmap);
if(m_nextIndex < this->currentIndex())
m_animationPixmap = mergePixmap(nextPixmap, currentPixmap);
else
m_animationPixmap = mergePixmap(currentPixmap, nextPixmap);
}
void TranslationStackedWidget::updateAnimation()
{
if(m_nextIndex < this->currentIndex())
{
// Last page
m_animation->setStartValue(-this->width() + 1);
m_animation->setEndValue(0);
}
else
{
// Next page
m_animation->setStartValue(0);
m_animation->setEndValue(-this->width() - 1);
}
}
void TranslationStackedWidget::paintEvent(QPaintEvent *event)
{
if(m_animation->state() == QVariantAnimation::Running)
{
QPainter painter(this);
painter.drawPixmap(QPoint(m_animationX, 0), m_animationPixmap);
}
else
QStackedWidget::paintEvent(event);
}
void TranslationStackedWidget::resizeEvent(QResizeEvent *event)
{
if(m_animation->state() == QVariantAnimation::Running)
{
this->updateFrame();
this->updateAnimation();
}
QStackedWidget::resizeEvent(event);
}