TTKMusicPlayer  4.3.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  painter.setRenderHint(QPainter::SmoothPixmapTransform);
34  renderPreviousWidget(&painter);
35  renderCurrentWidget(&painter);
36  }
37  else
38  {
39  QWidget::paintEvent(event);
40  }
41 }
42 
44 {
45  QWidget *w = widget(m_previousIndex);
46  QPixmap pix(w->size());
47  pix.fill(Qt::transparent);
48  w->setAttribute(Qt::WA_TranslucentBackground, true);
49  w->render(&pix);
50  w->setAttribute(Qt::WA_TranslucentBackground, false);
51 
52  switch(m_type)
53  {
54  case Module::BottomToTop: painter->translate(0, -m_currentValue); break;
55  case Module::TopToBottom: painter->translate(0, m_currentValue); break;
56  case Module::LeftToRight: painter->translate(m_currentValue, 0); break;
57  case Module::RightToLeft: painter->translate(-m_currentValue, 0); break;
58  default: break;
59  }
60 
61  painter->drawPixmap(0, 0, pix);
62 }
63 
65 {
66  QWidget *w = widget(m_currentIndex);
67  QPixmap pix(w->size());
68  pix.fill(Qt::transparent);
69  w->setAttribute(Qt::WA_TranslucentBackground, true);
70  w->render(&pix);
71  w->setAttribute(Qt::WA_TranslucentBackground, false);
72 
73  switch(m_type)
74  {
75  case Module::BottomToTop: painter->drawPixmap(0, height(), pix); break;
76  case Module::TopToBottom: painter->drawPixmap(0, -height(), pix); break;
77  case Module::LeftToRight: painter->drawPixmap(-width(), 0, pix); break;
78  case Module::RightToLeft: painter->drawPixmap(width(), 0, pix); break;
79  default: break;
80  }
81 }
82 
84 {
85  start(m_currentIndex, current);
86 }
87 
88 void MusicAnimationStackedWidget::start(int previous, int current)
89 {
90  if(m_isAnimating)
91  {
92  return;
93  }
94 
95  m_previousIndex = previous;
96  m_currentIndex = current;
97 
98  QWidget *w = widget(m_currentIndex);
99  if(!w)
100  {
101  return;
102  }
103 
104  const int offsetx = frameRect().width();
105  const int offsety = frameRect().height();
106  w->setGeometry(0, 0, offsetx, offsety);
107 
108  currentWidget()->hide();
109  m_isAnimating = true;
110  m_animation->start();
111 }
112 
114 {
115  m_type = type;
116  m_animation->setStartValue(0);
117  m_animation->setEndValue(length);
118 }
119 
121 {
122  return m_previousIndex;
123 }
124 
126 {
127  return m_currentIndex;
128 }
129 
131 {
132  m_animation->setDuration(duration);
133 }
134 
136 {
137  return m_animation->duration();
138 }
139 
140 void MusicAnimationStackedWidget::valueChanged(const QVariant &value)
141 {
142  m_currentValue = value.toFloat();
143  update();
144 }
145 
147 {
148  m_currentValue = 0;
149  m_isAnimating = false;
150 
151  QWidget *w = widget(m_currentIndex);
152  w->show();
153  w->raise();
154 
155  setCurrentWidget(w);
156  update();
157 }
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)
#define const
Definition: zconf.h:233