videowidget.cpp 12.6 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
#include <qcoreapplication.h>
15 16
#include <qdir.h>
#include <qevent.h>
17
#include <qlabel.h>
18
#include <qlayout.h>
19
#include <qmenu.h>
20
#include <qstackedlayout.h>
21
#include <qtoolbar.h>
22 23
#include <qtoolbutton.h>
#include <qwidgetaction.h>
24

Laurent Montel's avatar
Laurent Montel committed
25
#include <QIcon>
Laurent Montel's avatar
Laurent Montel committed
26
#include <KLocalizedString>
27

28
#include <phonon/mediaobject.h>
29
#include <phonon/seekslider.h>
30 31 32
#include <phonon/videoplayer.h>

#include "core/annotations.h"
33
#include "core/area.h"
34 35
#include "core/document.h"
#include "core/movie.h"
36
#include "snapshottaker.h"
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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;
}

57 58 59 60
/* Private storage. */
class VideoWidget::Private
{
public:
61 62
    Private( Okular::Movie *m, Okular::Document *doc, VideoWidget *qq )
        : q( qq ), movie( m ), document( doc ), player( 0 ), loaded( false )
63 64 65
    {
    }

66 67 68 69 70 71
    ~Private()
    {
        if ( player )
            player->stop();
    }

72 73
    enum PlayPauseMode { PlayMode, PauseMode };

74
    void load();
75
    void setupPlayPauseAction( PlayPauseMode mode );
76 77 78
    void setPosterImage( const QImage& );
    void takeSnapshot();
    void videoStopped();
79
    void stateChanged(Phonon::State, Phonon::State);
80 81 82

    // slots
    void finished();
83
    void playOrPause();
84 85

    VideoWidget *q;
86
    Okular::Movie *movie;
87 88 89
    Okular::Document *document;
    Okular::NormalizedRect geom;
    Phonon::VideoPlayer *player;
90
    Phonon::SeekSlider *seekSlider;
91 92 93
    QToolBar *controlBar;
    QAction *playPauseAction;
    QAction *stopAction;
94 95
    QAction *seekSliderAction;
    QAction *seekSliderMenuAction;
96 97
    QStackedLayout *pageLayout;
    QLabel *posterImagePage;
98
    bool loaded : 1;
99
    double repetitionsLeft;
100 101
};

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static QUrl urlFromUrlString(const QString &url, Okular::Document *document)
{
    QUrl newurl;
    if ( url.startsWith( QLatin1Char( '/' ) ) )
    {
        newurl = QUrl::fromLocalFile( url );
    }
    else
    {
        newurl = QUrl( url );
        if ( newurl.isRelative() )
        {
            newurl = document->currentDocument().adjusted(QUrl::RemoveFilename);
            newurl.setPath( newurl.path() + url );
        }
    }
    return newurl;
}

121 122
void VideoWidget::Private::load()
{
123
    repetitionsLeft = movie->playRepetitions();
124 125 126 127 128
    if ( loaded )
        return;

    loaded = true;

129
    player->load( urlFromUrlString( movie->url(), document ) );
130

131 132 133
    connect( player->mediaObject(), SIGNAL( stateChanged( Phonon::State, Phonon::State ) ),
             q, SLOT( stateChanged( Phonon::State, Phonon::State ) ) );

134
    seekSlider->setEnabled( true );
135 136
}

137 138 139 140
void VideoWidget::Private::setupPlayPauseAction( PlayPauseMode mode )
{
    if ( mode == PlayMode )
    {
Laurent Montel's avatar
Laurent Montel committed
141
        playPauseAction->setIcon( QIcon::fromTheme( QStringLiteral("media-playback-start") ) );
142 143 144 145
        playPauseAction->setText( i18nc( "start the movie playback", "Play" ) );
    }
    else if ( mode == PauseMode )
    {
Laurent Montel's avatar
Laurent Montel committed
146
        playPauseAction->setIcon( QIcon::fromTheme( QStringLiteral("media-playback-pause") ) );
147 148 149 150
        playPauseAction->setText( i18nc( "pause the movie playback", "Pause" ) );
    }
}

