1 /*****************************************************************************
2 * interface_widgets.cpp : Custom widgets for the main interface
3 ****************************************************************************
4 * Copyright (C) 2006-2008 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Jean-Baptiste Kempf <jb@videolan.org>
9 * Rafaël Carré <funman@videolanorg>
10 * Ilkka Ollakka <ileoo@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * ( at your option ) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 #include "components/interface_widgets.hpp"
33 #include "menus.hpp" /* Popup menu on bgWidget */
38 #include <QToolButton>
40 #include <QResizeEvent>
43 #include <QWidgetAction>
44 #include <QDesktopWidget>
49 # include <X11/Xlib.h>
50 # include <qx11info_x11.h>
51 static void videoSync( void )
53 /* Make sure the X server has processed all requests.
54 * This protects other threads using distinct connections from getting
55 * the video widget window in an inconsistent states. */
56 XSync( QX11Info::display(), False );
59 # define videoSync() (void)0
64 class ReparentableWidget : public QWidget
69 ReparentableWidget( VideoWidget *owner ) : owner( owner )
74 void keyPressEvent( QKeyEvent *e )
76 emit owner->keyPressed( e );
80 /**********************************************************************
81 * Video Widget. A simple frame on which video is drawn
82 * This class handles resize issues
83 **********************************************************************/
85 VideoWidget::VideoWidget( intf_thread_t *_p_i )
88 , reparentable( NULL )
90 /* Set the policy to expand in both directions */
91 // setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
93 layout = new QHBoxLayout( this );
94 layout->setContentsMargins( 0, 0, 0, 0 );
98 VideoWidget::~VideoWidget()
100 /* Ensure we are not leaking the video output. This would crash. */
101 assert( reparentable == NULL );
105 * Request the video to avoid the conflicts
107 WId VideoWidget::request( int *pi_x, int *pi_y,
108 unsigned int *pi_width, unsigned int *pi_height,
111 msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
113 if( reparentable != NULL )
115 msg_Dbg( p_intf, "embedded video already in use" );
120 *pi_width = size().width();
121 *pi_height = size().height();
124 /* The Qt4 UI needs a fixed a widget ("this"), so that the parent layout is
125 * not messed up when we the video is reparented. Hence, we create an extra
126 * reparentable widget, that will be within the VideoWidget in windowed
127 * mode, and within the root window (NULL parent) in full-screen mode.
129 reparentable = new ReparentableWidget( this );
130 QLayout *innerLayout = new QHBoxLayout( reparentable );
131 innerLayout->setContentsMargins( 0, 0, 0, 0 );
133 /* The owner of the video window needs a stable handle (WinId). Reparenting
134 * in Qt4-X11 changes the WinId of the widget, so we need to create another
135 * dummy widget that stays within the reparentable widget. */
136 QWidget *stable = new QWidget();
137 QPalette plt = palette();
138 plt.setColor( QPalette::Window, Qt::black );
139 stable->setPalette( plt );
140 stable->setAutoFillBackground(true);
141 /* Indicates that the widget wants to draw directly onto the screen.
142 Widgets with this attribute set do not participate in composition
144 stable->setAttribute( Qt::WA_PaintOnScreen, true );
146 innerLayout->addWidget( stable );
148 layout->addWidget( reparentable );
151 /* HACK: Only one X11 client can subscribe to mouse button press events.
152 * VLC currently handles those in the video display.
153 * Force Qt4 to unsubscribe from mouse press and release events. */
154 Display *dpy = QX11Info::display();
155 Window w = stable->winId();
156 XWindowAttributes attr;
158 XGetWindowAttributes( dpy, w, &attr );
159 attr.your_event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
160 XSelectInput( dpy, w, attr.your_event_mask );
164 msg_Dbg( p_intf, "embedded video ready (handle %p)",
165 (void *)stable->winId() );
167 return stable->winId();
170 /* Set the Widget to the correct Size */
171 /* Function has to be called by the parent
172 Parent has to care about resizing itself */
173 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
175 if (reparentable->windowState() & Qt::WindowFullScreen )
177 msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
178 videoSize.setWidth( w );
179 videoSize.setHeight( h );
180 if( !isVisible() ) show();
181 updateGeometry(); // Needed for deinterlace
185 void VideoWidget::SetFullScreen( bool b_fs )
187 const Qt::WindowStates curstate = reparentable->windowState();
188 Qt::WindowStates newstate = curstate;
189 Qt::WindowFlags newflags = reparentable->windowFlags();
194 newstate |= Qt::WindowFullScreen;
195 newflags |= Qt::WindowStaysOnTopHint;
199 newstate &= ~Qt::WindowFullScreen;
200 newflags &= ~Qt::WindowStaysOnTopHint;
202 if( newstate == curstate )
203 return; /* no changes needed */
206 { /* Go full-screen */
207 int numscreen = var_InheritInteger( p_intf, "qt-fullscreen-screennumber" );
208 /* if user hasn't defined screennumber, or screennumber that is bigger
209 * than current number of screens, take screennumber where current interface
212 if( numscreen == -1 || numscreen > QApplication::desktop()->numScreens() )
213 numscreen = QApplication::desktop()->screenNumber( p_intf->p_sys->p_mi );
215 QRect screenres = QApplication::desktop()->screenGeometry( numscreen );
217 reparentable->setParent( NULL, newflags );
218 reparentable->setWindowState( newstate );
219 /* To be sure window is on proper-screen in xinerama */
220 if( !screenres.contains( reparentable->pos() ) )
222 msg_Dbg( p_intf, "Moving video to correct screen");
223 reparentable->move( QPoint( screenres.x(), screenres.y() ) );
225 reparentable->show();
229 reparentable->setWindowFlags( newflags );
230 reparentable->setWindowState( newstate );
231 layout->addWidget( reparentable );
236 void VideoWidget::release( void )
238 msg_Dbg( p_intf, "Video is not needed anymore" );
239 //layout->removeWidget( reparentable );
242 /* Come back to default thumbnail for Windows 7 taskbar */
243 LPTASKBARLIST3 p_taskbl;
247 if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
248 NULL, CLSCTX_INPROC_SERVER,
250 (void **)&p_taskbl) )
252 p_taskbl->vt->HrInit(p_taskbl);
254 HWND hroot = GetAncestor(reparentable->winId(),GA_ROOT);
256 if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, NULL))
257 msg_Err(p_intf, "SetThumbNailClip failed");
258 msg_Err(p_intf, "Releasing taskbar | root handle = %08x", hroot);
259 p_taskbl->vt->Release(p_taskbl);
273 QSize VideoWidget::sizeHint() const
278 /**********************************************************************
279 * Background Widget. Show a simple image background. Currently,
280 * it's album art if present or cone.
281 **********************************************************************/
282 #define ICON_SIZE 128
283 #define MAX_BG_SIZE 400
284 #define MIN_BG_SIZE 128
286 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
287 :QWidget( NULL ), p_intf( _p_i )
289 /* We should use that one to take the more size it can */
290 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
292 /* A dark background */
293 setAutoFillBackground( true );
294 QPalette plt = palette();
295 plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
296 plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
299 /* A cone in the middle */
301 label->setMargin( 5 );
302 /* label->setMaximumHeight( MAX_BG_SIZE );
303 label->setMaximumWidth( MAX_BG_SIZE );
304 label->setMinimumHeight( MIN_BG_SIZE );
305 label->setMinimumWidth( MIN_BG_SIZE );*/
306 label->setAlignment( Qt::AlignCenter );
307 if( QDate::currentDate().dayOfYear() >= 354 )
308 label->setPixmap( QPixmap( ":/logo/vlc128-christmas.png" ) );
310 label->setPixmap( QPixmap( ":/logo/vlc128.png" ) );
312 QGridLayout *backgroundLayout = new QGridLayout( this );
313 backgroundLayout->addWidget( label, 0, 1 );
314 backgroundLayout->setColumnStretch( 0, 1 );
315 backgroundLayout->setColumnStretch( 2, 1 );
317 CONNECT( THEMIM->getIM(), artChanged( QString ),
318 this, updateArt( const QString& ) );
321 BackgroundWidget::~BackgroundWidget()
324 void BackgroundWidget::resizeEvent( QResizeEvent * event )
326 if( event->size().height() <= MIN_BG_SIZE )
332 void BackgroundWidget::updateArt( const QString& url )
336 if( QDate::currentDate().dayOfYear() >= 354 )
337 label->setPixmap( QPixmap( ":/logo/vlc128-christmas.png" ) );
339 label->setPixmap( QPixmap( ":/logo/vlc128.png" ) );
343 QPixmap pixmap( url );
344 if( pixmap.width() > label->maximumWidth() ||
345 pixmap.height() > label->maximumHeight() )
347 pixmap = pixmap.scaled( label->maximumWidth(),
348 label->maximumHeight(), Qt::KeepAspectRatio );
351 label->setPixmap( pixmap );
355 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
357 QVLCMenu::PopupMenu( p_intf, true );
362 #include <QPushButton>
363 #include <QHBoxLayout>
365 /**********************************************************************
366 * Visualization selector panel
367 **********************************************************************/
368 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
369 QFrame( NULL ), p_intf( _p_i )
371 QHBoxLayout *layout = new QHBoxLayout( this );
372 layout->setMargin( 0 );
373 QPushButton *prevButton = new QPushButton( "Prev" );
374 QPushButton *nextButton = new QPushButton( "Next" );
375 layout->addWidget( prevButton );
376 layout->addWidget( nextButton );
378 layout->addStretch( 10 );
379 layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
381 current = new QLabel( qtr( "None" ) );
382 layout->addWidget( current );
384 BUTTONACT( prevButton, prev() );
385 BUTTONACT( nextButton, next() );
388 setMaximumHeight( 35 );
391 VisualSelector::~VisualSelector()
394 void VisualSelector::prev()
396 char *psz_new = aout_VisualPrev( p_intf );
399 current->setText( qfu( psz_new ) );
404 void VisualSelector::next()
406 char *psz_new = aout_VisualNext( p_intf );
409 current->setText( qfu( psz_new ) );
415 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, QWidget *parent )
416 : QLabel( parent ), p_intf( _p_intf )
418 setToolTip( qtr( "Current playback speed.\nClick to adjust" ) );
420 /* Create the Speed Control Widget */
421 speedControl = new SpeedControlWidget( p_intf, this );
422 speedControlMenu = new QMenu( this );
424 QWidgetAction *widgetAction = new QWidgetAction( speedControl );
425 widgetAction->setDefaultWidget( speedControl );
426 speedControlMenu->addAction( widgetAction );
428 /* Change the SpeedRate in the Status Bar */
429 CONNECT( THEMIM->getIM(), rateChanged( float ), this, setRate( float ) );
431 DCONNECT( THEMIM, inputChanged( input_thread_t * ),
432 speedControl, activateOnState() );
433 setRate( var_InheritFloat( p_intf, "rate" ) );
436 SpeedLabel::~SpeedLabel()
439 delete speedControlMenu;
442 /****************************************************************************
443 * Small right-click menu for rate control
444 ****************************************************************************/
446 void SpeedLabel::showSpeedMenu( QPoint pos )
448 speedControlMenu->exec( QCursor::pos() - pos
449 + QPoint( 0, height() ) );
452 void SpeedLabel::setRate( float rate )
455 str.setNum( rate, 'f', 2 );
459 speedControl->updateControls( rate );
462 /**********************************************************************
463 * Speed control widget
464 **********************************************************************/
465 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
466 : QFrame( _parent ), p_intf( _p_i )
468 QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
469 sizePolicy.setHorizontalStretch( 0 );
470 sizePolicy.setVerticalStretch( 0 );
472 speedSlider = new QSlider( this );
473 speedSlider->setSizePolicy( sizePolicy );
474 speedSlider->setMaximumSize( QSize( 80, 200 ) );
475 speedSlider->setOrientation( Qt::Vertical );
476 speedSlider->setTickPosition( QSlider::TicksRight );
478 speedSlider->setRange( -34, 34 );
479 speedSlider->setSingleStep( 1 );
480 speedSlider->setPageStep( 1 );
481 speedSlider->setTickInterval( 17 );
483 CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
485 QToolButton *normalSpeedButton = new QToolButton( this );
486 normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
487 normalSpeedButton->setAutoRaise( true );
488 normalSpeedButton->setText( "1x" );
489 normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
491 CONNECT( normalSpeedButton, clicked(), this, resetRate() );
493 QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
494 speedControlLayout->setContentsMargins( 4, 4, 4, 4 );
495 speedControlLayout->setSpacing( 4 );
496 speedControlLayout->addWidget( speedSlider );
497 speedControlLayout->addWidget( normalSpeedButton );
502 void SpeedControlWidget::activateOnState()
504 speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
507 void SpeedControlWidget::updateControls( float rate )
509 if( speedSlider->isSliderDown() )
511 //We don't want to change anything if the user is using the slider
515 double value = 17 * log( rate ) / log( 2 );
516 int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
518 if( sliderValue < speedSlider->minimum() )
520 sliderValue = speedSlider->minimum();
522 else if( sliderValue > speedSlider->maximum() )
524 sliderValue = speedSlider->maximum();
527 speedSlider->setValue( sliderValue );
530 void SpeedControlWidget::updateRate( int sliderValue )
532 double speed = pow( 2, (double)sliderValue / 17 );
533 int rate = INPUT_RATE_DEFAULT / speed;
535 THEMIM->getIM()->setRate(rate);
538 void SpeedControlWidget::resetRate()
540 THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
543 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
544 : QLabel( parent ), p_intf( _p_i )
546 setContextMenuPolicy( Qt::ActionsContextMenu );
547 CONNECT( this, updateRequested(), this, askForUpdate() );
549 setMinimumHeight( 128 );
550 setMinimumWidth( 128 );
551 setMaximumHeight( 128 );
552 setMaximumWidth( 128 );
553 setScaledContents( false );
554 setAlignment( Qt::AlignCenter );
556 QList< QAction* > artActions = actions();
557 QAction *action = new QAction( qtr( "Download cover art" ), this );
558 CONNECT( action, triggered(), this, askForUpdate() );
564 CoverArtLabel::~CoverArtLabel()
566 QList< QAction* > artActions = actions();
567 foreach( QAction *act, artActions )
571 void CoverArtLabel::showArtUpdate( const QString& url )
574 if( !url.isEmpty() && pix.load( url ) )
576 pix = pix.scaled( maximumWidth(), maximumHeight(),
577 Qt::KeepAspectRatioByExpanding );
581 pix = QPixmap( ":/noart.png" );
586 void CoverArtLabel::askForUpdate()
588 THEMIM->getIM()->requestArtUpdate();
591 TimeLabel::TimeLabel( intf_thread_t *_p_intf )
592 : QLabel(), p_intf( _p_intf ), bufTimer( new QTimer(this) ),
593 buffering( false ), showBuffering(false), bufVal( -1 )
595 b_remainingTime = false;
596 setText( " --:--/--:-- " );
597 setAlignment( Qt::AlignRight | Qt::AlignVCenter );
598 setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
599 bufTimer->setSingleShot( true );
601 CONNECT( THEMIM->getIM(), positionUpdated( float, int64_t, int ),
602 this, setDisplayPosition( float, int64_t, int ) );
603 CONNECT( THEMIM->getIM(), cachingChanged( float ),
604 this, updateBuffering( float ) );
605 CONNECT( bufTimer, timeout(), this, updateBuffering() );
608 void TimeLabel::setDisplayPosition( float pos, int64_t t, int length )
610 showBuffering = false;
615 setText( " --:--/--:-- " );
619 int time = t / 1000000;
621 secstotimestr( psz_length, length );
622 secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
626 timestr.sprintf( " %s%s/%s ", (b_remainingTime && length) ? "-" : "",
627 psz_time, ( !length && time ) ? "--:--" : psz_length );
631 cachedLength = length;
634 void TimeLabel::setDisplayPosition( float pos )
636 if( pos == -1.f || cachedLength == 0 )
638 setText( " --:--/--:-- " );
642 int time = pos * cachedLength;
643 secstotimestr( psz_time,
644 ( b_remainingTime && cachedLength ?
645 cachedLength - time : time ) );
647 timestr.sprintf( " %s%s/%s ", (b_remainingTime && cachedLength) ? "-" : "",
648 psz_time, ( !cachedLength && time ) ? "--:--" : psz_length );
654 void TimeLabel::toggleTimeDisplay()
656 b_remainingTime = !b_remainingTime;
660 void TimeLabel::updateBuffering( float _buffered )
663 if( !buffering || bufVal == 0 )
665 showBuffering = false;
667 bufTimer->start(200);
669 else if( bufVal == 1 )
671 showBuffering = buffering = false;
677 void TimeLabel::updateBuffering()
679 showBuffering = true;
683 void TimeLabel::paintEvent( QPaintEvent* event )
688 r.setLeft( r.width() * bufVal );
691 p.fillRect( r, palette().color( QPalette::Highlight ) );
693 QLabel::paintEvent( event );