TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicanimationstackedwidget.cpp
Go to the documentation of this file.
2 
3 #include <QPainter>
4 
6  : QStackedWidget(parent),
7  m_isAnimating(false),
8  m_currentValue(0),
9  m_currentIndex(0),
10  m_previousIndex(0),
11  m_type(Module::LeftToRight)
12 {
13  m_animation = new QPropertyAnimation(this, {}, this);
14  m_animation->setDuration(120);
15  m_animation->setEasingCurve(QEasingCurve::Linear);
16  m_animation->setStartValue(0);
17  m_animation->setEndValue(0);
18 
19  connect(m_animation, SIGNAL(valueChanged(QVariant)), SLOT(valueChanged(QVariant)));
20  connect(m_animation, SIGNAL(finished()), SLOT(animationFinished()));
21 }
22 
24 {
25  delete m_animation;
26 }
27 
29 {
30  if(m_isAnimating)
31  {
32  QPainter painter(this);
33  renderCurrentWidget(&painter);
34  renderPreviousWidget(&painter);
35  }
36  else
37  {
38  QWidget::paintEvent(event);
39  }
40 }
41 
43 {
44  QWidget *w = widget(m_previousIndex);
45  QPixmap pix(w->size());
46  pix.fill(Qt::transparent);
47  w->setAttribute(Qt::WA_TranslucentBackground, true);
48  w->render(&pix);
49  w->setAttribute(Qt::WA_TranslucentBackground, false);
50 
51  switch(m_type)
52  {
53  case Module::BottomToTop: painter->drawPixmap(0, height() / 2, pix); break;
54  case Module::TopToBottom: painter->drawPixmap(0, -height() / 2, pix); break;
55  case Module::LeftToRight: painter->drawPixmap(width() / 2, 0, pix); break;
56  case Module::RightToLeft: painter->drawPixmap(-width() / 2, 0, pix); break;
57  default: break;
58  }
59 }
60 
62 {
63  QWidget *w = widget(m_currentIndex);
64  QPixmap pix(w->size());
65  pix.fill(Qt::transparent);
66  w->setAttribute(Qt::WA_TranslucentBackground, true);
67  w->render(&pix);
68  w->setAttribute(Qt::WA_TranslucentBackground, false);
69 
70  switch(m_type)
71  {
73  {
74  painter->translate(0, m_currentValue);
75  painter->drawPixmap(0, -height() / 2, pix);
76  break;
77  }
79  {
80  painter->translate(0, m_currentValue);
81  painter->drawPixmap(0, height() / 2, pix);
82  break;
83  }
85  {
86  painter->translate(m_currentValue, 0);
87  painter->drawPixmap(-width() / 2, 0, pix);
88  break;
89  }
91  {
92  painter->translate(m_currentValue, 0);
93  painter->drawPixmap(width() / 2, 0, pix);
94  break;
95  }
96  default: break;
97  }
98 }
99 
101 {
102  if(m_isAnimating)
103  {
104  return;
105  }
106 
108  m_currentIndex = index;
109 
110  QWidget *w = widget(m_currentIndex);
111  if(!w)
112  {
113  return;
114  }
115 
116  const int offsetx = frameRect().width();
117  const int offsety = frameRect().height();
118  w->setGeometry(0, 0, offsetx, offsety);
119 
120  currentWidget()->hide();
121  m_isAnimating = true;
122  m_animation->start();
123 }
124 
125 void MusicAnimationStackedWidget::setIndex(int previous, int current)
126 {
127  m_currentIndex = current;
128  m_previousIndex = previous;
129 }
130 
132 {
133  switch(m_type = type)
134  {
135  case Module::BottomToTop:
136  case Module::LeftToRight:
137  {
138  m_animation->setStartValue(-length / 2);
139  m_animation->setEndValue(length / 2);
140  break;
141  }
142  case Module::TopToBottom:
143  case Module::RightToLeft:
144  {
145  m_animation->setStartValue(length / 2);
146  m_animation->setEndValue(-length / 2);
147  break;
148  }
149  default: break;
150  }
151 }
152 
154 {
155  return m_previousIndex;
156 }
157 
159 {
160  return m_currentIndex;
161 }
162 
164 {
165  m_animation->setDuration(duration);
166 }
167 
169 {
170  return m_animation->duration();
171 }
172 
173 void MusicAnimationStackedWidget::valueChanged(const QVariant &value)
174 {
175  m_currentValue = value.toFloat();
176  update();
177 }
178 
180 {
181  m_currentValue = 0;
182  m_isAnimating = false;
183 
184  QWidget *w = widget(m_currentIndex);
185  w->show();
186  w->raise();
187 
188  setCurrentWidget(w);
189  update();
190 }
void renderPreviousWidget(QPainter *painter)
MusicAnimationStackedWidget(QWidget *parent=nullptr)
void valueChanged(const QVariant &value)
virtual void paintEvent(QPaintEvent *event) overridefinal
void setLength(int length, Module type)
void renderCurrentWidget(QPainter *painter)
void setIndex(int previous, int current)