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 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
104 unsigned int *pi_width, unsigned int *pi_height )
106 msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
107 emit askVideoWidgetToShow( *pi_width, *pi_height );
110 msg_Dbg( p_intf, "embedded video already in use" );
115 msg_Dbg( p_intf, "embedded video ready (handle %p)", winId() );
117 return ( void* )winId();
120 /* Set the Widget to the correct Size */
121 /* Function has to be called by the parent
122 Parent has to care about resizing himself*/
123 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
125 msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
126 videoSize.rwidth() = w;
127 videoSize.rheight() = h;
128 if( isHidden() ) show();
129 updateGeometry(); // Needed for deinterlace
132 void VideoWidget::release( void )
134 msg_Dbg( p_intf, "Video is not needed anymore" );
136 videoSize.rwidth() = 0;
137 videoSize.rheight() = 0;
142 QSize VideoWidget::sizeHint() const
147 /**********************************************************************
148 * Background Widget. Show a simple image background. Currently,
149 * it's album art if present or cone.
150 **********************************************************************/
151 #define ICON_SIZE 128
152 #define MAX_BG_SIZE 400
153 #define MIN_BG_SIZE 128
155 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
156 :QWidget( NULL ), p_intf( _p_i )
158 /* We should use that one to take the more size it can */
159 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
161 /* A dark background */
162 setAutoFillBackground( true );
164 plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
165 plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
168 /* A cone in the middle */
170 label->setMargin( 5 );
171 label->setMaximumHeight( MAX_BG_SIZE );
172 label->setMaximumWidth( MAX_BG_SIZE );
173 label->setMinimumHeight( MIN_BG_SIZE );
174 label->setMinimumWidth( MIN_BG_SIZE );
175 if( QDate::currentDate().dayOfYear() >= 354 )
176 label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
178 label->setPixmap( QPixmap( ":/vlc128.png" ) );
180 QGridLayout *backgroundLayout = new QGridLayout( this );
181 backgroundLayout->addWidget( label, 0, 1 );
182 backgroundLayout->setColumnStretch( 0, 1 );
183 backgroundLayout->setColumnStretch( 2, 1 );
185 CONNECT( THEMIM->getIM(), artChanged( QString ),
186 this, updateArt( QString ) );
189 BackgroundWidget::~BackgroundWidget()
192 void BackgroundWidget::resizeEvent( QResizeEvent * event )
194 if( event->size().height() <= MIN_BG_SIZE )
200 void BackgroundWidget::updateArt( QString url )
204 if( QDate::currentDate().dayOfYear() >= 354 )
205 label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
207 label->setPixmap( QPixmap( ":/vlc128.png" ) );
211 label->setPixmap( QPixmap( url ) );
215 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
217 QVLCMenu::PopupMenu( p_intf, true );
221 #include <QPushButton>
222 #include <QHBoxLayout>
224 /**********************************************************************
225 * Visualization selector panel
226 **********************************************************************/
227 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
228 QFrame( NULL ), p_intf( _p_i )
230 QHBoxLayout *layout = new QHBoxLayout( this );
231 layout->setMargin( 0 );
232 QPushButton *prevButton = new QPushButton( "Prev" );
233 QPushButton *nextButton = new QPushButton( "Next" );
234 layout->addWidget( prevButton );
235 layout->addWidget( nextButton );
237 layout->addStretch( 10 );
238 layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
240 current = new QLabel( qtr( "None" ) );
241 layout->addWidget( current );
243 BUTTONACT( prevButton, prev() );
244 BUTTONACT( nextButton, next() );
247 setMaximumHeight( 35 );
250 VisualSelector::~VisualSelector()
253 void VisualSelector::prev()
255 char *psz_new = aout_VisualPrev( p_intf );
258 current->setText( qfu( psz_new ) );
263 void VisualSelector::next()
265 char *psz_new = aout_VisualNext( p_intf );
268 current->setText( qfu( psz_new ) );
274 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, const QString text )
275 : QLabel( text ), p_intf( _p_intf )
277 setToolTip( qtr( "Current playback speed.\nRight click to adjust" ) );
278 setContextMenuPolicy ( Qt::CustomContextMenu );
280 /* Create the Speed Control Widget */
281 speedControl = new SpeedControlWidget( p_intf, this );
282 speedControlMenu = new QMenu( this );
284 QWidgetAction *widgetAction = new QWidgetAction( speedControl );
285 widgetAction->setDefaultWidget( speedControl );
286 speedControlMenu->addAction( widgetAction );
288 /* Speed Label behaviour:
289 - right click gives the vertical speed slider */
290 CONNECT( this, customContextMenuRequested( QPoint ),
291 this, showSpeedMenu( QPoint ) );
293 /* Change the SpeedRate in the Status Bar */
294 CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
296 CONNECT( THEMIM, inputChanged( input_thread_t * ),
297 speedControl, activateOnState() );
300 /****************************************************************************
301 * Small right-click menu for rate control
302 ****************************************************************************/
303 void SpeedLabel::showSpeedMenu( QPoint pos )
305 speedControlMenu->exec( QCursor::pos() - pos
306 + QPoint( 0, height() ) );
309 void SpeedLabel::setRate( int rate )
312 str.setNum( ( 1000 / (double)rate ), 'f', 2 );
316 speedControl->updateControls( rate );
319 /**********************************************************************
320 * Speed control widget
321 **********************************************************************/
322 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
323 : QFrame( _parent ), p_intf( _p_i )
325 QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
326 sizePolicy.setHorizontalStretch( 0 );
327 sizePolicy.setVerticalStretch( 0 );
329 speedSlider = new QSlider;
330 speedSlider->setSizePolicy( sizePolicy );
331 speedSlider->setMaximumSize( QSize( 80, 200 ) );
332 speedSlider->setOrientation( Qt::Vertical );
333 speedSlider->setTickPosition( QSlider::TicksRight );
335 speedSlider->setRange( -34, 34 );
336 speedSlider->setSingleStep( 1 );
337 speedSlider->setPageStep( 1 );
338 speedSlider->setTickInterval( 17 );
340 CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
342 QToolButton *normalSpeedButton = new QToolButton( this );
343 normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
344 normalSpeedButton->setAutoRaise( true );
345 normalSpeedButton->setText( "1x" );
346 normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
348 CONNECT( normalSpeedButton, clicked(), this, resetRate() );
350 QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
351 speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
352 speedControlLayout->setSpacing( 4 );
353 speedControlLayout->addWidget( speedSlider );
354 speedControlLayout->addWidget( normalSpeedButton );
359 void SpeedControlWidget::activateOnState()
361 speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
364 void SpeedControlWidget::updateControls( int rate )
366 if( speedSlider->isSliderDown() )
368 //We don't want to change anything if the user is using the slider
372 double value = 17 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
373 int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
375 if( sliderValue < speedSlider->minimum() )
377 sliderValue = speedSlider->minimum();
379 else if( sliderValue > speedSlider->maximum() )
381 sliderValue = speedSlider->maximum();
384 //Block signals to avoid feedback loop
385 speedSlider->blockSignals( true );
386 speedSlider->setValue( sliderValue );
387 speedSlider->blockSignals( false );
390 void SpeedControlWidget::updateRate( int sliderValue )
392 double speed = pow( 2, (double)sliderValue / 17 );
393 int rate = INPUT_RATE_DEFAULT / speed;
395 THEMIM->getIM()->setRate(rate);
398 void SpeedControlWidget::resetRate()
400 THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
403 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
404 : QLabel( parent ), p_intf( _p_i )
406 setContextMenuPolicy( Qt::ActionsContextMenu );
407 CONNECT( this, updateRequested(), this, doUpdate() );
408 CONNECT( THEMIM->getIM(), artChanged( QString ),
409 this, doUpdate( QString ) );
411 setMinimumHeight( 128 );
412 setMinimumWidth( 128 );
413 setMaximumHeight( 128 );
414 setMaximumWidth( 128 );
415 setScaledContents( true );
416 QList< QAction* > artActions = actions();
417 QAction *action = new QAction( qtr( "Download cover art" ), this );
419 CONNECT( action, triggered(), this, doUpdate() );
424 CoverArtLabel::~CoverArtLabel()
426 QList< QAction* > artActions = actions();
427 foreach( QAction *act, artActions )
431 void CoverArtLabel::doUpdate( QString url )
434 if( !url.isEmpty() && pix.load( url ) )
440 setPixmap( QPixmap( ":/noart.png" ) );
444 void CoverArtLabel::doUpdate()
446 THEMIM->getIM()->requestArtUpdate();
449 TimeLabel::TimeLabel( intf_thread_t *_p_intf ) :QLabel(), p_intf( _p_intf )
451 b_remainingTime = false;
452 setText( " --:--/--:-- " );
453 setAlignment( Qt::AlignRight | Qt::AlignVCenter );
454 setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
457 CONNECT( THEMIM->getIM(), cachingChanged( float ),
458 this, setCaching( float ) );
459 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
460 this, setDisplayPosition( float, int, int ) );
463 void TimeLabel::setDisplayPosition( float pos, int time, int length )
465 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
466 secstotimestr( psz_length, length );
467 secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
471 timestr.sprintf( "%s/%s", psz_time,
472 ( !length && time ) ? "--:--" : psz_length );
474 /* Add a minus to remaining time*/
475 if( b_remainingTime && length ) setText( " -"+timestr+" " );
476 else setText( " "+timestr+" " );
479 void TimeLabel::toggleTimeDisplay()
481 b_remainingTime = !b_remainingTime;
484 void TimeLabel::setCaching( float f_cache )
486 setText( "Buffering" );