151 152
void VideoWidget::Private::takeSnapshot()
{
153 154
    const QUrl url = urlFromUrlString( movie->url(), document );
    SnapshotTaker *taker = new SnapshotTaker( url, q );
155 156 157 158 159 160

    q->connect( taker, SIGNAL( finished( const QImage& ) ), q, SLOT( setPosterImage( const QImage& ) ) );
}

void VideoWidget::Private::videoStopped()
{
161
    if ( movie->showPosterImage() )
162 163 164 165 166
        pageLayout->setCurrentIndex( 1 );
    else
        q->hide();
}

167 168
void VideoWidget::Private::finished()
{
169
    switch ( movie->playMode() )
170
    {
171
        case Okular::Movie::PlayLimited:
172
        case Okular::Movie::PlayOpen:
173 174 175 176 177 178 179 180 181 182 183 184
            repetitionsLeft -= 1.0;
            if ( repetitionsLeft < 1e-5 ) { // allow for some calculation error
                // playback has ended
                stopAction->setEnabled( false );
                setupPlayPauseAction( PlayMode );
                if ( movie->playMode() == Okular::Movie::PlayLimited )
                    controlBar->setVisible( false );
                videoStopped();
            } else
                // not done yet, repeat
                // if repetitionsLeft is less than 1, we are supposed to stop midway, but not even Adobe reader does this
                player->play();
185 186 187 188 189 190 191 192 193 194 195 196
            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;
    }
}

197 198 199 200 201 202 203 204 205 206 207 208 209
void VideoWidget::Private::playOrPause()
{
    if ( player->isPlaying() )
    {
        player->pause();
        setupPlayPauseAction( PlayMode );
    }
    else
    {
        q->play();
    }
}

210 211 212 213 214
void VideoWidget::Private::setPosterImage( const QImage &image )
{
    if ( !image.isNull() )
    {
        // cache the snapshot image
215
        movie->setPosterImage( image );
216 217 218 219 220
    }

    posterImagePage->setPixmap( QPixmap::fromImage( image ) );
}

221 222 223 224 225 226
void VideoWidget::Private::stateChanged( Phonon::State newState, Phonon::State )
{
    if ( newState == Phonon::PlayingState )
        pageLayout->setCurrentIndex( 0 );
}

227 228
VideoWidget::VideoWidget( const Okular::Annotation *annotation, Okular::Movie *movie, Okular::Document *document, QWidget *parent )
    : QWidget( parent ), d( new Private( movie, document, this ) )
229
{
230 231 232 233
    // do not propagate the mouse events to the parent widget;
    // they should be tied to this widget, not spread around...
    setAttribute( Qt::WA_NoMousePropagation );

234 235 236 237
    // Setup player page
    QWidget *playerPage = new QWidget;

    QVBoxLayout *mainlay = new QVBoxLayout( playerPage );
238
    mainlay->setMargin( 0 );
239
    mainlay->setSpacing( 0 );
240

241 242
    d->player = new Phonon::VideoPlayer( Phonon::NoCategory, playerPage );
    d->player->installEventFilter( playerPage );
243 244
    mainlay->addWidget( d->player );

245
    d->controlBar = new QToolBar( playerPage );
246 247 248 249 250 251 252 253
    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(
Laurent Montel's avatar
Laurent Montel committed
254
        QIcon::fromTheme( QStringLiteral("media-playback-stop") ),
255
        i18nc( "stop the movie playback", "Stop" ),
Laurent Montel's avatar
Laurent Montel committed
256
        this, SLOT(stop()) );
257
    d->stopAction->setEnabled( false );
258 259 260 261 262 263 264 265
    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(
Laurent Montel's avatar
Laurent Montel committed
266
        d->controlBar, verticalSeekSlider, QIcon::fromTheme( QStringLiteral("player-time") ) );
267
    d->seekSliderMenuAction->setVisible( false );
268

269
    d->controlBar->setVisible( movie->showControls() );
270

Laurent Montel's avatar
Laurent Montel committed
271 272
    connect( d->player, SIGNAL(finished()), this, SLOT(finished()) );
    connect( d->playPauseAction, SIGNAL(triggered()), this, SLOT(playOrPause()) );
273

274
    d->geom = annotation->transformedBoundingRectangle();
275 276 277 278 279 280 281 282 283 284 285 286 287 288

    // Setup poster image page
    d->posterImagePage = new QLabel;
    d->posterImagePage->setScaledContents( true );
    d->posterImagePage->installEventFilter( this );
    d->posterImagePage->setCursor( Qt::PointingHandCursor );

    d->pageLayout = new QStackedLayout( this );
    d->pageLayout->setMargin( 0 );
    d->pageLayout->setSpacing( 0 );
    d->pageLayout->addWidget( playerPage );
    d->pageLayout->addWidget( d->posterImagePage );


289
    if ( movie->showPosterImage() )
290 291 292
    {
        d->pageLayout->setCurrentIndex( 1 );

293
        const QImage posterImage = movie->posterImage();
294 295 296 297 298 299 300 301 302 303 304 305 306
        if ( posterImage.isNull() )
        {
            d->takeSnapshot();
        }
        else
        {
            d->setPosterImage( posterImage );
        }
    }
    else
    {
        d->pageLayout->setCurrentIndex( 0 );
    }
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
}

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

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

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

