-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvlcwidget.cpp
More file actions
1082 lines (908 loc) · 34.7 KB
/
Copy pathvlcwidget.cpp
File metadata and controls
1082 lines (908 loc) · 34.7 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "vlcwidget.h"
#include "vlckits.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QDebug>
#include <QMenu>
#include <QMenuBar>
#include <QGraphicsDropShadowEffect>
void CALLBACK TimeProc(HWND hwnd,UINT msg, UINT_PTR id, DWORD time);
VLCWidget::VLCWidget(QWidget *parent)
: CFramelessWidget(parent)
{
Qt::WindowFlags flags = windowFlags();
setWindowFlags(flags);
WINBOOL WINAPI EnableWindow(HWND hWnd,WINBOOL bEnable);
p = new VLCkits(this);
resize(1000,800);
setWindowTitle("天空是蔚蓝色");
QWidget *line = new QWidget(this);
line->setFixedHeight(1); // 设置分隔线的高度
line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
line->setStyleSheet("background-color: grey;"); // 设置分隔线的颜色
//------------------------标题栏初始化----------------------------------
QHBoxLayout *hlayout2 = new QHBoxLayout;
hlayout2->setContentsMargins(0, 0, 0, 0); // 去除内边距
hlayout2->setSpacing(0);
QPushButton *btr = new QPushButton(this);
QPushButton *btf = new QPushButton(this);
QPushButton *btc = new QPushButton(this);
QPushButton *titlog = new QPushButton(this);
titlog->setIcon(QIcon(":/playctrlBar/back.jpg"));
titlog->setFixedSize(50, 50); // 增加按钮大小到 50x50
titlog->setIconSize(QSize(45, 45)); // 增加图标大小为 45x45
titlog->setStyleSheet("QPushButton {"
"background-color: transparent;"
"border: none;"
"margin: 0; padding: 0;" // 添加:去除内边距和外边距
"}");
// 创建一个文字标签
QLabel *logoText = new QLabel("千纸鹤", this);
logoText->setStyleSheet("QLabel { color: white; font-size: 14px; font-weight: bold; }");
hlayout2->addWidget(titlog);
hlayout2->addSpacing(5);
hlayout2->addWidget(logoText);
hlayout2->addStretch();
hlayout2->addWidget(btr);
hlayout2->addSpacing(20);
hlayout2->addWidget(btf);
hlayout2->addSpacing(20);
hlayout2->addWidget(btc);
// hlayout2->setContentsMargins(0, 0, 0, 0);
btr->setIcon(QIcon(":/playctrlBar/min.svg"));
btf->setIcon(QIcon(":/playctrlBar/normal.svg"));
btc->setIcon(QIcon(":/playctrlBar/close.svg"));
btr->setStyleSheet("QPushButton {"
"background-color: transparent;" // 设置背景颜色为透明
"border: none;" // 去除边框
"}");
titlog->setStyleSheet("QPushButton {"
"background-color: transparent;" // 设置背景颜色为透明
"border: none;" // 去除边框
"}");
this->setStyleSheet("QWidget {"
"background-color: black;"
"}");
QVBoxLayout *vlayout = new QVBoxLayout(this);
vlayout->setContentsMargins(0, 0, 0, 10); // 移除边距
m_videoWidget = new QWidget(this);
m_videoWidget->setStyleSheet("background-color: #202020;"); // 黑色背景
m_videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
bt1 = new QPushButton("打开文件", this);
bt1->setStyleSheet("QPushButton {"
"background-color: transparent;" // 设置背景颜色为透明
"border: none;" // 去除边框
"color: white;" // 白色字体
"font: bold;"
"}");
QPushButton *bt2 = new QPushButton(this);//播放/暂停视频
QPushButton *bt4 = new QPushButton(this);//停止
QPushButton *bt5 = new QPushButton(this);//切换到上一个视频
QPushButton *bt6 = new QPushButton(this);//切换到下一个视频
bt7 = new QPushButton(this);//全屏按钮
bt1->setIcon(QIcon(":/playctrlBar/openfile_hover.svg"));
bt2->setIcon(QIcon(":/playctrlBar/pause.svg"));
bt4->setIcon(QIcon(":/playctrlBar/stop.svg"));
bt5->setIcon(QIcon(":/playctrlBar/pre.svg"));
bt6->setIcon(QIcon(":/playctrlBar/next.svg"));
bt7->setIcon(QIcon(":/playctrlBar/fullscreen.svg"));
//时间标签
QLabel *timelab = new QLabel("00:00/00:00");
timelab->setStyleSheet("QLabel {"
"color: white;" // 设置标签的字体颜色为白色
"font: bold;"
"}");
//音量按钮
volumeBtn = new QPushButton(this);
volumeBtn->setFixedSize(30, 30);
volumeBtn->setIcon(QIcon(":/playctrlBar/audio_open.svg"));
volumeBtn->setIconSize(QSize(24, 24));
volumeBtn->setFlat(true); // 去除边框
// 音量滑块(垂直方向)
vslider = new QSlider(Qt::Vertical, this);
vslider->setRange(0, 100);//设置范围
vslider->setValue(30); // 默认音量
vslider->setFixedHeight(180);
vslider->setFixedWidth(45); // 显式设置滑块宽度(使其更宽)
vslider->setStyleSheet("QSlider::groove:vertical {"
" background: #555;"
" width: 8px;"
"}"
"QSlider::handle:vertical {"
" background: white;"
" height: 15px;"
" width: 18px;" // 新增:手柄宽度设置
" margin: -8px 0;"
" border-radius: 12px;"
"}");
// 音量弹出窗口
volumePopup = new QWidget(this);
volumePopup->setFixedSize(vslider->width() -2, vslider->height() -2); // 只比滑块大一点点
volumePopup->setStyleSheet("background-color: rgba(50, 50, 50, 200); border-radius: 6px;");
volumePopup->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
volumePopup->setAttribute(Qt::WA_TranslucentBackground);
volumePopup->installEventFilter(this); // 为音量弹出窗口安装事件过滤器
QVBoxLayout *volumeLayout = new QVBoxLayout(volumePopup);
// volumeLayout->setContentsMargins(5, 10, 5, 10);
volumeLayout->setContentsMargins(0, 0, 0, 0); // 无内边距
volumeLayout->setSpacing(0); // 无间距
volumeLayout->addWidget(vslider);
volumePopup->hide();
QSlider *pslider = new QSlider(Qt::Horizontal,this);
pslider->setValue(0);
pslider->setRange(0, 100);
pslider->setEnabled(false);
pslider->setObjectName("progressSlider");
// 设置进度条样式
pslider->setStyleSheet(
"QSlider::groove:horizontal {"
" background: transparent;"
" height: 6px;"
" border-radius: 3px;"
"}"
"QSlider::sub-page:horizontal {"
" background: #808080;"
" border-radius: 3px;"
"}"
"QSlider::add-page:horizontal {"
" background: rgba(255, 255, 255, 0.3);"
" border-radius: 3px;"
"}"
"QSlider::handle:horizontal {"
" background: white;"
" width: 16px;"
" height: 16px;"
" margin: -5px 0;"
" border-radius: 8px;"
"}"
);
// 初始化播放列表相关
m_playlistVisible = false;
m_playlistWidget = new QListWidget(this);
m_playlistWidget->setFixedWidth(200); // 设置播放列表宽度
m_playlistWidget->setStyleSheet("QListWidget { background-color: rgba(0, 0, 0, 180); color: white; }"
"QListWidget::item:selected { background-color: rgba(100, 100, 100, 200); }");
m_playlistWidget->hide(); // 初始隐藏
// 切换播放列表显示的按钮
m_togglePlaylistBtn = new QPushButton(this);
m_togglePlaylistBtn->setIcon(QIcon(":/playctrlBar/effect.svg"));
m_togglePlaylistBtn->setStyleSheet("QPushButton { background-color: transparent; color: white; border: none; }");
QHBoxLayout *mainhlayout = new QHBoxLayout;
mainhlayout->addWidget(m_videoWidget,1);
mainhlayout->addWidget(m_playlistWidget);
// 添加循环模式标签
QLabel *loopLabel = new QLabel(this);
loopLabel->setText("列表循环");
loopLabel->setObjectName("loopLabel");
loopLabel->setFixedWidth(60);
loopLabel->setStyleSheet("QLabel { color: white; }");
// 让视频区域也能触发右键菜单
m_videoWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_videoWidget, &QWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
QPoint globalPos = m_videoWidget->mapToGlobal(pos);
QPoint widgetPos = this->mapFromGlobal(globalPos);
emit customContextMenuRequested(widgetPos);
});
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
QHBoxLayout *hlayout1 = new QHBoxLayout;
hlayout1->addSpacing(10);
hlayout1->addWidget(timelab);//时间标签
hlayout1->addStretch();
hlayout1->addWidget(bt4);//停止
hlayout1->addWidget(bt5);//上一个
hlayout1->addWidget(bt2);//播放/暂停
hlayout1->addWidget(bt6);//下一个
hlayout1->addWidget(volumeBtn);//音量进度
hlayout1->addStretch();
hlayout1->addWidget(loopLabel);//播放模式
hlayout1->addSpacing(10);
hlayout1->addWidget(m_togglePlaylistBtn);//播放列表
hlayout1->addSpacing(10);
hlayout1->addWidget(bt7);//全屏按钮
hlayout1->addSpacing(10);
//整体布局
vlayout->addLayout(hlayout2);
vlayout->setSpacing(0);
vlayout->addWidget(line);
vlayout->addLayout(mainhlayout,1);
vlayout->addWidget(pslider);//时间进度条
vlayout->addLayout(hlayout1);
//==============================================================================================
connect(bt1,&QPushButton::clicked,[=]{
QStringList paths = QFileDialog::getOpenFileNames(this,
"",
"E:/QtTest/videos",
"(*.mp4)");
if(!paths.isEmpty())
{
for(QString &path :paths)
{
path = QDir::toNativeSeparators(path);
}
}else{
return;
}
// 保存文件列表并开始播放第一个
m_mediaList = paths;
m_currentIndex = 0;
p->setVideoWidget(m_videoWidget);
p->open(m_videoWidget, m_mediaList[m_currentIndex]);
//设置音量默认值
p->setVolume(p->getcurrvl(vslider->value()));
pslider->setEnabled(true);
SetTimer(NULL, 1, 300, TimeProc);
// vslider->setEnabled(true);
bt1->hide();
updatePlaylist();
});
// 标题栏控制窗口
connect(btf, &QPushButton::clicked, this, [this]() {
if (isMaximized())
showNormal();
else
showMaximized();
});
// connect(btc, &QPushButton::clicked, this, &QWidget::close);
connect(btr, &QPushButton::clicked, this, &QWidget::showMinimized);
//功能按钮
connect(bt2, &QPushButton::clicked, p, &VLCkits::play);
connect(bt4, &QPushButton::clicked, p, &VLCkits::stop);
connect(bt5, &QPushButton::clicked, this, &VLCWidget::playPrevious); // 上一个
connect(bt6, &QPushButton::clicked, this, &VLCWidget::playNext); // 下一个
connect(bt7, &QPushButton::clicked, this, &VLCWidget::toggleFullscreen);//全屏
//音量
connect(vslider, &QSlider::valueChanged, p, &VLCkits::setVolume);
// 连接 VLCkits 的信号
connect(p, &VLCkits::sigTimeSliderPos, pslider, &QSlider::setValue);
connect(p, &VLCkits::sigTimeText, timelab, &QLabel::setText);
connect(p, &VLCkits::sigVolumeSliderPos, vslider, &QSlider::setValue);
// 连接媒体结束信号
connect(p, &VLCkits::mediaEndReached, this, &VLCWidget::onMediaEndReached);
//进度条
connect(pslider, &QSlider::sliderPressed, [=]() {
p->setUserSeeking(true);
p->pauseTimer(); // 增加一个暂停定时器的接口
});
connect(pslider, &QSlider::sliderReleased, [=]() {
p->setUserSeeking(false);
p->setPosition(pslider->value());
p->resumeTimer(); // 恢复定时器
});
// 连接播放列表按钮的信号
connect(m_togglePlaylistBtn, &QPushButton::clicked, this, &VLCWidget::togglePlaylistVisibility);
// 连接播放列表的双击信号
connect(m_playlistWidget, &QListWidget::itemDoubleClicked, this, &VLCWidget::onPlaylistItemDoubleClicked);
// 添加点击功能(例如显示关于对话框)
connect(titlog, &QPushButton::clicked, this, [](){
QMessageBox aboutBox;
aboutBox.setWindowTitle("关于播放器");
aboutBox.setText("<p style='font-size: 12pt; line-height: 150%;'>"
"<b>千纸鹤播放器</b><br>"
"版本: v1.0<br>"
"© 2025-08-13 <br><br>"
"<span style='color:#999; font-size: 9pt;'>开发技术: Qt VLC框架</span>"
"</p>");
// 设置对话框样式
aboutBox.setStyleSheet(
"QMessageBox {"
" background-color: #202020;" // 深灰背景
"}"
"QLabel {"
" color: #fff;" // 白色文本
" margin: 15px;" // 增加内容边距
"}"
"QPushButton {"
" background-color: #333;"
" color: white;"
" border: 1px solid #555;"
" border-radius: 4px;"
" padding: 5px 15px;"
" min-width: 80px;"
"}"
"QPushButton:hover {"
" background-color: #444;"
"}"
);
aboutBox.exec();
});
//==============================================================================================
//右键菜单栏-------------------------------------------------------------------------------------
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &QWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
QMenu menu;
menu.addAction("打开文件", this, &VLCWidget::openFile);
menu.addAction("显示播放列表", this, &VLCWidget::showPlaylist);
menu.addAction("清空播放列表", this, [this]() {
m_mediaList.clear();
m_currentIndex = -1;
updatePlaylist();
bt1->show();
p->stop();
});
menu.addAction("上一个", this, &VLCWidget::playPrevious);
menu.addAction("下一个", this, &VLCWidget::playNext);
menu.addAction("全屏", this, &VLCWidget::toggleFullscreen);
menu.addAction("关闭", this, &QWidget::close);
// 添加循环模式菜单
QMenu *loopMenu = menu.addMenu("循环模式");
QAction *noLoopAction = loopMenu->addAction("不循环");
QAction *loopOneAction = loopMenu->addAction("单曲循环");
QAction *loopAllAction = loopMenu->addAction("列表循环");
QAction *randomAction = loopMenu->addAction("随机播放");
// 设置当前选中状态
noLoopAction->setCheckable(true);
loopOneAction->setCheckable(true);
loopAllAction->setCheckable(true);
randomAction->setCheckable(true);
noLoopAction->setChecked(m_currentLoopMode == VLCkits::NoLoop);
loopOneAction->setChecked(m_currentLoopMode == VLCkits::LoopOne);
loopAllAction->setChecked(m_currentLoopMode == VLCkits::LoopAll);
randomAction->setChecked(m_currentLoopMode == VLCkits::Random);
// 连接循环模式设置
connect(noLoopAction, &QAction::triggered, this, [this]() {
setLoopMode(VLCkits::NoLoop);
});
connect(loopOneAction, &QAction::triggered, this, [this]() {
setLoopMode(VLCkits::LoopOne);
});
connect(loopAllAction, &QAction::triggered, this, [this]() {
setLoopMode(VLCkits::LoopAll);
});
connect(randomAction, &QAction::triggered, this, [this]() {
setLoopMode(VLCkits::Random);
});
// QAction *deleteAction = menu.addAction("删除");
// // 如果没有选中项,禁用删除选项
// if (m_playlistWidget->selectedItems().isEmpty()) {
// deleteAction->setEnabled(false);
// }
// if (menu.exec(m_playlistWidget->mapToGlobal(pos)) == deleteAction) {
// deleteSelectedItems();
// }
QString styleSheet = "QMenu {"
"background-color: #333;"
"color: white;"
"border: 1px solid #666;"
"}"
"QMenu::item {"
"padding: 5px 25px;"
"}"
"QMenu::item:selected {"
"background-color: #666;"
"}";
menu.setStyleSheet(styleSheet);
menu.exec(mapToGlobal(pos));
});
// 为播放列表添加上下文菜单
m_playlistWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_playlistWidget, &QListWidget::customContextMenuRequested,
this, [this](const QPoint &pos) {
QMenu menu;
menu.setStyleSheet(
"QMenu {"
" background-color: #333;"
" color: white;"
" border: 1px solid #555;"
" border-radius: 3px;"
"}"
"QMenu::item {"
" padding: 6px 20px;"
"}"
"QMenu::item:selected {"
" background-color: #555;"
"}"
);
QAction *deleteAction = menu.addAction("删除");
menu.addAction("添加", this, &VLCWidget::openFile);
// 如果没有选中项,禁用删除选项
if (m_playlistWidget->selectedItems().isEmpty()) {
deleteAction->setEnabled(false);
}
if (menu.exec(m_playlistWidget->mapToGlobal(pos)) == deleteAction) {
deleteSelectedItems();
}
});
//----------------------------------------------------------------------------------------------
volumeBtn->installEventFilter(this);//添加事件过滤器
loadPlaylist();//加载播放列表
//----------------------------------------------------------------------------------------------
connect(btc, &QPushButton::clicked, this, [this]() {
// 创建自定义对话框
QDialog dialog(this);
dialog.setWindowFlags(dialog.windowFlags() | Qt::FramelessWindowHint); // 无边框
dialog.setAttribute(Qt::WA_TranslucentBackground); // 透明背景
dialog.setFixedSize(300, 150);
// 主容器(用于实现圆角和阴影)
QWidget *container = new QWidget(&dialog);
container->setStyleSheet(
"QWidget {"
" background-color: #333;"
" border: 1px solid #555;"
" border-radius: 10px;"
"}"
);
// // 添加阴影效果
// QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
// shadow->setBlurRadius(15);
// shadow->setColor(QColor(0, 0, 0, 150));
// shadow->setOffset(0, 0);
// container->setGraphicsEffect(shadow);
QVBoxLayout *dialogLayout = new QVBoxLayout(&dialog);
dialogLayout->addWidget(container);
dialogLayout->setContentsMargins(15, 15, 15, 15); // 添加边距实现阴影效果
// 容器内部布局
QVBoxLayout *layout = new QVBoxLayout(container);
layout->setContentsMargins(20, 20, 20, 20);
layout->setSpacing(15);
// 添加内容
QHBoxLayout *messageLayout = new QHBoxLayout();
QLabel *iconLabel = new QLabel();
// 使用标准警告图标
QPixmap warningIcon = style()->standardPixmap(QStyle::SP_MessageBoxWarning);
iconLabel->setPixmap(warningIcon.scaled(40, 40, Qt::KeepAspectRatio, Qt::SmoothTransformation));
messageLayout->addWidget(iconLabel);
QLabel *textLabel = new QLabel("确定要退出程序吗?");
textLabel->setStyleSheet("QLabel { color: white; font-size: 14px; font-weight: bold; }");
messageLayout->addWidget(textLabel, 1); // 文本标签占据剩余空间
messageLayout->setSpacing(15);
layout->addLayout(messageLayout);
// 添加按钮
QHBoxLayout *buttonLayout = new QHBoxLayout();
QPushButton *cancelButton = new QPushButton("取消");
QPushButton *confirmButton = new QPushButton("确认");
cancelButton->setFixedSize(80, 30);
confirmButton->setFixedSize(80, 30);
cancelButton->setStyleSheet(
"QPushButton {"
" background-color: #444;"
" color: white;"
" border: 1px solid #555;"
" border-radius: 4px;"
"}"
"QPushButton:hover {"
" background-color: #555;"
"}"
);
confirmButton->setStyleSheet(
"QPushButton {"
" background-color: #d9534f;" // 红色按钮
" color: white;"
" border: 1px solid #d43f3a;"
" border-radius: 4px;"
"}"
"QPushButton:hover {"
" background-color: #c9302c;"
"}"
);
buttonLayout->addStretch();
buttonLayout->addWidget(cancelButton);
buttonLayout->addSpacing(10);
buttonLayout->addWidget(confirmButton);
buttonLayout->addStretch();
layout->addLayout(buttonLayout);
// 连接按钮信号
connect(cancelButton, &QPushButton::clicked, &dialog, &QDialog::reject);
connect(confirmButton, &QPushButton::clicked, &dialog, &QDialog::accept);
// 设置回车键触发确认按钮
confirmButton->setDefault(true);
// 显示对话框并等待用户选择
if (dialog.exec() == QDialog::Accepted) {
// 用户点击确认,关闭程序
this->close();
}
});
}
VLCWidget::~VLCWidget()
{
}
//鼠标双击进入全屏
void VLCWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_UNUSED(event);
if(this->isFullScreen()) {
this->showNormal();
// bt7->show();
} else{
this->showFullScreen();
// bt7->hide();
}
}
//esc退出全屏
void VLCWidget::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape) {
this->showNormal();
}
}
BOOL CALLBACK EnumVLC(HWND hwnd, LPARAM lParam)
{
Q_UNUSED(lParam);
TCHAR szTitle[1024];
int nLen = GetWindowText(hwnd, szTitle, 1024);
if(nLen > 0)
{
EnableWindow(hwnd, FALSE);
KillTimer(NULL, 1);
}
return TRUE;
}
void CALLBACK TimeProc(HWND hwnd,UINT msg, UINT_PTR id, DWORD time)
{
Q_UNUSED(hwnd);
Q_UNUSED(msg);
Q_UNUSED(id);
Q_UNUSED(time);
HWND vlcHwnd = FindWindowEx(NULL,NULL,NULL, L"天空是蔚蓝色");
if(vlcHwnd)
{
EnumChildWindows(vlcHwnd,EnumVLC ,NULL);
}
}
//鼠标悬停音量按钮
bool VLCWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == volumeBtn) {
if (event->type() == QEvent::Enter) {
// 鼠标进入:显示音量弹窗
QPoint pos = volumeBtn->mapToGlobal(QPoint(0, -volumePopup->height()));
volumePopup->move(pos);
volumePopup->show();
return true;
} else if (event->type() == QEvent::Leave) {
// 鼠标离开:延迟隐藏(防止滑动时消失)
QTimer::singleShot(500, this, [this]() {
if (!volumePopup->underMouse()) {
volumePopup->hide();
}
});
return true;
}
}
// 处理滑块弹窗的悬停,防止立即消失
if (obj == volumePopup || obj == vslider) {
if (event->type() == QEvent::Enter) {
volumePopup->show(); // 保持显示
return true;
} else if (event->type() == QEvent::Leave) {
if (obj == volumePopup) {
QTimer::singleShot(500, this, [this]() {
if (!volumePopup->underMouse()) {
volumePopup->hide();
}
});
}
return true;
}
}
return QWidget::eventFilter(obj, event);
}
void VLCWidget::updateCurrentFileName()
{
if (m_currentIndex >= 0 && m_currentIndex < m_mediaList.size()) {
QFileInfo info(m_mediaList[m_currentIndex]);
}
}
//打开文件按钮位于中心
void VLCWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
// 确保 m_videoWidget 和 bt1 存在
if (m_videoWidget && bt1) {
// 计算 m_videoWidget 在父窗口中的全局位置和大小
QRect videoRect = m_videoWidget->geometry();
int btnX = videoRect.x() + (videoRect.width() - bt1->width()) / 2;
int btnY = videoRect.y() + (videoRect.height() - bt1->height()) / 2;
bt1->move(btnX, btnY);
}
}
void VLCWidget::toggleFullscreen()
{
if (this->isFullScreen()) {
// 如果当前是全屏模式,则退出全屏
this->showNormal();
} else {
// 如果当前不是全屏模式,则进入全屏
this->showFullScreen();
// bt7->hide();
}
}
//-----------------------------------------------------------------------------
// 切换播放列表可见性
void VLCWidget::togglePlaylistVisibility()
{
m_playlistVisible = !m_playlistVisible;
m_playlistWidget->setVisible(m_playlistVisible);
}
// 双击播放列表项处理
void VLCWidget::onPlaylistItemDoubleClicked(QListWidgetItem *item)
{
int row = m_playlistWidget->row(item);
if (row >= 0 && row < m_mediaList.size()) {
m_currentIndex = row;
p->open(m_videoWidget, m_mediaList[m_currentIndex]);
updateCurrentFileName();
bt1->hide();
}
}
// 更新播放列表显示
void VLCWidget::updatePlaylist()
{
m_playlistWidget->clear();
// if (m_mediaList.isEmpty()) {
// // 添加提示信息
// QListWidgetItem *item = new QListWidgetItem("播放列表为空");
// item->setFlags(item->flags() & ~Qt::ItemIsEnabled); // 禁用项
// item->setForeground(Qt::gray); // 灰色文本
// m_playlistWidget->addItem(item);
// return;
// }
for (int i = 0; i < m_mediaList.size(); ++i) {
QFileInfo fileInfo(m_mediaList[i]);
QString displayText = QString("%1. %2").arg(i + 1).arg(fileInfo.fileName());
QListWidgetItem *item = new QListWidgetItem(displayText);
if (i == m_currentIndex) {
item->setBackground(QColor(100, 100, 100, 200)); // 当前播放项高亮
}
m_playlistWidget->addItem(item);
}
// 保存播放列表到文件
savePlaylist();
}
//void VLCWidget::updatePlaylist()
//{
// m_playlistWidget->clear();
// // 获取当前选中的索引(在清除之前)
// QList<int> selectedIndexes;
// for (QListWidgetItem *item : m_playlistWidget->selectedItems()) {
// selectedIndexes.append(m_playlistWidget->row(item));
// }
// for (int i = 0; i < m_mediaList.size(); ++i) {
// QFileInfo fileInfo(m_mediaList[i]);
// QString displayText = QString("%1. %2").arg(i + 1).arg(fileInfo.fileName());
// QListWidgetItem *item = new QListWidgetItem(displayText);
// // 设置当前播放项的高亮(使用深灰色)
// if (i == m_currentIndex) {
// item->setBackground(QColor(70, 70, 70, 200));
// }
// // 设置选中项的高亮(使用蓝色)
// if (selectedIndexes.contains(i)) {
// item->setBackground(QColor(65, 105, 225, 200)); // 皇家蓝色
// }
// m_playlistWidget->addItem(item);
// }
// // 重新应用选择状态
// for (int index : selectedIndexes) {
// if (index < m_playlistWidget->count()) {
// m_playlistWidget->item(index)->setSelected(true);
// }
// }
// // 保存播放列表到文件
// savePlaylist();
//}
// 保存播放列表到文件
void VLCWidget::savePlaylist()
{
QFile file("playlist.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
for (const QString &path : m_mediaList) {
out << path << "\n";
}
file.close();
}else{
QFile::remove("playlst.txt");
}
}
// 从文件加载播放列表
void VLCWidget::loadPlaylist()
{
QFile file("playlist.txt");
if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_mediaList.clear();
QTextStream in(&file);
while (!in.atEnd()) {
QString path = in.readLine();
if (!path.isEmpty() && QFile::exists(path)) {
m_mediaList.append(path);
}
}
file.close();
if (!m_mediaList.isEmpty()) {
m_currentIndex = 0;
updatePlaylist();
}
}
}
void VLCWidget::openFile()
{
QStringList paths = QFileDialog::getOpenFileNames(this,
"",
"E:/QtTest/videos",
"(*.mp4)");
if(!paths.isEmpty())
{
if(m_mediaList.isEmpty())
{
m_currentIndex = 0;
}
for(QString &path :paths)
{
path = QDir::toNativeSeparators(path);
// 添加到播放列表(如果不存在)
if (!m_mediaList.contains(path)) {
m_mediaList.append(path);
}
}
updatePlaylist();
// 如果当前没有在播放,开始播放第一个文件
if (!p->isPlaying() && !m_mediaList.isEmpty()) {
// m_currentIndex = 0;
p->setVideoWidget(m_videoWidget);
p->open(m_videoWidget, m_mediaList[m_currentIndex]);
p->setVolume(p->getcurrvl(vslider->value()));
SetTimer(NULL, 1, 300, TimeProc);
bt1->hide();
}
// 更新播放列表显示
updatePlaylist();
}
}
void VLCWidget::playPrevious()
{
if (m_mediaList.isEmpty()){
openFile();
return;
}
m_currentIndex = (m_currentIndex - 1 + m_mediaList.size()) % m_mediaList.size();
p->open(m_videoWidget, m_mediaList[m_currentIndex]);
updateCurrentFileName();
updatePlaylist(); // 更新播放列表显示
}
void VLCWidget::playNext()
{
if (m_mediaList.isEmpty()){
openFile();
return;}
m_currentIndex = (m_currentIndex + 1) % m_mediaList.size();
p->open(m_videoWidget, m_mediaList[m_currentIndex]);
updateCurrentFileName();
updatePlaylist(); // 更新播放列表显示
}
void VLCWidget::showPlaylist()
{
if (!m_playlistVisible) {
togglePlaylistVisibility();
}
}
void VLCWidget::closeEvent(QCloseEvent *event)
{
// 清空播放列表
m_mediaList.clear();
m_currentIndex = -1;
// 保存空播放列表
savePlaylist();
// 停止播放
if (p) {
p->stop();
}
// 接受关闭事件
event->accept();
}
void VLCWidget::onMediaEndReached()
{
if (m_mediaList.isEmpty()) {
return;
}
switch (m_currentLoopMode) {
case VLCkits::LoopOne:
// 单曲循环:重新播放当前视频
p->open(m_videoWidget, m_mediaList[m_currentIndex]);
break;
case VLCkits::LoopAll:
// 列表循环:播放下一个视频
playNext();
break;
case VLCkits::NoLoop:
// 不循环:停止播放
p->stop();
break;
case VLCkits::Random:
// 随机播放:随机选择一个视频
if (m_mediaList.size() > 1) {
int newIndex;
do {
newIndex = QRandomGenerator::global()->bounded(m_mediaList.size());
} while (newIndex == m_currentIndex && m_mediaList.size() > 1);
m_currentIndex = newIndex;
}
p->open(m_videoWidget, m_mediaList[m_currentIndex]);
break;
}
}
// 实现设置循环模式函数
void VLCWidget::setLoopMode(VLCkits::LoopMode mode)
{
m_currentLoopMode = mode;
p->setLoopMode(mode);
updateLoopModeDisplay();
// 更新状态栏显示当前循环模式
// 这里可以添加状态栏更新代码
}
void VLCWidget::updateLoopModeDisplay()
{
QLabel *loopLabel = findChild<QLabel*>("loopLabel");
if (!loopLabel) return;
switch (m_currentLoopMode) {
case VLCkits::NoLoop:
loopLabel->setText("不循环");