summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/phonon/seekslider.cpp
blob: b5b25f00c50a52c3dcbb69e935ffb6d8d3d6dd44 (plain)
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
/*  This file is part of the KDE project
    Copyright (C) 2006-2007 Matthias Kretz <kretz@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), Nokia Corporation 
    (or its successors, if any) and the KDE Free Qt Foundation, which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "seekslider.h"
#include "seekslider_p.h"
#include "mediaobject.h"
#include "phonondefs_p.h"

#include <QtGui/QMouseEvent>
#include <QtGui/QApplication>

QT_BEGIN_NAMESPACE

#ifndef QT_NO_PHONON_SEEKSLIDER

namespace Phonon
{

SeekSlider::SeekSlider(QWidget *parent)
    : QWidget(parent)
    , k_ptr(new SeekSliderPrivate(this))
{
    K_D(SeekSlider);
    connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_seek(int)));
}

SeekSlider::SeekSlider(MediaObject *mo, QWidget *parent)
    : QWidget(parent)
    , k_ptr(new SeekSliderPrivate(this))
{
    K_D(SeekSlider);
    connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_seek(int)));
    setMediaObject(mo);
}

/*SeekSlider::SeekSlider(SeekSliderPrivate &_d, QWidget *parent)
    : QWidget(parent)
    , k_ptr(&_d)
{
} */

SeekSlider::~SeekSlider()
{
    delete k_ptr;
}

void SeekSlider::setMediaObject(MediaObject *media)
{
    K_D(SeekSlider);
    if (d->media) {
        disconnect(d->media, 0, this, 0);
    }
    d->media = media;

    if (media) {
        connect(media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
                SLOT(_k_stateChanged(Phonon::State)));
        connect(media, SIGNAL(totalTimeChanged(qint64)), SLOT(_k_length(qint64)));
        connect(media, SIGNAL(tick(qint64)), SLOT(_k_tick(qint64)));
        connect(media, SIGNAL(seekableChanged(bool)), SLOT(_k_seekableChanged(bool)));
        connect(media, SIGNAL(currentSourceChanged(const Phonon::MediaSource&)), SLOT(_k_currentSourceChanged()));
        d->_k_stateChanged(media->state());
        d->_k_seekableChanged(media->isSeekable());
        d->_k_length(media->totalTime());
    } else {
        d->_k_stateChanged(Phonon::StoppedState);
        d->_k_seekableChanged(false);
    }
}

MediaObject *SeekSlider::mediaObject() const
{
    K_D(const SeekSlider);
    return d->media;
}

void SeekSliderPrivate::_k_seek(int msec)
{
    if (!ticking && media) {
        media->seek(msec);
    }
}

void SeekSliderPrivate::_k_tick(qint64 msec)
{
    ticking = true;
    slider.setValue(msec);
    ticking = false;
}

void SeekSliderPrivate::_k_length(qint64 msec)
{
    ticking = true;
    slider.setRange(0, msec);
    ticking = false;
}

void SeekSliderPrivate::_k_seekableChanged(bool isSeekable)
{
    if (!isSeekable || !media) {
        setEnabled(false);
    } else {
        switch (media->state()) {
        case Phonon::PlayingState:
            if (media->tickInterval() == 0) {
                // if the tick signal is not enabled the slider is useless
                // set the tickInterval to some common value
                media->setTickInterval(350);
            }
        case Phonon::BufferingState:
        case Phonon::PausedState:
            setEnabled(true);
            break;
        case Phonon::StoppedState:
        case Phonon::LoadingState:
        case Phonon::ErrorState:
            setEnabled(false);
            ticking = true;
            slider.setValue(0);
            ticking = false;
            break;
        }
    }
}

void SeekSliderPrivate::_k_currentSourceChanged()
{
    //this releases the mouse and makes the seek slider stop seeking if the current source has changed
    QMouseEvent event(QEvent::MouseButtonRelease, QPoint(), Qt::LeftButton, 0, 0);
    QApplication::sendEvent(&slider, &event);
}

void SeekSliderPrivate::setEnabled(bool x)
{
    slider.setEnabled(x);
    iconLabel.setPixmap(icon.pixmap(iconSize, x ? QIcon::Normal : QIcon::Disabled));
}

void SeekSliderPrivate::_k_stateChanged(State newstate)
{
    if (!media || !media->isSeekable()) {
        setEnabled(false);
        return;
    }
    switch (newstate) {
    case Phonon::PlayingState:
        if (media->tickInterval() == 0) {
            // if the tick signal is not enabled the slider is useless
            // set the tickInterval to some common value
            media->setTickInterval(350);
        }
    case Phonon::BufferingState:
    case Phonon::PausedState:
        setEnabled(true);
        break;
    case Phonon::StoppedState:
    case Phonon::LoadingState:
    case Phonon::ErrorState:
        setEnabled(false);
        ticking = true;
        slider.setValue(0);
        ticking = false;
        break;
    }
}

bool SeekSlider::hasTracking() const
{
    return k_ptr->slider.hasTracking();
}

void SeekSlider::setTracking(bool tracking)
{
    k_ptr->slider.setTracking(tracking);
}

int SeekSlider::pageStep() const
{
    return k_ptr->slider.pageStep();
}

void SeekSlider::setPageStep(int milliseconds)
{
    k_ptr->slider.setPageStep(milliseconds);
}

int SeekSlider::singleStep() const
{
    return k_ptr->slider.singleStep();
}

void SeekSlider::setSingleStep(int milliseconds)
{
    k_ptr->slider.setSingleStep(milliseconds);
}

bool SeekSlider::isIconVisible() const
{
    K_D(const SeekSlider);
    return d->iconLabel.isVisible();
}

void SeekSlider::setIconVisible(bool vis)
{
    K_D(SeekSlider);
    d->iconLabel.setVisible(vis);
}

Qt::Orientation SeekSlider::orientation() const
{
    return k_ptr->slider.orientation();
}

void SeekSlider::setOrientation(Qt::Orientation o)
{
    K_D(SeekSlider);
    Qt::Alignment align = (o == Qt::Horizontal ? Qt::AlignVCenter : Qt::AlignHCenter);
    d->layout.setAlignment(&d->iconLabel, align);
    d->layout.setAlignment(&d->slider, align);
    d->layout.setDirection(o == Qt::Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom);
    d->slider.setOrientation(o);
}

QSize SeekSlider::iconSize() const
{
    return k_ptr->iconSize;
}

void SeekSlider::setIconSize(const QSize &iconSize)
{
    K_D(SeekSlider);
    d->iconSize = iconSize;
    d->iconLabel.setPixmap(d->icon.pixmap(d->iconSize, d->slider.isEnabled() ? QIcon::Normal : QIcon::Disabled));
}

} // namespace Phonon

#endif //QT_NO_PHONON_SEEKSLIDER

QT_END_NAMESPACE

#include "moc_seekslider.cpp"

// vim: sw=4 ts=4