-
Notifications
You must be signed in to change notification settings - Fork 4
/
seekslider.cpp
105 lines (88 loc) · 2.91 KB
/
seekslider.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
#include "seekslider.h"
#include <QMouseEvent>
#include <QApplication>
SeekSlider::SeekSlider(QWidget *parent)
: QSlider(parent),
m_mp(NULL)
{
setOrientation(Qt::Horizontal);
setMinimum(0);
setMaximum(100);
setEnabled(false);
m_maxVal = 1;
connect(this, SIGNAL(sliderPressed()), this, SLOT(aboutToMoveSlider()));
connect(this, SIGNAL(sliderReleased()), this, SLOT(sliderReleasedSlot()));
}
void SeekSlider::setMaxVal(const qint64 i) {
if (m_maxVal != i) {
m_maxVal = i;
setValue(currentValue());
}
}
void SeekSlider::setMediaObject(QMediaPlayer *mp) {
m_mp = mp;
setMaxVal(mp->duration());
connect(mp, SIGNAL(positionChanged(qint64)), this, SLOT(seek(qint64)));
connect(mp, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(stateChanged(QMediaPlayer::State)));
connect(mp, SIGNAL(audioAvailableChanged(bool)), this, SLOT(audioAvailableChanged(bool)));
connect(mp, SIGNAL(seekableChanged(bool)), this, SLOT(seekableChanged(bool)));
connect(mp, SIGNAL(durationChanged(qint64)), this, SLOT(mediaDurationChanged(qint64)));
connect(mp, SIGNAL(mediaChanged(QMediaContent)), this, SLOT(mediaChanged(QMediaContent)));
}
qint64 SeekSlider::currentValue() {
return qint64( (value()*m_maxVal) / 100 );
}
SeekSlider::~SeekSlider() {
}
void SeekSlider::aboutToMoveSlider() {
Q_ASSERT(m_mp!=NULL);
if (m_mp->state()==QMediaPlayer::PlayingState) {
m_mp->pause();
}
}
void SeekSlider::sliderReleasedSlot() {
Q_ASSERT(m_mp!=NULL);
if (m_mp->state()==QMediaPlayer::PausedState) {
m_mp->setPosition(currentValue());
m_mp->play();
}
}
void SeekSlider::mediaChanged(QMediaContent) {
Q_ASSERT(m_mp!=NULL);
mediaDurationChanged(m_mp->duration());
}
void SeekSlider::mediaDurationChanged(const qint64 d) {
if (m_maxVal != d)
m_maxVal = d;
setMaxVal(d);
setValue(currentValue());
}
void SeekSlider::seek(const qint64 v) {
if (v != currentValue()) {
int localVal = (int)(100*v)/m_maxVal;
setSliderPosition(localVal);
}
}
void SeekSlider::setValue(const qint64 v) {
if (v != currentValue()) {
int localVal = (int)(100*v)/m_maxVal;
QSlider::setValue(localVal);
}
}
void SeekSlider::stateChanged(QMediaPlayer::State s) {
switch (s) {
case QMediaPlayer::StoppedState: setEnabled(false); break;
case QMediaPlayer::PlayingState:
case QMediaPlayer::PausedState: setEnabled(true); break;
}
}
void SeekSlider::audioAvailableChanged(const bool available) {
setEnabled(available);
}
void SeekSlider::seekableChanged(const bool canSeek) {
setEnabled(canSeek);
}
void SeekSlider::currentMediaChanged(QMediaContent) {
QMouseEvent event(QEvent::MouseButtonRelease, QPoint(), Qt::LeftButton, 0, 0);
QApplication::sendEvent(this, &event);
}