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>
47 # include <X11/Xlib.h>
48 # include <qx11info_x11.h>
49 static void videoSync( void )
51 /* Make sure the X server has processed all requests.
52 * This protects other threads using distinct connections from getting
53 * the video widget window in an inconsistent states. */
54 XSync( QX11Info::display(), False );
57 # define videoSync() (void)0
62 class ReparentableWidget : public QWidget
67 ReparentableWidget( VideoWidget *owner ) : owner( owner )
72 void keyPressEvent( QKeyEvent *e )
74 emit owner->keyPressed( e );
78 /**********************************************************************
79 * Video Widget. A simple frame on which video is drawn
80 * This class handles resize issues
81 **********************************************************************/
83 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
87 videoSize.rwidth() = -1;
88 videoSize.rheight() = -1;
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 reparentable->setLayout( innerLayout );
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 msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
181 videoSize.rwidth() = w;
182 videoSize.rheight() = h;
183 if( !isVisible() ) show();
184 updateGeometry(); // Needed for deinterlace
188 void VideoWidget::SetFullScreen( bool b_fs )
190 const Qt::WindowStates curstate = reparentable->windowState();
191 Qt::WindowStates newstate = curstate;
192 Qt::WindowFlags newflags = reparentable->windowFlags();
197 newstate |= Qt::WindowFullScreen;
198 newflags |= Qt::WindowStaysOnTopHint;
202 newstate &= ~Qt::WindowFullScreen;
203 newflags &= ~Qt::WindowStaysOnTopHint;
205 if( newstate == curstate )
206 return; /* no changes needed */
209 { /* Go full-screen */
210 int numscreen = config_GetInt( p_intf, "qt-fullscreen-screennumber" );
211 /* if user hasn't defined screennumber, or screennumber that is bigger
212 * than current number of screens, take screennumber where current interface
215 if( numscreen == -1 || numscreen > QApplication::desktop()->numScreens() )
216 numscreen = QApplication::desktop()->screenNumber( p_intf->p_sys->p_mi );
218 QRect screenres = QApplication::desktop()->screenGeometry( numscreen );
220 reparentable->setParent( NULL );
221 reparentable->setWindowState( newstate );
222 reparentable->setWindowFlags( newflags );
223 /* To be sure window is on proper-screen in xinerama */
224 if( !screenres.contains( reparentable->pos() ) )
226 msg_Dbg( p_intf, "Moving video to correct screen");
227 reparentable->move( QPoint( screenres.x(), screenres.y() ) );
229 reparentable->show();
233 reparentable->setWindowFlags( newflags );
234 reparentable->setWindowState( newstate );
235 layout->addWidget( reparentable );
240 void VideoWidget::release( void )
242 msg_Dbg( p_intf, "Video is not needed anymore" );
243 //layout->removeWidget( reparentable );
246 /* Come back to default thumbnail for Windows 7 taskbar */
247 LPTASKBARLIST3 p_taskbl;
248 OSVERSIONINFO winVer;
249 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
250 if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 && winVer.dwMajorVersion > 0 )
254 if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
255 NULL, CLSCTX_INPROC_SERVER,
257 (void **)&p_taskbl) )
259 p_taskbl->vt->HrInit(p_taskbl);
261 HWND hroot = GetAncestor(reparentable->winId(),GA_ROOT);
263 if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, NULL))
264 msg_Err(p_intf, "SetThumbNailClip failed");
265 msg_Err(p_intf, "Releasing taskbar | root handle = %08x", hroot);
266 p_taskbl->vt->Release(p_taskbl);
274 videoSize.rwidth() = 0;
275 videoSize.rheight() = 0;
281 QSize VideoWidget::sizeHint() const
286 /**********************************************************************
287 * Background Widget. Show a simple image background. Currently,
288 * it's album art if present or cone.
289 **********************************************************************/
290 #define ICON_SIZE 128
291 #define MAX_BG_SIZE 400
292 #define MIN_BG_SIZE 128
294 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
295 :QWidget( NULL ), p_intf( _p_i )
297 /* We should use that one to take the more size it can */
298 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
300 /* A dark background */
301 setAutoFillBackground( true );
303 plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
304 plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
307 /* A cone in the middle */
309 label->setMargin( 5 );
310 label->setMaximumHeight( MAX_BG_SIZE );
311 label->setMaximumWidth( MAX_BG_SIZE );
312 label->setMinimumHeight( MIN_BG_SIZE );
313 label->setMinimumWidth( MIN_BG_SIZE );
314 label->setAlignment( Qt::AlignCenter );
315 if( QDate::currentDate().dayOfYear() >= 354 )
316 label->setPixmap( QPixmap( ":/logo/vlc128-christmas.png" ) );
318 label->setPixmap( QPixmap( ":/logo/vlc128.png" ) );
320 QGridLayout *backgroundLayout = new QGridLayout( this );
321 backgroundLayout->addWidget( label, 0, 1 );
322 backgroundLayout->setColumnStretch( 0, 1 );
323 backgroundLayout->setColumnStretch( 2, 1 );
325 CONNECT( THEMIM->getIM(), artChanged( QString ),
326 this, updateArt( const QString& ) );
329 BackgroundWidget::~BackgroundWidget()
332 void BackgroundWidget::resizeEvent( QResizeEvent * event )
334 if( event->size().height() <= MIN_BG_SIZE )
340 void BackgroundWidget::updateArt( const QString& url )
344 if( QDate::currentDate().dayOfYear() >= 354 )
345 label->setPixmap( QPixmap( ":/logo/vlc128-christmas.png" ) );
347 label->setPixmap( QPixmap( ":/logo/vlc128.png" ) );
351 QPixmap pixmap( url );
352 if( pixmap.width() > label->maximumWidth() ||
353 pixmap.height() > label->maximumHeight() )
355 pixmap = pixmap.scaled( label->maximumWidth(),
356 label->maximumHeight(), Qt::KeepAspectRatio );
359 label->setPixmap( pixmap );
363 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
365 QVLCMenu::PopupMenu( p_intf, true );
370 #include <QPushButton>
371 #include <QHBoxLayout>
373 /**********************************************************************
374 * Visualization selector panel
375 **********************************************************************/
376 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
377 QFrame( NULL ), p_intf( _p_i )
379 QHBoxLayout *layout = new QHBoxLayout( this );
380 layout->setMargin( 0 );
381 QPushButton *prevButton = new QPushButton( "Prev" );
382 QPushButton *nextButton = new QPushButton( "Next" );
383 layout->addWidget( prevButton );
384 layout->addWidget( nextButton );
386 layout->addStretch( 10 );
387 layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
389 current = new QLabel( qtr( "None" ) );
390 layout->addWidget( current );
392 BUTTONACT( prevButton, prev() );
393 BUTTONACT( nextButton, next() );
396 setMaximumHeight( 35 );
399 VisualSelector::~VisualSelector()
402 void VisualSelector::prev()
404 char *psz_new = aout_VisualPrev( p_intf );
407 current->setText( qfu( psz_new ) );
412 void VisualSelector::next()
414 char *psz_new = aout_VisualNext( p_intf );
417 current->setText( qfu( psz_new ) );
423 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, const QString& text,
425 : QLabel( text, parent ), p_intf( _p_intf )
427 setToolTip( qtr( "Current playback speed.\nClick to adjust" ) );
429 /* Create the Speed Control Widget */
430 speedControl = new SpeedControlWidget( p_intf, this );
431 speedControlMenu = new QMenu( this );
433 QWidgetAction *widgetAction = new QWidgetAction( speedControl );
434 widgetAction->setDefaultWidget( speedControl );
435 speedControlMenu->addAction( widgetAction );
437 /* Change the SpeedRate in the Status Bar */
438 CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
440 CONNECT( THEMIM, inputChanged( input_thread_t * ),
441 speedControl, activateOnState() );
444 SpeedLabel::~SpeedLabel()
447 delete speedControlMenu;
449 /****************************************************************************
450 * Small right-click menu for rate control
451 ****************************************************************************/
452 void SpeedLabel::showSpeedMenu( QPoint pos )
454 speedControlMenu->exec( QCursor::pos() - pos
455 + QPoint( 0, height() ) );
458 void SpeedLabel::setRate( int rate )
461 str.setNum( ( 1000 / (double)rate ), 'f', 2 );
465 speedControl->updateControls( rate );
468 /**********************************************************************
469 * Speed control widget
470 **********************************************************************/
471 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
472 : QFrame( _parent ), p_intf( _p_i )
474 QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
475 sizePolicy.setHorizontalStretch( 0 );
476 sizePolicy.setVerticalStretch( 0 );
478 speedSlider = new QSlider( this );
479 speedSlider->setSizePolicy( sizePolicy );
480 speedSlider->setMaximumSize( QSize( 80, 200 ) );
481 speedSlider->setOrientation( Qt::Vertical );
482 speedSlider->setTickPosition( QSlider::TicksRight );
484 speedSlider->setRange( -34, 34 );
485 speedSlider->setSingleStep( 1 );
486 speedSlider->setPageStep( 1 );
487 speedSlider->setTickInterval( 17 );
489 CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
491 QToolButton *normalSpeedButton = new QToolButton( this );
492 normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
493 normalSpeedButton->setAutoRaise( true );
494 normalSpeedButton->setText( "1x" );
495 normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
497 CONNECT( normalSpeedButton, clicked(), this, resetRate() );
499 QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
500 speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
501 speedControlLayout->setSpacing( 4 );
502 speedControlLayout->addWidget( speedSlider );
503 speedControlLayout->addWidget( normalSpeedButton );
508 void SpeedControlWidget::activateOnState()
510 speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
513 void SpeedControlWidget::updateControls( int rate )
515 if( speedSlider->isSliderDown() )
517 //We don't want to change anything if the user is using the slider
521 double value = 17 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
522 int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
524 if( sliderValue < speedSlider->minimum() )
526 sliderValue = speedSlider->minimum();
528 else if( sliderValue > speedSlider->maximum() )
530 sliderValue = speedSlider->maximum();
533 //Block signals to avoid feedback loop
534 speedSlider->blockSignals( true );
535 speedSlider->setValue( sliderValue );
536 speedSlider->blockSignals( false );
539 void SpeedControlWidget::updateRate( int sliderValue )
541 double speed = pow( 2, (double)sliderValue / 17 );
542 int rate = INPUT_RATE_DEFAULT / speed;
544 THEMIM->getIM()->setRate(rate);
547 void SpeedControlWidget::resetRate()
549 THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
552 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
553 : QLabel( parent ), p_intf( _p_i )
555 setContextMenuPolicy( Qt::ActionsContextMenu );
556 CONNECT( this, updateRequested(), this, askForUpdate() );
558 setMinimumHeight( 128 );
559 setMinimumWidth( 128 );
560 setMaximumHeight( 128 );
561 setMaximumWidth( 128 );
562 setScaledContents( false );
563 setAlignment( Qt::AlignCenter );
565 QList< QAction* > artActions = actions();
566 QAction *action = new QAction( qtr( "Download cover art" ), this );
567 CONNECT( action, triggered(), this, askForUpdate() );
573 CoverArtLabel::~CoverArtLabel()
575 QList< QAction* > artActions = actions();
576 foreach( QAction *act, artActions )
580 void CoverArtLabel::showArtUpdate( const QString& url )
583 if( !url.isEmpty() && pix.load( url ) )
585 pix = pix.scaled( maximumWidth(), maximumHeight(),
586 Qt::KeepAspectRatioByExpanding );
590 pix = QPixmap( ":/noart.png" );
595 void CoverArtLabel::askForUpdate()
597 THEMIM->getIM()->requestArtUpdate();
600 TimeLabel::TimeLabel( intf_thread_t *_p_intf ) :QLabel(), p_intf( _p_intf )
602 b_remainingTime = false;
603 setText( " --:--/--:-- " );
604 setAlignment( Qt::AlignRight | Qt::AlignVCenter );
605 setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
608 CONNECT( THEMIM->getIM(), cachingChanged( float ),
609 this, setCaching( float ) );
610 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
611 this, setDisplayPosition( float, int, int ) );
614 void TimeLabel::setDisplayPosition( float pos, int time, int length )
618 setText( " --:--/--:-- " );
622 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
623 secstotimestr( psz_length, length );
624 secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
628 timestr.sprintf( "%s/%s", psz_time,
629 ( !length && time ) ? "--:--" : psz_length );
631 /* Add a minus to remaining time*/
632 if( b_remainingTime && length ) setText( " -"+timestr+" " );
633 else setText( " "+timestr+" " );
636 void TimeLabel::toggleTimeDisplay()
638 b_remainingTime = !b_remainingTime;
641 void TimeLabel::setCaching( float f_cache )
644 amount.setNum( (int)(100 * f_cache) );
645 msg_Dbg( p_intf, "New caching: %d", (int)(100*f_cache));
646 setText( "Buff: " + amount + "%" );