forked from ij96/163-lyrics-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlyricshighlighter.cpp
More file actions
81 lines (70 loc) · 3.07 KB
/
Copy pathlyricshighlighter.cpp
File metadata and controls
81 lines (70 loc) · 3.07 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
#include "lyricshighlighter.h"
LyricsHighlighter::LyricsHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) {}
void LyricsHighlighter::highlightBlock(const QString &text) {
QRegularExpression expression;
QRegularExpressionMatchIterator i;
expression.setPatternOptions(QRegularExpression::MultilineOption);
// Time tags
QTextCharFormat time_format;
time_format.setFontFamily("Courier");
time_format.setFontItalic(false);
time_format.setFontFixedPitch(true);
time_format.setForeground(Qt::blue);
time_format.setFontWeight(QFont::Light);
expression.setPattern("^(?:\\[[0-9:.]+\\])+");
i = expression.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
setFormat(match.capturedStart(), match.capturedLength(), time_format);
}
// Meta tags
QTextCharFormat meta_tag_format;
meta_tag_format.setFontFamily("Courier");
meta_tag_format.setFontItalic(false);
meta_tag_format.setFontFixedPitch(true);
meta_tag_format.setForeground(Qt::red);
meta_tag_format.setFontWeight(QFont::Light);
QTextCharFormat meta_info_format;
meta_info_format.setFontFamily(document()->defaultFont().family());
meta_info_format.setFontItalic(false);
meta_info_format.setFontFixedPitch(false);
meta_info_format.setForeground(Qt::darkRed);
meta_info_format.setFontWeight(QFont::Normal);
expression.setPattern("^\\[[^\\]:]*[^0-9\\]:]+[^\\]:]*:([^\\]]*)\\]");
i = expression.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
setFormat(match.capturedStart(0), match.capturedLength(0), meta_tag_format);
setFormat(match.capturedStart(1), match.capturedLength(1), meta_info_format);
}
// Unknown tags
QTextCharFormat unknown_format;
unknown_format.setFontFamily(document()->defaultFont().family());
unknown_format.setFontItalic(false);
unknown_format.setFontFixedPitch(false);
unknown_format.setForeground(Qt::gray);
unknown_format.setFontWeight(QFont::Normal);
expression.setPattern("(?:\\][^[]+)"
"(?=((?:\\[[^\\[]+\\])+))"
"|(\\[[^:]*\\]"
"|\\[[0-9]+:[^\\]:]*[^0-9.:\\]]+[^\\]:]*\\])");
i = expression.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
setFormat(match.capturedStart(1), match.capturedLength(1), unknown_format);
setFormat(match.capturedStart(2), match.capturedLength(2), unknown_format);
}
// Lyrics text
QTextCharFormat text_format;
text_format.setFontFamily(document()->defaultFont().family());
text_format.setFontItalic(false);
text_format.setFontFixedPitch(false);
text_format.setForeground(Qt::black);
text_format.setFontWeight(QFont::Normal);
expression.setPattern("(?:^|\\])([^[\\]]+)(?:$|\\[)");
i = expression.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
setFormat(match.capturedStart(1), match.capturedLength(1), text_format);
}
}