]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
66b75b364187d2a5e72f24148bd08ccf56450f6a
[vlc] / modules / gui / qt4 / components / interface_widgets.cpp
1 /*****************************************************************************
2  * interface_widgets.cpp : Custom widgets for the main interface
3  ****************************************************************************
4  * Copyright ( C ) 2006 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 <vlc_vout.h>
32
33 #include "dialogs_provider.hpp"
34 #include "components/interface_widgets.hpp"
35 #include "main_interface.hpp"
36 #include "input_manager.hpp"
37 #include "menus.hpp"
38 #include "util/input_slider.hpp"
39 #include "util/customwidgets.hpp"
40
41 #include <QLabel>
42 #include <QSpacerItem>
43 #include <QCursor>
44 #include <QPushButton>
45 #include <QToolButton>
46 #include <QHBoxLayout>
47 #include <QMenu>
48 #include <QPalette>
49 #include <QResizeEvent>
50 #include <QDate>
51
52 #ifdef Q_WS_X11
53 # include <X11/Xlib.h>
54 # include <qx11info_x11.h>
55 #endif
56
57 #include <math.h>
58
59 #define I_PLAY_TOOLTIP N_("Play\nIf the playlist is empty, open a media")
60
61 /**********************************************************************
62  * Video Widget. A simple frame on which video is drawn
63  * This class handles resize issues
64  **********************************************************************/
65
66 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
67 {
68     /* Init */
69     i_vout = 0;
70     hide(); setMinimumSize( 16, 16 );
71     videoSize.rwidth() = -1;
72     videoSize.rheight() = -1;
73     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
74
75     /* Black background is more coherent for a Video Widget IMVHO */
76     QPalette plt =  palette();
77     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
78     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
79     setPalette( plt );
80     setAttribute( Qt::WA_PaintOnScreen, true );
81
82     /* The core can ask through a callback to show the video. */
83 #if HAS_QT43
84     connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
85              this, SLOT(SetSizing(unsigned int, unsigned int )),
86              Qt::BlockingQueuedConnection );
87 #else
88 #error This is broken. Fix it with a QEventLoop with a processEvents () 
89     connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
90              this, SLOT(SetSizing(unsigned int, unsigned int )) );
91 #endif
92 }
93
94 void VideoWidget::paintEvent(QPaintEvent *ev)
95 {
96     QFrame::paintEvent(ev);
97 #ifdef Q_WS_X11
98     XFlush( QX11Info::display() );
99 #endif
100 }
101
102 VideoWidget::~VideoWidget()
103 {
104     vout_thread_t *p_vout = i_vout
105         ? (vout_thread_t *)vlc_object_get( i_vout ) : NULL;
106
107     if( p_vout )
108     {
109         if( !p_intf->psz_switch_intf )
110         {
111             if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
112                 vout_Control( p_vout, VOUT_REPARENT );
113         }
114         else
115         {
116             if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
117                 vout_Control( p_vout, VOUT_CLOSE );
118         }
119         vlc_object_release( p_vout );
120     }
121 }
122
123 /**
124  * Request the video to avoid the conflicts
125  **/
126 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
127                             unsigned int *pi_width, unsigned int *pi_height )
128 {
129     msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
130     emit askVideoWidgetToShow( *pi_width, *pi_height );
131     if( i_vout )
132     {
133         msg_Dbg( p_intf, "embedded video already in use" );
134         return NULL;
135     }
136     i_vout = p_nvout->i_object_id;
137     msg_Dbg( p_intf, "embedded video ready (handle %p)", winId() );
138     return ( void* )winId();
139 }
140
141 /* Set the Widget to the correct Size */
142 /* Function has to be called by the parent
143    Parent has to care about resizing himself*/
144 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
145 {
146     msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
147     videoSize.rwidth() = w;
148     videoSize.rheight() = h;
149     if( isHidden() ) show();
150     updateGeometry(); // Needed for deinterlace
151 }
152
153 void VideoWidget::release( void *p_win )
154 {
155     msg_Dbg( p_intf, "Video is not needed anymore" );
156     i_vout = 0;
157     videoSize.rwidth() = 0;
158     videoSize.rheight() = 0;
159     hide();
160     updateGeometry(); // Needed for deinterlace
161 }
162
163 QSize VideoWidget::sizeHint() const
164 {
165     return videoSize;
166 }
167
168 /**********************************************************************
169  * Background Widget. Show a simple image background. Currently,
170  * it's album art if present or cone.
171  **********************************************************************/
172 #define ICON_SIZE 128
173 #define MAX_BG_SIZE 400
174 #define MIN_BG_SIZE 64
175
176 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
177                  :QWidget( NULL ), p_intf( _p_i )
178 {
179     /* We should use that one to take the more size it can */
180     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
181
182     /* A dark background */
183     setAutoFillBackground( true );
184     plt =  palette();
185     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
186     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
187     setPalette( plt );
188
189     /* A cone in the middle */
190     label = new QLabel;
191     label->setMargin( 5 );
192     label->setMaximumHeight( MAX_BG_SIZE );
193     label->setMaximumWidth( MAX_BG_SIZE );
194     label->setMinimumHeight( MIN_BG_SIZE );
195     label->setMinimumWidth( MIN_BG_SIZE );
196     if( QDate::currentDate().dayOfYear() >= 354 )
197         label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
198     else
199         label->setPixmap( QPixmap( ":/vlc128.png" ) );
200
201     QGridLayout *backgroundLayout = new QGridLayout( this );
202     backgroundLayout->addWidget( label, 0, 1 );
203     backgroundLayout->setColumnStretch( 0, 1 );
204     backgroundLayout->setColumnStretch( 2, 1 );
205
206     CONNECT( THEMIM->getIM(), artChanged( QString ), this, updateArt( QString ) );
207 }
208
209 BackgroundWidget::~BackgroundWidget()
210 {}
211
212 void BackgroundWidget::resizeEvent( QResizeEvent * event )
213 {
214     if( event->size().height() <= MIN_BG_SIZE )
215         label->hide();
216     else
217         label->show();
218 }
219
220 void BackgroundWidget::updateArt( QString url )
221 {
222     if( url.isEmpty() )
223     {
224         if( QDate::currentDate().dayOfYear() >= 354 )
225             label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
226         else
227             label->setPixmap( QPixmap( ":/vlc128.png" ) );
228         return;
229     }
230     else
231     {
232         label->setPixmap( QPixmap( url ) );
233     }
234 }
235
236 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
237 {
238     QVLCMenu::PopupMenu( p_intf, true );
239 }
240
241 #if 0
242 /**********************************************************************
243  * Visualization selector panel
244  **********************************************************************/
245 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
246                                 QFrame( NULL ), p_intf( _p_i )
247 {
248     QHBoxLayout *layout = new QHBoxLayout( this );
249     layout->setMargin( 0 );
250     QPushButton *prevButton = new QPushButton( "Prev" );
251     QPushButton *nextButton = new QPushButton( "Next" );
252     layout->addWidget( prevButton );
253     layout->addWidget( nextButton );
254
255     layout->addStretch( 10 );
256     layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
257
258     current = new QLabel( qtr( "None" ) );
259     layout->addWidget( current );
260
261     BUTTONACT( prevButton, prev() );
262     BUTTONACT( nextButton, next() );
263
264     setLayout( layout );
265     setMaximumHeight( 35 );
266 }
267
268 VisualSelector::~VisualSelector()
269 {}
270
271 void VisualSelector::prev()
272 {
273     char *psz_new = aout_VisualPrev( p_intf );
274     if( psz_new )
275     {
276         current->setText( qfu( psz_new ) );
277         free( psz_new );
278     }
279 }
280
281 void VisualSelector::next()
282 {
283     char *psz_new = aout_VisualNext( p_intf );
284     if( psz_new )
285     {
286         current->setText( qfu( psz_new ) );
287         free( psz_new );
288     }
289 }
290 #endif
291
292 /**********************************************************************
293  * TEH controls
294  **********************************************************************/
295
296 #define setupSmallButton( aButton ){  \
297     aButton->setMaximumSize( QSize( 26, 26 ) ); \
298     aButton->setMinimumSize( QSize( 26, 26 ) ); \
299     aButton->setIconSize( QSize( 20, 20 ) ); }
300
301 /* init static variables in advanced controls */
302 mtime_t AdvControlsWidget::timeA = 0;
303 mtime_t AdvControlsWidget::timeB = 0;
304
305 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i, bool b_fsCreation = false ) :
306                                            QFrame( NULL ), p_intf( _p_i )
307 {
308     QHBoxLayout *advLayout = new QHBoxLayout( this );
309     advLayout->setMargin( 0 );
310     advLayout->setSpacing( 0 );
311     advLayout->setAlignment( Qt::AlignBottom );
312
313     /* A to B Button */
314     ABButton = new QPushButton;
315     setupSmallButton( ABButton );
316     advLayout->addWidget( ABButton );
317     BUTTON_SET_ACT_I( ABButton, "", atob_nob,
318       qtr( "Loop from point A to point B continuously.\nClick to set point A" ),
319       fromAtoB() );
320     timeA = timeB = 0;
321     i_last_input_id = 0;
322     /* in FS controller we skip this, because we dont want to have it double
323        controlled */
324     if( !b_fsCreation )
325         CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
326                  this, AtoBLoop( float, int, int ) );
327     /* set up synchronization between main controller and fs controller */
328     CONNECT( THEMIM->getIM(), advControlsSetIcon(), this, setIcon() );
329     connect( this, SIGNAL( timeChanged() ),
330         THEMIM->getIM(), SIGNAL( advControlsSetIcon()));
331 #if 0
332     frameButton = new QPushButton( "Fr" );
333     frameButton->setMaximumSize( QSize( 26, 26 ) );
334     frameButton->setIconSize( QSize( 20, 20 ) );
335     advLayout->addWidget( frameButton );
336     BUTTON_SET_ACT( frameButton, "Fr", qtr( "Frame by frame" ), frame() );
337 #endif
338
339     recordButton = new QPushButton;
340     setupSmallButton( recordButton );
341     advLayout->addWidget( recordButton );
342     BUTTON_SET_ACT_I( recordButton, "", record,
343             qtr( "Record" ), record() );
344
345     /* Snapshot Button */
346     snapshotButton = new QPushButton;
347     setupSmallButton( snapshotButton );
348     advLayout->addWidget( snapshotButton );
349     BUTTON_SET_ACT_I( snapshotButton, "", snapshot,
350             qtr( "Take a snapshot" ), snapshot() );
351 }
352
353 AdvControlsWidget::~AdvControlsWidget()
354 {}
355
356 void AdvControlsWidget::enableInput( bool enable )
357 {
358     int i_input_id = 0;
359     if( THEMIM->getInput() != NULL )
360     {
361         input_item_t *p_item = input_GetItem( THEMIM->getInput() );
362         i_input_id = p_item->i_id;
363     }
364     ABButton->setEnabled( enable );
365     recordButton->setEnabled( enable );
366
367     if( enable && ( i_last_input_id != i_input_id ) )
368     {
369         timeA = timeB = 0;
370         i_last_input_id = i_input_id;
371         emit timeChanged();
372     }
373 }
374
375 void AdvControlsWidget::enableVideo( bool enable )
376 {
377     snapshotButton->setEnabled( enable );
378 #if 0
379     frameButton->setEnabled( enable );
380 #endif
381 }
382
383 void AdvControlsWidget::snapshot()
384 {
385     vout_thread_t *p_vout =
386         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
387     if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
388 }
389
390 /* Function called when the button is clicked() */
391 void AdvControlsWidget::fromAtoB()
392 {
393     if( !timeA )
394     {
395         timeA = var_GetTime( THEMIM->getInput(), "time"  );
396         emit timeChanged();
397         return;
398     }
399     if( !timeB )
400     {
401         timeB = var_GetTime( THEMIM->getInput(), "time"  );
402         var_SetTime( THEMIM->getInput(), "time" , timeA );
403         emit timeChanged();
404         return;
405     }
406     timeA = 0;
407     timeB = 0;
408     emit timeChanged();
409 }
410
411 /* setting/synchro icons after click on main or fs controller */
412 void AdvControlsWidget::setIcon()
413 {
414     if( !timeA && !timeB)
415     {
416         ABButton->setIcon( QIcon( ":/atob_nob" ) );
417         ABButton->setToolTip( qtr( "Loop from point A to point B continuously\nClick to set point A" ) );
418     }
419     else if( timeA && !timeB )
420     {
421         ABButton->setIcon( QIcon( ":/atob_noa" ) );
422         ABButton->setToolTip( qtr( "Click to set point B" ) );
423     }
424     else if( timeA && timeB )
425     {
426         ABButton->setIcon( QIcon( ":/atob" ) );
427         ABButton->setToolTip( qtr( "Stop the A to B loop" ) );
428     }
429 }
430
431 /* Function called regularly when in an AtoB loop */
432 void AdvControlsWidget::AtoBLoop( float f_pos, int i_time, int i_length )
433 {
434     if( timeB )
435     {
436         if( ( i_time >= (int)( timeB/1000000 ) )
437             || ( i_time < (int)( timeA/1000000 ) ) )
438             var_SetTime( THEMIM->getInput(), "time" , timeA );
439     }
440 }
441
442 /* FIXME Record function */
443 void AdvControlsWidget::record()
444 {
445     input_thread_t *p_input = THEMIM->getInput();
446     if( p_input )
447     {
448         /* This method won't work fine if the stream can't be cut anywhere */
449         if( var_Type( p_input, "record-toggle" ) == VLC_VAR_VOID )
450             var_TriggerCallback( p_input, "record-toggle" );
451         else
452         {
453             /* 'record' access-filter is not loaded, we open Save dialog */
454             input_item_t *p_item = input_GetItem( p_input );
455             if( !p_item )
456                 return;
457
458             char *psz = input_item_GetURI( p_item );
459             if( psz )
460                 THEDP->streamingDialog( NULL, psz, true );
461         }
462     }
463 }
464
465 #if 0
466 //FIXME Frame by frame function
467 void AdvControlsWidget::frame(){}
468 #endif
469
470 /*****************************
471  * DA Control Widget !
472  *****************************/
473 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
474                                 MainInterface *_p_mi,
475                                 bool b_advControls,
476                                 bool b_shiny,
477                                 bool b_fsCreation) :
478                                 QFrame( _p_mi ), p_intf( _p_i )
479 {
480     setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Maximum );
481
482     /** The main Slider **/
483     slider = new InputSlider( Qt::Horizontal, NULL );
484     /* Update the position when the IM has changed */
485     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
486              slider, setPosition( float, int, int ) );
487     /* And update the IM, when the position has changed */
488     CONNECT( slider, sliderDragged( float ),
489              THEMIM->getIM(), sliderUpdate( float ) );
490
491     /** Slower and faster Buttons **/
492     slowerButton = new QToolButton;
493     slowerButton->setAutoRaise( true );
494     slowerButton->setMaximumSize( QSize( 26, 20 ) );
495
496     BUTTON_SET_ACT( slowerButton, "-", qtr( "Slower" ), slower() );
497
498     fasterButton = new QToolButton;
499     fasterButton->setAutoRaise( true );
500     fasterButton->setMaximumSize( QSize( 26, 20 ) );
501
502     BUTTON_SET_ACT( fasterButton, "+", qtr( "Faster" ), faster() );
503
504     /* advanced Controls handling */
505     b_advancedVisible = b_advControls;
506
507     advControls = new AdvControlsWidget( p_intf, b_fsCreation );
508     if( !b_advancedVisible ) advControls->hide();
509
510     /** Disc and Menus handling */
511     discFrame = new QWidget( this );
512
513     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
514     discLayout->setSpacing( 0 );
515     discLayout->setMargin( 0 );
516
517     prevSectionButton = new QPushButton( discFrame );
518     setupSmallButton( prevSectionButton );
519     discLayout->addWidget( prevSectionButton );
520
521     menuButton = new QPushButton( discFrame );
522     setupSmallButton( menuButton );
523     discLayout->addWidget( menuButton );
524
525     nextSectionButton = new QPushButton( discFrame );
526     setupSmallButton( nextSectionButton );
527     discLayout->addWidget( nextSectionButton );
528
529     BUTTON_SET_IMG( prevSectionButton, "", dvd_prev, "" );
530     BUTTON_SET_IMG( nextSectionButton, "", dvd_next, "" );
531     BUTTON_SET_IMG( menuButton, "", dvd_menu, qtr( "Menu" ) );
532
533     discFrame->hide();
534
535     /* Change the navigation button display when the IM navigation changes */
536     CONNECT( THEMIM->getIM(), navigationChanged( int ),
537              this, setNavigation( int ) );
538     /* Changes the IM navigation when triggered on the nav buttons */
539     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
540              sectionPrev() );
541     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
542              sectionNext() );
543     CONNECT( menuButton, clicked(), THEMIM->getIM(),
544              sectionMenu() );
545
546     /**
547      * Telextext QFrame
548      * TODO: Merge with upper menu in a StackLayout
549      **/
550     telexFrame = new QWidget( this );
551     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
552     telexLayout->setSpacing( 0 );
553     telexLayout->setMargin( 0 );
554
555     telexOn = new QPushButton;
556     setupSmallButton( telexOn );
557     telexLayout->addWidget( telexOn );
558
559     telexTransparent = new QPushButton;
560     setupSmallButton( telexTransparent );
561     telexLayout->addWidget( telexTransparent );
562     b_telexTransparent = false;
563
564     telexPage = new QSpinBox;
565     telexPage->setRange( 0, 999 );
566     telexPage->setValue( 100 );
567     telexPage->setAccelerated( true );
568     telexPage->setWrapping( true );
569     telexPage->setAlignment( Qt::AlignRight );
570     telexPage->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
571     telexLayout->addWidget( telexPage );
572
573     telexFrame->hide(); /* default hidden */
574
575     CONNECT( telexPage, valueChanged( int ), THEMIM->getIM(),
576              telexGotoPage( int ) );
577     CONNECT( THEMIM->getIM(), setNewTelexPage( int ),
578               telexPage, setValue( int ) );
579
580     BUTTON_SET_IMG( telexOn, "", tv, qtr( "Teletext on" ) );
581
582     CONNECT( telexOn, clicked(), THEMIM->getIM(),
583              telexToggleButtons() );
584     CONNECT( telexOn, clicked( bool ), THEMIM->getIM(),
585              telexToggle( bool ) );
586     CONNECT( THEMIM->getIM(), toggleTelexButtons(),
587               this, toggleTeletext() );
588     b_telexEnabled = false;
589     telexTransparent->setEnabled( false );
590     telexPage->setEnabled( false );
591
592     BUTTON_SET_IMG( telexTransparent, "", tvtelx, qtr( "Teletext" ) );
593     CONNECT( telexTransparent, clicked( bool ),
594              THEMIM->getIM(), telexSetTransparency() );
595     CONNECT( THEMIM->getIM(), toggleTelexTransparency(),
596               this, toggleTeletextTransparency() );
597     CONNECT( THEMIM->getIM(), teletextEnabled( bool ),
598              this, enableTeletext( bool ) );
599
600     /** Play Buttons **/
601     QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
602     sizePolicy.setHorizontalStretch( 0 );
603     sizePolicy.setVerticalStretch( 0 );
604
605     /* Play */
606     playButton = new QPushButton;
607     playButton->setSizePolicy( sizePolicy );
608     playButton->setMaximumSize( QSize( 36, 36 ) );
609     playButton->setMinimumSize( QSize( 36, 36 ) );
610     playButton->setIconSize( QSize( 30, 30 ) );
611
612
613     /** Prev + Stop + Next Block **/
614     controlButLayout = new QHBoxLayout;
615     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
616
617     /* Prev */
618     QPushButton *prevButton = new QPushButton;
619     prevButton->setSizePolicy( sizePolicy );
620     setupSmallButton( prevButton );
621
622     controlButLayout->addWidget( prevButton );
623
624     /* Stop */
625     QPushButton *stopButton = new QPushButton;
626     stopButton->setSizePolicy( sizePolicy );
627     setupSmallButton( stopButton );
628
629     controlButLayout->addWidget( stopButton );
630
631     /* next */
632     QPushButton *nextButton = new QPushButton;
633     nextButton->setSizePolicy( sizePolicy );
634     setupSmallButton( nextButton );
635
636     controlButLayout->addWidget( nextButton );
637
638     /* Add this block to the main layout */
639
640     BUTTON_SET_ACT_I( playButton, "", play_b, qtr( I_PLAY_TOOLTIP ), play() );
641     BUTTON_SET_ACT_I( prevButton, "" , previous_b,
642                       qtr( "Previous media in the playlist" ), prev() );
643     BUTTON_SET_ACT_I( nextButton, "", next_b,
644                       qtr( "Next media in the playlist" ), next() );
645     BUTTON_SET_ACT_I( stopButton, "", stop_b, qtr( "Stop playback" ), stop() );
646
647     /*
648      * Other first Line buttons
649      */
650     /** Fullscreen/Visualisation **/
651     fullscreenButton = new QPushButton;
652     BUTTON_SET_ACT_I( fullscreenButton, "", fullscreen,
653             qtr( "Toggle the video in fullscreen" ), fullscreen() );
654     setupSmallButton( fullscreenButton );
655
656     if( !b_fsCreation )
657     {
658         /** Playlist Button **/
659         playlistButton = new QPushButton;
660         setupSmallButton( playlistButton );
661         BUTTON_SET_IMG( playlistButton, "" , playlist, qtr( "Show playlist" ) );
662         CONNECT( playlistButton, clicked(), _p_mi, togglePlaylist() );
663
664         /** extended Settings **/
665         extSettingsButton = new QPushButton;
666         BUTTON_SET_ACT_I( extSettingsButton, "", extended,
667                 qtr( "Show extended settings" ), extSettings() );
668         setupSmallButton( extSettingsButton );
669     }
670
671     /* Volume */
672     hVolLabel = new VolumeClickHandler( p_intf, this );
673
674     volMuteLabel = new QLabel;
675     volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
676     volMuteLabel->installEventFilter( hVolLabel );
677
678     if( b_shiny )
679     {
680         volumeSlider = new SoundSlider( this,
681             config_GetInt( p_intf, "volume-step" ),
682             config_GetInt( p_intf, "qt-volume-complete" ),
683             config_GetPsz( p_intf, "qt-slider-colours" ) );
684     }
685     else
686     {
687         volumeSlider = new QSlider( this );
688         volumeSlider->setOrientation( Qt::Horizontal );
689     }
690     volumeSlider->setMaximumSize( QSize( 200, 40 ) );
691     volumeSlider->setMinimumSize( QSize( 85, 30 ) );
692     volumeSlider->setFocusPolicy( Qt::NoFocus );
693
694     /* Set the volume from the config */
695     volumeSlider->setValue( ( config_GetInt( p_intf, "volume" ) ) *
696                               VOLUME_MAX / (AOUT_VOLUME_MAX/2) );
697
698     /* Force the update at build time in order to have a muted icon if needed */
699     updateVolume( volumeSlider->value() );
700
701     /* Volume control connection */
702     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
703     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
704
705     if( !b_fsCreation )
706     {
707         controlLayout = new QGridLayout( this );
708
709         controlLayout->setSpacing( 0 );
710         controlLayout->setLayoutMargins( 7, 5, 7, 3, 6 );
711
712         controlLayout->addWidget( slider, 0, 1, 1, 18 );
713         controlLayout->addWidget( slowerButton, 0, 0 );
714         controlLayout->addWidget( fasterButton, 0, 19 );
715
716         controlLayout->addWidget( discFrame, 1, 8, 2, 3, Qt::AlignBottom );
717         controlLayout->addWidget( telexFrame, 1, 8, 2, 5, Qt::AlignBottom );
718
719         controlLayout->addWidget( playButton, 2, 0, 2, 2, Qt::AlignBottom );
720         controlLayout->setColumnMinimumWidth( 2, 10 );
721         controlLayout->setColumnStretch( 2, 0 );
722
723         controlLayout->addLayout( controlButLayout, 3, 3, 1, 3, Qt::AlignBottom );
724         /* Column 6 is unused */
725         controlLayout->setColumnStretch( 6, 0 );
726         controlLayout->setColumnStretch( 7, 0 );
727         controlLayout->setColumnMinimumWidth( 7, 10 );
728
729         controlLayout->addWidget( fullscreenButton, 3, 8, Qt::AlignBottom );
730         controlLayout->addWidget( playlistButton, 3, 9, Qt::AlignBottom );
731         controlLayout->addWidget( extSettingsButton, 3, 10, Qt::AlignBottom );
732         controlLayout->setColumnStretch( 11, 0 ); /* telex alignment */
733
734         controlLayout->setColumnStretch( 12, 0 );
735         controlLayout->setColumnMinimumWidth( 12, 10 );
736
737         controlLayout->addWidget( advControls, 3, 13, 1, 3, Qt::AlignBottom );
738
739         controlLayout->setColumnStretch( 16, 10 );
740         controlLayout->setColumnMinimumWidth( 16, 10 );
741
742         controlLayout->addWidget( volMuteLabel, 3, 17, Qt::AlignBottom );
743         controlLayout->addWidget( volumeSlider, 3, 18, 1 , 2, Qt::AlignBottom );
744     }
745
746     updateInput();
747 }
748
749 ControlsWidget::~ControlsWidget()
750 {}
751
752 void ControlsWidget::toggleTeletext()
753 {
754     bool b_enabled = THEMIM->teletextState();
755     if( b_telexEnabled )
756     {
757         telexTransparent->setEnabled( false );
758         telexPage->setEnabled( false );
759         b_telexEnabled = false;
760     }
761     else if( b_enabled )
762     {
763         telexTransparent->setEnabled( true );
764         telexPage->setEnabled( true );
765         b_telexEnabled = true;
766     }
767 }
768
769 void ControlsWidget::enableTeletext( bool b_enable )
770 {
771     telexFrame->setVisible( b_enable );
772     bool b_on = THEMIM->teletextState();
773
774     telexOn->setChecked( b_on );
775     telexTransparent->setEnabled( b_on );
776     telexPage->setEnabled( b_on );
777     b_telexEnabled = b_on;
778 }
779
780 void ControlsWidget::toggleTeletextTransparency()
781 {
782     if( b_telexTransparent )
783     {
784         telexTransparent->setIcon( QIcon( ":/tvtelx" ) );
785         telexTransparent->setToolTip( qtr( "Teletext" ) );
786         b_telexTransparent = false;
787     }
788     else
789     {
790         telexTransparent->setIcon( QIcon( ":/tvtelx-transparent" ) );
791         telexTransparent->setToolTip( qtr( "Transparent" ) );
792         b_telexTransparent = true;
793     }
794 }
795
796 void ControlsWidget::stop()
797 {
798     THEMIM->stop();
799 }
800
801 void ControlsWidget::play()
802 {
803     if( THEPL->current.i_size == 0 )
804     {
805         /* The playlist is empty, open a file requester */
806         THEDP->openFileDialog();
807         setStatus( 0 );
808         return;
809     }
810     THEMIM->togglePlayPause();
811 }
812
813 void ControlsWidget::prev()
814 {
815     THEMIM->prev();
816 }
817
818 void ControlsWidget::next()
819 {
820     THEMIM->next();
821 }
822
823 void ControlsWidget::setNavigation( int navigation )
824 {
825 #define HELP_PCH N_( "Previous chapter" )
826 #define HELP_NCH N_( "Next chapter" )
827
828     // 1 = chapter, 2 = title, 0 = no
829     if( navigation == 0 )
830     {
831         discFrame->hide();
832     } else if( navigation == 1 ) {
833         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
834         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
835         menuButton->show();
836         discFrame->show();
837     } else {
838         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
839         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
840         menuButton->hide();
841         discFrame->show();
842     }
843 }
844
845 static bool b_my_volume;
846 void ControlsWidget::updateVolume( int i_sliderVolume )
847 {
848     if( !b_my_volume )
849     {
850         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
851         aout_VolumeSet( p_intf, i_res );
852     }
853     if( i_sliderVolume == 0 )
854     {
855         volMuteLabel->setPixmap( QPixmap(":/volume-muted" ) );
856         volMuteLabel->setToolTip( qtr( "Unmute" ) );
857         return;
858     }
859
860     if( i_sliderVolume < VOLUME_MAX / 3 )
861         volMuteLabel->setPixmap( QPixmap( ":/volume-low" ) );
862     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
863         volMuteLabel->setPixmap( QPixmap( ":/volume-high" ) );
864     else volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
865     volMuteLabel->setToolTip( qtr( "Mute" ) );
866 }
867
868 void ControlsWidget::updateVolume()
869 {
870     /* Audio part */
871     audio_volume_t i_volume;
872     aout_VolumeGet( p_intf, &i_volume );
873     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
874     int i_gauge = volumeSlider->value();
875     b_my_volume = false;
876     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
877     {
878         b_my_volume = true;
879         volumeSlider->setValue( i_volume );
880         b_my_volume = false;
881     }
882 }
883
884 void ControlsWidget::updateInput()
885 {
886     /* Activate the interface buttons according to the presence of the input */
887     enableInput( THEMIM->getIM()->hasInput() );
888     enableVideo( THEMIM->getIM()->hasVideo() && THEMIM->getIM()->hasInput() );
889 }
890
891 void ControlsWidget::setStatus( int status )
892 {
893     if( status == PLAYING_S ) /* Playing */
894     {
895         playButton->setIcon( QIcon( ":/pause_b" ) );
896         playButton->setToolTip( qtr( "Pause the playback" ) );
897     }
898     else
899     {
900         playButton->setIcon( QIcon( ":/play_b" ) );
901         playButton->setToolTip( qtr( I_PLAY_TOOLTIP ) );
902     }
903 }
904
905 /**
906  * TODO
907  * This functions toggle the fullscreen mode
908  * If there is no video, it should first activate Visualisations...
909  *  This has also to be fixed in enableVideo()
910  */
911 void ControlsWidget::fullscreen()
912 {
913     vout_thread_t *p_vout =
914         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
915     if( p_vout)
916     {
917         var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
918         vlc_object_release( p_vout );
919     }
920 }
921
922 void ControlsWidget::extSettings()
923 {
924     THEDP->extendedDialog();
925 }
926
927 void ControlsWidget::slower()
928 {
929     THEMIM->getIM()->slower();
930 }
931
932 void ControlsWidget::faster()
933 {
934     THEMIM->getIM()->faster();
935 }
936
937 void ControlsWidget::enableInput( bool enable )
938 {
939     slowerButton->setEnabled( enable );
940     slider->setEnabled( enable );
941     slider->setSliderPosition ( 0 );
942     fasterButton->setEnabled( enable );
943
944     /* Advanced Buttons too */
945     advControls->enableInput( enable );
946 }
947
948 void ControlsWidget::enableVideo( bool enable )
949 {
950     // TODO Later make the fullscreenButton toggle Visualisation and so on.
951     fullscreenButton->setEnabled( enable );
952
953     /* Advanced Buttons too */
954     advControls->enableVideo( enable );
955 }
956
957 void ControlsWidget::toggleAdvanced()
958 {
959     if( advControls && !b_advancedVisible )
960     {
961         advControls->show();
962         b_advancedVisible = true;
963     }
964     else
965     {
966         advControls->hide();
967         b_advancedVisible = false;
968     }
969     emit advancedControlsToggled( b_advancedVisible );
970 }
971
972
973 /**********************************************************************
974  * Fullscrenn control widget
975  **********************************************************************/
976 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i,
977         MainInterface *_p_mi, bool b_advControls, bool b_shiny )
978         : ControlsWidget( _p_i, _p_mi, b_advControls, b_shiny, true ),
979           i_mouse_last_x( -1 ), i_mouse_last_y( -1 ), b_mouse_over(false),
980           b_slow_hide_begin(false), i_slow_hide_timeout(1),
981           b_fullscreen( false ), i_hide_timeout( 1 ), p_vout(NULL)
982 {
983     setWindowFlags( Qt::ToolTip );
984
985     setFrameShape( QFrame::StyledPanel );
986     setFrameStyle( QFrame::Sunken );
987     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
988
989     QGridLayout *fsLayout = new QGridLayout( this );
990     fsLayout->setLayoutMargins( 5, 1, 5, 1, 5 );
991
992     /* First line */
993     slider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum);
994     fsLayout->addWidget( slowerButton, 0, 0 );
995     fsLayout->addWidget( slider, 0, 1, 1, 8 );
996     fsLayout->addWidget( fasterButton, 0, 9 );
997
998     fsLayout->addWidget( playButton, 1, 0, 1, 2 );
999     fsLayout->addLayout( controlButLayout, 1, 2 );
1000
1001     fsLayout->addWidget( discFrame, 1, 3 );
1002     fsLayout->addWidget( telexFrame, 1, 4 );
1003     fsLayout->addWidget( fullscreenButton, 1, 5 );
1004     fsLayout->addWidget( advControls, 1, 6, Qt::AlignVCenter );
1005
1006     fsLayout->addWidget( volMuteLabel, 1, 7 );
1007     fsLayout->addWidget( volumeSlider, 1, 8, 1, 2 );
1008
1009     /* hiding timer */
1010     p_hideTimer = new QTimer( this );
1011     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
1012     p_hideTimer->setSingleShot( true );
1013
1014     /* slow hiding timer */
1015 #if HAVE_TRANSPARENCY
1016     p_slowHideTimer = new QTimer( this );
1017     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
1018 #endif
1019
1020     adjustSize ();  /* need to get real width and height for moving */
1021
1022     /* center down */
1023     QDesktopWidget * p_desktop = QApplication::desktop();
1024
1025     move( p_desktop->width() / 2 - width() / 2,
1026           p_desktop->height() - height() );
1027
1028 #ifdef WIN32TRICK
1029     setWindowOpacity( 0.0 );
1030     b_fscHidden = true;
1031     adjustSize();
1032     show();
1033 #endif
1034
1035     fullscreenButton->setIcon( QIcon( ":/defullscreen" ) );
1036
1037     vlc_mutex_init_recursive( &lock );
1038 }
1039
1040 FullscreenControllerWidget::~FullscreenControllerWidget()
1041 {
1042     detachVout();
1043     vlc_mutex_destroy( &lock );
1044 }
1045
1046 /**
1047  * Show fullscreen controller
1048  */
1049 void FullscreenControllerWidget::showFSC()
1050 {
1051     adjustSize();
1052 #ifdef WIN32TRICK
1053     // after quiting and going to fs, we need to call show()
1054     if( isHidden() )
1055         show();
1056
1057     if( b_fscHidden )
1058     {
1059         b_fscHidden = false;
1060         setWindowOpacity( 1.0 );
1061     }
1062 #else
1063     show();
1064 #endif
1065
1066 #if HAVE_TRANSPARENCY
1067     setWindowOpacity( DEFAULT_OPACITY );
1068 #endif
1069 }
1070
1071 /**
1072  * Hide fullscreen controller
1073  * FIXME: under windows it have to be done by moving out of screen
1074  *        because hide() doesnt work
1075  */
1076 void FullscreenControllerWidget::hideFSC()
1077 {
1078 #ifdef WIN32TRICK
1079     b_fscHidden = true;
1080     setWindowOpacity( 0.0 );    // simulate hidding
1081 #else
1082     hide();
1083 #endif
1084 }
1085
1086 /**
1087  * Plane to hide fullscreen controller
1088  */
1089 void FullscreenControllerWidget::planHideFSC()
1090 {
1091     vlc_mutex_lock( &lock );
1092     int i_timeout = i_hide_timeout;
1093     vlc_mutex_unlock( &lock );
1094
1095     p_hideTimer->start( i_timeout );
1096
1097 #if HAVE_TRANSPARENCY
1098     b_slow_hide_begin = true;
1099     i_slow_hide_timeout = i_timeout;
1100     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
1101 #endif
1102 }
1103
1104 /**
1105  * Hidding fullscreen controller slowly
1106  * Linux: need composite manager
1107  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
1108  */
1109 void FullscreenControllerWidget::slowHideFSC()
1110 {
1111 #if HAVE_TRANSPARENCY
1112     if( b_slow_hide_begin )
1113     {
1114         b_slow_hide_begin = false;
1115
1116         p_slowHideTimer->stop();
1117         /* the last part of time divided to 100 pieces */
1118         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
1119
1120     }
1121     else
1122     {
1123 #ifdef WIN32TRICK
1124          if ( windowOpacity() > 0.0 && !b_fscHidden )
1125 #else
1126          if ( windowOpacity() > 0.0 )
1127 #endif
1128          {
1129              /* we should use 0.01 because of 100 pieces ^^^
1130                 but than it cannt be done in time */
1131              setWindowOpacity( windowOpacity() - 0.02 );
1132          }
1133
1134          if ( windowOpacity() <= 0.0 )
1135              p_slowHideTimer->stop();
1136     }
1137 #endif
1138 }
1139
1140 /**
1141  * event handling
1142  * events: show, hide, start timer for hidding
1143  */
1144 void FullscreenControllerWidget::customEvent( QEvent *event )
1145 {
1146     bool b_fs;
1147
1148     switch( event->type() )
1149     {
1150         case FullscreenControlToggle_Type:
1151             vlc_mutex_lock( &lock );
1152             b_fs = b_fullscreen;
1153             vlc_mutex_unlock( &lock );
1154             if( b_fs )
1155 #ifdef WIN32TRICK
1156                 if( b_fscHidden )
1157 #else
1158                 if( isHidden() )
1159 #endif
1160                 {
1161                     p_hideTimer->stop();
1162                     showFSC();
1163                 }
1164                 else
1165                     hideFSC();
1166             break;
1167         case FullscreenControlShow_Type:
1168             vlc_mutex_lock( &lock );
1169             b_fs = b_fullscreen;
1170             vlc_mutex_unlock( &lock );
1171
1172             if( b_fs )  // FIXME I am not sure about that one
1173                 showFSC();
1174             break;
1175         case FullscreenControlHide_Type:
1176             hideFSC();
1177             break;
1178         case FullscreenControlPlanHide_Type:
1179             if( !b_mouse_over ) // Only if the mouse is not over FSC
1180                 planHideFSC();
1181             break;
1182     }
1183 }
1184
1185 /**
1186  * On mouse move
1187  * moving with FSC
1188  */
1189 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
1190 {
1191     if ( event->buttons() == Qt::LeftButton )
1192     {
1193         int i_moveX = event->globalX() - i_mouse_last_x;
1194         int i_moveY = event->globalY() - i_mouse_last_y;
1195
1196         move( x() + i_moveX, y() + i_moveY );
1197
1198         i_mouse_last_x = event->globalX();
1199         i_mouse_last_y = event->globalY();
1200     }
1201 }
1202
1203 /**
1204  * On mouse press
1205  * store position of cursor
1206  */
1207 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
1208 {
1209     i_mouse_last_x = event->globalX();
1210     i_mouse_last_y = event->globalY();
1211 }
1212
1213 /**
1214  * On mouse go above FSC
1215  */
1216 void FullscreenControllerWidget::enterEvent( QEvent *event )
1217 {
1218     b_mouse_over = true;
1219
1220     p_hideTimer->stop();
1221 #if HAVE_TRANSPARENCY
1222     p_slowHideTimer->stop();
1223 #endif
1224 }
1225
1226 /**
1227  * On mouse go out from FSC
1228  */
1229 void FullscreenControllerWidget::leaveEvent( QEvent *event )
1230 {
1231     planHideFSC();
1232
1233     b_mouse_over = false;
1234 }
1235
1236 /**
1237  * When you get pressed key, send it to video output
1238  * FIXME: clearing focus by clearFocus() to not getting
1239  * key press events didnt work
1240  */
1241 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
1242 {
1243     int i_vlck = qtEventToVLCKey( event );
1244     if( i_vlck > 0 )
1245     {
1246         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1247         event->accept();
1248     }
1249     else
1250         event->ignore();
1251 }
1252
1253 /* */
1254 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object, const char *variable,
1255                                                         vlc_value_t old_val, vlc_value_t new_val,
1256                                                         void *data )
1257 {
1258     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
1259     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1260
1261     p_fs->fullscreenChanged( p_vout, new_val.b_bool, var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1262
1263     return VLC_SUCCESS;
1264 }
1265 /* */
1266 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
1267                                                  vlc_value_t old_val, vlc_value_t new_val,
1268                                                  void *data )
1269 {
1270     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1271
1272     /* Show event */
1273     IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1274     QApplication::postEvent( p_fs, static_cast<QEvent *>(eShow) );
1275
1276     /* Plan hide event */
1277     IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1278     QApplication::postEvent( p_fs, static_cast<QEvent *>(eHide) );
1279
1280     return VLC_SUCCESS;
1281 }
1282
1283
1284 /**
1285  * It is called when video start
1286  */
1287 void FullscreenControllerWidget::attachVout( vout_thread_t *p_nvout )
1288 {
1289     assert( p_nvout && !p_vout );
1290
1291     p_vout = p_nvout;
1292
1293     vlc_mutex_lock( &lock );
1294     var_AddCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this ); /* I miss a add and fire */
1295     fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ), var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1296     vlc_mutex_unlock( &lock );
1297 }
1298 /**
1299  * It is called after turn off video.
1300  */
1301 void FullscreenControllerWidget::detachVout()
1302 {
1303     if( p_vout )
1304     {
1305         var_DelCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this );
1306         vlc_mutex_lock( &lock );
1307         fullscreenChanged( p_vout, false, 0 );
1308         vlc_mutex_unlock( &lock );
1309         p_vout = NULL;
1310     }
1311 }
1312
1313 /**
1314  * Register and unregister callback for mouse moving
1315  */
1316 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout, bool b_fs, int i_timeout )
1317 {
1318     vlc_mutex_lock( &lock );
1319     if( b_fs && !b_fullscreen )
1320     {
1321         b_fullscreen = true;
1322         i_hide_timeout = i_timeout;
1323         var_AddCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1324     }
1325     else if( !b_fs && b_fullscreen )
1326     {
1327         b_fullscreen = false;
1328         i_hide_timeout = i_timeout;
1329         var_DelCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1330
1331         /* Force fs hidding */
1332         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1333         QApplication::postEvent( this, static_cast<QEvent *>(eHide) );
1334     }
1335     vlc_mutex_unlock( &lock );
1336 }
1337
1338 /**********************************************************************
1339  * Speed control widget
1340  **********************************************************************/
1341 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
1342                              QFrame( NULL ), p_intf( _p_i )
1343 {
1344     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
1345     sizePolicy.setHorizontalStretch( 0 );
1346     sizePolicy.setVerticalStretch( 0 );
1347
1348     speedSlider = new QSlider;
1349     speedSlider->setSizePolicy( sizePolicy );
1350     speedSlider->setMaximumSize( QSize( 80, 200 ) );
1351     speedSlider->setOrientation( Qt::Vertical );
1352     speedSlider->setTickPosition( QSlider::TicksRight );
1353
1354     speedSlider->setRange( -24, 24 );
1355     speedSlider->setSingleStep( 1 );
1356     speedSlider->setPageStep( 1 );
1357     speedSlider->setTickInterval( 12 );
1358
1359     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
1360
1361     QToolButton *normalSpeedButton = new QToolButton( this );
1362     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
1363     normalSpeedButton->setAutoRaise( true );
1364     normalSpeedButton->setText( "1x" );
1365     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
1366
1367     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
1368
1369     QVBoxLayout *speedControlLayout = new QVBoxLayout;
1370     speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
1371     speedControlLayout->setSpacing( 4 );
1372     speedControlLayout->addWidget( speedSlider );
1373     speedControlLayout->addWidget( normalSpeedButton );
1374     setLayout( speedControlLayout );
1375 }
1376
1377 SpeedControlWidget::~SpeedControlWidget()
1378 {}
1379
1380 void SpeedControlWidget::setEnable( bool b_enable )
1381 {
1382     speedSlider->setEnabled( b_enable );
1383 }
1384
1385 void SpeedControlWidget::updateControls( int rate )
1386 {
1387     if( speedSlider->isSliderDown() )
1388     {
1389         //We don't want to change anything if the user is using the slider
1390         return;
1391     }
1392
1393     double value = 12 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
1394     int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
1395
1396     if( sliderValue < speedSlider->minimum() )
1397     {
1398         sliderValue = speedSlider->minimum();
1399     }
1400     else if( sliderValue > speedSlider->maximum() )
1401     {
1402         sliderValue = speedSlider->maximum();
1403     }
1404
1405     //Block signals to avoid feedback loop
1406     speedSlider->blockSignals( true );
1407     speedSlider->setValue( sliderValue );
1408     speedSlider->blockSignals( false );
1409 }
1410
1411 void SpeedControlWidget::updateRate( int sliderValue )
1412 {
1413     double speed = pow( 2, (double)sliderValue / 12 );
1414     int rate = INPUT_RATE_DEFAULT / speed;
1415
1416     THEMIM->getIM()->setRate(rate);
1417 }
1418
1419 void SpeedControlWidget::resetRate()
1420 {
1421     THEMIM->getIM()->setRate(INPUT_RATE_DEFAULT);
1422 }