]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Qt: Change the rate scale in order to have 0.96x in the slider in order to help peopl...
[vlc] / modules / gui / qt4 / components / interface_widgets.cpp
1 /*****************************************************************************
2  * interface_widgets.cpp : Custom widgets for the main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "components/interface_widgets.hpp"
32
33 #include "input_manager.hpp"     /* Rate control */
34 #include "menus.hpp"             /* Popup menu on bgWidget */
35
36 #include <vlc_vout.h>
37
38 #include <QLabel>
39 #include <QToolButton>
40 #include <QPalette>
41 #include <QResizeEvent>
42 #include <QDate>
43
44 #ifdef Q_WS_X11
45 # include <X11/Xlib.h>
46 # include <qx11info_x11.h>
47 #endif
48
49 #include <math.h>
50
51 /**********************************************************************
52  * Video Widget. A simple frame on which video is drawn
53  * This class handles resize issues
54  **********************************************************************/
55
56 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
57 {
58     /* Init */
59     p_vout = NULL;
60     videoSize.rwidth() = -1;
61     videoSize.rheight() = -1;
62
63     hide();
64
65     /* Set the policy to expand in both directions */
66 //    setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
67
68     /* Black background is more coherent for a Video Widget */
69     QPalette plt =  palette();
70     plt.setColor( QPalette::Window, Qt::black );
71     setPalette( plt );
72     setAutoFillBackground(true);
73
74     /* Indicates that the widget wants to draw directly onto the screen.
75        Widgets with this attribute set do not participate in composition
76        management */
77     setAttribute( Qt::WA_PaintOnScreen, true );
78
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 );
83 }
84
85 void VideoWidget::paintEvent(QPaintEvent *ev)
86 {
87     QFrame::paintEvent(ev);
88 #ifdef Q_WS_X11
89     XFlush( QX11Info::display() );
90 #endif
91 }
92
93 VideoWidget::~VideoWidget()
94 {
95     /* Ensure we are not leaking the video output. This would crash. */
96     assert( !p_vout );
97 }
98
99 /**
100  * Request the video to avoid the conflicts
101  **/
102 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
103                             unsigned int *pi_width, unsigned int *pi_height )
104 {
105     msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
106     emit askVideoWidgetToShow( *pi_width, *pi_height );
107     if( p_vout )
108     {
109         msg_Dbg( p_intf, "embedded video already in use" );
110         return NULL;
111     }
112     p_vout = p_nvout;
113 #ifndef NDEBUG
114     msg_Dbg( p_intf, "embedded video ready (handle %p)", winId() );
115 #endif
116     return ( void* )winId();
117 }
118
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 )
123 {
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
129 }
130
131 void VideoWidget::release( void )
132 {
133     msg_Dbg( p_intf, "Video is not needed anymore" );
134     p_vout = NULL;
135     videoSize.rwidth() = 0;
136     videoSize.rheight() = 0;
137     updateGeometry();
138     hide();
139 }
140
141 QSize VideoWidget::sizeHint() const
142 {
143     return videoSize;
144 }
145
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
153
154 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
155                  :QWidget( NULL ), p_intf( _p_i )
156 {
157     /* We should use that one to take the more size it can */
158     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
159
160     /* A dark background */
161     setAutoFillBackground( true );
162     plt = palette();
163     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
164     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
165     setPalette( plt );
166
167     /* A cone in the middle */
168     label = new QLabel;
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" ) );
176     else
177         label->setPixmap( QPixmap( ":/vlc128.png" ) );
178
179     QGridLayout *backgroundLayout = new QGridLayout( this );
180     backgroundLayout->addWidget( label, 0, 1 );
181     backgroundLayout->setColumnStretch( 0, 1 );
182     backgroundLayout->setColumnStretch( 2, 1 );
183
184     CONNECT( THEMIM->getIM(), artChanged( input_item_t* ),
185              this, updateArt( input_item_t* ) );
186 }
187
188 BackgroundWidget::~BackgroundWidget()
189 {}
190
191 void BackgroundWidget::resizeEvent( QResizeEvent * event )
192 {
193     if( event->size().height() <= MIN_BG_SIZE )
194         label->hide();
195     else
196         label->show();
197 }
198
199 void BackgroundWidget::updateArt( input_item_t *p_item )
200 {
201     QString url;
202     if( p_item )
203     {
204         char *psz_art = input_item_GetArtURL( p_item );
205         url = psz_art;
206         free( psz_art );
207     }
208
209     if( url.isEmpty() )
210     {
211         if( QDate::currentDate().dayOfYear() >= 354 )
212             label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
213         else
214             label->setPixmap( QPixmap( ":/vlc128.png" ) );
215     }
216     else
217     {
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 ) );
222     }
223 }
224
225 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
226 {
227     QVLCMenu::PopupMenu( p_intf, true );
228 }
229
230 #if 0
231 #include <QPushButton>
232 #include <QHBoxLayout>
233
234 /**********************************************************************
235  * Visualization selector panel
236  **********************************************************************/
237 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
238                                 QFrame( NULL ), p_intf( _p_i )
239 {
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 );
246
247     layout->addStretch( 10 );
248     layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
249
250     current = new QLabel( qtr( "None" ) );
251     layout->addWidget( current );
252
253     BUTTONACT( prevButton, prev() );
254     BUTTONACT( nextButton, next() );
255
256     setLayout( layout );
257     setMaximumHeight( 35 );
258 }
259
260 VisualSelector::~VisualSelector()
261 {}
262
263 void VisualSelector::prev()
264 {
265     char *psz_new = aout_VisualPrev( p_intf );
266     if( psz_new )
267     {
268         current->setText( qfu( psz_new ) );
269         free( psz_new );
270     }
271 }
272
273 void VisualSelector::next()
274 {
275     char *psz_new = aout_VisualNext( p_intf );
276     if( psz_new )
277     {
278         current->setText( qfu( psz_new ) );
279         free( psz_new );
280     }
281 }
282 #endif
283
284 /**********************************************************************
285  * Speed control widget
286  **********************************************************************/
287 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
288                              QFrame( NULL ), p_intf( _p_i )
289 {
290     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
291     sizePolicy.setHorizontalStretch( 0 );
292     sizePolicy.setVerticalStretch( 0 );
293
294     speedSlider = new QSlider;
295     speedSlider->setSizePolicy( sizePolicy );
296     speedSlider->setMaximumSize( QSize( 80, 200 ) );
297     speedSlider->setOrientation( Qt::Vertical );
298     speedSlider->setTickPosition( QSlider::TicksRight );
299
300     speedSlider->setRange( -34, 34 );
301     speedSlider->setSingleStep( 1 );
302     speedSlider->setPageStep( 1 );
303     speedSlider->setTickInterval( 17 );
304
305     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
306
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" ) );
312
313     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
314
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 );
321 }
322
323 SpeedControlWidget::~SpeedControlWidget()
324 {}
325
326 void SpeedControlWidget::setEnable( bool b_enable )
327 {
328     speedSlider->setEnabled( b_enable );
329 }
330
331 void SpeedControlWidget::updateControls( int rate )
332 {
333     if( speedSlider->isSliderDown() )
334     {
335         //We don't want to change anything if the user is using the slider
336         return;
337     }
338
339     double value = 17 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
340     int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
341
342     if( sliderValue < speedSlider->minimum() )
343     {
344         sliderValue = speedSlider->minimum();
345     }
346     else if( sliderValue > speedSlider->maximum() )
347     {
348         sliderValue = speedSlider->maximum();
349     }
350
351     //Block signals to avoid feedback loop
352     speedSlider->blockSignals( true );
353     speedSlider->setValue( sliderValue );
354     speedSlider->blockSignals( false );
355 }
356
357 void SpeedControlWidget::updateRate( int sliderValue )
358 {
359     double speed = pow( 2, (double)sliderValue / 17 );
360     int rate = INPUT_RATE_DEFAULT / speed;
361
362     THEMIM->getIM()->setRate(rate);
363 }
364
365 void SpeedControlWidget::resetRate()
366 {
367     THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
368 }
369
370 static int downloadCoverCallback( vlc_object_t *p_this,
371                                   char const *psz_var,
372                                   vlc_value_t oldvar, vlc_value_t newvar,
373                                   void *data )
374 {
375     if( !strcmp( psz_var, "item-change" ) )
376     {
377         CoverArtLabel *art = static_cast< CoverArtLabel* >( data );
378         if( art )
379             art->requestUpdate();
380     }
381     return VLC_SUCCESS;
382 }
383
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()
388 {
389     setContextMenuPolicy( Qt::ActionsContextMenu );
390     CONNECT( this, updateRequested(), this, doUpdate() );
391
392     playlist_t *p_playlist = pl_Hold( p_this );
393     var_AddCallback( p_playlist, "item-change",
394                      downloadCoverCallback, this );
395     pl_Release( p_this );
396
397     setMinimumHeight( 128 );
398     setMinimumWidth( 128 );
399     setMaximumHeight( 128 );
400     setMaximumWidth( 128 );
401     setScaledContents( true );
402
403     doUpdate();
404 }
405
406 void CoverArtLabel::downloadCover()
407 {
408     if( p_input )
409     {
410         playlist_t *p_playlist = pl_Hold( p_this );
411         playlist_AskForArtEnqueue( p_playlist, p_input );
412         pl_Release( p_this );
413     }
414 }
415
416 void CoverArtLabel::doUpdate()
417 {
418     if( !p_input )
419     {
420         setPixmap( QPixmap( ":/noart.png" ) );
421         QList< QAction* > artActions = actions();
422         if( !artActions.isEmpty() )
423             foreach( QAction *act, artActions )
424                 removeAction( act );
425         prevArt = "";
426     }
427     else
428     {
429         char *psz_meta = input_item_GetArtURL( p_input );
430         if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
431         {
432             QString artUrl = qfu( psz_meta ).replace( "file://", "" );
433             if( artUrl != prevArt )
434             {
435                 QPixmap pix;
436                 if( pix.load( artUrl ) )
437                     setPixmap( pix );
438                 else
439                 {
440                     msg_Dbg( p_this, "Qt could not load image '%s'",
441                              qtu( artUrl ) );
442                     setPixmap( QPixmap( ":/noart.png" ) );
443                 }
444             }
445             QList< QAction* > artActions = actions();
446             if( !artActions.isEmpty() )
447             {
448                 foreach( QAction *act, artActions )
449                     removeAction( act );
450             }
451             prevArt = artUrl;
452         }
453         else
454         {
455             if( prevArt != "" )
456                 setPixmap( QPixmap( ":/noart.png" ) );
457             prevArt = "";
458             QList< QAction* > artActions = actions();
459             if( artActions.isEmpty() )
460             {
461                 QAction *action = new QAction( qtr( "Download cover art" ),
462                                                this );
463                 addAction( action );
464                 CONNECT( action, triggered(),
465                          this, downloadCover() );
466             }
467         }
468         free( psz_meta );
469     }
470 }
471
472 TimeLabel::TimeLabel( intf_thread_t *_p_intf  ) :QLabel(), p_intf( _p_intf )
473 {
474    b_remainingTime = false;
475    setText( " --:--/--:-- " );
476    setAlignment( Qt::AlignRight | Qt::AlignVCenter );
477    setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
478
479
480    CONNECT( THEMIM->getIM(), statusChanged( int ),
481             this, setStatus( int ) );
482    CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
483              this, setDisplayPosition( float, int, int ) );
484 }
485
486 void TimeLabel::setDisplayPosition( float pos, int time, int length )
487 {
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
491                                                            : time );
492
493     QString timestr;
494     timestr.sprintf( "%s/%s", psz_time,
495                             ( !length && time ) ? "--:--" : psz_length );
496
497     /* Add a minus to remaining time*/
498     if( b_remainingTime && length ) setText( " -"+timestr+" " );
499     else setText( " "+timestr+" " );
500 }
501
502 void TimeLabel::toggleTimeDisplay()
503 {
504     b_remainingTime = !b_remainingTime;
505 }
506
507 void TimeLabel::setStatus( int i_status )
508 {
509     msg_Warn( p_intf, "Status: %i", i_status );
510
511     if( i_status == OPENING_S )
512         setText( "Buffering" );
513 }
514
515