videowidget.cpp 8.62 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
#include <qmenu.h>
18
#include <qtoolbar.h>
19
20
#include <qtoolbutton.h>
#include <qwidgetaction.h>
21
22
23

#include <kicon.h>
#include <klocale.h>
24

25
#include <phonon/seekslider.h>
26
27
28
29
30
31
32
#include <phonon/videoplayer.h>

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

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
static QAction* createToolBarButtonWithWidgetPopup( QToolBar* toolBar, QWidget *widget, const QIcon &icon )
{
    QToolButton *button = new QToolButton( toolBar );
    QAction *action = toolBar->addWidget( button );
    button->setAutoRaise( true );
    button->setIcon( icon );
    button->setPopupMode( QToolButton::InstantPopup );
    QMenu *menu = new QMenu( button );
    button->setMenu( menu );
    QWidgetAction *widgetAction = new QWidgetAction( menu );
    QWidget *dummy = new QWidget( menu );
    widgetAction->setDefaultWidget( dummy );
    QVBoxLayout *dummyLayout = new QVBoxLayout( dummy );
    dummyLayout->setMargin( 5 );
    dummyLayout->addWidget( widget );
    menu->addAction( widgetAction );
    return action;
}

52
53
54
55
56
57
58
59
60
/* Private storage. */
class VideoWidget::Private
{
public:
    Private( Okular::MovieAnnotation *ma, Okular::Document *doc, VideoWidget *qq )
        : q( qq ), anno( ma ), document( doc ), loaded( false )
    {
    }

61
62
    enum PlayPauseMode { PlayMode, PauseMode };

63
    void load();
64
    void setupPlayPauseAction( PlayPauseMode mode );
65
66
67

    // slots
    void finished();
68
    void playOrPause();
69
70
71
72
73
74

    VideoWidget *q;
    Okular::MovieAnnotation *anno;
    Okular::Document *document;
    Okular::NormalizedRect geom;
    Phonon::VideoPlayer *player;
75
    Phonon::SeekSlider *seekSlider;
76
77
78
    QToolBar *controlBar;
    QAction *playPauseAction;
    QAction *stopAction;
79
80
    QAction *seekSliderAction;
    QAction *seekSliderMenuAction;
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
    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 );
106
107

    seekSlider->setEnabled( true );
108
109
}

110
111
112
113
114
115
116
117
118
119
120
121
122
123
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" ) );
    }
}

124
125
126
127
128
129
130
void VideoWidget::Private::finished()
{
    switch ( anno->movie()->playMode() )
    {
        case Okular::Movie::PlayOnce:
        case Okular::Movie::PlayOpen:
            // playback has ended, nothing to do
131
132
133
134
            stopAction->setEnabled( false );
            setupPlayPauseAction( PlayMode );
            if ( anno->movie()->playMode() == Okular::Movie::PlayOnce )
                controlBar->setVisible( false );
135
            q->setVisible(false);
136
137
138
139
140
141
142
143
144
145
146
147
            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;
    }
}

148
149
150
151
152
153
154
155
156
157
158
159
160
void VideoWidget::Private::playOrPause()
{
    if ( player->isPlaying() )
    {
        player->pause();
        setupPlayPauseAction( PlayMode );
    }
    else
    {
        q->play();
    }
}

161
162
163
VideoWidget::VideoWidget( Okular::MovieAnnotation *movieann, Okular::Document *document, QWidget *parent )
    : QWidget( parent ), d( new Private( movieann, document, this ) )
{
164
165
166
167
    // do not propagate the mouse events to the parent widget;
    // they should be tied to this widget, not spread around...
    setAttribute( Qt::WA_NoMousePropagation );

168
169
    QVBoxLayout *mainlay = new QVBoxLayout( this );
    mainlay->setMargin( 0 );
170
    mainlay->setSpacing( 0 );
171
172

    d->player = new Phonon::VideoPlayer( Phonon::NoCategory, this );
173
    d->player->installEventFilter( this );
174
175
    mainlay->addWidget( d->player );

176
177
178
179
180
181
182
183
184
185
186
    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" ),
Laurent Montel's avatar
Laurent Montel committed
187
        this, SLOT(stop()) );
188
    d->stopAction->setEnabled( false );
189
190
191
192
193
194
195
196
197
198
    d->controlBar->addSeparator();
    d->seekSlider = new Phonon::SeekSlider( d->player->mediaObject(), d->controlBar );
    d->seekSliderAction = d->controlBar->addWidget( d->seekSlider );
    d->seekSlider->setEnabled( false );

    Phonon::SeekSlider *verticalSeekSlider = new Phonon::SeekSlider( d->player->mediaObject(), 0 );
    verticalSeekSlider->setMaximumHeight( 100 );
    d->seekSliderMenuAction = createToolBarButtonWithWidgetPopup(
        d->controlBar, verticalSeekSlider, KIcon( "player-time" ) );
    d->seekSliderMenuAction->setVisible( false );
199
200
201

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

Laurent Montel's avatar
Laurent Montel committed
202
203
    connect( d->player, SIGNAL(finished()), this, SLOT(finished()) );
    connect( d->playPauseAction, SIGNAL(triggered()), this, SLOT(playOrPause()) );
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

    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;
}

223
224
225
226
227
bool VideoWidget::isPlaying() const
{
    return d->player->isPlaying();
}

228
229
230
231
232
233
234
235
void VideoWidget::pageEntered()
{
    if ( d->anno->movie()->autoPlay() ) {
        show();
        QMetaObject::invokeMethod(this, "play", Qt::QueuedConnection);
    }
}

236
237
238
239
void VideoWidget::play()
{
    d->load();
    d->player->play();
240
241
    d->stopAction->setEnabled( true );
    d->setupPlayPauseAction( Private::PauseMode );
242
243
244
245
246
}

void VideoWidget::stop()
{
    d->player->stop();
247
248
    d->stopAction->setEnabled( false );
    d->setupPlayPauseAction( Private::PlayMode );
249
250
251
252
253
}

void VideoWidget::pause()
{
    d->player->pause();
254
    d->setupPlayPauseAction( Private::PlayMode );
255
256
}

257
bool VideoWidget::eventFilter( QObject * object, QEvent * event )
258
{
259
    if ( object == d->player )
260
    {
261
        switch ( event->type() )
262
        {
263
            case QEvent::MouseButtonPress:
264
            {
265
266
267
268
269
270
271
272
273
                QMouseEvent * me = static_cast< QMouseEvent * >( event );
                if ( me->button() == Qt::LeftButton )
                {
                    if ( !d->player->isPlaying() )
                    {
                        play();
                    }
                    event->accept();
                }
274
            }
275
            default: ;
276
277
278
        }
    }

279
    return false;
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
bool VideoWidget::event( QEvent * event )
{
    switch ( event->type() )
    {
        case QEvent::ToolTip:
            // "eat" the help events (= tooltips), avoid parent widgets receiving them
            event->accept();
            return true;
            break;
        default: ;
    }

    return QWidget::event( event );
}

void VideoWidget::resizeEvent( QResizeEvent * event )
{
    const QSize &s = event->size();
    int usedSpace = d->seekSlider->geometry().left() + d->seekSlider->iconSize().width();
    // try to give the slider at least 30px of space
    if ( s.width() < ( usedSpace + 30 ) )
    {
        d->seekSliderAction->setVisible( false );
        d->seekSliderMenuAction->setVisible( true );
    }
    else
    {
        d->seekSliderAction->setVisible( true );
        d->seekSliderMenuAction->setVisible( false );
    }
}

314
#include "videowidget.moc"