videowidget.cpp 5.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/***************************************************************************
 *   Copyright (C) 2008 by Pino Toscano <pino@kde.org>                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "videowidget.h"

// qt/kde includes
13
#include <qaction.h>
14
15
16
#include <qdir.h>
#include <qevent.h>
#include <qlayout.h>
17
18
19
20
#include <qtoolbar.h>

#include <kicon.h>
#include <klocale.h>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include <phonon/videoplayer.h>

#include "core/area.h"
#include "core/annotations.h"
#include "core/document.h"
#include "core/movie.h"

/* Private storage. */
class VideoWidget::Private
{
public:
    Private( Okular::MovieAnnotation *ma, Okular::Document *doc, VideoWidget *qq )
        : q( qq ), anno( ma ), document( doc ), loaded( false )
    {
    }

38
39
    enum PlayPauseMode { PlayMode, PauseMode };

40
    void load();
41
    void setupPlayPauseAction( PlayPauseMode mode );
42
43
44

    // slots
    void finished();
45
    void playOrPause();
46
47
48
49
50
51

    VideoWidget *q;
    Okular::MovieAnnotation *anno;
    Okular::Document *document;
    Okular::NormalizedRect geom;
    Phonon::VideoPlayer *player;
52
53
54
    QToolBar *controlBar;
    QAction *playPauseAction;
    QAction *stopAction;
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
    bool loaded : 1;
};

void VideoWidget::Private::load()
{
    if ( loaded )
        return;

    loaded = true;

    QString url = anno->movie()->url();
    KUrl newurl;
    if ( QDir::isRelativePath( url ) )
    {
        newurl = document->currentDocument();
        newurl.setFileName( url );
    }
    else
    {
        newurl = url;
    }
    if ( newurl.isLocalFile() )
        player->load( newurl.toLocalFile() );
    else
        player->load( newurl );
}

82
83
84
85
86
87
88
89
90
91
92
93
94
95
void VideoWidget::Private::setupPlayPauseAction( PlayPauseMode mode )
{
    if ( mode == PlayMode )
    {
        playPauseAction->setIcon( KIcon( "media-playback-start" ) );
        playPauseAction->setText( i18nc( "start the movie playback", "Play" ) );
    }
    else if ( mode == PauseMode )
    {
        playPauseAction->setIcon( KIcon( "media-playback-pause" ) );
        playPauseAction->setText( i18nc( "pause the movie playback", "Pause" ) );
    }
}

96
97
98
99
100
101
102
void VideoWidget::Private::finished()
{
    switch ( anno->movie()->playMode() )
    {
        case Okular::Movie::PlayOnce:
        case Okular::Movie::PlayOpen:
            // playback has ended, nothing to do
103
104
105
106
            stopAction->setEnabled( false );
            setupPlayPauseAction( PlayMode );
            if ( anno->movie()->playMode() == Okular::Movie::PlayOnce )
                controlBar->setVisible( false );
107
108
109
110
111
112
113
114
115
116
117
118
            break;
        case Okular::Movie::PlayRepeat:
            // repeat the playback
            player->play();
            break;
        case Okular::Movie::PlayPalindrome:
            // FIXME we should play backward, but we cannot
            player->play();
            break;
    }
}

119
120
121
122
123
124
125
126
127
128
129
130
131
void VideoWidget::Private::playOrPause()
{
    if ( player->isPlaying() )
    {
        player->pause();
        setupPlayPauseAction( PlayMode );
    }
    else
    {
        q->play();
    }
}

132
133
134
VideoWidget::VideoWidget( Okular::MovieAnnotation *movieann, Okular::Document *document, QWidget *parent )
    : QWidget( parent ), d( new Private( movieann, document, this ) )
{
135
136
137
138
    // do not propagate the mouse events to the parent widget;
    // they should be tied to this widget, not spread around...
    setAttribute( Qt::WA_NoMousePropagation );

139
140
    QVBoxLayout *mainlay = new QVBoxLayout( this );
    mainlay->setMargin( 0 );
141
    mainlay->setSpacing( 0 );
142
143

    d->player = new Phonon::VideoPlayer( Phonon::NoCategory, this );
144
    d->player->installEventFilter( this );
145
146
    mainlay->addWidget( d->player );

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    d->controlBar = new QToolBar( this );
    d->controlBar->setIconSize( QSize( 16, 16 ) );
    d->controlBar->setAutoFillBackground( true );
    mainlay->addWidget( d->controlBar );

    d->playPauseAction = new QAction( d->controlBar );
    d->controlBar->addAction( d->playPauseAction );
    d->setupPlayPauseAction( Private::PlayMode );
    d->stopAction = d->controlBar->addAction(
        KIcon( "media-playback-stop" ),
        i18nc( "stop the movie playback", "Stop" ),
        this, SLOT( stop() ) );
    d->stopAction->setEnabled( false );

    d->controlBar->setVisible( movieann->movie()->showControls() );

163
    connect( d->player, SIGNAL( finished() ), this, SLOT( finished() ) );
164
    connect( d->playPauseAction, SIGNAL( triggered() ), this, SLOT( playOrPause() ) );
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

    d->geom = movieann->transformedBoundingRectangle();
}

VideoWidget::~VideoWidget()
{
    delete d;
}

void VideoWidget::setNormGeometry( const Okular::NormalizedRect &rect )
{
    d->geom = rect;
}

Okular::NormalizedRect VideoWidget::normGeometry() const
{
    return d->geom;
}

void VideoWidget::play()
{
    d->load();
    d->player->play();
188
189
    d->stopAction->setEnabled( true );
    d->setupPlayPauseAction( Private::PauseMode );
190
191
192
193
194
}

void VideoWidget::stop()
{
    d->player->stop();
195
196
    d->stopAction->setEnabled( false );
    d->setupPlayPauseAction( Private::PlayMode );
197
198
199
200
201
}

void VideoWidget::pause()
{
    d->player->pause();
202
    d->setupPlayPauseAction( Private::PlayMode );
203
204
}

205
bool VideoWidget::eventFilter( QObject * object, QEvent * event )
206
{
207
    if ( object == d->player )
208
    {
209
210
        QMouseEvent * me = static_cast< QMouseEvent * >( event );
        if ( me->button() == Qt::LeftButton )
211
        {
212
213
214
215
216
            if ( !d->player->isPlaying() )
            {
                play();
            }
            event->accept();
217
218
219
        }
    }

220
    return false;
221
222
223
}

#include "videowidget.moc"