-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlyricstreamwidget.h
350 lines (317 loc) · 12.1 KB
/
lyricstreamwidget.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#ifndef LYRICSTREAMWIDGET_H
#define LYRICSTREAMWIDGET_H
#include <QWidget>
#include <QPainter>
#include "desktoplyricwidget.h"
class LyricStreamWidget : public QWidget
{
Q_OBJECT
public:
LyricStreamWidget(QWidget* parent = nullptr) : QWidget(parent), settings(QApplication::applicationDirPath()+"/musics.ini", QSettings::Format::IniFormat),
updateTimer(new QTimer(this))
{
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMenu()));
playingColor = qvariant_cast<QColor>(settings.value("lyric/playingColor", playingColor));
waitingColor = qvariant_cast<QColor>(settings.value("lyric/waitingColor", waitingColor));
pointSize = settings.value("lryic/pointSize", pointSize).toInt();
connect(updateTimer, SIGNAL(timeout()), this, SLOT(update()));
updateTimer->setInterval(16);
}
void updateFixedHeight()
{
QFont font = this->font();
font.setPointSize(pointSize);
QFontMetrics fm(font);
this->lineSpacing = fm.height() + (fm.lineSpacing() - fm.height()) * 2;
setFixedHeight((lyricStream.size() + verticalMargin * 2) * lineSpacing);
}
void setLyric(QString text)
{
// 检测是不是全是毫秒还是10毫秒
int ms10x = 10;
QRegularExpression re10("\\[\\d{2}:\\d{2}\\.([1-9]\\d{2})\\]");
QRegularExpressionMatch match10;
if (text.lastIndexOf(re10, -1, &match10) != -1)
{
int val = match10.captured(1).toInt();
qDebug() << " 匹配项:" << val;
if (val > 0) // 存在不为0的三位数
{
qDebug() << "检测到勉强受支持的歌词格式:第三位是毫秒";
ms10x = 1;
}
}
// 遍历每一行
QStringList sl = text.split("\n", QString::SkipEmptyParts);
LyricBean prevLyric(false);
qint64 currentTime = 0;
lyricStream.clear();
foreach (QString line, sl)
{
QRegularExpression re("^\\[(\\d{2}):(\\d{2})\\.(\\d{2,3})\\](\\[(\\d{2}):(\\d{2})\\.(\\d{2,3})\\])?(.*)$");
QRegularExpressionMatch match;
if (line.indexOf(re, 0, &match) == -1)
{
LyricBean lyric;
lyric.start = currentTime;
lyric.end = 0;
lyric.text = line;
lyricStream.append(lyric);
continue;
}
QStringList caps = match.capturedTexts();
LyricBean lyric;
int minute = caps.at(1).toInt();
int second = caps.at(2).toInt();
int ms10 = caps.at(3).toInt();
lyric.start = minute * 60000 + second*1000 + ms10 * ms10x;
if (!caps.at(4).isEmpty()) // 有终止时间
{
int minute = caps.at(5).toInt();
int second = caps.at(6).toInt();
int ms10 = caps.at(7).toInt();
lyric.end = minute * 60000 + second*1000 + ms10 * ms10x;
}
lyric.text = caps.at(8);
lyricStream.append(lyric);
currentTime = lyric.start;
}
currentRow = 0;
updateFixedHeight();
update();
}
/**
* @return 当前行有没有改变
*/
bool setPosition(qint64 position)
{
int prevRow = currentRow;
if (!lyricStream.size())
return false;
if (currentRow < 0 || currentRow >= lyricStream.size())
currentRow = 0;
LyricBean lyric = lyricStream.at(currentRow);
if (currentRow == lyricStream.size()-1 && lyric.start <= position) // 已经到末尾了
return false;
LyricBean nextLyric = currentRow == lyricStream.size()-1 ? LyricBean(false) : lyricStream.at(currentRow + 1);
if (lyric.start <= position && nextLyric.start >= position) // 不需要改变
return false;
if (lyric.start > position) // 什么情况?从头强制重新开始!
currentRow = 0;
while (currentRow+1 < lyricStream.size() && lyricStream.at(currentRow+1).start < position)
{
currentRow++;
}
if (currentRow == prevRow)
return false;
switchRowTimestamp = QDateTime::currentMSecsSinceEpoch();
updateTimer->start();
update();
return true;
}
int getCurrentTop() const
{
return (currentRow + verticalMargin + 0.5) * lineSpacing;
}
void setColors(QColor p, QColor w)
{
playingColor = p;
waitingColor = w;
update();
}
protected:
void paintEvent(QPaintEvent *event) override
{
QPainter painter(this);
QFont font(painter.font());
font.setPointSize(pointSize);
painter.setFont(font);
painter.setPen(waitingColor);
QFontMetrics fm(font);
int lineSpacing = fm.height() + (fm.lineSpacing() - fm.height()) * 2;
double rowOffset = verticalMargin;
qint64 currentTimestamp = QDateTime::currentMSecsSinceEpoch();
qint64 aniDelta = currentTimestamp - switchRowTimestamp;
bool animating = aniDelta < switchRowDuration;
const double scale = 0.5;
for (int i = 0; i < lyricStream.size(); i++)
{
double row = i + rowOffset;
int top = row * lineSpacing;
if (i == currentRow - 1 && animating)
{
painter.save();
double prop = scale - scale * aniDelta / switchRowDuration;
QFont font(painter.font());
font.setPointSize(pointSize * (1+prop));
painter.setFont(font);
QRect lineRect(0, top - lineSpacing*prop/2, width(), lineSpacing * (1 + prop));
painter.drawText(lineRect, Qt::AlignCenter, lyricStream.at(i).text);
painter.restore();
rowOffset += prop;
}
else if (i == currentRow)
{
painter.save();
if (animating)
{
double prop = scale * aniDelta / switchRowDuration;
QRect lineRect(0, top - lineSpacing*prop/2, width(), lineSpacing * (1 + 2 * prop));
QFont font(painter.font());
font.setPointSize(pointSize * (1+prop));
painter.setPen(playingColor);
painter.setFont(font);
painter.drawText(lineRect, Qt::AlignCenter, lyricStream.at(i).text);
rowOffset += prop;
}
else
{
QRect lineRect(0, top - lineSpacing*0.25, width(), lineSpacing * (1+scale*2));
QFont font(painter.font());
font.setPointSize(pointSize * (1+scale));
painter.setPen(playingColor);
painter.setFont(font);
painter.drawText(lineRect, Qt::AlignCenter, lyricStream.at(i).text);
rowOffset += scale;
if (updateTimer->isActive())
updateTimer->stop();
}
painter.restore();
}
else
{
QRect lineRect(0, top, width(), lineSpacing);
painter.drawText(lineRect, Qt::AlignCenter, lyricStream.at(i).text);
}
}
}
void adjustLyricTime(int begin, int offset)
{
if (!lyricStream.size())
return ;
int ms10 = 10;
foreach (LyricBean lyric, lyricStream)
{
if (lyric.start % 10 != 0)
{
ms10 = 1;
qDebug() << "第三位使用毫秒" << lyric.start << lyric.text;
break;
}
}
QStringList sl;
for (int i = 0; i < lyricStream.size(); i++)
{
LyricBean line = lyricStream.at(i);
if (i >= begin && line.start != 0)
{
line.start += offset;
if (line.end)
line.end += offset;
if (line.start < 0)
line.start = 0;
if (line.end < 0)
line.end = 0;
lyricStream[i] = line;
}
// 歌词转换回字符串
int minute = line.start / 60000;
int second = line.start % 60000 / 1000;
int msecond = line.start % 1000 / ms10;
QString s = QString("[%1:%2.%3]")
.arg(minute, 2, 10, QLatin1Char('0'))
.arg(second, 2, 10, QLatin1Char('0'))
.arg(msecond, ms10 == 1 ? 3 : 2, 10, QLatin1Char('0'));
if (line.end)
{
int minute = line.end / 60000;
int second = line.end % 60000 / 1000;
int msecond = line.end % 1000 / ms10;
s += QString("[%1:%2.%3]")
.arg(minute, 2, 10, QLatin1Char('0'))
.arg(second, 2, 10, QLatin1Char('0'))
.arg(msecond, ms10 == 1 ? 3 : 2, 10, QLatin1Char('0'));
}
s += line.text;
sl << s;
}
emit signalAdjustLyricTime(sl.join("\n"));
}
signals:
void signalSwitchCoverBlur();
void signalAdjustLyricTime(QString);
private slots:
void showMenu()
{
QMenu* menu = new QMenu(this);
QAction *playingC = new QAction("选择已播放颜色",this);
QAction *waitingC = new QAction("选择未播放颜色", this);
QMenu *fontMenu = new QMenu("字体大小", menu);
QMenu *timeMenu = new QMenu("调整速度", menu);
menu->addAction(playingC);
menu->addAction(waitingC);
menu->addMenu(fontMenu);
menu->addMenu(timeMenu);
connect(playingC, &QAction::triggered, [=]{
QColor c = QColorDialog::getColor(playingColor, this, "选择已播放颜色", QColorDialog::ShowAlphaChannel);
if (!c.isValid())
return ;
if (c != playingColor)
{
settings.setValue("lyric/playingColor", playingColor = c);
update();
}
});
connect(waitingC, &QAction::triggered, [=]{
QColor c = QColorDialog::getColor(waitingColor, this, "选择未播放颜色", QColorDialog::ShowAlphaChannel);
if (!c.isValid())
return ;
if (c != waitingColor)
{
settings.setValue("lyric/waitingColor", waitingColor = c);
update();
}
});
for (int i = 8; i < 30; i++)
{
QAction* fm = new QAction(QString::number(i), this);
fontMenu->addAction(fm);
if(pointSize == i)
{
fm->setCheckable(true);
fm->setChecked(true);
}
connect(fm, &QAction::triggered, [=]{
pointSize = i;
settings.setValue("lyric/pointSize", pointSize);
updateFixedHeight();
update();
});
}
timeMenu->addAction("加快5秒",[=]{adjustLyricTime(0, -5000);});
timeMenu->addAction("加快2秒",[=]{adjustLyricTime(0, -2000);});
timeMenu->addAction("加快1秒",[=]{adjustLyricTime(0, -1000);});
timeMenu->addAction("减慢1秒",[=]{adjustLyricTime(0, +1000);});
timeMenu->addAction("减慢2秒",[=]{adjustLyricTime(0, +2000);});
timeMenu->addAction("减慢5秒",[=]{adjustLyricTime(0, +5000);});
menu->exec(cursor().pos());
QList<QAction*> list = menu->actions();
foreach(QAction* action, list)
delete action;
delete menu;
}
private:
QSettings settings;
LyricStream lyricStream;
int currentRow = -1;
qint64 switchRowTimestamp = 0;
const int switchRowDuration = 200;
QTimer* updateTimer;
int lineSpacing =30;
int verticalMargin = 5; // 上下间距5行
int pointSize = 12;
QColor playingColor = QColor(0, 255, 255);
QColor waitingColor = Qt::white;
};
#endif // LYRICSTREAMWIDGET_H