00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifdef HAVE_CONFIG_H
00012 #include <config.h>
00013 #endif
00014
00015 #include <time.h>
00016 #ifdef HAVE_USLEEP
00017 #include <unistd.h>
00018 #endif
00019 #include <qaccel.h>
00020 #include <qcursor.h>
00021
00022 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00023 #include <X11/Xlib.h>
00024 #include <X11/Xutil.h>
00025 #else
00026 #define XEvent void
00027 #endif
00028
00029 #include <kaction.h>
00030 #include <klocale.h>
00031 #include "kvideowidget.h"
00032
00033
00034 class KFullscreenVideoWidget : public KVideoWidget
00035 {
00036 public:
00037 KFullscreenVideoWidget( KVideoWidget *parent = 0, const char *name = 0 );
00038
00039 protected:
00040 virtual void windowActivationChange( bool );
00041 virtual bool x11Event( XEvent *event );
00042
00043 private:
00044 KVideoWidget *videoWidget;
00045 };
00046
00047 KFullscreenVideoWidget::KFullscreenVideoWidget( KVideoWidget *parent, const char *name )
00048 : KVideoWidget( parent, name, WType_TopLevel | WStyle_Customize | WStyle_NoBorder )
00049 {
00050 this->videoWidget = parent;
00051 setEraseColor( black );
00052 setCursor(QCursor(Qt::BlankCursor));
00053 }
00054
00055 void KFullscreenVideoWidget::windowActivationChange( bool )
00056 {
00057 if (!isActiveWindow())
00058 {
00059 videoWidget->setWindowed();
00060 }
00061 }
00062
00063 bool KFullscreenVideoWidget::x11Event( XEvent *event )
00064 {
00065 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00066 if (event->type == ClientMessage &&
00067 event->xclient.message_type ==
00068 XInternAtom( qt_xdisplay(), "VPO_RESIZE_NOTIFY", False ))
00069 {
00070 videoWidget->resizeNotify( event->xclient.data.l[0], event->xclient.data.l[1] );
00071 }
00072 #endif
00073 return false;
00074 }
00075
00076 KVideoWidget::KVideoWidget( KXMLGUIClient *clientParent, QWidget *parent, const char *name, WFlags f )
00077 : KXMLGUIClient( clientParent ),
00078 QWidget( parent, name, f )
00079 {
00080 init();
00081
00082 QString toolbarName = i18n("Video Toolbar");
00083 setXML(QString("<!DOCTYPE kpartgui>\n<kpartgui name=\"kvideowidget\" version=\"1\"><MenuBar><Menu name=\"edit\"><Separator/><Action name=\"double_size\"/><Action name=\"normal_size\"/><Action name=\"half_size\"/><Separator/><Action name=\"fullscreen_mode\"/></Menu></MenuBar><Toolbar name=\"VideoToolbar\"><text>Video Toolbar</text><Action name=\"fullscreen_mode\"/></Toolbar></kpartgui>"), true);
00084 }
00085
00086 KVideoWidget::KVideoWidget( QWidget *parent, const char *name, WFlags f )
00087 : QWidget( parent, name, f )
00088 {
00089 init();
00090 }
00091
00092 void KVideoWidget::init(void)
00093 {
00094 setMinimumSize(0, 0);
00095 setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00096 setFocusPolicy( ClickFocus );
00097
00098 fullscreenWidget = 0;
00099 poVideo = Arts::VideoPlayObject::null();
00100 videoWidth = 0;
00101 videoHeight = 0;
00102
00103
00104 new KToggleAction( i18n("Fullscreen &Mode"), "window_fullscreen",
00105 CTRL+SHIFT+Key_F, this, SLOT(fullscreenActivated()),
00106 actionCollection(), "fullscreen_mode" );
00107 new KRadioAction( i18n("&Half Size"), ALT+Key_0,
00108 this, SLOT(halfSizeActivated()),
00109 actionCollection(), "half_size" );
00110 new KRadioAction( i18n("&Normal Size"), ALT+Key_1,
00111 this, SLOT(normalSizeActivated()),
00112 actionCollection(), "normal_size" );
00113 new KRadioAction( i18n("&Double Size"), ALT+Key_2,
00114 this, SLOT(doubleSizeActivated()),
00115 actionCollection(), "double_size" );
00116
00117 ((KToggleAction *)action( "half_size" ))->setExclusiveGroup( "KVideoWidget::zoom" );
00118 ((KToggleAction *)action( "normal_size" ))->setExclusiveGroup( "KVideoWidget::zoom" );
00119 ((KToggleAction *)action( "double_size" ))->setExclusiveGroup( "KVideoWidget::zoom" );
00120
00121 action("double_size")->setEnabled(false);
00122 action("half_size")->setEnabled(false);
00123 action("normal_size")->setEnabled(false);
00124 action("fullscreen_mode")->setEnabled(false);
00125 }
00126
00127 KVideoWidget::~KVideoWidget()
00128 {
00129 if (isEmbedded())
00130 {
00131 poVideo.x11WindowId( -1 );
00132 poVideo = Arts::VideoPlayObject::null();
00133 }
00134
00135 if (fullscreenWidget)
00136 {
00137 delete fullscreenWidget;
00138 }
00139 }
00140
00141 void KVideoWidget::embed( Arts::VideoPlayObject vpo )
00142 {
00143 bool enable;
00144 if (vpo.isNull())
00145 {
00146 if (isEmbedded())
00147 {
00148 poVideo.x11WindowId( -1 );
00149 poVideo = Arts::VideoPlayObject::null();
00150 }
00151
00152 setBackgroundMode( PaletteBackground );
00153 repaint();
00154
00155
00156 videoWidth = 0;
00157 videoHeight = 0;
00158
00159 if (isHalfSize() || isNormalSize() || isDoubleSize())
00160 emit adaptSize( 0, 0 );
00161
00162 enable = false;
00163 updateGeometry();
00164 }
00165 else
00166 {
00167 if (isEmbedded())
00168 {
00169 poVideo.x11WindowId( -1 );
00170 }
00171
00172 poVideo = vpo;
00173
00174
00175 if (fullscreenWidget)
00176 {
00177 poVideo.x11WindowId( fullscreenWidget->winId() );
00178 fullscreenWidget->setBackgroundMode( NoBackground );
00179
00180 setEraseColor( black );
00181 }
00182 else
00183 {
00184 poVideo.x11WindowId( winId() );
00185 setBackgroundMode( NoBackground );
00186 }
00187 enable = true;
00188 }
00189 action("double_size")->setEnabled(enable);
00190 action("half_size")->setEnabled(enable);
00191 action("normal_size")->setEnabled(enable);
00192 action("fullscreen_mode")->setEnabled(enable);
00193 }
00194
00195 QImage KVideoWidget::snapshot( Arts::VideoPlayObject vpo )
00196 {
00197 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00198 Window root;
00199 Pixmap pixmap;
00200 XImage *xImage;
00201 unsigned int width, height, border, depth;
00202 int x, y;
00203
00204 if (vpo.isNull() || (long)(pixmap = vpo.x11Snapshot()) == -1)
00205 return QImage();
00206
00207
00208 XGetGeometry( qt_xdisplay(), pixmap, &root, &x, &y, &width, &height, &border, &depth );
00209
00210 xImage = XGetImage( qt_xdisplay(), pixmap, 0, 0, width, height, 1, XYPixmap );
00211
00212 if (xImage == 0)
00213 {
00214 XFreePixmap( qt_xdisplay(), pixmap );
00215 return QImage();
00216 }
00217
00218
00219 QImage qImage = QImage( (uchar *)xImage->data, width/32, height, 32, (QRgb *)0, 0, QImage::IgnoreEndian ).copy();
00220
00221
00222 XDestroyImage( xImage );
00223 XFreePixmap( qt_xdisplay(), pixmap );
00224
00225 return qImage;
00226 #else
00227 return 0;
00228 #endif
00229 }
00230
00231 bool KVideoWidget::isEmbedded()
00232 {
00233 return !poVideo.isNull();
00234 }
00235
00236 bool KVideoWidget::isFullscreen()
00237 {
00238 return ((KToggleAction *)action( "fullscreen_mode" ))->isChecked();
00239 }
00240
00241 bool KVideoWidget::isHalfSize()
00242 {
00243 return ((KToggleAction *)action( "half_size" ))->isChecked();
00244 }
00245
00246 bool KVideoWidget::isNormalSize()
00247 {
00248 return ((KToggleAction *)action( "normal_size" ))->isChecked();
00249 }
00250
00251 bool KVideoWidget::isDoubleSize()
00252 {
00253 return ((KToggleAction *)action( "double_size" ))->isChecked();
00254 }
00255
00256 void KVideoWidget::setFullscreen()
00257 {
00258 if (!isFullscreen())
00259 {
00260 ((KToggleAction *)action( "fullscreen_mode" ))->setChecked( true );
00261 fullscreenActivated();
00262 }
00263 }
00264
00265 void KVideoWidget::setWindowed()
00266 {
00267 if (isFullscreen())
00268 {
00269 ((KToggleAction *)action( "fullscreen_mode" ))->setChecked( false );
00270 fullscreenActivated();
00271 }
00272 }
00273
00274 void KVideoWidget::setHalfSize()
00275 {
00276 ((KToggleAction *)action( "half_size" ))->setChecked( true );
00277 halfSizeActivated();
00278 }
00279
00280 void KVideoWidget::setNormalSize()
00281 {
00282 ((KToggleAction *)action( "normal_size" ))->setChecked( true );
00283 normalSizeActivated();
00284 }
00285
00286 void KVideoWidget::setDoubleSize()
00287 {
00288 ((KToggleAction *)action( "double_size" ))->setChecked( true );
00289 doubleSizeActivated();
00290 }
00291
00292 QSize KVideoWidget::sizeHint() const
00293 {
00294 return QSize( videoWidth, videoHeight );
00295 }
00296
00297 int KVideoWidget::heightForWidth( int w ) const
00298 {
00299 if(videoWidth == 0)
00300 return 0;
00301 else
00302 return int( double(w)*double(videoHeight)/double(videoWidth) );
00303 }
00304
00305 void KVideoWidget::mousePressEvent( QMouseEvent *event )
00306 {
00307 QPoint pos = mapToGlobal( event->pos() );
00308
00309 emit mouseButtonPressed( event->button(), pos, event->state() );
00310
00311
00312 if ( event->button() == RightButton )
00313 emit rightButtonPressed( pos );
00314 }
00315
00316 void KVideoWidget::mouseDoubleClickEvent( QMouseEvent *event )
00317 {
00318 emit mouseButtonDoubleClick( mapToGlobal( event->pos() ), event->state() );
00319 }
00320
00321 void KVideoWidget::resizeEvent( QResizeEvent *event )
00322 {
00323 QWidget::resizeEvent( event );
00324
00325 if (width() > minimumWidth() || height() > minimumHeight())
00326 {
00327 if (width() == QMAX( (videoWidth / 2), minimumWidth() ) &&
00328 height() == QMAX( (videoHeight / 2), minimumHeight() ))
00329 ((KToggleAction *)action( "half_size" ))->setChecked( true );
00330 else if (width() == QMAX( videoWidth, minimumWidth() ) &&
00331 height() == QMAX( videoHeight, minimumHeight() ))
00332 ((KToggleAction *)action( "normal_size" ))->setChecked( true );
00333 else if (width() == QMAX( (2 * videoWidth), minimumWidth() ) &&
00334 height() == QMAX( (2 * videoHeight), minimumHeight() ))
00335 ((KToggleAction *)action( "double_size" ))->setChecked( true );
00336 else
00337 {
00338 ((KToggleAction *)action( "half_size" ))->setChecked( false );
00339 ((KToggleAction *)action( "normal_size" ))->setChecked( false );
00340 ((KToggleAction *)action( "double_size" ))->setChecked( false );
00341 }
00342 }
00343 }
00344
00345 void KVideoWidget::resizeNotify( int width, int height )
00346 {
00347 if(!isEmbedded()) return;
00348
00349 videoWidth = width;
00350 videoHeight = height;
00351
00352 if (isHalfSize())
00353 emit adaptSize( (videoWidth / 2), (videoHeight / 2) );
00354 else if (isNormalSize())
00355 emit adaptSize( videoWidth, videoHeight );
00356 else if (isDoubleSize())
00357 emit adaptSize( (2 * videoWidth), (2 * videoHeight) );
00358
00359 updateGeometry();
00360 }
00361
00362 bool KVideoWidget::x11Event( XEvent *event )
00363 {
00364 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00365 if (event->type == ClientMessage &&
00366 event->xclient.message_type ==
00367 XInternAtom( qt_xdisplay(), "VPO_RESIZE_NOTIFY", False ))
00368 {
00369 resizeNotify( event->xclient.data.l[0], event->xclient.data.l[1] );
00370 }
00371 #endif
00372 return false;
00373 }
00374
00375 void KVideoWidget::fullscreenActivated()
00376 {
00377 if (isFullscreen() == (fullscreenWidget != 0))
00378 return;
00379
00380 if (isFullscreen())
00381 {
00382 fullscreenWidget = new KFullscreenVideoWidget( this );
00383
00384
00385 connect( fullscreenWidget, SIGNAL(mouseButtonPressed( int, const QPoint &, int )),
00386 this, SIGNAL(mouseButtonPressed( int, const QPoint &, int)) );
00387
00388 connect( fullscreenWidget, SIGNAL(mouseButtonDoubleClick( const QPoint &, int )),
00389 this, SIGNAL(mouseButtonDoubleClick( const QPoint &, int )) );
00390
00391
00392 connect( fullscreenWidget, SIGNAL(rightButtonPressed(const QPoint &)),
00393 this, SIGNAL(rightButtonPressed(const QPoint &)) );
00394
00395
00396 QAccel *a = new QAccel( fullscreenWidget );
00397 a->connectItem( a->insertItem( Key_Escape ),
00398 this, SLOT(setWindowed()) );
00399
00400 fullscreenWidget->showFullScreen();
00401 fullscreenWidget->setFocus();
00402
00403 if (isEmbedded())
00404 {
00405 poVideo.x11WindowId( fullscreenWidget->winId() );
00406 fullscreenWidget->setBackgroundMode( NoBackground );
00407 }
00408 }
00409 else
00410 {
00411 if (isEmbedded())
00412 {
00413 poVideo.x11WindowId( winId() );
00414 setBackgroundMode( NoBackground );
00415 }
00416
00417 delete fullscreenWidget;
00418 fullscreenWidget = 0;
00419 }
00420 }
00421
00422 void KVideoWidget::halfSizeActivated()
00423 {
00424 if (isHalfSize())
00425 {
00426 if(isEmbedded()) emit adaptSize( (videoWidth / 2), (videoHeight / 2) );
00427 setWindowed();
00428 }
00429 }
00430
00431 void KVideoWidget::normalSizeActivated()
00432 {
00433 if (isNormalSize())
00434 {
00435 if(isEmbedded()) emit adaptSize( videoWidth, videoHeight );
00436 setWindowed();
00437 }
00438 }
00439
00440 void KVideoWidget::doubleSizeActivated()
00441 {
00442 if (isDoubleSize())
00443 {
00444 if(isEmbedded()) emit adaptSize( (2 * videoWidth), (2 * videoHeight) );
00445 setWindowed();
00446 }
00447 }
00448
00449 #include "kvideowidget.moc"