summaryrefslogtreecommitdiffstats
path: root/src/multimedia/imageviewer/qmediaimageviewer.cpp
blob: c832ba62b120358a470d18ba50b9e614a97cb972 (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
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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmediaimageviewer.h"

#include "qmediaobject_p.h"
#include "qmediaimageviewerservice_p.h"

#include <qmediaplaylist.h>
#include <qmediaplaylistsourcecontrol.h>
#include <qmediacontent.h>
#include <qmediaresource.h>
#include "qvideosurfaceoutput_p.h"

#include <QtCore/qcoreevent.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qpointer.h>
#include <QtCore/qtextstream.h>

QT_BEGIN_NAMESPACE

namespace
{
    class QMediaImageViewerPrivateRegisterMetaTypes
    {
    public:
        QMediaImageViewerPrivateRegisterMetaTypes()
        {
            qRegisterMetaType<QMediaImageViewer::MediaStatus>();
            qRegisterMetaType<QMediaImageViewer::State>();
        }
    } _registerMetaTypes;
}

class QMediaImageViewerPrivate : public QMediaObjectPrivate
{
    Q_DECLARE_NON_CONST_PUBLIC(QMediaImageViewer)
public:
    QMediaImageViewerPrivate():
        viewerControl(0), playlist(0),
        state(QMediaImageViewer::StoppedState), timeout(3000), pauseTime(0)
    {
    }

    void _q_mediaStatusChanged(QMediaImageViewer::MediaStatus status);
    void _q_playlistMediaChanged(const QMediaContent &content);
    void _q_playlistDestroyed();

    QMediaImageViewerControl *viewerControl;
    QMediaPlaylist *playlist;
    QPointer<QObject> videoOutput;
    QVideoSurfaceOutput surfaceOutput;
    QMediaImageViewer::State state;
    int timeout;
    int pauseTime;
    QTime time;
    QBasicTimer timer;
    QMediaContent media;
};

void QMediaImageViewerPrivate::_q_mediaStatusChanged(QMediaImageViewer::MediaStatus status)
{
    switch (status) {
    case QMediaImageViewer::NoMedia:
    case QMediaImageViewer::LoadingMedia:
        emit q_func()->mediaStatusChanged(status);
        break;
    case QMediaImageViewer::LoadedMedia:
        if (state == QMediaImageViewer::PlayingState) {
            time.start();
            timer.start(qMax(0, timeout), q_func());
            q_func()->addPropertyWatch("elapsedTime");
        }
        emit q_func()->mediaStatusChanged(status);
        emit q_func()->elapsedTimeChanged(0);
        break;
    case QMediaImageViewer::InvalidMedia:
        emit q_func()->mediaStatusChanged(status);

        if (state == QMediaImageViewer::PlayingState) {
            playlist->next();
            if (playlist->currentIndex() < 0)
                emit q_func()->stateChanged(state = QMediaImageViewer::StoppedState);
        }
        break;
    }
}

void QMediaImageViewerPrivate::_q_playlistMediaChanged(const QMediaContent &content)
{
    media = content;
    pauseTime = 0;

    viewerControl->showMedia(media);

    emit q_func()->mediaChanged(media);
}

void QMediaImageViewerPrivate::_q_playlistDestroyed()
{
    playlist = 0;
    timer.stop();

    if (state != QMediaImageViewer::StoppedState)
        emit q_func()->stateChanged(state = QMediaImageViewer::StoppedState);

    q_func()->setMedia(QMediaContent());
}

/*!
    \class QMediaImageViewer
    \brief The QMediaImageViewer class provides a means of viewing image media.
    \inmodule QtMultimedia
    \ingroup multimedia
    \since 1.0


    QMediaImageViewer is used together with a media display object such as
    QVideoWidget to present an image.  A display object is attached to the
    image viewer by means of the bind function.

    \snippet doc/src/snippets/multimedia-snippets/media.cpp Binding

    QMediaImageViewer can be paired with a QMediaPlaylist to create a slide
    show of images. Constructing a QMediaPlaylist with a pointer to an
    instance of QMediaImageViewer will attach it to the image viewer;
    changing the playlist's selection will then change the media displayed
    by the image viewer.  With a playlist attached QMediaImageViewer's
    play(), pause(), and stop() slots can be control the progression of the
    playlist.  The \l timeout property determines how long an image is
    displayed for before progressing to the next in the playlist, and the
    \l elapsedTime property holds how the duration the current image has
    been displayed for.

    \snippet doc/src/snippets/multimedia-snippets/media.cpp Playlist
*/

/*!
    \enum QMediaImageViewer::State

    Enumerates the possible control states an image viewer may be in.  The
    control state of an image viewer determines whether the image viewer is
    automatically progressing through images in an attached playlist.

    \value StoppedState The image viewer is stopped, and will not automatically move to the next
    image.  The \l elapsedTime is fixed at 0.
    \value PlayingState The slide show is playing, and will move to the next image when the
    \l elapsedTime reaches the \l timeout.  The \l elapsedTime is being incremented.
    \value PausedState The image viewer is paused, and will not automatically move the to next
    image.  The \l elapsedTime is fixed at the time the image viewer was paused.
*/

/*!
    \enum QMediaImageViewer::MediaStatus

    Enumerates the status of an image viewer's current media.

    \value NoMedia  There is no current media.
    \value LoadingMedia The image viewer is loading the current media.
    \value LoadedMedia The image viewer has loaded the current media.
    \value InvalidMedia The current media cannot be loaded.
*/

/*!
    Constructs a new image viewer with the given \a parent.
*/
QMediaImageViewer::QMediaImageViewer(QObject *parent)
    : QMediaObject(*new QMediaImageViewerPrivate, parent, new QMediaImageViewerService)
{
    Q_D(QMediaImageViewer);

    d->viewerControl = qobject_cast<QMediaImageViewerControl*>(
            d->service->requestControl(QMediaImageViewerControl_iid));

    connect(d->viewerControl, SIGNAL(mediaStatusChanged(QMediaImageViewer::MediaStatus)),
            this, SLOT(_q_mediaStatusChanged(QMediaImageViewer::MediaStatus)));
}

/*!
    Destroys an image viewer.
*/
QMediaImageViewer::~QMediaImageViewer()
{
    Q_D(QMediaImageViewer);

    delete d->service;
}

/*!
    \property QMediaImageViewer::state
    \brief the playlist control state of a slide show.
    \since 1.0
*/

QMediaImageViewer::State QMediaImageViewer::state() const
{
    return d_func()->state;
}

/*!
    \fn QMediaImageViewer::stateChanged(QMediaImageViewer::State state)

    Signals that the playlist control \a state of an image viewer has changed.
    \since 1.0
*/

/*!
    \property QMediaImageViewer::mediaStatus
    \brief the status of the current media.
    \since 1.0
*/

QMediaImageViewer::MediaStatus QMediaImageViewer::mediaStatus() const
{
    return d_func()->viewerControl->mediaStatus();
}

/*!
    \fn QMediaImageViewer::mediaStatusChanged(QMediaImageViewer::MediaStatus status)

    Signals the the \a status of the current media has changed.
    \since 1.0
*/

/*!
    \property QMediaImageViewer::media
    \brief the media an image viewer is presenting.
    \since 1.0
*/

QMediaContent QMediaImageViewer::media() const
{
    Q_D(const QMediaImageViewer);

    return d->media;
}

void QMediaImageViewer::setMedia(const QMediaContent &media)
{
    Q_D(QMediaImageViewer);

    if (d->playlist && d->playlist->currentMedia() != media) {
        disconnect(d->playlist, SIGNAL(currentMediaChanged(QMediaContent)),
                   this, SLOT(_q_playlistMediaChanged(QMediaContent)));
        disconnect(d->playlist, SIGNAL(destroyed()), this, SLOT(_q_playlistDestroyed()));

        d->playlist = 0;
    }

    d->media = media;

    if (d->timer.isActive()) {
        d->pauseTime = 0;
        d->timer.stop();
        removePropertyWatch("elapsedTime");
        emit elapsedTimeChanged(0);
    }

    if (d->state != QMediaImageViewer::StoppedState)
        emit stateChanged(d->state = QMediaImageViewer::StoppedState);

    d->viewerControl->showMedia(d->media);

    emit mediaChanged(d->media);
}

/*!
  Use \a playlist as the source of images to be displayed in the viewer.
  \since 1.0
*/
void QMediaImageViewer::setPlaylist(QMediaPlaylist *playlist)
{
    Q_D(QMediaImageViewer);

    if (d->playlist) {
        disconnect(d->playlist, SIGNAL(currentMediaChanged(QMediaContent)),
                   this, SLOT(_q_playlistMediaChanged(QMediaContent)));
        disconnect(d->playlist, SIGNAL(destroyed()), this, SLOT(_q_playlistDestroyed()));

        QMediaObject::unbind(d->playlist);
    }

    d->playlist = playlist;

    if (d->playlist) {
        connect(d->playlist, SIGNAL(currentMediaChanged(QMediaContent)),
                this, SLOT(_q_playlistMediaChanged(QMediaContent)));
        connect(d->playlist, SIGNAL(destroyed()), this, SLOT(_q_playlistDestroyed()));

        QMediaObject::bind(d->playlist);

        setMedia(d->playlist->currentMedia());
    } else {
        setMedia(QMediaContent());
    }
}

/*!
  Returns the current playlist, or 0 if none.
  \since 1.0
*/
QMediaPlaylist *QMediaImageViewer::playlist() const
{
    return d_func()->playlist;
}

/*!
    \fn QMediaImageViewer::mediaChanged(const QMediaContent &media)

    Signals that the \a media an image viewer is presenting has changed.
    \since 1.0
*/

/*!
    \property QMediaImageViewer::timeout
    \brief the amount of time in milliseconds an image is displayed for before moving to the next
    image.

    The timeout only applies if the image viewer has a playlist attached and is in the PlayingState.
    \since 1.0
*/

int QMediaImageViewer::timeout() const
{
    return d_func()->timeout;
}

void QMediaImageViewer::setTimeout(int timeout)
{
    Q_D(QMediaImageViewer);

    d->timeout = qMax(0, timeout);

    if (d->timer.isActive())
        d->timer.start(qMax(0, d->timeout - d->pauseTime - d->time.elapsed()), this);
}

/*!
    \property QMediaImageViewer::elapsedTime
    \brief the amount of time in milliseconds that has elapsed since the current image was loaded.

    The elapsed time only increases while the image viewer is in the PlayingState.  If stopped the
    elapsed time will be reset to 0.
    \since 1.0
*/

int QMediaImageViewer::elapsedTime() const
{
    Q_D(const QMediaImageViewer);

    int elapsedTime = d->pauseTime;

    if (d->timer.isActive())
        elapsedTime += d->time.elapsed();

    return elapsedTime;
}

/*!
    \fn QMediaImageViewer::elapsedTimeChanged(int time)

    Signals that the amount of \a time in milliseconds since the current
    image was loaded has changed.

    This signal is emitted at a regular interval when the image viewer is
    in the PlayingState and an image is loaded.  The notification interval
    is controlled by the QMediaObject::notifyInterval property.

    \since 1.0
    \sa timeout, QMediaObject::notifyInterval
*/

/*!
    Sets a video \a widget as the current video output.

    This will unbind any previous video output bound with setVideoOutput().
    \since 1.1
*/

void QMediaImageViewer::setVideoOutput(QVideoWidget *widget)
{
    Q_D(QMediaImageViewer);

    if (d->videoOutput)
        unbind(d->videoOutput);

    // We don't know (in this library) that QVideoWidget inherits QObject
    QObject *widgetObject = reinterpret_cast<QObject*>(widget);

    d->videoOutput = widgetObject && bind(widgetObject) ? widgetObject : 0;
}

/*!
    Sets a video \a item as the current video output.

    This will unbind any previous video output bound with setVideoOutput().
    \since 1.1
*/

void QMediaImageViewer::setVideoOutput(QGraphicsVideoItem *item)
{
    Q_D(QMediaImageViewer);

    if (d->videoOutput)
        unbind(d->videoOutput);

    // We don't know (in this library) that QGraphicsVideoItem (multiply) inherits QObject
    // but QObject inheritance depends on QObject coming first, so try this out.
    QObject *itemObject = reinterpret_cast<QObject*>(item);

    d->videoOutput = itemObject && bind(itemObject) ? itemObject : 0;
}

/*!
    Sets a video \a surface as the video output of a image viewer.

    If a video output has already been set on the image viewer the new surface
    will replace it.
    \since 1.2
*/

void QMediaImageViewer::setVideoOutput(QAbstractVideoSurface *surface)
{
    Q_D(QMediaImageViewer);

    d->surfaceOutput.setVideoSurface(surface);

    if (d->videoOutput != &d->surfaceOutput) {
        if (d->videoOutput)
            unbind(d->videoOutput);

        d->videoOutput = bind(&d->surfaceOutput) ? &d->surfaceOutput : 0;
    }
}

/*!
    \internal
    \since 1.0
*/
bool QMediaImageViewer::bind(QObject *object)
{
    if (QMediaPlaylist *playlist = qobject_cast<QMediaPlaylist *>(object)) {
        setPlaylist(playlist);

        return true;
    } else {
        return QMediaObject::bind(object);
    }
}

/*!
     \internal
     \since 1.0
 */
void QMediaImageViewer::unbind(QObject *object)
{
    if (object == d_func()->playlist)
        setPlaylist(0);
    else
        QMediaObject::unbind(object);
}

/*!
    Starts a slide show.

    If the playlist has no current media this will start at the beginning of the playlist, otherwise
    it will resume from the current media.

    If no playlist is attached to an image viewer this will do nothing.
    \since 1.0
*/
void QMediaImageViewer::play()
{
    Q_D(QMediaImageViewer);

    if (d->playlist && d->playlist->mediaCount() > 0 && d->state != PlayingState) {
        d->state = PlayingState;

        switch (d->viewerControl->mediaStatus()) {
        case NoMedia:
        case InvalidMedia:
            d->playlist->next();
            if (d->playlist->currentIndex() < 0)
                d->state = StoppedState;
            break;
        case LoadingMedia:
            break;
        case LoadedMedia:
            d->time.start();
            d->timer.start(qMax(0, d->timeout - d->pauseTime), this);
            break;
        }

        if (d->state == PlayingState)
            emit stateChanged(d->state);
    }
}

/*!
    Pauses a slide show.

    The current media and elapsed time are retained.  If resumed, the current image will be
    displayed for the remainder of the time out period before the next image is loaded.
    \since 1.0
*/
void QMediaImageViewer::pause()
{
    Q_D(QMediaImageViewer);

    if (d->state == PlayingState) {
        if (d->viewerControl->mediaStatus() == LoadedMedia) {
            d->pauseTime += d->timeout - d->time.elapsed();
            d->timer.stop();
            removePropertyWatch("elapsedTime");
        }

        emit stateChanged(d->state = PausedState);
        emit elapsedTimeChanged(d->pauseTime);
    }
}

/*!
    Stops a slide show.

    The current media is retained, but the elapsed time is discarded.  If resumed, the current
    image will be displayed for the full time out period before the next image is loaded.
    \since 1.0
*/
void QMediaImageViewer::stop()
{
    Q_D(QMediaImageViewer);

    switch (d->state) {
    case PlayingState:
        d->timer.stop();
        removePropertyWatch("elapsedTime");
        // fall through.
    case PausedState:
        d->pauseTime = 0;
        d->state = QMediaImageViewer::StoppedState;

        emit stateChanged(d->state);
        emit elapsedTimeChanged(0);
        break;
    case StoppedState:
        break;
    }
}

/*!
    \reimp

    \internal
    \since 1.0
*/
void QMediaImageViewer::timerEvent(QTimerEvent *event)
{
    Q_D(QMediaImageViewer);

    if (event->timerId() == d->timer.timerId()) {
        d->timer.stop();
        removePropertyWatch("elapsedTime");
        emit elapsedTimeChanged(d->pauseTime = d->timeout);

        d->playlist->next();

        if (d->playlist->currentIndex() < 0) {
            d->pauseTime = 0;
            emit stateChanged(d->state = StoppedState);
            emit elapsedTimeChanged(0);
        }
    } else {
        QMediaObject::timerEvent(event);
    }
}

#include "moc_qmediaimageviewer.cpp"
QT_END_NAMESPACE