-
Notifications
You must be signed in to change notification settings - Fork 2
/
osdlyricswidget.cpp
197 lines (172 loc) · 5.58 KB
/
osdlyricswidget.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
196
197
#include "osdlyricswidget.h"
#include <QMouseEvent>
#include <QPainter>
#include <QApplication>
#include <QScreen>
#include <QDebug>
#ifdef Q_OS_LINUX
#include <QX11Info>
#include <X11/extensions/shape.h>
#endif
#ifdef Q_OS_WIN32
#include <windows.h>
#endif
OSDLyricsWidget::OSDLyricsWidget(QWidget *parent) :
QWidget(parent)
{
this->lrcLineList = new QList<LrcLine>();
this->timer = new QTimer(this);
this->timer->setInterval(100);
this->timer->start();
connect(this->timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
// this->currentLyrics = QString("戴维营教育嵌入式Linux Qt5开发培训学习作品");
this->setFixedSize(1000,100);
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
this->setAttribute(Qt::WA_TranslucentBackground,true);
this->setAttribute(Qt::WA_TransparentForMouseEvents,true);
this->setAttribute(Qt::WA_MouseTracking,false);
this->setWindowOpacity(1);
QSize size = QApplication::screens().first()->geometry().size();
this->move( (size.width() - this->width())/2, (size.height() - this->height()) - 100 );
#ifdef Q_OS_LINUX
XShapeCombineRectangles(QX11Info::display(), winId(), ShapeInput, 0,
0, NULL, 0, ShapeSet, YXBanded);
#endif
#ifdef Q_OS_WIN
SetWindowLong((HWND)winId(), GWL_EXSTYLE, GetWindowLong((HWND)winId(), GWL_EXSTYLE) |
WS_EX_TRANSPARENT | WS_EX_LAYERED);
SetWindowPos((HWND)winId(),HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
#endif
}
OSDLyricsWidget::~OSDLyricsWidget()
{
delete this->lrcLineList;
delete this->timer;
}
void OSDLyricsWidget::mousePressEvent(QMouseEvent *event)
{
this->windowPos = this->pos();
QPoint mousePos = event->globalPos();
this->dPos = mousePos - windowPos;
}
void OSDLyricsWidget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - this->dPos);
}
void OSDLyricsWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
if(this->currentLyrics.isEmpty())
{
return;
}
QPainter painter(this);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.setRenderHints(QPainter::TextAntialiasing|QPainter::Antialiasing);
QFont font = QFont("宋体", 36);
painter.setFont(font);
int textPixalLength = 0;
#if 1
QFontMetrics fontMetrics = QFontMetrics(font);
textPixalLength = fontMetrics.boundingRect(this->currentLyrics).width();
#else
textPixalLength = this->rect().width();
#endif
QRect rect_l = this->rect();
painter.fillRect(rect_l, Qt::transparent);
painter.setPen(QColor("lightgreen"));
painter.drawText(rect_l,Qt::AlignLeft|Qt::AlignVCenter,this->currentLyrics);
float percent = 1 - (1.0*this->curLyricTimeLeft)/this->curLyricTime;
QRect rect_h(rect_l.x(), rect_l.y(),textPixalLength*percent,rect_l.height());
painter.setPen(QColor("transparent"));
painter.drawRect(rect_h);
painter.setPen(QColor("red"));
painter.drawText(rect_h,Qt::AlignLeft|Qt::AlignVCenter,this->currentLyrics);
// qDebug()<<"lrc:"<<this->currentLyrics << "percent:"<<percent;
}
void OSDLyricsWidget::handleNewLyricsData(QByteArray *lrcData)
{
this->lrcLineList->clear();
this->currentLyrics = "";
if(NULL==lrcData)
{
return;
}
QStringList lrcList = QString(*lrcData).split("\n");
foreach (QString lrc, lrcList) {
if(lrc.size()<=5) continue;
lrc = lrc.toLower().simplified();
if(lrc.startsWith("[ti:") || lrc.startsWith("[ar:") || lrc.startsWith("[al:"))
{
continue;
}
//process lyric line
this->processLyricLine(lrc);
}
qSort(*this->lrcLineList);
}
void OSDLyricsWidget::processLyricLine(QString line)
{
if(!line.startsWith("[")) return;
int timeStampEndPos = line.lastIndexOf("]");
if(timeStampEndPos < 9 ) return;
QString lrcLineStr = line.mid(timeStampEndPos+1);
QString timeStampStr = line.left(timeStampEndPos+1);
QStringList timeStrList = timeStampStr.split("]");
foreach (QString timeStr, timeStrList) {
if(!timeStr.startsWith("[")) continue;
timeStr.remove(0,1);
int min = 0, sec = 0, msec = 0;
::sscanf(timeStr.toStdString().data(),"%d:%d.%d", &min, &sec, &msec);
long milliseconds = min*60*1000 + sec*1000 + msec*10;
LrcLine lrc_line;
lrc_line.milliseconds = milliseconds;
lrc_line.lineStr = lrcLineStr;
this->lrcLineList->append(lrc_line);
}
}
void OSDLyricsWidget::updateLyrics(long timePos)
{
for(int i = 1; i < this->lrcLineList->count(); i++)
{
LrcLine line = this->lrcLineList->at(i);
if(line.milliseconds > timePos)
{
if(this->curIndex == i)
{
break;
}
this->curIndex = i;
LrcLine prevLine = this->lrcLineList->at(i-1);
this->currentLyrics = prevLine.lineStr;
this->curLyricTimeLeft = this->curLyricTime = line.milliseconds - prevLine.milliseconds;
break;
}
}
this->update();
this->updateGeometry();
}
void OSDLyricsWidget::handleTimeout()
{
if(this->curLyricTimeLeft > (unsigned long)this->timer->interval())
{
this->curLyricTimeLeft -= this->timer->interval();
}else
{
this->curLyricTimeLeft = 0;
}
// qDebug()<<"left:"<<this->curLyricTimeLeft;
}
void OSDLyricsWidget::pauseTimer(bool stop)
{
if(stop){
this->timer->stop();
}else
{
this->timer->start();
}
}
bool LRCLine::operator <(const LRCLine &line) const
{
return this->milliseconds < line.milliseconds;
}