1 /*****************************************************************************
2 * interface_widgets.cpp : Custom widgets for the main interface
3 ****************************************************************************
4 * Copyright (C) 2006-2010 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>
51 # include <X11/Xlib.h>
52 # include <qx11info_x11.h>
53 static void videoSync( void )
55 /* Make sure the X server has processed all requests.
56 * This protects other threads using distinct connections from getting
57 * the video widget window in an inconsistent states. */
58 XSync( QX11Info::display(), False );
61 # define videoSync() (void)0
67 class ReparentableWidget : public QWidget
72 ReparentableWidget( VideoWidget *owner ) : owner( owner )
77 void keyPressEvent( QKeyEvent *e )
79 emit owner->keyPressed( e );
83 /**********************************************************************
84 * Video Widget. A simple frame on which video is drawn
85 * This class handles resize issues
86 **********************************************************************/
88 VideoWidget::VideoWidget( intf_thread_t *_p_i )
91 , reparentable( NULL )
93 /* Set the policy to expand in both directions */
94 // setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
96 layout = new QHBoxLayout( this );
97 layout->setContentsMargins( 0, 0, 0, 0 );
101 VideoWidget::~VideoWidget()
103 /* Ensure we are not leaking the video output. This would crash. */
104 assert( reparentable == NULL );
108 * Request the video to avoid the conflicts
110 WId VideoWidget::request( int *pi_x, int *pi_y,
111 unsigned int *pi_width, unsigned int *pi_height,
114 msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
116 if( reparentable != NULL )
118 msg_Dbg( p_intf, "embedded video already in use" );
123 *pi_width = size().width();
124 *pi_height = size().height();
127 /* The Qt4 UI needs a fixed a widget ("this"), so that the parent layout is
128 * not messed up when we the video is reparented. Hence, we create an extra
129 * reparentable widget, that will be within the VideoWidget in windowed
130 * mode, and within the root window (NULL parent) in full-screen mode.
132 reparentable = new ReparentableWidget( this );
133 QLayout *innerLayout = new QHBoxLayout( reparentable );
134 innerLayout->setContentsMargins( 0, 0, 0, 0 );
136 /* The owner of the video window needs a stable handle (WinId). Reparenting
137 * in Qt4-X11 changes the WinId of the widget, so we need to create another
138 * dummy widget that stays within the reparentable widget. */
139 QWidget *stable = new QWidget();
140 QPalette plt = palette();
141 plt.setColor( QPalette::Window, Qt::black );
142 stable->setPalette( plt );
143 stable->setAutoFillBackground(true);
144 /* Indicates that the widget wants to draw directly onto the screen.
145 Widgets with this attribute set do not participate in composition
147 stable->setAttribute( Qt::WA_PaintOnScreen, true );
149 innerLayout->addWidget( stable );
151 layout->addWidget( reparentable );
154 /* HACK: Only one X11 client can subscribe to mouse button press events.
155 * VLC currently handles those in the video display.
156 * Force Qt4 to unsubscribe from mouse press and release events. */
157 Display *dpy = QX11Info::display();
158 Window w = stable->winId();
159 XWindowAttributes attr;
161 XGetWindowAttributes( dpy, w, &attr );
162 attr.your_event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
163 XSelectInput( dpy, w, attr.your_event_mask );
167 msg_Dbg( p_intf, "embedded video ready (handle %p)",
168 (void *)stable->winId() );
170 return stable->winId();
173 /* Set the Widget to the correct Size */
174 /* Function has to be called by the parent
175 Parent has to care about resizing itself */
176 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
178 if (reparentable->windowState() & Qt::WindowFullScreen )
180 if( !isVisible() ) show();
182 emit sizeChanged( w, h );
186 void VideoWidget::SetFullScreen( bool b_fs )
188 const Qt::WindowStates curstate = reparentable->windowState();
189 Qt::WindowStates newstate = curstate;
190 Qt::WindowFlags newflags = reparentable->windowFlags();
195 newstate |= Qt::WindowFullScreen;
196 newflags |= Qt::WindowStaysOnTopHint;
200 newstate &= ~Qt::WindowFullScreen;
201 newflags &= ~Qt::WindowStaysOnTopHint;
203 if( newstate == curstate )
204 return; /* no changes needed */
207 { /* Go full-screen */
208 int numscreen = var_InheritInteger( p_intf, "qt-fullscreen-screennumber" );
209 /* if user hasn't defined screennumber, or screennumber that is bigger
210 * than current number of screens, take screennumber where current interface
213 if( numscreen == -1 || numscreen > QApplication::desktop()->numScreens() )
214 numscreen = QApplication::desktop()->screenNumber( p_intf->p_sys->p_mi );
216 QRect screenres = QApplication::desktop()->screenGeometry( numscreen );
218 reparentable->setParent( NULL, newflags );
219 reparentable->setWindowState( newstate );
220 /* To be sure window is on proper-screen in xinerama */
221 if( !screenres.contains( reparentable->pos() ) )
223 msg_Dbg( p_intf, "Moving video to correct screen");
224 reparentable->move( QPoint( screenres.x(), screenres.y() ) );
226 reparentable->show();
230 reparentable->setWindowFlags( newflags );
231 reparentable->setWindowState( newstate );
232 layout->addWidget( reparentable );
237 void VideoWidget::release( void )
239 msg_Dbg( p_intf, "Video is not needed anymore" );
240 //layout->removeWidget( reparentable );
243 /* Come back to default thumbnail for Windows 7 taskbar */
244 LPTASKBARLIST3 p_taskbl;
248 if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
249 NULL, CLSCTX_INPROC_SERVER,
251 (void **)&p_taskbl) )
253 p_taskbl->vt->HrInit(p_taskbl);
255 HWND hroot = GetAncestor(reparentable->winId(),GA_ROOT);
257 if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, NULL))
258 msg_Err(p_intf, "SetThumbNailClip failed");
259 msg_Err(p_intf, "Releasing taskbar | root handle = %08x", hroot);
260 p_taskbl->vt->Release(p_taskbl);
272 /**********************************************************************
273 * Background Widget. Show a simple image background. Currently,
274 * it's album art if present or cone.
275 **********************************************************************/
277 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
278 :QWidget( NULL ), p_intf( _p_i ), b_expandPixmap( false )
280 /* A dark background */
281 setAutoFillBackground( true );
282 QPalette plt = palette();
283 plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
284 plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
287 /* Init the cone art */
290 CONNECT( THEMIM->getIM(), artChanged( QString ),
291 this, updateArt( const QString& ) );
294 void BackgroundWidget::updateArt( const QString& url )
296 if ( !url.isEmpty() )
302 if( QDate::currentDate().dayOfYear() >= 354 )
303 pixmapUrl = QString( ":/logo/vlc128-christmas.png" );
305 pixmapUrl = QString( ":/logo/vlc128.png" );
310 void BackgroundWidget::paintEvent( QPaintEvent *e )
312 int i_maxwidth, i_maxheight;
313 QPixmap pixmap = QPixmap( pixmapUrl );
314 QPainter painter(this);
318 i_maxwidth = std::min( maximumWidth(), width() ) - MARGIN * 2;
319 i_maxheight = std::min( maximumHeight(), height() ) - MARGIN * 2;
321 if ( height() > MARGIN * 2 )
323 /* Scale down the pixmap if the widget is too small */
324 if( pixmap.width() > i_maxwidth || pixmap.height() > i_maxheight )
326 pixmap = pixmap.scaled( i_maxwidth, i_maxheight,
327 Qt::KeepAspectRatio, Qt::SmoothTransformation );
330 if ( b_expandPixmap &&
331 pixmap.width() < width() && pixmap.height() < height() )
333 /* Scale up the pixmap to fill widget's size */
334 f_alpha = ( (float) pixmap.height() / (float) height() );
335 pixmap = pixmap.scaled(
336 width() - MARGIN * 2,
337 height() - MARGIN * 2,
339 ( f_alpha < .2 )? /* Don't waste cpu when not visible */
340 Qt::SmoothTransformation:
341 Qt::FastTransformation
343 /* Non agressive alpha compositing when sizing up */
344 pMask = QBitmap( pixmap.width(), pixmap.height() );
345 pMask.fill( QColor::fromRgbF( 1.0, 1.0, 1.0, f_alpha ) );
346 pixmap.setMask( pMask );
350 MARGIN + ( i_maxwidth - pixmap.width() ) /2,
351 MARGIN + ( i_maxheight - pixmap.height() ) /2,
354 QWidget::paintEvent( e );
357 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
359 QVLCMenu::PopupMenu( p_intf, true );
364 #include <QPushButton>
365 #include <QHBoxLayout>
367 /**********************************************************************
368 * Visualization selector panel
369 **********************************************************************/
370 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
371 QFrame( NULL ), p_intf( _p_i )
373 QHBoxLayout *layout = new QHBoxLayout( this );
374 layout->setMargin( 0 );
375 QPushButton *prevButton = new QPushButton( "Prev" );
376 QPushButton *nextButton = new QPushButton( "Next" );
377 layout->addWidget( prevButton );
378 layout->addWidget( nextButton );
380 layout->addStretch( 10 );
381 layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
383 current = new QLabel( qtr( "None" ) );
384 layout->addWidget( current );
386 BUTTONACT( prevButton, prev() );
387 BUTTONACT( nextButton, next() );
390 setMaximumHeight( 35 );
393 VisualSelector::~VisualSelector()
396 void VisualSelector::prev()
398 char *psz_new = aout_VisualPrev( p_intf );
401 current->setText( qfu( psz_new ) );
406 void VisualSelector::next()
408 char *psz_new = aout_VisualNext( p_intf );
411 current->setText( qfu( psz_new ) );
417 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, QWidget *parent )
418 : QLabel( parent ), p_intf( _p_intf )
420 tooltipStringPattern = qtr( "Current playback speed: %1\nClick to adjust" );
422 /* Create the Speed Control Widget */
423 speedControl = new SpeedControlWidget( p_intf, this );
424 speedControlMenu = new QMenu( this );
426 QWidgetAction *widgetAction = new QWidgetAction( speedControl );
427 widgetAction->setDefaultWidget( speedControl );
428 speedControlMenu->addAction( widgetAction );
430 /* Change the SpeedRate in the Status Bar */
431 CONNECT( THEMIM->getIM(), rateChanged( float ), this, setRate( float ) );
433 DCONNECT( THEMIM, inputChanged( input_thread_t * ),
434 speedControl, activateOnState() );
435 setRate( var_InheritFloat( p_intf, "rate" ) );
438 SpeedLabel::~SpeedLabel()
441 delete speedControlMenu;
444 /****************************************************************************
445 * Small right-click menu for rate control
446 ****************************************************************************/
448 void SpeedLabel::showSpeedMenu( QPoint pos )
450 speedControlMenu->exec( QCursor::pos() - pos
451 + QPoint( 0, height() ) );
454 void SpeedLabel::setRate( float rate )
457 str.setNum( rate, 'f', 2 );
460 setToolTip( tooltipStringPattern.arg( str ) );
461 speedControl->updateControls( rate );
464 /**********************************************************************
465 * Speed control widget
466 **********************************************************************/
467 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
468 : QFrame( _parent ), p_intf( _p_i )
470 QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
471 sizePolicy.setHorizontalStretch( 0 );
472 sizePolicy.setVerticalStretch( 0 );
474 speedSlider = new QSlider( this );
475 speedSlider->setSizePolicy( sizePolicy );
476 speedSlider->setMaximumSize( QSize( 80, 200 ) );
477 speedSlider->setOrientation( Qt::Vertical );
478 speedSlider->setTickPosition( QSlider::TicksRight );
480 speedSlider->setRange( -34, 34 );
481 speedSlider->setSingleStep( 1 );
482 speedSlider->setPageStep( 1 );
483 speedSlider->setTickInterval( 17 );
485 CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
487 QToolButton *normalSpeedButton = new QToolButton( this );
488 normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
489 normalSpeedButton->setAutoRaise( true );
490 normalSpeedButton->setText( "1x" );
491 normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
493 CONNECT( normalSpeedButton, clicked(), this, resetRate() );
495 QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
496 speedControlLayout->setContentsMargins( 4, 4, 4, 4 );
497 speedControlLayout->setSpacing( 4 );
498 speedControlLayout->addWidget( speedSlider );
499 speedControlLayout->addWidget( normalSpeedButton );
506 void SpeedControlWidget::activateOnState()
508 speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
511 void SpeedControlWidget::updateControls( float rate )
513 if( speedSlider->isSliderDown() )
515 //We don't want to change anything if the user is using the slider
519 double value = 17 * log( rate ) / log( 2 );
520 int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
522 if( sliderValue < speedSlider->minimum() )
524 sliderValue = speedSlider->minimum();
526 else if( sliderValue > speedSlider->maximum() )
528 sliderValue = speedSlider->maximum();
530 lastValue = sliderValue;
532 speedSlider->setValue( sliderValue );
535 void SpeedControlWidget::updateRate( int sliderValue )
537 if( sliderValue == lastValue )
540 double speed = pow( 2, (double)sliderValue / 17 );
541 int rate = INPUT_RATE_DEFAULT / speed;
543 THEMIM->getIM()->setRate(rate);
546 void SpeedControlWidget::resetRate()
548 THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
551 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
552 : QLabel( parent ), p_intf( _p_i )
554 setContextMenuPolicy( Qt::ActionsContextMenu );
555 CONNECT( this, updateRequested(), this, askForUpdate() );
557 setMinimumHeight( 128 );
558 setMinimumWidth( 128 );
559 setMaximumHeight( 128 );
560 setMaximumWidth( 128 );
561 setScaledContents( false );
562 setAlignment( Qt::AlignCenter );
564 QList< QAction* > artActions = actions();
565 QAction *action = new QAction( qtr( "Download cover art" ), this );
566 CONNECT( action, triggered(), this, askForUpdate() );
572 CoverArtLabel::~CoverArtLabel()
574 QList< QAction* > artActions = actions();
575 foreach( QAction *act, artActions )
579 void CoverArtLabel::showArtUpdate( const QString& url )
582 if( !url.isEmpty() && pix.load( url ) )
584 pix = pix.scaled( maximumWidth(), maximumHeight(),
585 Qt::KeepAspectRatioByExpanding );
589 pix = QPixmap( ":/noart.png" );
594 void CoverArtLabel::askForUpdate()
596 THEMIM->getIM()->requestArtUpdate();
599 TimeLabel::TimeLabel( intf_thread_t *_p_intf )
600 : QLabel(), p_intf( _p_intf ), bufTimer( new QTimer(this) ),
601 buffering( false ), showBuffering(false), bufVal( -1 )
603 b_remainingTime = false;
604 setText( " --:--/--:-- " );
605 setAlignment( Qt::AlignRight | Qt::AlignVCenter );
606 setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
607 bufTimer->setSingleShot( true );
609 CONNECT( THEMIM->getIM(), positionUpdated( float, int64_t, int ),
610 this, setDisplayPosition( float, int64_t, int ) );
611 CONNECT( THEMIM->getIM(), cachingChanged( float ),
612 this, updateBuffering( float ) );
613 CONNECT( bufTimer, timeout(), this, updateBuffering() );
616 void TimeLabel::setDisplayPosition( float pos, int64_t t, int length )
618 showBuffering = false;
623 setText( " --:--/--:-- " );
627 int time = t / 1000000;
629 secstotimestr( psz_length, length );
630 secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
633 QString timestr = QString( " %1%2/%3 " )
634 .arg( QString( (b_remainingTime && length) ? "-" : "" ) )
635 .arg( QString( psz_time ) )
636 .arg( QString( ( !length && time ) ? "--:--" : psz_length ) );
640 cachedLength = length;
643 void TimeLabel::setDisplayPosition( float pos )
645 if( pos == -1.f || cachedLength == 0 )
647 setText( " --:--/--:-- " );
651 int time = pos * cachedLength;
652 secstotimestr( psz_time,
653 ( b_remainingTime && cachedLength ?
654 cachedLength - time : time ) );
655 QString timestr = QString( " %1%2/%3 " )
656 .arg( QString( (b_remainingTime && cachedLength) ? "-" : "" ) )
657 .arg( QString( psz_time ) )
658 .arg( QString( ( !cachedLength && time ) ? "--:--" : psz_length ) );
664 void TimeLabel::toggleTimeDisplay()
666 b_remainingTime = !b_remainingTime;
670 void TimeLabel::updateBuffering( float _buffered )
673 if( !buffering || bufVal == 0 )
675 showBuffering = false;
677 bufTimer->start(200);
679 else if( bufVal == 1 )
681 showBuffering = buffering = false;
687 void TimeLabel::updateBuffering()
689 showBuffering = true;
693 void TimeLabel::paintEvent( QPaintEvent* event )
698 r.setLeft( r.width() * bufVal );
701 p.fillRect( r, palette().color( QPalette::Highlight ) );
703 QLabel::paintEvent( event );