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