324 325 326 327 328
bool VideoWidget::isPlaying() const
{
    return d->player->isPlaying();
}

329 330 331 332 333
void VideoWidget::pageInitialized()
{
    hide();
}

334 335
void VideoWidget::pageEntered()
{
336
    if ( d->movie->showPosterImage() )
337 338 339 340 341
    {
        d->pageLayout->setCurrentIndex( 1 );
        show();
    }

342
    if ( d->movie->autoPlay() )
343
    {
344 345 346 347 348
        show();
        QMetaObject::invokeMethod(this, "play", Qt::QueuedConnection);
    }
}

349 350
void VideoWidget::pageLeft()
{
351
    d->player->stop();
352 353 354 355 356
    d->videoStopped();

    hide();
}

357 358
void VideoWidget::play()
{
Jaan Vajakas's avatar
Jaan Vajakas committed
359
    d->controlBar->setVisible( d->movie->showControls() );
360
    d->load();
361
    // if d->repetitionsLeft is less than 1, we are supposed to stop midway, but not even Adobe reader does this
362
    d->player->play();
363 364
    d->stopAction->setEnabled( true );
    d->setupPlayPauseAction( Private::PauseMode );
365 366 367 368 369
}

void VideoWidget::stop()
{
    d->player->stop();
370 371
    d->stopAction->setEnabled( false );
    d->setupPlayPauseAction( Private::PlayMode );
372 373 374 375 376
}

void VideoWidget::pause()
{
    d->player->pause();
377
    d->setupPlayPauseAction( Private::PlayMode );
378 379
}

380
bool VideoWidget::eventFilter( QObject * object, QEvent * event )
381
{
382
    if ( object == d->player || object == d->posterImagePage )
383
    {
384
        switch ( event->type() )
385
        {
386
            case QEvent::MouseButtonPress:
387
            {
388 389 390 391 392 393 394 395 396
                QMouseEvent * me = static_cast< QMouseEvent * >( event );
                if ( me->button() == Qt::LeftButton )
                {
                    if ( !d->player->isPlaying() )
                    {
                        play();
                    }
                    event->accept();
                }
397
            }
398 399 400 401 402 403 404 405 406 407 408 409
            case QEvent::Wheel:
            {
                if ( object == d->posterImagePage )
                {
                    QWheelEvent * we = static_cast< QWheelEvent * >( event );

                    // forward wheel events to parent widget
                    QWheelEvent *copy = new QWheelEvent( we->pos(), we->globalPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation() );
                    QCoreApplication::postEvent( parentWidget(), copy );
                }
                break;
            }
410
            default: ;
411 412 413
        }
    }

414
    return false;
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
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 );
    }
}

449
#include "moc_videowidget.cpp"