TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musiclrcmanager.cpp
Go to the documentation of this file.
1 #include "musiclrcmanager.h"
2 #include "musicwidgetutils.h"
3 
4 #include <QFontDatabase>
5 
7  : m_index(Color::Null)
8 {
9 
10 }
11 
12 MusicLrcColor::MusicLrcColor(const QList<QColor> &front, const QList<QColor> &back, Color index)
13  : m_frontColor(front),
14  m_backColor(back),
15  m_index(index)
16 {
17 
18 }
19 
21 {
22  QList<QColor> front, back;
23  switch(index)
24  {
26  {
27  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
28  front << QColor(250, 218, 131) << QColor(250, 218, 131) << QColor(250, 218, 131);
29  break;
30  }
32  {
33  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
34  front << QColor(93, 177, 240) << QColor(93, 177, 240) << QColor(93, 177, 240);
35  break;
36  }
38  {
39  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
40  front << QColor(139, 135, 150) << QColor(139, 135, 150) << QColor(139, 135, 150);
41  break;
42  }
44  {
45  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
46  front << QColor(225, 125, 179) << QColor(225, 125, 179) << QColor(225, 125, 179);
47  break;
48  }
50  {
51  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
52  front << QColor(157, 196, 0) << QColor(157, 196, 0) << QColor(157, 196, 0);
53  break;
54  }
56  {
57  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
58  front << QColor(255, 42, 45) << QColor(255, 42, 45) << QColor(255, 42, 45);
59  break;
60  }
62  {
63  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
64  front << QColor(138, 0, 226) << QColor(138, 0, 226) << QColor(138, 0, 226);
65  break;
66  }
68  {
69  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
70  front << QColor(228, 157, 0) << QColor(228, 157, 0) << QColor(228, 157, 0);
71  break;
72  }
74  {
75  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
76  front << QColor(23, 211, 190) << QColor(23, 211, 190) << QColor(23, 211, 190);
77  break;
78  }
80  {
81  back << QColor(255, 255, 255) << QColor(255, 255, 255) << QColor(255, 255, 255);
82  front << QColor(255, 255, 0) << QColor(255, 255, 0) << QColor(255, 255, 0);
83  break;
84  }
86  {
87  back << QColor(0, 52, 138) << QColor(0, 128, 192) << QColor(3, 202, 252);
88  front << QColor(130, 247, 253) << QColor(255, 255, 255) << QColor(3, 233, 252);
89  break;
90  }
92  {
93  back << QColor(255, 172, 0) << QColor(255, 0, 0) << QColor(170, 0, 0);
94  front << QColor(255, 255, 164) << QColor(255, 255, 0) << QColor(255, 100, 26);
95  break;
96  }
98  {
99  back << QColor(225, 225, 225) << QColor(106, 106, 106) << QColor(0, 0, 0);
100  front << QColor(255, 255, 255) << QColor(222, 222, 222) << QColor(128, 255, 255);
101  break;
102  }
104  {
105  back << QColor(2, 166, 174) << QColor(128, 255, 255) << QColor(2, 166, 174);
106  front << QColor(255, 128, 0) << QColor(255, 255, 0) << QColor(255, 128, 0);
107  break;
108  }
110  {
111  back << QColor(64, 0, 128) << QColor(255, 128, 255) << QColor(64, 0, 128);
112  front << QColor(255, 55, 146) << QColor(255, 243, 134) << QColor(255, 55, 146);
113  break;
114  }
116  {
117  back << QColor(147, 255, 38) << QColor(70, 176, 0) << QColor(0, 85, 0);
118  front << QColor(255, 255, 255) << QColor(154, 255, 17) << QColor(255, 255, 0);
119  break;
120  }
121  default: break;
122  };
123 
124  return MusicLrcColor(front, back, index);
125 }
126 
127 QList<QColor> TTK::readColorConfig(const QString &value)
128 {
129  QList<QColor> colors;
130  const QStringList &rgbs = value.split(";", QtSkipEmptyParts);
131  for(const QString &rgb : qAsConst(rgbs))
132  {
133  const QStringList &var = rgb.split(",");
134  if(var.count() != 3)
135  {
136  continue;
137  }
138  colors << QColor(var[0].toInt(), var[1].toInt(), var[2].toInt());
139  }
140  return colors;
141 }
142 
143 QString TTK::writeColorConfig(const QList<QColor> &colors)
144 {
145  QString value;
146  for(const QColor &rgb : qAsConst(colors))
147  {
148  value.append(QString("%1,%2,%3;").arg(rgb.red()).arg(rgb.green()).arg(rgb.blue()));
149  }
150  return value;
151 }
152 
153 
155  : QLabel(parent),
156  m_lrcMaskWidth(0),
157  m_lrcMaskWidthInterval(0),
158  m_intervalCount(0.0f),
159  m_lrcPerWidth(0),
160  m_transparent(100),
161  m_speedLevel(1)
162 {
163  m_font.setBold(true);
164  m_linearGradient.setStart(0, 0);
165  m_maskLinearGradient.setStart(0, 0);
166 
167  m_timer = new QTimer(this);
168  connect(m_timer, SIGNAL(timeout()), SLOT(updateMask()));
169 }
170 
172 {
173  delete m_timer;
174 }
175 
176 void MusicLrcManager::startDrawLrcMask(qint64 intervaltime)
177 {
178  m_intervalCount = 0.0f;
180 
181  const float count = intervaltime / m_speedLevel;
182  m_lrcMaskWidthInterval = (count != 0) ? m_position.x() / count : 0;
183  m_lrcMaskWidth = 0;
184  m_timer->start(LRC_PER_TIME);
185 }
186 
188 {
189  m_timer->stop();
190  update();
191 }
192 
194 {
195  m_timer->start(LRC_PER_TIME);
196 }
197 
199 {
200  if(index < 0)
201  {
202  return;
203  }
204 
205  const QStringList &family = QFontDatabase().families(QFontDatabase::Any);
206  if(!family.isEmpty())
207  {
208  if(index >= family.count())
209  {
210  index = 0;
211  }
212  m_font.setFamily(family[index]);
213  }
214 }
215 
217 {
218  m_font.setBold(type == 1 || type == 3);
219  m_font.setItalic(type == 2 || type == 3);
220 }
221 
223 {
224  QLinearGradient linearGradient;
225  QList<QColor> cl = color.m_backColor;
226  for(int i = 0; i < cl.count(); ++i)
227  {
228  QColor rgb = cl[i];
229  rgb.setAlpha(m_transparent * 2.55);
230  linearGradient.setColorAt((i + 1) * 1.0 / cl.count(), rgb);
231  }
232  m_linearGradient = linearGradient;
233 
234  QLinearGradient maskLinearGradient;
235  cl = color.m_frontColor;
236  for(int i = 0; i < cl.count(); ++i)
237  {
238  QColor rgb = cl[i];
239  rgb.setAlpha(m_transparent * 2.55);
240  maskLinearGradient.setColorAt((i + 1) * 1.0 / cl.count(), rgb);
241  }
242  m_maskLinearGradient = maskLinearGradient;
243 
244  update();
245 }
246 
248 {
249  m_position = QPoint(x, y);
250  m_lrcPerWidth = x;
251 }
252 
253 void MusicLrcManager::setSelfPosition(const QPoint &point)
254 {
255  m_position = point;
256 }
257 
259 {
260  return m_position.x();
261 }
262 
264 {
265  return m_position.y();
266 }
267 
269 {
270  m_position.setX(x);
271 }
272 
274 {
275  m_position.setY(y);
276 }
277 
279 {
280  m_intervalCount = 0.0f;
281  m_lrcMaskWidth = 0.0f;
282  m_timer->stop();
283  update();
284 }
285 
287 {
288  if(size > 0)
289  {
290  m_font.setPointSize(size);
291  }
292 
293  setText(text());
294  update();
295 }
296 
298 {
299  //At a fixed period of time covered length increases.
301  update();
302 }
303 
304 void MusicLrcManager::setText(const QString &str)
305 {
307  QLabel::setText(str);
308 }
#define QtSkipEmptyParts
String skip empty parts.
Definition: ttkqtcompat.h:129
The class of the lrc color.
MusicLrcManager(QWidget *parent=nullptr)
void setFontFamily(int index)
static constexpr int LRC_PER_TIME
voidpf void uLong size
Definition: ioapi.h:136
float m_lrcMaskWidthInterval
QString writeColorConfig(const QList< QColor > &colors)
QList< QColor > m_backColor
QLinearGradient m_maskLinearGradient
#define qAsConst
Definition: ttkqtglobal.h:53
void setLrcFontSize(int size)
void setFontType(int type)
void setText(const QString &str)
void startDrawLrcMask(qint64 intervaltime)
void setSelfPosition(int x, int y)
TTK_MODULE_EXPORT int fontTextWidth(const QFont &font, const QString &text)
MusicLrcColor mapIndexToColor(MusicLrcColor::Color index)
QList< QColor > m_frontColor
void setLinearGradientColor(const MusicLrcColor &color)
QLinearGradient m_linearGradient
QList< QColor > readColorConfig(const QString &value)