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>
50 # include <X11/Xlib.h>
51 # include <qx11info_x11.h>
52 static void videoSync( void )
54 /* Make sure the X server has processed all requests.
55 * This protects other threads using distinct connections from getting
56 * the video widget window in an inconsistent states. */
57 XSync( QX11Info::display(), False );
60 # define videoSync() (void)0
66 class ReparentableWidget : public QWidget
71 ReparentableWidget( VideoWidget *owner ) : owner( owner )
76 void keyPressEvent( QKeyEvent *e )
78 emit owner->keyPressed( e );
82 /**********************************************************************
83 * Video Widget. A simple frame on which video is drawn
84 * This class handles resize issues
85 **********************************************************************/
87 VideoWidget::VideoWidget( intf_thread_t *_p_i )
90 , reparentable( NULL )
92 /* Set the policy to expand in both directions */
93 // setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
95 layout = new QHBoxLayout( this );
96 layout->setContentsMargins( 0, 0, 0, 0 );
100 VideoWidget::~VideoWidget()
102 /* Ensure we are not leaking the video output. This would crash. */
103 assert( reparentable == NULL );
107 * Request the video to avoid the conflicts
109 WId VideoWidget::request( int *pi_x, int *pi_y,
110 unsigned int *pi_width, unsigned int *pi_height,
113 msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
115 if( reparentable != NULL )
117 msg_Dbg( p_intf, "embedded video already in use" );
122 *pi_width = size().width();
123 *pi_height = size().height();
126 /* The Qt4 UI needs a fixed a widget ("this"), so that the parent layout is
127 * not messed up when we the video is reparented. Hence, we create an extra
128 * reparentable widget, that will be within the VideoWidget in windowed
129 * mode, and within the root window (NULL parent) in full-screen mode.
131 reparentable = new ReparentableWidget( this );
132 QLayout *innerLayout = new QHBoxLayout( reparentable );
133 innerLayout->setContentsMargins( 0, 0, 0, 0 );
135 /* The owner of the video window needs a stable handle (WinId). Reparenting
136 * in Qt4-X11 changes the WinId of the widget, so we need to create another
137 * dummy widget that stays within the reparentable widget. */
138 QWidget *stable = new QWidget();
139 QPalette plt = palette();
140 plt.setColor( QPalette::Window, Qt::black );
141 stable->setPalette( plt );
142 stable->setAutoFillBackground(true);
143 /* Indicates that the widget wants to draw directly onto the screen.
144 Widgets with this attribute set do not participate in composition
146 stable->setAttribute( Qt::WA_PaintOnScreen, true );
148 innerLayout->addWidget( stable );
150 layout->addWidget( reparentable );
153 /* HACK: Only one X11 client can subscribe to mouse button press events.
154 * VLC currently handles those in the video display.
155 * Force Qt4 to unsubscribe from mouse press and release events. */
156 Display *dpy = QX11Info::display();
157 Window w = stable->winId();
158 XWindowAttributes attr;
160 XGetWindowAttributes( dpy, w, &attr );
161 attr.your_event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
162 XSelectInput( dpy, w, attr.your_event_mask );
166 msg_Dbg( p_intf, "embedded video ready (handle %p)",
167 (void *)stable->winId() );
169 return stable->winId();
172 /* Set the Widget to the correct Size */
173 /* Function has to be called by the parent
174 Parent has to care about resizing itself */
175 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
177 if (reparentable->windowState() & Qt::WindowFullScreen )
179 if( !isVisible() ) show();
181 emit sizeChanged( w, h );
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);
271 /**********************************************************************
272 * Background Widget. Show a simple image background. Currently,
273 * it's album art if present or cone.
274 **********************************************************************/
275 #define ICON_SIZE 128
276 #define MAX_BG_SIZE 400
277 #define MIN_BG_SIZE 128
280 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
281 :QWidget( NULL ), p_intf( _p_i )
283 /* We should use that one to take the more size it can */
284 //setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
286 /* A dark background */
287 setAutoFillBackground( true );
288 QPalette plt = palette();
289 plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
290 plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
293 /* A cone in the middle */
295 label->setMargin( MARGIN );
296 label->setAlignment( Qt::AlignCenter );
298 /* Init the cone art */
301 /* Grid, because of the visual selector */
302 QGridLayout *backgroundLayout = new QGridLayout( this );
303 backgroundLayout->setMargin( 0 );
304 backgroundLayout->addWidget( label, 0, 1 );
305 backgroundLayout->setColumnStretch( 0, 1 );
306 backgroundLayout->setColumnStretch( 2, 1 );
308 CONNECT( THEMIM->getIM(), artChanged( QString ),
309 this, updateArt( const QString& ) );
315 void BackgroundWidget::resizeEvent( QResizeEvent * event )
317 if( event->size().height() <= MIN_BG_SIZE + MARGIN * 2 + 2 )
323 void BackgroundWidget::updateArt( const QString& url )
328 if( QDate::currentDate().dayOfYear() >= 354 )
329 label->setPixmap( QPixmap( ":/logo/vlc128-christmas.png" ) );
331 label->setPixmap( QPixmap( ":/logo/vlc128.png" ) );
335 QPixmap pixmap( url );
336 if( pixmap.width() > label->maximumWidth() ||
337 pixmap.height() > label->maximumHeight() )
339 pixmap = pixmap.scaled( label->maximumWidth(),
340 label->maximumHeight(), Qt::KeepAspectRatio );
343 label->setPixmap( pixmap );
347 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
349 QVLCMenu::PopupMenu( p_intf, true );
354 #include <QPushButton>
355 #include <QHBoxLayout>
357 /**********************************************************************
358 * Visualization selector panel
359 **********************************************************************/
360 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
361 QFrame( NULL ), p_intf( _p_i )
363 QHBoxLayout *layout = new QHBoxLayout( this );
364 layout->setMargin( 0 );
365 QPushButton *prevButton = new QPushButton( "Prev" );
366 QPushButton *nextButton = new QPushButton( "Next" );
367 layout->addWidget( prevButton );
368 layout->addWidget( nextButton );
370 layout->addStretch( 10 );
371 layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
373 current = new QLabel( qtr( "None" ) );
374 layout->addWidget( current );
376 BUTTONACT( prevButton, prev() );
377 BUTTONACT( nextButton, next() );
380 setMaximumHeight( 35 );
383 VisualSelector::~VisualSelector()
386 void VisualSelector::prev()
388 char *psz_new = aout_VisualPrev( p_intf );
391 current->setText( qfu( psz_new ) );
396 void VisualSelector::next()
398 char *psz_new = aout_VisualNext( p_intf );
401 current->setText( qfu( psz_new ) );
407 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, QWidget *parent )
408 : QLabel( parent ), p_intf( _p_intf )
410 setToolTip( qtr( "Current playback speed.\nClick to adjust" ) );
412 /* Create the Speed Control Widget */
413 speedControl = new SpeedControlWidget( p_intf, this );
414 speedControlMenu = new QMenu( this );
416 QWidgetAction *widgetAction = new QWidgetAction( speedControl );
417 widgetAction->setDefaultWidget( speedControl );
418 speedControlMenu->addAction( widgetAction );
420 /* Change the SpeedRate in the Status Bar */
421 CONNECT( THEMIM->getIM(), rateChanged( float ), this, setRate( float ) );
423 DCONNECT( THEMIM, inputChanged( input_thread_t * ),
424 speedControl, activateOnState() );
425 setRate( var_InheritFloat( p_intf, "rate" ) );
428 SpeedLabel::~SpeedLabel()
431 delete speedControlMenu;
434 /****************************************************************************
435 * Small right-click menu for rate control
436 ****************************************************************************/
438 void SpeedLabel::showSpeedMenu( QPoint pos )
440 speedControlMenu->exec( QCursor::pos() - pos
441 + QPoint( 0, height() ) );
444 void SpeedLabel::setRate( float rate )
447 str.setNum( rate, 'f', 2 );
451 speedControl->updateControls( rate );
454 /**********************************************************************
455 * Speed control widget
456 **********************************************************************/
457 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
458 : QFrame( _parent ), p_intf( _p_i )
460 QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
461 sizePolicy.setHorizontalStretch( 0 );
462 sizePolicy.setVerticalStretch( 0 );
464 speedSlider = new QSlider( this );
465 speedSlider->setSizePolicy( sizePolicy );
466 speedSlider->setMaximumSize( QSize( 80, 200 ) );
467 speedSlider->setOrientation( Qt::Vertical );
468 speedSlider->setTickPosition( QSlider::TicksRight );
470 speedSlider->setRange( -34, 34 );
471 speedSlider->setSingleStep( 1 );
472 speedSlider->setPageStep( 1 );
473 speedSlider->setTickInterval( 17 );
475 CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
477 QToolButton *normalSpeedButton = new QToolButton( this );
478 normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
479 normalSpeedButton->setAutoRaise( true );
480 normalSpeedButton->setText( "1x" );
481 normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
483 CONNECT( normalSpeedButton, clicked(), this, resetRate() );
485 QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
486 speedControlLayout->setContentsMargins( 4, 4, 4, 4 );
487 speedControlLayout->setSpacing( 4 );
488 speedControlLayout->addWidget( speedSlider );
489 speedControlLayout->addWidget( normalSpeedButton );
494 void SpeedControlWidget::activateOnState()
496 speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
499 void SpeedControlWidget::updateControls( float rate )
501 if( speedSlider->isSliderDown() )
503 //We don't want to change anything if the user is using the slider
507 double value = 17 * log( rate ) / log( 2 );
508 int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
510 if( sliderValue < speedSlider->minimum() )
512 sliderValue = speedSlider->minimum();
514 else if( sliderValue > speedSlider->maximum() )
516 sliderValue = speedSlider->maximum();
519 speedSlider->setValue( sliderValue );
522 void SpeedControlWidget::updateRate( int sliderValue )
524 double speed = pow( 2, (double)sliderValue / 17 );
525 int rate = INPUT_RATE_DEFAULT / speed;
527 THEMIM->getIM()->setRate(rate);
530 void SpeedControlWidget::resetRate()
532 THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
535 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
536 : QLabel( parent ), p_intf( _p_i )
538 setContextMenuPolicy( Qt::ActionsContextMenu );
539 CONNECT( this, updateRequested(), this, askForUpdate() );
541 setMinimumHeight( 128 );
542 setMinimumWidth( 128 );
543 setMaximumHeight( 128 );
544 setMaximumWidth( 128 );
545 setScaledContents( false );
546 setAlignment( Qt::AlignCenter );
548 QList< QAction* > artActions = actions();
549 QAction *action = new QAction( qtr( "Download cover art" ), this );
550 CONNECT( action, triggered(), this, askForUpdate() );
556 CoverArtLabel::~CoverArtLabel()
558 QList< QAction* > artActions = actions();
559 foreach( QAction *act, artActions )
563 void CoverArtLabel::showArtUpdate( const QString& url )
566 if( !url.isEmpty() && pix.load( url ) )
568 pix = pix.scaled( maximumWidth(), maximumHeight(),
569 Qt::KeepAspectRatioByExpanding );
573 pix = QPixmap( ":/noart.png" );
578 void CoverArtLabel::askForUpdate()
580 THEMIM->getIM()->requestArtUpdate();
583 TimeLabel::TimeLabel( intf_thread_t *_p_intf )
584 : QLabel(), p_intf( _p_intf ), bufTimer( new QTimer(this) ),
585 buffering( false ), showBuffering(false), bufVal( -1 )
587 b_remainingTime = false;
588 setText( " --:--/--:-- " );
589 setAlignment( Qt::AlignRight | Qt::AlignVCenter );
590 setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
591 bufTimer->setSingleShot( true );
593 CONNECT( THEMIM->getIM(), positionUpdated( float, int64_t, int ),
594 this, setDisplayPosition( float, int64_t, int ) );
595 CONNECT( THEMIM->getIM(), cachingChanged( float ),
596 this, updateBuffering( float ) );
597 CONNECT( bufTimer, timeout(), this, updateBuffering() );
600 void TimeLabel::setDisplayPosition( float pos, int64_t t, int length )
602 showBuffering = false;
607 setText( " --:--/--:-- " );
611 int time = t / 1000000;
613 secstotimestr( psz_length, length );
614 secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
618 timestr.sprintf( " %s%s/%s ", (b_remainingTime && length) ? "-" : "",
619 psz_time, ( !length && time ) ? "--:--" : psz_length );
623 cachedLength = length;
626 void TimeLabel::setDisplayPosition( float pos )
628 if( pos == -1.f || cachedLength == 0 )
630 setText( " --:--/--:-- " );
634 int time = pos * cachedLength;
635 secstotimestr( psz_time,
636 ( b_remainingTime && cachedLength ?
637 cachedLength - time : time ) );
639 timestr.sprintf( " %s%s/%s ", (b_remainingTime && cachedLength) ? "-" : "",
640 psz_time, ( !cachedLength && time ) ? "--:--" : psz_length );
646 void TimeLabel::toggleTimeDisplay()
648 b_remainingTime = !b_remainingTime;
652 void TimeLabel::updateBuffering( float _buffered )
655 if( !buffering || bufVal == 0 )
657 showBuffering = false;
659 bufTimer->start(200);
661 else if( bufVal == 1 )
663 showBuffering = buffering = false;
669 void TimeLabel::updateBuffering()
671 showBuffering = true;
675 void TimeLabel::paintEvent( QPaintEvent* event )
680 r.setLeft( r.width() * bufVal );
683 p.fillRect( r, palette().color( QPalette::Highlight ) );
685 QLabel::paintEvent( event );