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 "input_manager.hpp" /* Rate control */
34 #include "menus.hpp" /* Popup menu on bgWidget */
39 #include <QToolButton>
41 #include <QResizeEvent>
45 # include <X11/Xlib.h>
46 # include <qx11info_x11.h>
51 /**********************************************************************
52 * Video Widget. A simple frame on which video is drawn
53 * This class handles resize issues
54 **********************************************************************/
56 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
60 videoSize.rwidth() = -1;
61 videoSize.rheight() = -1;
65 /* Set the policy to expand in both directions */
66 // setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
68 /* Black background is more coherent for a Video Widget */
69 QPalette plt = palette();
70 plt.setColor( QPalette::Window, Qt::black );
72 setAutoFillBackground(true);
74 /* Indicates that the widget wants to draw directly onto the screen.
75 Widgets with this attribute set do not participate in composition
77 setAttribute( Qt::WA_PaintOnScreen, true );
79 /* The core can ask through a callback to show the video. */
80 connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
81 this, SLOT(SetSizing(unsigned int, unsigned int )),
82 Qt::BlockingQueuedConnection );
85 void VideoWidget::paintEvent(QPaintEvent *ev)
87 QFrame::paintEvent(ev);
89 XFlush( QX11Info::display() );
93 VideoWidget::~VideoWidget()
95 /* Ensure we are not leaking the video output. This would crash. */
100 * Request the video to avoid the conflicts
102 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
103 unsigned int *pi_width, unsigned int *pi_height )
105 msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
106 emit askVideoWidgetToShow( *pi_width, *pi_height );
109 msg_Dbg( p_intf, "embedded video already in use" );
114 msg_Dbg( p_intf, "embedded video ready (handle %p)", winId() );
116 return ( void* )winId();
119 /* Set the Widget to the correct Size */
120 /* Function has to be called by the parent
121 Parent has to care about resizing himself*/
122 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
124 msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
125 videoSize.rwidth() = w;
126 videoSize.rheight() = h;
127 if( isHidden() ) show();
128 updateGeometry(); // Needed for deinterlace
131 void VideoWidget::release( void )
133 msg_Dbg( p_intf, "Video is not needed anymore" );
135 videoSize.rwidth() = 0;
136 videoSize.rheight() = 0;
141 QSize VideoWidget::sizeHint() const
146 /**********************************************************************
147 * Background Widget. Show a simple image background. Currently,
148 * it's album art if present or cone.
149 **********************************************************************/
150 #define ICON_SIZE 128
151 #define MAX_BG_SIZE 400
152 #define MIN_BG_SIZE 128
154 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
155 :QWidget( NULL ), p_intf( _p_i )
157 /* We should use that one to take the more size it can */
158 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
160 /* A dark background */
161 setAutoFillBackground( true );
163 plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
164 plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
167 /* A cone in the middle */
169 label->setMargin( 5 );
170 label->setMaximumHeight( MAX_BG_SIZE );
171 label->setMaximumWidth( MAX_BG_SIZE );
172 label->setMinimumHeight( MIN_BG_SIZE );
173 label->setMinimumWidth( MIN_BG_SIZE );
174 if( QDate::currentDate().dayOfYear() >= 354 )
175 label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
177 label->setPixmap( QPixmap( ":/vlc128.png" ) );
179 QGridLayout *backgroundLayout = new QGridLayout( this );
180 backgroundLayout->addWidget( label, 0, 1 );
181 backgroundLayout->setColumnStretch( 0, 1 );
182 backgroundLayout->setColumnStretch( 2, 1 );
184 CONNECT( THEMIM->getIM(), artChanged( input_item_t* ),
185 this, updateArt( input_item_t* ) );
188 BackgroundWidget::~BackgroundWidget()
191 void BackgroundWidget::resizeEvent( QResizeEvent * event )
193 if( event->size().height() <= MIN_BG_SIZE )
199 void BackgroundWidget::updateArt( input_item_t *p_item )
204 char *psz_art = input_item_GetArtURL( p_item );
211 if( QDate::currentDate().dayOfYear() >= 354 )
212 label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
214 label->setPixmap( QPixmap( ":/vlc128.png" ) );
218 url = url.replace( "file://", QString("" ) );
219 /* Taglib seems to define a attachment://, It won't work yet */
220 url = url.replace( "attachment://", QString("" ) );
221 label->setPixmap( QPixmap( url ) );
225 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
227 QVLCMenu::PopupMenu( p_intf, true );
231 #include <QPushButton>
232 #include <QHBoxLayout>
234 /**********************************************************************
235 * Visualization selector panel
236 **********************************************************************/
237 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
238 QFrame( NULL ), p_intf( _p_i )
240 QHBoxLayout *layout = new QHBoxLayout( this );
241 layout->setMargin( 0 );
242 QPushButton *prevButton = new QPushButton( "Prev" );
243 QPushButton *nextButton = new QPushButton( "Next" );
244 layout->addWidget( prevButton );
245 layout->addWidget( nextButton );
247 layout->addStretch( 10 );
248 layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
250 current = new QLabel( qtr( "None" ) );
251 layout->addWidget( current );
253 BUTTONACT( prevButton, prev() );
254 BUTTONACT( nextButton, next() );
257 setMaximumHeight( 35 );
260 VisualSelector::~VisualSelector()
263 void VisualSelector::prev()
265 char *psz_new = aout_VisualPrev( p_intf );
268 current->setText( qfu( psz_new ) );
273 void VisualSelector::next()
275 char *psz_new = aout_VisualNext( p_intf );
278 current->setText( qfu( psz_new ) );
284 /**********************************************************************
285 * Speed control widget
286 **********************************************************************/
287 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
288 QFrame( NULL ), p_intf( _p_i )
290 QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
291 sizePolicy.setHorizontalStretch( 0 );
292 sizePolicy.setVerticalStretch( 0 );
294 speedSlider = new QSlider;
295 speedSlider->setSizePolicy( sizePolicy );
296 speedSlider->setMaximumSize( QSize( 80, 200 ) );
297 speedSlider->setOrientation( Qt::Vertical );
298 speedSlider->setTickPosition( QSlider::TicksRight );
300 speedSlider->setRange( -34, 34 );
301 speedSlider->setSingleStep( 1 );
302 speedSlider->setPageStep( 1 );
303 speedSlider->setTickInterval( 17 );
305 CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
307 QToolButton *normalSpeedButton = new QToolButton( this );
308 normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
309 normalSpeedButton->setAutoRaise( true );
310 normalSpeedButton->setText( "1x" );
311 normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
313 CONNECT( normalSpeedButton, clicked(), this, resetRate() );
315 QVBoxLayout *speedControlLayout = new QVBoxLayout;
316 speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
317 speedControlLayout->setSpacing( 4 );
318 speedControlLayout->addWidget( speedSlider );
319 speedControlLayout->addWidget( normalSpeedButton );
320 setLayout( speedControlLayout );
323 SpeedControlWidget::~SpeedControlWidget()
326 void SpeedControlWidget::setEnable( bool b_enable )
328 speedSlider->setEnabled( b_enable );
331 void SpeedControlWidget::updateControls( int rate )
333 if( speedSlider->isSliderDown() )
335 //We don't want to change anything if the user is using the slider
339 double value = 17 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
340 int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
342 if( sliderValue < speedSlider->minimum() )
344 sliderValue = speedSlider->minimum();
346 else if( sliderValue > speedSlider->maximum() )
348 sliderValue = speedSlider->maximum();
351 //Block signals to avoid feedback loop
352 speedSlider->blockSignals( true );
353 speedSlider->setValue( sliderValue );
354 speedSlider->blockSignals( false );
357 void SpeedControlWidget::updateRate( int sliderValue )
359 double speed = pow( 2, (double)sliderValue / 17 );
360 int rate = INPUT_RATE_DEFAULT / speed;
362 THEMIM->getIM()->setRate(rate);
365 void SpeedControlWidget::resetRate()
367 THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
370 static int downloadCoverCallback( vlc_object_t *p_this,
372 vlc_value_t oldvar, vlc_value_t newvar,
375 if( !strcmp( psz_var, "item-change" ) )
377 CoverArtLabel *art = static_cast< CoverArtLabel* >( data );
379 art->requestUpdate();
384 CoverArtLabel::CoverArtLabel( QWidget *parent,
385 vlc_object_t *_p_this,
386 input_item_t *_p_input )
387 : QLabel( parent ), p_this( _p_this), p_input( _p_input ), prevArt()
389 setContextMenuPolicy( Qt::ActionsContextMenu );
390 CONNECT( this, updateRequested(), this, doUpdate() );
392 playlist_t *p_playlist = pl_Hold( p_this );
393 var_AddCallback( p_playlist, "item-change",
394 downloadCoverCallback, this );
395 pl_Release( p_this );
397 setMinimumHeight( 128 );
398 setMinimumWidth( 128 );
399 setMaximumHeight( 128 );
400 setMaximumWidth( 128 );
401 setScaledContents( true );
406 void CoverArtLabel::downloadCover()
410 playlist_t *p_playlist = pl_Hold( p_this );
411 playlist_AskForArtEnqueue( p_playlist, p_input );
412 pl_Release( p_this );
416 void CoverArtLabel::doUpdate()
420 setPixmap( QPixmap( ":/noart.png" ) );
421 QList< QAction* > artActions = actions();
422 if( !artActions.isEmpty() )
423 foreach( QAction *act, artActions )
429 char *psz_meta = input_item_GetArtURL( p_input );
430 if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
432 QString artUrl = qfu( psz_meta ).replace( "file://", "" );
433 if( artUrl != prevArt )
436 if( pix.load( artUrl ) )
440 msg_Dbg( p_this, "Qt could not load image '%s'",
442 setPixmap( QPixmap( ":/noart.png" ) );
445 QList< QAction* > artActions = actions();
446 if( !artActions.isEmpty() )
448 foreach( QAction *act, artActions )
456 setPixmap( QPixmap( ":/noart.png" ) );
458 QList< QAction* > artActions = actions();
459 if( artActions.isEmpty() )
461 QAction *action = new QAction( qtr( "Download cover art" ),
464 CONNECT( action, triggered(),
465 this, downloadCover() );
472 TimeLabel::TimeLabel( intf_thread_t *_p_intf ) :QLabel(), p_intf( _p_intf )
474 b_remainingTime = false;
475 setText( " --:--/--:-- " );
476 setAlignment( Qt::AlignRight | Qt::AlignVCenter );
477 setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
480 CONNECT( THEMIM->getIM(), statusChanged( int ),
481 this, setStatus( int ) );
482 CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
483 this, setDisplayPosition( float, int, int ) );
486 void TimeLabel::setDisplayPosition( float pos, int time, int length )
488 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
489 secstotimestr( psz_length, length );
490 secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
494 timestr.sprintf( "%s/%s", psz_time,
495 ( !length && time ) ? "--:--" : psz_length );
497 /* Add a minus to remaining time*/
498 if( b_remainingTime && length ) setText( " -"+timestr+" " );
499 else setText( " "+timestr+" " );
502 void TimeLabel::toggleTimeDisplay()
504 b_remainingTime = !b_remainingTime;
507 void TimeLabel::setStatus( int i_status )
509 msg_Warn( p_intf, "Status: %i", i_status );
511 if( i_status == OPENING_S )
512 setText( "Buffering" );