TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicbarragewidget.cpp
Go to the documentation of this file.
1 #include "musicbarragewidget.h"
2 #include "musicwidgetutils.h"
3 #include "musicbarragerequest.h"
4 
6  : QPropertyAnimation(parent)
7 {
8  initialize();
9 }
10 
11 MusicBarrageAnimation::MusicBarrageAnimation(QObject *target, const QByteArray &name, QObject *parent)
12  : QPropertyAnimation(target, name, parent)
13 {
14  initialize();
15 }
16 
18 {
19  setDuration(TTK::random(10 * TTK_DN_S2MS) + TTK_DN_S2MS);
21  start();
22 }
23 
25 {
27  const int height = TTK::random(size.height());
28  setStartValue(QPoint(0, height));
29  setEndValue(QPoint(size.width(), height));
30 }
31 
33 {
34  setDuration(TTK::random(10000) + TTK_DN_S2MS);
35  setEasingCurve(QEasingCurve::Linear);
36 
37  connect(this, SIGNAL(finished()), SLOT(animationFinished()));
38 }
39 
40 
42  : QObject(parent),
43  m_state(false),
44  m_parent(TTKObjectCast(QWidget*, parent))
45 {
46 
47  m_sizeTimer = new QTimer(this);
48  m_sizeTimer->setSingleShot(true);
49  m_sizeTimer->setInterval(TTK_DN_S2MS / 2);
50  connect(m_sizeTimer, SIGNAL(timeout()), SLOT(sizeChanged()));
51 
53  connect(m_networkRequest, SIGNAL(downLoadRawDataChanged(QByteArray)), SLOT(downLoadFinished(QByteArray)));
54 }
55 
57 {
58  m_sizeTimer->stop();
59  delete m_sizeTimer;
60  clearItems();
61  delete m_networkRequest;
62 }
63 
65 {
66  if(!m_state)
67  {
68  return;
69  }
70 
71  for(int i = 0; i < m_labels.count(); ++i)
72  {
73  m_labels[i]->show();
74  m_animations[i]->start();
75  }
76 }
77 
79 {
80  if(!m_state)
81  {
82  return;
83  }
84 
85  for(int i = 0; i < m_labels.count(); ++i)
86  {
87  m_labels[i]->hide();
88  m_animations[i]->pause();
89  }
90 }
91 
93 {
94  for(int i = 0; i < m_labels.count(); ++i)
95  {
96  m_labels[i]->hide();
97  m_animations[i]->stop();
98  }
99 }
100 
102 {
103  stop();
104  m_parentSize = size;
105  m_sizeTimer->start();
106 }
107 
108 void MusicBarrageWidget::setBarrage(const QString &name, const QString &id)
109 {
110  if(m_lastQueryID == id)
111  {
112  return;
113  }
114 
115  clearBarrages();
116  m_lastQueryID = id;
118 }
119 
121 {
122  m_state = on;
123  if(m_state && !m_barrageRecords.isEmpty())
124  {
125  clearItems();
126  createLabel();
127  createAnimation();
128  start();
129  }
130  else
131  {
132  stop();
133  }
134 }
135 
137 {
138  TTK::initRandom();
139 
140  QLabel *label = createLabel(record);
141  createAnimation(label);
142  m_barrageRecords << record;
143 
144  if(m_state)
145  {
146  start();
147  }
148 }
149 
151 {
152  for(MusicBarrageAnimation *animation : qAsConst(m_animations))
153  {
154  animation->setSize(m_parentSize);
155  }
156 
157  start();
158 }
159 
160 void MusicBarrageWidget::downLoadFinished(const QByteArray &bytes)
161 {
162  TTKAbstractXml xml;
163  if(xml.fromByteArray(bytes))
164  {
165  for(const TTKXmlNode &node : xml.readMultiNodeByTagName("d"))
166  {
167  if(m_barrageRecords.count() > 20) // limit size
168  {
169  break;
170  }
171 
172  QString attrValue;
173  for(const TTKXmlAttr &attr : qAsConst(node.m_attrs))
174  {
175  if(attr.m_key == "p")
176  {
177  attrValue = attr.m_value.toString();
178  break;
179  }
180  }
181 
182  if(!attrValue.isEmpty())
183  {
184  const QStringList &keys = attrValue.split(",");
185  if(keys.count() < 8)
186  {
187  continue;
188  }
189 
190  const int size = keys[2].toInt();
191 
192  MusicBarrageRecord record;
193  if(size < 25)
194  {
195  record.m_size = 15;
196  }
197  else if(size > 25)
198  {
199  record.m_size = 30;
200  }
201  else
202  {
203  record.m_size = 20;
204  }
205 
206  record.m_color = QColor(keys[3].toInt()).name();
207  record.m_value = node.m_text;
208  addBarrage(record);
209  }
210  }
211  }
212 }
213 
215 {
216  qDeleteAll(m_labels);
217  qDeleteAll(m_animations);
218  m_labels.clear();
219  m_animations.clear();
220 }
221 
223 {
224  TTK::initRandom();
225  for(const MusicBarrageRecord &record : qAsConst(m_barrageRecords))
226  {
227  createLabel(record);
228  }
229 }
230 
232 {
233  clearItems();
234  m_barrageRecords.clear();
235 }
236 
238 {
239  QLabel *label = new QLabel(m_parent);
240  label->setStyleSheet(QString("QLabel{ color:%1; }").arg(record.m_color));
241  label->setText(record.m_value);
242 
243  TTK::Widget::setFontSize(label, record.m_size);
244  label->resize(TTK::Widget::fontTextWidth(label->font(), label->text()), TTK::Widget::fontTextHeight(label->font()));
245  label->hide();
246  m_labels << label;
247  return label;
248 }
249 
251 {
252  for(QLabel *label : qAsConst(m_labels))
253  {
254  createAnimation(label);
255  }
256 }
257 
259 {
260  MusicBarrageAnimation *anim = new MusicBarrageAnimation(label, "pos");
261  anim->setSize(m_parentSize);
262  m_animations << anim;
263 }
void setSize(const QSize &size)
void downLoadFinished(const QByteArray &bytes)
The class of the ttk xml interface.
void addBarrage(const MusicBarrageRecord &record)
QString toString() const
voidpf void uLong size
Definition: ioapi.h:136
TTK_MODULE_EXPORT void initRandom()
Definition: ttktime.cpp:7
The class of the barrage record item.
MusicBarrageWidget(QObject *parent=nullptr)
MusicBarrageRecordList m_barrageRecords
TTK_MODULE_EXPORT int random(int value=RAND_MAX)
Definition: ttktime.cpp:14
TTK_MODULE_EXPORT void setFontSize(QWidget *widget, int size)
void startToRequest(const QString &data)
#define qAsConst
Definition: ttkqtglobal.h:53
const char * name
Definition: http_parser.c:458
void barrageStateChanged(bool on)
bool fromByteArray(const QByteArray &data)
MusicBarrageAnimation(QObject *parent=nullptr)
QList< MusicBarrageAnimation * > m_animations
The class of the ttk xml attribute.
The class of the ttk xml node.
QList< QLabel * > m_labels
TTKXmlNodeList readMultiNodeByTagName(const QString &tagName) const
void setBarrage(const QString &name, const QString &id)
The namespace of the barrage request interface.
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
TTK_MODULE_EXPORT int fontTextWidth(const QFont &font, const QString &text)
void setSize(const QSize &size)
TTK_MODULE_EXPORT int fontTextHeight(const QFont &font)
MusicBarrageRequest * m_networkRequest
The class of the barrage animation.
#define TTKObjectCast(x, y)
Definition: ttkqtglobal.h:60