forked from spygg/QBaiduFm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlyrics.cpp
136 lines (112 loc) · 3.36 KB
/
lyrics.cpp
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
#include "lyrics.h"
#include "ui_lyrics.h"
#include <QRegExp>
#include <QDebug>
#include <algorithm>
Lyrics::Lyrics(QWidget *parent) :
QFrame(parent),
ui(new Ui::Lyrics)
{
ui->setupUi(this);
setFrameStyle(QFrame::NoFrame);
ui->label->setWordWrap(true);
ui->label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_iCurrentIndex = 0;
}
Lyrics::~Lyrics()
{
delete ui;
}
void Lyrics::currentSongChannged(QString songName, QString aristName, QString ablumName)
{
m_songName = songName;
m_aristName = aristName;
m_ablumName = ablumName;
QString lrc;
lrc.clear();
lrc += QString("<p align='center'><span style=' font-style:italic;'>");
lrc += m_aristName + "--" + m_songName;
lrc += QString("</span></p>");
ui->label->setText(lrc);
}
void Lyrics::setLrc(QString lrc)
{
QTextStream stream(&lrc);
QTextCodec *codec = QTextCodec::codecForName("utf-8");
stream.setCodec(codec);
m_iCurrentIndex = 0;
m_lyric.clear();
m_lyric = parse(stream);
}
QList<QLyric> Lyrics::parse(QTextStream &stream)
{
QRegExp timeExp;
timeExp.setPatternSyntax(QRegExp::RegExp);
timeExp.setCaseSensitivity(Qt::CaseSensitive);
timeExp.setPattern("\\[([0-9]{2}):([0-9]{2})\\.([0-9]{2})\\]");
QList<QLyric> result;
while (!stream.atEnd()) {
QString line = stream.readLine();
int ret = timeExp.indexIn(line);
QList<QTime> ticks;
int lastindex = 0;
while (ret >= 0) {
QStringList tstr = timeExp.capturedTexts();
QTime time(0, tstr[1].toInt(), tstr[2].toInt(), tstr[3].toInt());
ticks.append(time);
lastindex = ret + timeExp.matchedLength();
ret = timeExp.indexIn(line, lastindex);
}
QString lyricstr = line.right(line.size() - lastindex);
for (const QTime& t : ticks) {
QLyric lyric;
lyric.time = t.minute() * 60 * 1000 + t.second() * 1000 + t.msec();
lyric.lyric = lyricstr;
// qDebug() << lyricstr;
result.append(lyric);
}
}
std::sort(result.begin(), result.end(), [] (const QLyric& a, const QLyric& b) -> bool {
return a.time < b.time;
});
return result;
}
void Lyrics::setTickLrc(qint64 position)
{
if(m_lyric.size() <= 0 || m_iCurrentIndex >= m_lyric.size())
return;
for(int i = 0; i < m_lyric.size(); i++)
{
if(position > m_lyric.at(i).time)
{
m_iCurrentIndex = i;
}
}
if(position > m_lyric.at(m_iCurrentIndex).time)
{
QString lrc;
lrc.clear();
lrc += QString("<p align='center'><span style=' font-style:italic;'>");
lrc += m_aristName + " - " + m_songName;
lrc += QString("</span></p>");
for(int j = m_iCurrentIndex - 1; j < m_lyric.size(); j++)
{
if(j < 0)
continue;
if(m_iCurrentIndex == j)
{
lrc += QString("<p align='center'><span style=' font-weight:600;'>");
lrc += m_lyric.at(m_iCurrentIndex).lyric;
lrc += QString("</span></p>");
}
else
{
lrc += "<p align='center'>";
lrc += m_lyric.at(j).lyric;
lrc += "</p>";
}
}
ui->label->setText(lrc);
m_iCurrentIndex++;
}
}