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>
46 # include <X11/Xlib.h>
47 # include <qx11info_x11.h>
52 /**********************************************************************
53 * Video Widget. A simple frame on which video is drawn
54 * This class handles resize issues
55 **********************************************************************/
57 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
61 videoSize.rwidth() = -1;
62 videoSize.rheight() = -1;
66 /* Set the policy to expand in both directions */
67 // setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
69 /* Black background is more coherent for a Video Widget */
70 QPalette plt = palette();
71 plt.setColor( QPalette::Window, Qt::black );
73 setAutoFillBackground(true);
75 /* Indicates that the widget wants to draw directly onto the screen.
76 Widgets with this attribute set do not participate in composition
78 setAttribute( Qt::WA_PaintOnScreen, true );
80 /* The core can ask through a callback to show the video. */
81 connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
82 this, SLOT(SetSizing(unsigned int, unsigned int )),
83 Qt::BlockingQueuedConnection );
86 void VideoWidget::paintEvent(QPaintEvent *ev)
88 QFrame::paintEvent(ev);
90 XFlush( QX11Info::display() );
94 VideoWidget::~VideoWidget()
96 /* Ensure we are not leaking the video output. This would crash. */
101 * Request the video to avoid the conflicts
103 WId VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
104 unsigned int *pi_width, unsigned int *pi_height,
107 msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
111 *pi_width = size().width();
112 *pi_height = size().height();
115 emit askVideoWidgetToShow( *pi_width, *pi_height );
118 msg_Dbg( p_intf, "embedded video already in use" );
123 msg_Dbg( p_intf, "embedded video ready (handle %p)", (void *)winId() );
128 /* Set the Widget to the correct Size */
129 /* Function has to be called by the parent
130 Parent has to care about resizing himself*/
131 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
133 msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
134 videoSize.rwidth() = w;
135 videoSize.rheight() = h;
136 if( isHidden() ) show();
137 updateGeometry(); // Needed for deinterlace
140 void VideoWidget::release( void )
142 msg_Dbg( p_intf, "Video is not needed anymore" );
144 videoSize.rwidth() = 0;
145 videoSize.rheight() = 0;
150 QSize VideoWidget::sizeHint() const
155 /**********************************************************************
156 * Background Widget. Show a simple image background. Currently,
157 * it's album art if present or cone.
158 **********************************************************************/
159 #define ICON_SIZE 128
160 #define MAX_BG_SIZE 400
161 #define MIN_BG_SIZE 128
163 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
164 :QWidget( NULL ), p_intf( _p_i )
166 /* We should use that one to take the more size it can */
167 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
169 /* A dark background */
170 setAutoFillBackground( true );
172 plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
173 plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
176 /* A cone in the middle */
178 label->setMargin( 5 );
179 label->setMaximumHeight( MAX_BG_SIZE );
180 label->setMaximumWidth( MAX_BG_SIZE );
181 label->setMinimumHeight( MIN_BG_SIZE );
182 label->setMinimumWidth( MIN_BG_SIZE );
183 if( QDate::currentDate().dayOfYear() >= 354 )
184 label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
186 label->setPixmap( QPixmap( ":/vlc128.png" ) );
188 QGridLayout *backgroundLayout = new QGridLayout( this );
189 backgroundLayout->addWidget( label, 0, 1 );
190 backgroundLayout->setColumnStretch( 0, 1 );
191 backgroundLayout->setColumnStretch( 2, 1 );
193 CONNECT( THEMIM->getIM(), artChanged( QString ),
194 this, updateArt( QString ) );
197 BackgroundWidget::~BackgroundWidget()
200 void BackgroundWidget::resizeEvent( QResizeEvent * event )
202 if( event->size().height() <= MIN_BG_SIZE )
208 void BackgroundWidget::updateArt( QString url )
212 if( QDate::currentDate().dayOfYear() >= 354 )
213 label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
215 label->setPixmap( QPixmap( ":/vlc128.png" ) );
219 label->setPixmap( QPixmap( url ) );
223 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
225 QVLCMenu::PopupMenu( p_intf, true );
230 #include <QPushButton>
231 #include <QHBoxLayout>
233 /**********************************************************************
234 * Visualization selector panel
235 **********************************************************************/
236 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
237 QFrame( NULL ), p_intf( _p_i )
239 QHBoxLayout *layout = new QHBoxLayout( this );
240 layout->setMargin( 0 );
241 QPushButton *prevButton = new QPushButton( "Prev" );
242 QPushButton *nextButton = new QPushButton( "Next" );
243 layout->addWidget( prevButton );
244 layout->addWidget( nextButton );
246 layout->addStretch( 10 );
247 layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
249 current = new QLabel( qtr( "None" ) );
250 layout->addWidget( current );
252 BUTTONACT( prevButton, prev() );
253 BUTTONACT( nextButton, next() );
256 setMaximumHeight( 35 );
259 VisualSelector::~VisualSelector()
262 void VisualSelector::prev()
264 char *psz_new = aout_VisualPrev( p_intf );
267 current->setText( qfu( psz_new ) );
272 void VisualSelector::next()
274 char *psz_new = aout_VisualNext( p_intf );
277 current->setText( qfu( psz_new ) );
283 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, const QString& text,
285 : QLabel( text, parent ), p_intf( _p_intf )
287 setToolTip( qtr( "Current playback speed.\nRight click to adjust" ) );
288 setContextMenuPolicy ( Qt::CustomContextMenu );
290 /* Create the Speed Control Widget */
291 speedControl = new SpeedControlWidget( p_intf, this );
292 speedControlMenu = new QMenu( this );
294 QWidgetAction *widgetAction = new QWidgetAction( speedControl );
295 widgetAction->setDefaultWidget( speedControl );
296 speedControlMenu->addAction( widgetAction );
298 /* Speed Label behaviour:
299 - right click gives the vertical speed slider */
300 CONNECT( this, customContextMenuRequested( QPoint ),
301 this, showSpeedMenu( QPoint ) );
303 /* Change the SpeedRate in the Status Bar */
304 CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
306 CONNECT( THEMIM, inputChanged( input_thread_t * ),
307 speedControl, activateOnState() );
310 /****************************************************************************
311 * Small right-click menu for rate control
312 ****************************************************************************/
313 void SpeedLabel::showSpeedMenu( QPoint pos )
315 speedControlMenu->exec( QCursor::pos() - pos
316 + QPoint( 0, height() ) );
319 void SpeedLabel::setRate( int rate )
322 str.setNum( ( 1000 / (double)rate ), 'f', 2 );
326 speedControl->updateControls( rate );
329 /**********************************************************************
330 * Speed control widget
331 **********************************************************************/
332 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
333 : QFrame( _parent ), p_intf( _p_i )
335 QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
336 sizePolicy.setHorizontalStretch( 0 );
337 sizePolicy.setVerticalStretch( 0 );
339 speedSlider = new QSlider( this );
340 speedSlider->setSizePolicy( sizePolicy );
341 speedSlider->setMaximumSize( QSize( 80, 200 ) );
342 speedSlider->setOrientation( Qt::Vertical );
343 speedSlider->setTickPosition( QSlider::TicksRight );
345 speedSlider->setRange( -34, 34 );
346 speedSlider->setSingleStep( 1 );
347 speedSlider->setPageStep( 1 );
348 speedSlider->setTickInterval( 17 );
350 CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
352 QToolButton *normalSpeedButton = new QToolButton( this );
353 normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
354 normalSpeedButton->setAutoRaise( true );
355 normalSpeedButton->setText( "1x" );
356 normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
358 CONNECT( normalSpeedButton, clicked(), this, resetRate() );
360 QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
361 speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
362 speedControlLayout->setSpacing( 4 );
363 speedControlLayout->addWidget( speedSlider );
364 speedControlLayout->addWidget( normalSpeedButton );
369 void SpeedControlWidget::activateOnState()
371 speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
374 void SpeedControlWidget::updateControls( int rate )
376 if( speedSlider->isSliderDown() )
378 //We don't want to change anything if the user is using the slider
382 double value = 17 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
383 int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
385 if( sliderValue < speedSlider->minimum() )
387 sliderValue = speedSlider->minimum();
389 else if( sliderValue > speedSlider->maximum() )
391 sliderValue = speedSlider->maximum();
394 //Block signals to avoid feedback loop
395 speedSlider->blockSignals( true );
396 speedSlider->setValue( sliderValue );
397 speedSlider->blockSignals( false );
400 void SpeedControlWidget::updateRate( int sliderValue )
402 double speed = pow( 2, (double)sliderValue / 17 );
403 int rate = INPUT_RATE_DEFAULT / speed;
405 THEMIM->getIM()->setRate(rate);
408 void SpeedControlWidget::resetRate()
410 THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
413 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
414 : QLabel( parent ), p_intf( _p_i )
416 setContextMenuPolicy( Qt::ActionsContextMenu );
417 CONNECT( this, updateRequested(), this, doUpdate() );
418 CONNECT( THEMIM->getIM(), artChanged( QString ),
419 this, doUpdate( QString ) );
421 setMinimumHeight( 128 );
422 setMinimumWidth( 128 );
423 setMaximumHeight( 128 );
424 setMaximumWidth( 128 );
425 setScaledContents( true );
426 QList< QAction* > artActions = actions();
427 QAction *action = new QAction( qtr( "Download cover art" ), this );
429 CONNECT( action, triggered(), this, doUpdate() );
434 CoverArtLabel::~CoverArtLabel()
436 QList< QAction* > artActions = actions();
437 foreach( QAction *act, artActions )
441 void CoverArtLabel::doUpdate( QString url )
444 if( !url.isEmpty() && pix.load( url ) )
450 setPixmap( QPixmap( ":/noart.png" ) );
454 void CoverArtLabel::doUpdate()
456 THEMIM->getIM()->requestArtUpdate();
459 TimeLabel::TimeLabel( intf_thread_t *_p_intf ) :QLabel(), p_intf( _p_intf )
461 b_remainingTime = false;
462 setText( " --:--/--:-- " );
463 setAlignment( Qt::AlignRight | Qt::AlignVCenter );
464 setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
467 CONNECT( THEMIM->getIM(), cachingChanged( float ),
468 this, setCaching( float ) );
469 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
470 this, setDisplayPosition( float, int, int ) );
473 void TimeLabel::setDisplayPosition( float pos, int time, int length )
477 setText( " --:--/--:-- " );
481 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
482 secstotimestr( psz_length, length );
483 secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
487 timestr.sprintf( "%s/%s", psz_time,
488 ( !length && time ) ? "--:--" : psz_length );
490 /* Add a minus to remaining time*/
491 if( b_remainingTime && length ) setText( " -"+timestr+" " );
492 else setText( " "+timestr+" " );
495 void TimeLabel::toggleTimeDisplay()
497 b_remainingTime = !b_remainingTime;
500 void TimeLabel::setCaching( float f_cache )
503 amount.setNum( (int)(100 * f_cache) );
504 msg_Dbg( p_intf, "New caching: %d", (int)(100*f_cache));
505 setText( "Buffering " + amount + "%" );