2008-08-25 01:26:47 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* 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
|
2008-08-25 09:23:11 +00:00
|
|
|
#include <qaction.h>
|
2008-08-25 01:26:47 +00:00
|
|
|
#include <qdir.h>
|
|
|
|
#include <qevent.h>
|
|
|
|
#include <qlayout.h>
|
2008-08-27 13:54:54 +00:00
|
|
|
#include <qmenu.h>
|
2008-08-25 09:23:11 +00:00
|
|
|
#include <qtoolbar.h>
|
2008-08-27 13:54:54 +00:00
|
|
|
#include <qtoolbutton.h>
|
|
|
|
#include <qwidgetaction.h>
|
2008-08-25 09:23:11 +00:00
|
|
|
|
|
|
|
#include <kicon.h>
|
|
|
|
#include <klocale.h>
|
2008-08-25 01:26:47 +00:00
|
|
|
|
2008-08-27 13:54:54 +00:00
|
|
|
#include <phonon/seekslider.h>
|
2008-08-25 01:26:47 +00:00
|
|
|
#include <phonon/videoplayer.h>
|
|
|
|
|
|
|
|
#include "core/area.h"
|
|
|
|
#include "core/annotations.h"
|
|
|
|
#include "core/document.h"
|
|
|
|
#include "core/movie.h"
|
|
|
|
|
2008-08-27 13:54:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-08-25 01:26:47 +00:00
|
|
|
/* Private storage. */
|
|
|
|
class VideoWidget::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private( Okular::MovieAnnotation *ma, Okular::Document *doc, VideoWidget *qq )
|
|
|
|
: q( qq ), anno( ma ), document( doc ), loaded( false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-08-25 09:23:11 +00:00
|
|
|
enum PlayPauseMode { PlayMode, PauseMode };
|
|
|
|
|
2008-08-25 01:26:47 +00:00
|
|
|
void load();
|
2008-08-25 09:23:11 +00:00
|
|
|
void setupPlayPauseAction( PlayPauseMode mode );
|
2008-08-25 01:26:47 +00:00
|
|
|
|
|
|
|
// slots
|
|
|
|
void finished();
|
2008-08-25 09:23:11 +00:00
|
|
|
void playOrPause();
|
2008-08-25 01:26:47 +00:00
|
|
|
|
|
|
|
VideoWidget *q;
|
|
|
|
Okular::MovieAnnotation *anno;
|
|
|
|
Okular::Document *document;
|
|
|
|
Okular::NormalizedRect geom;
|
|
|
|
Phonon::VideoPlayer *player;
|
2008-08-27 13:54:54 +00:00
|
|
|
Phonon::SeekSlider *seekSlider;
|
2008-08-25 09:23:11 +00:00
|
|
|
QToolBar *controlBar;
|
|
|
|
QAction *playPauseAction;
|
|
|
|
QAction *stopAction;
|
2008-08-27 13:54:54 +00:00
|
|
|
QAction *seekSliderAction;
|
|
|
|
QAction *seekSliderMenuAction;
|
2008-08-25 01:26:47 +00:00
|
|
|
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 );
|
2008-08-27 13:54:54 +00:00
|
|
|
|
|
|
|
seekSlider->setEnabled( true );
|
2008-08-25 01:26:47 +00:00
|
|
|
}
|
|
|
|
|
2008-08-25 09:23:11 +00:00
|
|
|
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" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 01:26:47 +00:00
|
|
|
void VideoWidget::Private::finished()
|
|
|
|
{
|
|
|
|
switch ( anno->movie()->playMode() )
|
|
|
|
{
|
|
|
|
case Okular::Movie::PlayOnce:
|
|
|
|
case Okular::Movie::PlayOpen:
|
|
|
|
// playback has ended, nothing to do
|
2008-08-25 09:23:11 +00:00
|
|
|
stopAction->setEnabled( false );
|
|
|
|
setupPlayPauseAction( PlayMode );
|
|
|
|
if ( anno->movie()->playMode() == Okular::Movie::PlayOnce )
|
|
|
|
controlBar->setVisible( false );
|
2008-08-25 01:26:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 09:23:11 +00:00
|
|
|
void VideoWidget::Private::playOrPause()
|
|
|
|
{
|
|
|
|
if ( player->isPlaying() )
|
|
|
|
{
|
|
|
|
player->pause();
|
|
|
|
setupPlayPauseAction( PlayMode );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
q->play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 01:26:47 +00:00
|
|
|
VideoWidget::VideoWidget( Okular::MovieAnnotation *movieann, Okular::Document *document, QWidget *parent )
|
|
|
|
: QWidget( parent ), d( new Private( movieann, document, this ) )
|
|
|
|
{
|
2008-08-25 02:04:27 +00:00
|
|
|
// do not propagate the mouse events to the parent widget;
|
|
|
|
// they should be tied to this widget, not spread around...
|
|
|
|
setAttribute( Qt::WA_NoMousePropagation );
|
|
|
|
|
2008-08-25 01:26:47 +00:00
|
|
|
QVBoxLayout *mainlay = new QVBoxLayout( this );
|
|
|
|
mainlay->setMargin( 0 );
|
2008-08-25 18:27:58 +00:00
|
|
|
mainlay->setSpacing( 0 );
|
2008-08-25 01:26:47 +00:00
|
|
|
|
|
|
|
d->player = new Phonon::VideoPlayer( Phonon::NoCategory, this );
|
2008-08-25 09:23:11 +00:00
|
|
|
d->player->installEventFilter( this );
|
2008-08-25 01:26:47 +00:00
|
|
|
mainlay->addWidget( d->player );
|
|
|
|
|
2008-08-25 09:23:11 +00:00
|
|
|
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 );
|
2008-08-27 13:54:54 +00:00
|
|
|
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 );
|
2008-08-25 09:23:11 +00:00
|
|
|
|
|
|
|
d->controlBar->setVisible( movieann->movie()->showControls() );
|
|
|
|
|
2008-08-25 01:26:47 +00:00
|
|
|
connect( d->player, SIGNAL( finished() ), this, SLOT( finished() ) );
|
2008-08-25 09:23:11 +00:00
|
|
|
connect( d->playPauseAction, SIGNAL( triggered() ), this, SLOT( playOrPause() ) );
|
2008-08-25 01:26:47 +00:00
|
|
|
|
|
|
|
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();
|
2008-08-25 09:23:11 +00:00
|
|
|
d->stopAction->setEnabled( true );
|
|
|
|
d->setupPlayPauseAction( Private::PauseMode );
|
2008-08-25 01:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoWidget::stop()
|
|
|
|
{
|
|
|
|
d->player->stop();
|
2008-08-25 09:23:11 +00:00
|
|
|
d->stopAction->setEnabled( false );
|
|
|
|
d->setupPlayPauseAction( Private::PlayMode );
|
2008-08-25 01:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoWidget::pause()
|
|
|
|
{
|
|
|
|
d->player->pause();
|
2008-08-25 09:23:11 +00:00
|
|
|
d->setupPlayPauseAction( Private::PlayMode );
|
2008-08-25 01:26:47 +00:00
|
|
|
}
|
|
|
|
|
2008-08-25 09:23:11 +00:00
|
|
|
bool VideoWidget::eventFilter( QObject * object, QEvent * event )
|
2008-08-25 01:26:47 +00:00
|
|
|
{
|
2008-08-25 09:23:11 +00:00
|
|
|
if ( object == d->player )
|
2008-08-25 01:26:47 +00:00
|
|
|
{
|
2008-08-27 13:54:54 +00:00
|
|
|
switch ( event->type() )
|
2008-08-25 01:26:47 +00:00
|
|
|
{
|
2008-08-27 13:54:54 +00:00
|
|
|
case QEvent::MouseButtonPress:
|
2008-08-25 09:23:11 +00:00
|
|
|
{
|
2008-08-27 13:54:54 +00:00
|
|
|
QMouseEvent * me = static_cast< QMouseEvent * >( event );
|
|
|
|
if ( me->button() == Qt::LeftButton )
|
|
|
|
{
|
|
|
|
if ( !d->player->isPlaying() )
|
|
|
|
{
|
|
|
|
play();
|
|
|
|
}
|
|
|
|
event->accept();
|
|
|
|
}
|
2008-08-25 09:23:11 +00:00
|
|
|
}
|
2008-08-27 13:54:54 +00:00
|
|
|
default: ;
|
2008-08-25 01:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 09:23:11 +00:00
|
|
|
return false;
|
2008-08-25 01:26:47 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 13:54:54 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 01:26:47 +00:00
|
|
|
#include "videowidget.moc"
|