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
|
#include "LVideoLabel.h"
#include <LuminaXDG.h>
#include <QCoreApplication>
LVideoLabel::LVideoLabel(QString file, bool icons, QWidget *parent) : QLabel(parent) {
thumbnail = QPixmap();
entered = false;
this->icons = icons;
filepath = file;
defaultThumbnail = LXDG::findIcon("unknown", "").pixmap(256,256);
QTimer::singleShot(0, this, SLOT(initializeBackend()) );
}
LVideoLabel::~LVideoLabel() {
mediaPlayer->deleteLater();
surface->deleteLater();
}
void LVideoLabel::initializeBackend(){
mediaPlayer = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
surface = new LVideoSurface(this);
mediaPlayer->setVideoOutput(surface);
mediaPlayer->setPlaybackRate(3);
mediaPlayer->setMuted(true);
this->setPixmap(defaultThumbnail.scaled(this->size(),Qt::IgnoreAspectRatio));
mediaPlayer->setMedia(QUrl::fromLocalFile(filepath));
mediaPlayer->play();
this->connect(surface, SIGNAL(frameReceived(QPixmap)), this, SLOT(stopVideo(QPixmap)));
this->connect(mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(stateChanged(QMediaPlayer::State)));
this->connect(mediaPlayer, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(setDuration(QMediaPlayer::MediaStatus)));
this->connect(this, SIGNAL(rollOver()), surface, SLOT(switchRollOver()));
}
void LVideoLabel::enableIcons() {
this->setPixmap(thumbnail.scaled(this->size(),Qt::IgnoreAspectRatio));
icons = true;
}
void LVideoLabel::disableIcons() {
this->setPixmap(defaultThumbnail.scaled(this->size(),Qt::IgnoreAspectRatio));
icons = false;
}
void LVideoLabel::stopVideo(QPixmap pix) {
if(!entered) {
emit frameReceived(pix);
if(thumbnail.isNull())
thumbnail = pix;
if(icons)
this->setPixmap(thumbnail.scaled(this->size(),Qt::IgnoreAspectRatio));
mediaPlayer->pause();
}else {
if(icons)
this->setPixmap(pix.scaled(this->size(),Qt::IgnoreAspectRatio));
}
}
void LVideoLabel::stateChanged(QMediaPlayer::State state) {
//qDebug() << state;
}
void LVideoLabel::setDuration(QMediaPlayer::MediaStatus status) {
//qDebug() << status;
if(status == QMediaPlayer::BufferedMedia && !entered) { //Set duration in the middle to capture the thumbnail
mediaPlayer->setPosition(mediaPlayer->duration() / 2);
mediaPlayer->play();
}else if(status == QMediaPlayer::EndOfMedia && entered) { //Loop back to the beginning if playback started and at the end of the video
mediaPlayer->setPosition(0);
mediaPlayer->play();
}else if(status == QMediaPlayer::InvalidMedia){
mediaPlayer->stop();
mediaPlayer->play();
}/*else if(status == QMediaPlayer::LoadingMedia) {
mediaPlayer->pause();
QTimer timer;
timer.setSingleShot(true);
timer.setInterval(300);
timer.start();
qDebug() << "Timer Started" << timer.remainingTime();
while(timer.isActive()) QCoreApplication::processEvents(QEventLoop::AllEvents, 5);
qDebug() << "Timer Finished" << timer.remainingTime();
mediaPlayer->setPosition(0);
mediaPlayer->play();
}*/
}
void LVideoLabel::resizeEvent(QResizeEvent *event) {
//Resize the current pixmap to match the new size
if(!thumbnail.isNull()){
if(icons)
this->setPixmap(thumbnail.scaled(this->size(),Qt::IgnoreAspectRatio));
else
this->setPixmap(defaultThumbnail.scaled(this->size(),Qt::IgnoreAspectRatio));
}
QLabel::resizeEvent(event);
}
//Start playing the video from the beginning when the mouse enters the label
void LVideoLabel::enterEvent(QEvent *event) {
if(icons) {
entered=true;
emit rollOver();
mediaPlayer->setPosition(0);
mediaPlayer->play();
}
QWidget::enterEvent(event);
}
//Stop the video and set the thumbnail back to the middle of the video when the mouse leaves the label
void LVideoLabel::leaveEvent(QEvent *event) {
if(icons) {
entered=false;
mediaPlayer->setPosition(mediaPlayer->duration() / 2);
emit rollOver();
}
QWidget::leaveEvent(event);
}
|