-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLyricWidget.cpp
195 lines (181 loc) · 6.78 KB
/
LyricWidget.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
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
#include "LyricWidget.h"
#include <algorithm>
#include <QTextCodec>
LyricWidget::LyricWidget(QWidget *parent) :
QWidget(parent)
{
if (this->objectName().isEmpty())
this->setObjectName(QString::fromUtf8("LyricWidget"));
this->resize(831, 411);
this->setStyleSheet(QString::fromUtf8(""));
label_3i = new QLabel(this);
label_3i->setObjectName(QString::fromUtf8("label_3i"));
label_3i->setGeometry(QRect(0, 30, 831, 31));
QFont font;
font.setFamily(QString::fromUtf8("Microsoft JhengHei UI"));
font.setPointSize(10);
font.setBold(true);
font.setWeight(75);
label_3i->setFont(font);
label_3i->setStyleSheet(QString::fromUtf8("color: rgba(255, 255, 255, 170);"));
label_3i->setAlignment(Qt::AlignCenter);
label_3i->setText(QString::fromLocal8Bit("I Lived (《赐予者》电影插曲|2015年苹果秋季发布会表演歌曲原曲) - OneRepublic"));
label_2i = new QLabel(this);
label_2i->setObjectName(QString::fromUtf8("label_2i"));
label_2i->setGeometry(QRect(0, 80, 831, 31));
label_2i->setFont(font);
label_2i->setStyleSheet(QString::fromUtf8("color: rgba(255, 255, 255, 170);"));
label_2i->setAlignment(Qt::AlignCenter);
label_2i->setText(QString::fromLocal8Bit("I Lived (《赐予者》电影插曲|2015年苹果秋季发布会表演歌曲原曲) - OneRepublic"));
label_i = new QLabel(this);
label_i->setObjectName(QString::fromUtf8("label_i"));
label_i->setGeometry(QRect(0, 190, 831, 31));
QFont font1;
font1.setFamily(QString::fromUtf8("Microsoft JhengHei UI"));
font1.setPointSize(12);
font1.setBold(true);
font1.setItalic(false);
font1.setUnderline(false);
font1.setWeight(75);
label_i->setFont(font1);
label_i->setStyleSheet(QString::fromUtf8("color: rgb(255, 255, 255);"));
label_i->setAlignment(Qt::AlignCenter);
label_i->setText(QString::fromLocal8Bit("I Lived (《赐予者》电影插曲|2015年苹果秋季发布会表演歌曲原曲) - OneRepublic"));
label_i1 = new QLabel(this);
label_i1->setObjectName(QString::fromUtf8("label_i1"));
label_i1->setGeometry(QRect(0, 250, 831, 31));
label_i1->setFont(font);
label_i1->setStyleSheet(QString::fromUtf8("color: rgba(255, 255, 255, 170);"));
label_i1->setAlignment(Qt::AlignCenter);
label_i1->setText(QString::fromLocal8Bit("I Lived (《赐予者》电影插曲|2015年苹果秋季发布会表演歌曲原曲) - OneRepublic"));
label_i2 = new QLabel(this);
label_i2->setObjectName(QString::fromUtf8("label_i2"));
label_i2->setGeometry(QRect(0, 300, 831, 31));
label_i2->setFont(font);
label_i2->setStyleSheet(QString::fromUtf8("color: rgba(255, 255, 255, 170);"));
label_i2->setAlignment(Qt::AlignCenter);
label_i2->setText(QString::fromLocal8Bit("I Lived (《赐予者》电影插曲|2015年苹果秋季发布会表演歌曲原曲) - OneRepublic"));
label_1i = new QLabel(this);
label_1i->setObjectName(QString::fromUtf8("label_1i"));
label_1i->setGeometry(QRect(0, 130, 831, 31));
label_1i->setFont(font);
label_1i->setStyleSheet(QString::fromUtf8("color: rgba(255, 255, 255, 170);"));
label_1i->setAlignment(Qt::AlignCenter);
label_1i->setText(QString::fromLocal8Bit("I Lived (《赐予者》电影插曲|2015年苹果秋季发布会表演歌曲原曲) - OneRepublic"));
label_i3 = new QLabel(this);
label_i3->setObjectName(QString::fromUtf8("label_i3"));
label_i3->setGeometry(QRect(0, 350, 831, 31));
label_i3->setFont(font);
label_i3->setStyleSheet(QString::fromUtf8("color: rgba(255, 255, 255, 170);"));
label_i3->setAlignment(Qt::AlignCenter);
label_i3->setText(QString::fromLocal8Bit("I Lived (《赐予者》电影插曲|2015年苹果秋季发布会表演歌曲原曲) - OneRepublic"));
// clear();//清空用于排版时的测试内容(.ui文件中)
}
LyricWidget::~LyricWidget()
{
}
//重载比较(歌词按时间排序)
bool operator <(const LyricLine& A, const LyricLine& B){
return A.time<B.time;
}
bool LyricWidget::process(QString filePath)
{
QFile lyricFile(filePath);
lyricFile.open(QFile::ReadOnly);
QString content(QString::fromLocal8Bit(lyricFile.readAll()));
lyricFile.close();
//先清空歌词
lines.clear();
const QRegExp rx("\\[(\\d+):(\\d+(\\.\\d+)?)\\]"); //用来查找时间标签的正则表达式
// 步骤1
int pos = rx.indexIn(content);
if (pos == -1) {
return false;
}
else {
int lastPos;
QList<int> timeLabels;
do {
// 步骤2
timeLabels << (rx.cap(1).toInt() * 60 + rx.cap(2).toDouble()) * 1000;
lastPos = pos + rx.matchedLength();
pos = rx.indexIn(content, lastPos);
if (pos == -1) {
QString text = content.mid(lastPos).trimmed();
foreach (const int& time, timeLabels)
lines.push_back(LyricLine(time, text));
break;
}
// 步骤3
QString text = content.mid(lastPos, pos - lastPos);
if (!text.isEmpty()) {
foreach (const int& time, timeLabels)
lines.push_back(LyricLine(time, text.trimmed()));
timeLabels.clear();
}
}
while (true);
// 步骤4
stable_sort(lines.begin(), lines.end());
}
if (lines.size()) {
return true;
}
return false;
}
int LyricWidget::getIndex(qint64 position)
{
if(!lines.size()){
return -1;
}else{
if(lines[0].time>=position){
return 0;
}
}
int i=1;
for(i=1;i<lines.size();i++){
if(lines[i-1].time<position && lines[i].time>=position){
return i-1;
}
}
return lines.size()-1;
}
void LyricWidget::show(qint64 position)
{
int index=getIndex(position);
if(index==-1){
label_3i->setText("");
label_2i->setText("");
label_1i->setText("");
label_i->setText(u8"当前歌曲无歌词");
label_i1->setText("");
label_i2->setText("");
label_i3->setText("");
}else{
label_3i->setText(getLyricText(index-3));
label_2i->setText(getLyricText(index-2));
label_1i->setText(getLyricText(index-1));
label_i->setText(getLyricText(index));
label_i1->setText(getLyricText(index+1));
label_i2->setText(getLyricText(index+2));
label_i3->setText(getLyricText(index+3));
}
}
QString LyricWidget::getLyricText(int index)
{
if(index<0 || index>=lines.size()){
return "";
}else{
return lines[index].text;
}
}
void LyricWidget::clear()
{
label_3i->setText("");
label_2i->setText("");
label_1i->setText("");
label_i->setText("");
label_i1->setText("");
label_i2->setText("");
label_i3->setText("");
}