-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdesktoplyricwidget.cpp
429 lines (383 loc) · 13.8 KB
/
desktoplyricwidget.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "desktoplyricwidget.h"
DesktopLyricWidget::DesktopLyricWidget(QWidget *parent) : QWidget(parent),
settings(QApplication::applicationDirPath()+"/musics.ini", QSettings::Format::IniFormat)
{
this->setWindowTitle("桌面歌词");
this->setMinimumSize(45, 15);
this->setMaximumSize(800, 100);
if ((jiWindow = settings.value("music/desktopLyricTrans", false).toBool()))
{
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
}
else
{
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
this->setAttribute(Qt::WA_TranslucentBackground, true);
}
this->setContextMenuPolicy(Qt::CustomContextMenu);
this->setMouseTracking(true);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMenu()));
lineMode = static_cast<LineMode>(settings.value("music/lineMode", lineMode).toInt());
alignMode = static_cast<AlignMode>(settings.value("music/alignMoed", alignMode).toInt());
playingColor = qvariant_cast<QColor>(settings.value("music/playingColor", playingColor));
waitingColor = qvariant_cast<QColor>(settings.value("music/waitingColor", waitingColor));
bgColor = qvariant_cast<QColor>(settings.value("music/bgColor", bgColor));
pointSize = settings.value("music/desktopLyricPointSize", pointSize).toInt();
QFont font = this->font();
font.setPointSize(pointSize);
QFontMetrics fm(this->font());
fontHeight = fm.height();
lineSpacing = fm.lineSpacing();
this->setMinimumHeight((fontHeight + boundaryWidth) * 2);
this->setMinimumWidth((fm.horizontalAdvance("一二三四五六七八九十十一十二")) * 2);
}
void DesktopLyricWidget::setLyric(QString text)
{
// 检测是不是全是毫秒还是10毫秒的
int ms10x = 10;
QRegularExpression re10("\\[\\d{2}:\\d{2}\\.([1-9]\\d{2})\\]");
QRegularExpressionMatch match10;
if (text.lastIndexOf(re10, -1, &match10) != -1)
{
int val = match10.captured(1).toInt();
if (val > 0) // 存在不为0的三位数
{
ms10x = 1;
}
}
// 遍历每一行
QStringList sl = text.split("\n", QString::SkipEmptyParts);
LyricBean prevLyric(false);
qint64 currentTime = 0;
lyricStream.clear();
foreach (QString line, sl)
{
QRegularExpression re("^\\[(\\d{2}):(\\d{2})\\.(\\d{2,3})\\](\\[(\\d{2}):(\\d{2})\\.(\\d{2,3})\\])?(.*)$");
QRegularExpressionMatch match;
if (line.indexOf(re, 0, &match) == -1)
{
LyricBean lyric;
lyric.start = currentTime;
lyric.end = 0;
lyric.text = line;
lyricStream.append(lyric);
continue;
}
QStringList caps = match.capturedTexts();
LyricBean lyric;
int minute = caps.at(1).toInt();
int second = caps.at(2).toInt();
int ms10 = caps.at(3).toInt();
lyric.start = minute * 60000 + second*1000 + ms10 * ms10x;
if (!caps.at(4).isEmpty()) // 有终止时间
{
int minute = caps.at(5).toInt();
int second = caps.at(6).toInt();
int ms10 = caps.at(7).toInt();
lyric.end = minute * 60000 + second*1000 + ms10 * ms10x;
}
lyric.text = caps.at(8);
lyricStream.append(lyric);
currentTime = lyric.start;
}
currentRow = 0;
update();
}
void DesktopLyricWidget::setColors(QColor p, QColor w)
{
playingColor = p;
waitingColor = w;
update();
}
void DesktopLyricWidget::showEvent(QShowEvent *event)
{
restoreGeometry(settings.value("music/desktopLyricGeometry").toByteArray());
}
void DesktopLyricWidget::hideEvent(QHideEvent *event)
{
settings.setValue("music/desktopLyricGeometry", this->saveGeometry());
}
bool DesktopLyricWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(eventType)
MSG* msg = static_cast<MSG*>(message);
switch(msg->message)
{
case WM_NCHITTEST:
const auto ratio = devicePixelRatioF(); // 解决4K下的问题
int xPos = static_cast<int>(GET_X_LPARAM(msg->lParam) / ratio - this->frameGeometry().x());
int yPos = static_cast<int>(GET_Y_LPARAM(msg->lParam) / ratio - this->frameGeometry().y());
if(xPos < boundaryWidth &&yPos<boundaryWidth) //左上角
*result = HTTOPLEFT;
else if(xPos>=width()-boundaryWidth&&yPos<boundaryWidth) //右上角
*result = HTTOPRIGHT;
else if(xPos<boundaryWidth&&yPos>=height()-boundaryWidth) //左下角
*result = HTBOTTOMLEFT;
else if(xPos>=width()-boundaryWidth&&yPos>=height()-boundaryWidth)//右下角
*result = HTBOTTOMRIGHT;
else if(xPos < boundaryWidth) //左边
*result = HTLEFT;
else if(xPos>=width()-boundaryWidth) //右边
*result = HTRIGHT;
/*else if(yPos<boundaryWidth) //上边
*result = HTTOP;*/
else if(yPos>=height()-boundaryWidth) //下边
*result = HTBOTTOM;
else //其他部分不做处理,返回false,留给其他事件处理器处理
return false;
return true;
}
return false; //此处返回false,留给其他事件处理器处理
}
void DesktopLyricWidget::enterEvent(QEvent *event)
{
hovering = true;
update();
}
void DesktopLyricWidget::leaveEvent(QEvent *event)
{
if (!this->geometry().contains(QCursor::pos()))
{
hovering = false;
}
else
{
QTimer::singleShot(300, [=]{
if (!this->geometry().contains(QCursor::pos()))
{
hovering = false;
update();
}
});
}
update();
}
void DesktopLyricWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
pressPos = event->pos();
}
QWidget::mousePressEvent(event);
}
void DesktopLyricWidget::mouseMoveEvent(QMouseEvent *event)
{
hovering = true;
if(event->buttons()&Qt::LeftButton)
{
move(QCursor::pos() - pressPos);
}
QWidget::mouseMoveEvent(event);
}
void DesktopLyricWidget::resizeEvent(QResizeEvent *event)
{
}
void DesktopLyricWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// 绘制背景
if (jiWindow)
{
painter.fillRect(this->rect(), bgColor);
}
if (hovering)
{
painter.setRenderHint(QPainter::Antialiasing, true);
QPainterPath path;
path.addRoundedRect(this->rect(), 5, 5);
painter.fillPath(path, QColor(64, 64, 64, 32));
}
// 绘制桌面歌词
QFont font;
font.setPointSize(pointSize);
font.setBold(true);
font.setPointSize(pointSize);
QFontMetrics fm(this->font());
fontHeight = fm.height();
lineSpacing = fm.lineSpacing();
painter.setFont(font);
// 文字区域
QRect rect = this->rect();
rect.setLeft(rect.left() + boundaryWidth);
rect.setTop(rect.top() + boundaryWidth);
rect.setRight(rect.right() - boundaryWidth);
rect.setBottom(rect.bottom() - boundaryWidth);
if (currentRow > -1 && currentRow < lyricStream.size())
{
bool cross = lineMode != SingleLine && currentRow % 2;
// 绘制当前句
if (currentRow + (cross ? 1 : 0) < lyricStream.size())
{
painter.setPen(cross ? waitingColor : playingColor);
QFlags<Qt::AlignmentFlag> align;
if (lineMode == SuitableLine || lineMode == DoubleLine)
{
align = Qt::AlignTop;
this->setMinimumHeight((fontHeight + boundaryWidth) * 4);
this->setMinimumWidth((fm.horizontalAdvance("一二三四五六七八九十十一十二")) * 2);
}
else if (lineMode == SingleLine)
{
align = Qt::AlignVCenter;
this->setMinimumHeight((fontHeight + boundaryWidth) * 2);
this->setMinimumWidth((fm.horizontalAdvance("一二三四五六七八九十十一十二")) * 2);
}
if (alignMode == AlignMid)
align |= Qt::AlignHCenter;
else if (alignMode == AlignRight)
align |= Qt::AlignRight;
else
align |= Qt::AlignLeft;
painter.drawText(rect, align, lyricStream.at(currentRow + (cross ? 1 : 0)).text);
}
// 绘制下一句
if (currentRow - (cross ? 1 : 0) < lyricStream.size()-1 && (lineMode == SuitableLine || lineMode == DoubleLine))
{
painter.setPen(cross ? playingColor : waitingColor);
QFlags<Qt::AlignmentFlag> align = Qt::AlignBottom;
if (alignMode == AlignMid)
align |= Qt::AlignHCenter;
else if (alignMode == AlignLeft)
align |= Qt::AlignLeft;
else
align |= Qt::AlignRight;
painter.drawText(rect, align, lyricStream.at(currentRow + (cross ? 0 : 1)).text);
}
}
}
void DesktopLyricWidget::showMenu()
{
QMenu* menu = new QMenu(this);
QMenu *lineMenu = new QMenu("行数", menu);
QMenu *alignMenu = new QMenu("对齐", menu);
QMenu *fontMenu = new QMenu("字体", menu);
QAction *playingC = new QAction("已播放颜色", this);
QAction *waitingC = new QAction("未播放颜色", this);
QAction *backgroundC = new QAction("背景颜色", this);
QAction *transMode = new QAction("透明模式", this);
QAction *hide = new QAction("隐藏", this);
menu->addAction(playingC);
menu->addAction(waitingC);
menu->addAction(backgroundC);
menu->addAction(transMode);
menu->addMenu(lineMenu);
menu->addMenu(alignMenu);
menu->addMenu(fontMenu);
menu->addAction(hide);
connect(playingC, &QAction::triggered, [=]{
QColor c = QColorDialog::getColor(playingColor, this, "选择已播放颜色", QColorDialog::ShowAlphaChannel);
if (!c.isValid())
return ;
if (c != playingColor)
{
settings.setValue("lyric/playingColor", playingColor = c);
update();
}
});
connect(waitingC, &QAction::triggered, [=]{
QColor c = QColorDialog::getColor(waitingColor, this, "选择未播放颜色", QColorDialog::ShowAlphaChannel);
if (!c.isValid())
return ;
if (c != waitingColor)
{
settings.setValue("lyric/waitingColor", waitingColor = c);
update();
}
});
connect(backgroundC, &QAction::triggered, [=]{
if (!jiWindow)
backgroundC->setEnabled(false);
QColor c = QColorDialog::getColor(bgColor, this, "选择背景颜色", QColorDialog::ShowAlphaChannel);
if (!c.isValid())
return ;
if (c != bgColor)
{
settings.setValue("lyric/bgColor", waitingColor = c);
update();
}
});
connect(transMode, &QAction::triggered, [=]{
if (!jiWindow)
transMode->setCheckable(true);
transMode->setChecked(true);
bool trans = settings.value("music/desktopLyricTrans", false).toBool();
settings.setValue("music/desktopLyricTrans", !trans);
emit signalSWitchTrans();
});
QStringList modeline{"自动", "单行", "双行"};
for (int index = 0; index < modeline.size(); index++)
{
QAction* lm = new QAction(modeline.at(index), this);
lineMenu->addAction(lm);
if (lineMode == index)
{
lm->setCheckable(true);
lm->setChecked(true);
}
connect(lm, &QAction::triggered, [=]{
lineMode = static_cast<LineMode>(index);
settings.setValue("music/lineMode", lineMode);
update();
});
}
QStringList modealign{"居中", "左右分离", "左对齐", "右对齐"};
for (int index = 0; index < modealign.size(); index++)
{
QAction* am = new QAction(modealign.at(index), this);
alignMenu->addAction(am);
if (alignMode == index)
{
am->setCheckable(true);
am->setChecked(true);
}
connect(am, &QAction::triggered, [=]{
alignMode = static_cast<AlignMode>(index);
settings.setValue("music/alignMode", alignMode);
update();
});
}
for (int i = 10; i < 40; i++)
{
QAction* fm = new QAction(QString::number(i), this);
fontMenu->addAction(fm);
if(pointSize == i)
{
fm->setCheckable(true);
fm->setChecked(true);
}
connect(fm, &QAction::triggered, [=]{
pointSize = i;
settings.setValue("lyric/pointSize", pointSize);
update();
});
}
connect(hide, &QAction::triggered, this, [=]{
this->hide();
emit signalhide();
});
menu->exec(cursor().pos());
QList<QAction*> list = menu->actions();
foreach(QAction* action, list)
delete action;
delete menu;
}
void DesktopLyricWidget::setPosition(qint64 position)
{
if (!lyricStream.size())
return ;
if (currentRow < 0 || currentRow >= lyricStream.size())
currentRow = 0;
LyricBean lyric = lyricStream.at(currentRow);
if (currentRow == lyricStream.size()-1 && lyric.start <= position) // 已经到末尾了
return ;
LyricBean nextLyric = currentRow == lyricStream.size()-1 ? LyricBean(false) : lyricStream.at(currentRow + 1);
if (lyric.start <= position && nextLyric.start >= position) // 不需要改变
return ;
if (lyric.start > position) // 什么情况?从头强制重新开始!
currentRow = 0;
while (currentRow+1 < lyricStream.size() && lyricStream.at(currentRow+1).start < position)
{
currentRow++;
}
update();
}