TTKMusicPlayer  4.3.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) noexcept
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::start(qint64 time)
177 {
178  if(time > 0)
179  {
180  m_intervalCount = 0.0f;
182 
183  const float count = time / m_speedLevel;
184  m_lrcMaskWidthInterval = (count != 0) ? m_position.x() / count : 0;
185  m_lrcMaskWidth = 0;
186  }
187 
188  m_timer->start(LRC_PER_TIME);
189 }
190 
192 {
193  m_timer->stop();
194  update();
195 }
196 
198 {
199  if(index < 0)
200  {
201  return;
202  }
203 
204  const QStringList &family = QtFontFamilies();
205  if(!family.isEmpty())
206  {
207  if(index >= family.count())
208  {
209  index = 0;
210  }
211  m_font.setFamily(family[index]);
212  }
213 }
214 
216 {
217  m_font.setBold(type == 1 || type == 3);
218  m_font.setItalic(type == 2 || type == 3);
219 }
220 
222 {
223  QLinearGradient linearGradient;
224  QList<QColor> cl = color.m_backColor;
225  for(int i = 0; i < cl.count(); ++i)
226  {
227  QColor rgb = cl[i];
228  rgb.setAlpha(m_transparent * 2.55);
229  linearGradient.setColorAt((i + 1) * 1.0 / cl.count(), rgb);
230  }
231  m_linearGradient = linearGradient;
232 
233  QLinearGradient maskLinearGradient;
234  cl = color.m_frontColor;
235  for(int i = 0; i < cl.count(); ++i)
236  {
237  QColor rgb = cl[i];
238  rgb.setAlpha(m_transparent * 2.55);
239  maskLinearGradient.setColorAt((i + 1) * 1.0 / cl.count(), rgb);
240  }
241  m_maskLinearGradient = maskLinearGradient;
242 
243  update();
244 }
245 
246 void MusicLrcManager::setSelfPosition(int x, int y) noexcept
247 {
248  m_position = QPoint(x, y);
249  m_lrcPerWidth = x;
250 }
251 
252 void MusicLrcManager::setSelfPosition(const QPoint &point) noexcept
253 {
254  m_position = point;
255 }
256 
257 int MusicLrcManager::x() const noexcept
258 {
259  return m_position.x();
260 }
261 
262 int MusicLrcManager::y() const noexcept
263 {
264  return m_position.y();
265 }
266 
267 void MusicLrcManager::setX(int x) noexcept
268 {
269  m_position.setX(x);
270 }
271 
272 void MusicLrcManager::setY(int y) noexcept
273 {
274  m_position.setY(y);
275 }
276 
278 {
279  m_intervalCount = 0.0f;
280  m_lrcMaskWidth = 0.0f;
281  m_timer->stop();
282  update();
283 }
284 
286 {
287  if(size > 0)
288  {
289  m_font.setPointSize(size);
290  }
291 
292  setText(text());
293  update();
294 }
295 
297 {
298  //At a fixed period of time covered length increases.
300  update();
301 }
302 
303 void MusicLrcManager::setText(const QString &str)
304 {
306  QLabel::setText(str);
307 }
#define QtSkipEmptyParts
String skip empty parts.
Definition: ttkqtcompat.h:143
The class of the lrc color.
MusicLrcManager(QWidget *parent=nullptr)
void setX(int x) noexcept
void setFontFamily(int index)
int x() const noexcept
void setSelfPosition(int x, int y) noexcept
static constexpr int LRC_PER_TIME
voidpf void uLong size
Definition: ioapi.h:136
float m_lrcMaskWidthInterval
void start(qint64 time=0)
QString writeColorConfig(const QList< QColor > &colors)
QList< QColor > m_backColor
QLinearGradient m_maskLinearGradient
#define qAsConst
Definition: ttkqtglobal.h:57
void setLrcFontSize(int size)
MusicLrcColor() noexcept
#define QtFontFamilies()
FontDatabase families.
Definition: ttkqtcompat.h:206
void setFontType(int type)
void setY(int y) noexcept
void setText(const QString &str)
TTK_MODULE_EXPORT int fontTextWidth(const QFont &font, const QString &text)
int y() const noexcept
MusicLrcColor mapIndexToColor(MusicLrcColor::Color index)
QList< QColor > m_frontColor
void setLinearGradientColor(const MusicLrcColor &color)
QLinearGradient m_linearGradient
#define const
Definition: zconf.h:233
QList< QColor > readColorConfig(const QString &value)