-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylrcwidget.cpp
163 lines (127 loc) · 4.2 KB
/
mylrcwidget.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
#include "mylrcwidget.h"
#include <QString>
#include <QPaintEvent>
#include <QFile>
#include <QIODevice>
#include <QTextStream>
#include <QDebug>
#include <QPainter>
#include <QRectF>
#include <QRegExp>
#include <QFont>
#include <QColor>
MyLRCWidget::MyLRCWidget(QWidget *parent)
: QWidget(parent)
{
}
void MyLRCWidget::loadFile(const QString &fileName)
{
lrcFile = fileName;
QFile file(lrcFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "open LRC file fail";
lrcVector.clear();
}
else {
file.close();
analysisLrc(lrcVector);
}
}
void MyLRCWidget::paintEvent(QPaintEvent *)
{
QFont lyricFont("Microsoft YaHei Ui");
lyricFont.setPixelSize(FONTPIXELSIZE);
QPainter painter(this);
painter.setPen(Qt::black);
painter.setFont(lyricFont);
painter.setRenderHint(QPainter::TextAntialiasing);
qreal centerLine = this->height() / 2 - LINESPACING;
if (lrcVector.isEmpty())
painter.drawText(QRectF(this->rect()), Qt::AlignCenter, QString(QObject::tr("No File")));
else {
//current line (first paint line)
int currentLine = getCurrentLine();
if (currentLine < 0)
return;
//qDebug() << "lrc start paint";
//draw current line
painter.setPen(QColor(150, 150, 150));
painter.drawText(QRectF(0, centerLine, this->width(), LINESPACING),
Qt::AlignHCenter, lrcVector.at(currentLine).getSentence());
painter.setPen(Qt::black);
//draw up
int i = 0;
for (int index = currentLine - 1; index != -1; index--) {
i++;
painter.drawText(QRectF(0, centerLine - i * LINESPACING, this->width(), LINESPACING),
Qt::AlignHCenter, lrcVector.at(index).getSentence());
}
//draw down
int j = 0;
for (int index = currentLine + 1; index < lrcVector.size() - 1; index++) {
j++;
painter.drawText(QRectF(0, centerLine + j * LINESPACING, this->width(), LINESPACING),
Qt::AlignHCenter, lrcVector.at(index).getSentence());
}
}
}
void MyLRCWidget::analysisLrc(QVector<MyLRCSentence> &lrc)
{
//clear last data
lrc.clear();
QFile file(lrcFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "open LRC file fail (in analysisLrc function)";
lrcVector.clear();
}
else {
QTextStream in(&file);
QString lineString;
for (int i = 0; !in.atEnd(); i++) {
lineString = in.readLine();
QString pattern("\\[(\\d+):(\\d+(\\.\\d+)?)\\](.*)");
QRegExp rx(pattern);
int pos = lineString.indexOf(rx);
//qDebug() << "pos: " << pos;
if (pos < 0)
continue;
else {
MyLRCSentence sent;
qint64 position = (rx.cap(1).toInt() * 60 + rx.cap(2).toDouble()) * 1000;
//qDebug() << rx.capturedTexts();
//qDebug() << position;
sent.setPosition(position);
sent.setSentence(rx.cap(4));
//qDebug() << position << " " << rx.cap(4);
lrc.push_back(sent);
}
}
qint64 positionEndMark = POSITIONENDMARK;
QString sentenceEndMark = SENTENCEENDMARK;
MyLRCSentence endMark;
endMark.setPosition(positionEndMark);
endMark.setSentence(sentenceEndMark);
lrc.push_back(endMark);
}
}
void MyLRCWidget::flashCurrentPosition(qint64 pos)
{
currentPosition = pos;
//qDebug() << "current position: " << currentPosition;
this->update();
}
int MyLRCWidget::getCurrentLine()
{
for (int i = 0; i < lrcVector.size(); i++) {
//use 'end_mark' to avoid 'at(i + 1)' make error
if (lrcVector.at(i).getPosition() == POSITIONENDMARK && lrcVector.at(i).getSentence() == SENTENCEENDMARK)
break;
if (lrcVector.at(i).getPosition() <= currentPosition && lrcVector.at(i + 1).getPosition() > currentPosition)
return i;
}
return -1;
}
void MyLRCWidget::tryToLoadLRCFile(const QString &fileName)
{
this->loadFile(fileName);
}