-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlyrics.cpp
More file actions
136 lines (121 loc) · 3.23 KB
/
Copy pathlyrics.cpp
File metadata and controls
136 lines (121 loc) · 3.23 KB
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"
Lyrics::Lyrics(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Lyrics)
{
ui->setupUi(this);
setWindowFlag(Qt::FramelessWindowHint);
connect(ui->Hidebtn,&QPushButton::clicked,this,[=](){
hide();
});
ui->Hidebtn->setIcon(QIcon(":/images/xiala.png"));
_animation=new QPropertyAnimation(this,"geometry",this);
_animation->setDuration(250);
_animation->setStartValue(QRect(10,10,this->width(),this->height()));
_animation->setEndValue(QRect(10,10+height(),width(),height()));
connect(ui->Hidebtn,&QPushButton::clicked,this,[=](){
_animation->start();
});
connect(_animation,&QPropertyAnimation::finished,this,[=](){
hide();
});
}
Lyrics::~Lyrics()
{
delete ui;
}
bool Lyrics::ParseLrc(const QString &lrcpath)
{
_linelist.clear();
QFile lrcfile(lrcpath);
if(lrcfile.open(QFile::ReadOnly)==false)
{
qDebug()<<"open lrcfile false";
return false;
}
while (!lrcfile.atEnd()) {
QString word=lrcfile.readLine(1024);
int left=word.indexOf('[');
int right=word.indexOf(']');
qint64 linetime=0;
int start=0;
int end=0;
QString time=word.mid(left,right-left+1);
start=1;
end=time.indexOf(':');
linetime+=time.mid(start,end-start).toInt()*60*1000;
start=end+1;
end=time.indexOf('.',start);
linetime+=time.mid(start,end-start).toInt()*1000;
start=end+1;
end=time.indexOf('.',start);
linetime+=time.mid(start,end-start).toInt();
QString lyrics=word.mid(right+1).trimmed();
_linelist.push_back(Ui::LyricsLine(linetime,lyrics.trimmed()));
}
for (auto e:_linelist)
{
qDebug()<<e._time<<":"<<e._text;
}
return true;
}
void Lyrics::SetMusicName(const QString &name)
{
ui->MusicName->setText(name);
}
void Lyrics::SetSinger(const QString &singer)
{
ui->Singer->setText(singer);
}
int Lyrics::GetLineLrcWordIndex(qint64 pos)
{
if(_linelist.isEmpty())
{
return -1;
}
if(pos<_linelist[0]._time)
{
return 0;
}
for(int i=1;i<_linelist.size();i++)
{
if(pos>=_linelist[i-1]._time&&pos<_linelist[i]._time)
{
return i;
}
}
return _linelist.size()-1;
}
QString Lyrics::GetLineLrcWord(qint64 index)
{
if(index<0||index>=_linelist.size())
{
return "";
}
return _linelist[index]._text;
}
void Lyrics::ShowLrc(int time)
{
//qDebug()<<"showlrc";
int index=GetLineLrcWordIndex(time);
if(index==-1)
{
ui->Line1->setText("");
ui->Line2->setText("");
ui->Line3->setText("");
ui->LineCenter->setText("当前歌曲无歌词");
ui->Line4->setText("");
ui->Line5->setText("");
ui->Line6->setText("");
}else
{
ui->Line1->setText(GetLineLrcWord(index-3));
ui->Line2->setText(GetLineLrcWord(index-2));
ui->Line3->setText(GetLineLrcWord(index-1));
ui->LineCenter->setText(GetLineLrcWord(index));
ui->Line4->setText(GetLineLrcWord(index+1));
ui->Line5->setText(GetLineLrcWord(index+2));
ui->Line6->setText(GetLineLrcWord(index+3));
}
}