]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Qt: hide record button when the record access-filter is not present
[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     /* Record Button */
340     recordButton = new QPushButton;
341     setupSmallButton( recordButton );
342     advLayout->addWidget( recordButton );
343     BUTTON_SET_ACT_I( recordButton, "", record,
344             qtr( "Record" ), record() );
345
346     /* Snapshot Button */
347     snapshotButton = new QPushButton;
348     setupSmallButton( snapshotButton );
349     advLayout->addWidget( snapshotButton );
350     BUTTON_SET_ACT_I( snapshotButton, "", snapshot,
351             qtr( "Take a snapshot" ), snapshot() );
352 }
353
354 AdvControlsWidget::~AdvControlsWidget()
355 {}
356
357 void AdvControlsWidget::enableInput( bool enable )
358 {
359     int i_input_id = 0;
360     if( THEMIM->getInput() != NULL )
361     {
362         input_item_t *p_item = input_GetItem( THEMIM->getInput() );
363         i_input_id = p_item->i_id;
364
365         if( var_Type( THEMIM->getInput(), "record-toggle" ) == VLC_VAR_VOID )
366             recordButton->setVisible( true );
367         else
368             recordButton->setVisible( false );
369     }
370     else
371         recordButton->setVisible( false );
372
373     ABButton->setEnabled( enable );
374     recordButton->setEnabled( enable );
375
376     if( enable && ( i_last_input_id != i_input_id ) )
377     {
378         timeA = timeB = 0;
379         i_last_input_id = i_input_id;
380         emit timeChanged();
381     }
382 }
383
384 void AdvControlsWidget::enableVideo( bool enable )
385 {
386     snapshotButton->setEnabled( enable );
387 #if 0
388     frameButton->setEnabled( enable );
389 #endif
390 }
391
392 void AdvControlsWidget::snapshot()
393 {
394     vout_thread_t *p_vout =
395         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
396     if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
397 }
398
399 /* Function called when the button is clicked() */
400 void AdvControlsWidget::fromAtoB()
401 {
402     if( !timeA )
403     {
404         timeA = var_GetTime( THEMIM->getInput(), "time"  );
405         emit timeChanged();
406         return;
407     }
408     if( !timeB )
409     {
410         timeB = var_GetTime( THEMIM->getInput(), "time"  );
411         var_SetTime( THEMIM->getInput(), "time" , timeA );
412         emit timeChanged();
413         return;
414     }
415     timeA = 0;
416     timeB = 0;
417     emit timeChanged();
418 }
419
420 /* setting/synchro icons after click on main or fs controller */
421 void AdvControlsWidget::setIcon()
422 {
423     if( !timeA && !timeB)
424     {
425         ABButton->setIcon( QIcon( ":/atob_nob" ) );
426         ABButton->setToolTip( qtr( "Loop from point A to point B continuously\nClick to set point A" ) );
427     }
428     else if( timeA && !timeB )
429     {
430         ABButton->setIcon( QIcon( ":/atob_noa" ) );
431         ABButton->setToolTip( qtr( "Click to set point B" ) );
432     }
433     else if( timeA && timeB )
434     {
435         ABButton->setIcon( QIcon( ":/atob" ) );
436         ABButton->setToolTip( qtr( "Stop the A to B loop" ) );
437     }
438 }
439
440 /* Function called regularly when in an AtoB loop */
441 void AdvControlsWidget::AtoBLoop( float f_pos, int i_time, int i_length )
442 {
443     if( timeB )
444     {
445         if( ( i_time >= (int)( timeB/1000000 ) )
446             || ( i_time < (int)( timeA/1000000 ) ) )
447             var_SetTime( THEMIM->getInput(), "time" , timeA );
448     }
449 }
450
451 /* FIXME Record function */
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         if( var_Type( p_input, "record-toggle" ) == VLC_VAR_VOID )
459             var_TriggerCallback( p_input, "record-toggle" );
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( qfu( HELP_PCH ) );
845         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
846         menuButton->show();
847         discFrame->show();
848     } else {
849         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
850         nextSectionButton->setToolTip( qfu( 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, 1, 5, 1, 5 );
1002
1003     /* First line */
1004     slider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum);
1005     fsLayout->addWidget( slowerButton, 0, 0 );
1006     fsLayout->addWidget( slider, 0, 1, 1, 8 );
1007     fsLayout->addWidget( fasterButton, 0, 9 );
1008
1009     fsLayout->addWidget( playButton, 1, 0, 1, 2 );
1010     fsLayout->addLayout( controlButLayout, 1, 2 );
1011
1012     fsLayout->addWidget( discFrame, 1, 3 );
1013     fsLayout->addWidget( telexFrame, 1, 4 );
1014     fsLayout->addWidget( fullscreenButton, 1, 5 );
1015     fsLayout->addWidget( advControls, 1, 6, Qt::AlignVCenter );
1016
1017     fsLayout->addWidget( volMuteLabel, 1, 7 );
1018     fsLayout->addWidget( volumeSlider, 1, 8, 1, 2 );
1019
1020     /* hiding timer */
1021     p_hideTimer = new QTimer( this );
1022     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
1023     p_hideTimer->setSingleShot( true );
1024
1025     /* slow hiding timer */
1026 #if HAVE_TRANSPARENCY
1027     p_slowHideTimer = new QTimer( this );
1028     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
1029 #endif
1030
1031     adjustSize ();  /* need to get real width and height for moving */
1032
1033     /* center down */
1034     QDesktopWidget * p_desktop = QApplication::desktop();
1035
1036     move( p_desktop->width() / 2 - width() / 2,
1037           p_desktop->height() - height() );
1038
1039 #ifdef WIN32TRICK
1040     setWindowOpacity( 0.0 );
1041     b_fscHidden = true;
1042     adjustSize();
1043     show();
1044 #endif
1045
1046     fullscreenButton->setIcon( QIcon( ":/defullscreen" ) );
1047
1048     vlc_mutex_init_recursive( &lock );
1049 }
1050
1051 FullscreenControllerWidget::~FullscreenControllerWidget()
1052 {
1053     detachVout();
1054     vlc_mutex_destroy( &lock );
1055 }
1056
1057 /**
1058  * Show fullscreen controller
1059  */
1060 void FullscreenControllerWidget::showFSC()
1061 {
1062     adjustSize();
1063 #ifdef WIN32TRICK
1064     // after quiting and going to fs, we need to call show()
1065     if( isHidden() )
1066         show();
1067
1068     if( b_fscHidden )
1069     {
1070         b_fscHidden = false;
1071         setWindowOpacity( 1.0 );
1072     }
1073 #else
1074     show();
1075 #endif
1076
1077 #if HAVE_TRANSPARENCY
1078     setWindowOpacity( DEFAULT_OPACITY );
1079 #endif
1080 }
1081
1082 /**
1083  * Hide fullscreen controller
1084  * FIXME: under windows it have to be done by moving out of screen
1085  *        because hide() doesnt work
1086  */
1087 void FullscreenControllerWidget::hideFSC()
1088 {
1089 #ifdef WIN32TRICK
1090     b_fscHidden = true;
1091     setWindowOpacity( 0.0 );    // simulate hidding
1092 #else
1093     hide();
1094 #endif
1095 }
1096
1097 /**
1098  * Plane to hide fullscreen controller
1099  */
1100 void FullscreenControllerWidget::planHideFSC()
1101 {
1102     vlc_mutex_lock( &lock );
1103     int i_timeout = i_hide_timeout;
1104     vlc_mutex_unlock( &lock );
1105
1106     p_hideTimer->start( i_timeout );
1107
1108 #if HAVE_TRANSPARENCY
1109     b_slow_hide_begin = true;
1110     i_slow_hide_timeout = i_timeout;
1111     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
1112 #endif
1113 }
1114
1115 /**
1116  * Hidding fullscreen controller slowly
1117  * Linux: need composite manager
1118  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
1119  */
1120 void FullscreenControllerWidget::slowHideFSC()
1121 {
1122 #if HAVE_TRANSPARENCY
1123     if( b_slow_hide_begin )
1124     {
1125         b_slow_hide_begin = false;
1126
1127         p_slowHideTimer->stop();
1128         /* the last part of time divided to 100 pieces */
1129         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
1130
1131     }
1132     else
1133     {
1134 #ifdef WIN32TRICK
1135          if ( windowOpacity() > 0.0 && !b_fscHidden )
1136 #else
1137          if ( windowOpacity() > 0.0 )
1138 #endif
1139          {
1140              /* we should use 0.01 because of 100 pieces ^^^
1141                 but than it cannt be done in time */
1142              setWindowOpacity( windowOpacity() - 0.02 );
1143          }
1144
1145          if ( windowOpacity() <= 0.0 )
1146              p_slowHideTimer->stop();
1147     }
1148 #endif
1149 }
1150
1151 /**
1152  * event handling
1153  * events: show, hide, start timer for hidding
1154  */
1155 void FullscreenControllerWidget::customEvent( QEvent *event )
1156 {
1157     bool b_fs;
1158
1159     switch( event->type() )
1160     {
1161         case FullscreenControlToggle_Type:
1162             vlc_mutex_lock( &lock );
1163             b_fs = b_fullscreen;
1164             vlc_mutex_unlock( &lock );
1165             if( b_fs )
1166 #ifdef WIN32TRICK
1167                 if( b_fscHidden )
1168 #else
1169                 if( isHidden() )
1170 #endif
1171                 {
1172                     p_hideTimer->stop();
1173                     showFSC();
1174                 }
1175                 else
1176                     hideFSC();
1177             break;
1178         case FullscreenControlShow_Type:
1179             vlc_mutex_lock( &lock );
1180             b_fs = b_fullscreen;
1181             vlc_mutex_unlock( &lock );
1182
1183             if( b_fs )  // FIXME I am not sure about that one
1184                 showFSC();
1185             break;
1186         case FullscreenControlHide_Type:
1187             hideFSC();
1188             break;
1189         case FullscreenControlPlanHide_Type:
1190             if( !b_mouse_over ) // Only if the mouse is not over FSC
1191                 planHideFSC();
1192             break;
1193     }
1194 }
1195
1196 /**
1197  * On mouse move
1198  * moving with FSC
1199  */
1200 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
1201 {
1202     if ( event->buttons() == Qt::LeftButton )
1203     {
1204         int i_moveX = event->globalX() - i_mouse_last_x;
1205         int i_moveY = event->globalY() - i_mouse_last_y;
1206
1207         move( x() + i_moveX, y() + i_moveY );
1208
1209         i_mouse_last_x = event->globalX();
1210         i_mouse_last_y = event->globalY();
1211     }
1212 }
1213
1214 /**
1215  * On mouse press
1216  * store position of cursor
1217  */
1218 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
1219 {
1220     i_mouse_last_x = event->globalX();
1221     i_mouse_last_y = event->globalY();
1222 }
1223
1224 /**
1225  * On mouse go above FSC
1226  */
1227 void FullscreenControllerWidget::enterEvent( QEvent *event )
1228 {
1229     b_mouse_over = true;
1230
1231     p_hideTimer->stop();
1232 #if HAVE_TRANSPARENCY
1233     p_slowHideTimer->stop();
1234 #endif
1235 }
1236
1237 /**
1238  * On mouse go out from FSC
1239  */
1240 void FullscreenControllerWidget::leaveEvent( QEvent *event )
1241 {
1242     planHideFSC();
1243
1244     b_mouse_over = false;
1245 }
1246
1247 /**
1248  * When you get pressed key, send it to video output
1249  * FIXME: clearing focus by clearFocus() to not getting
1250  * key press events didnt work
1251  */
1252 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
1253 {
1254     int i_vlck = qtEventToVLCKey( event );
1255     if( i_vlck > 0 )
1256     {
1257         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1258         event->accept();
1259     }
1260     else
1261         event->ignore();
1262 }
1263
1264 /* */
1265 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object, const char *variable,
1266                                                         vlc_value_t old_val, vlc_value_t new_val,
1267                                                         void *data )
1268 {
1269     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
1270     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1271
1272     p_fs->fullscreenChanged( p_vout, new_val.b_bool, var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1273
1274     return VLC_SUCCESS;
1275 }
1276 /* */
1277 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
1278                                                  vlc_value_t old_val, vlc_value_t new_val,
1279                                                  void *data )
1280 {
1281     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1282
1283     /* Show event */
1284     IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1285     QApplication::postEvent( p_fs, static_cast<QEvent *>(eShow) );
1286
1287     /* Plan hide event */
1288     IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1289     QApplication::postEvent( p_fs, static_cast<QEvent *>(eHide) );
1290
1291     return VLC_SUCCESS;
1292 }
1293
1294
1295 /**
1296  * It is called when video start
1297  */
1298 void FullscreenControllerWidget::attachVout( vout_thread_t *p_nvout )
1299 {
1300     assert( p_nvout && !p_vout );
1301
1302     p_vout = p_nvout;
1303
1304     vlc_mutex_lock( &lock );
1305     var_AddCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this ); /* I miss a add and fire */
1306     fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ), var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1307     vlc_mutex_unlock( &lock );
1308 }
1309 /**
1310  * It is called after turn off video.
1311  */
1312 void FullscreenControllerWidget::detachVout()
1313 {
1314     if( p_vout )
1315     {
1316         var_DelCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this );
1317         vlc_mutex_lock( &lock );
1318         fullscreenChanged( p_vout, false, 0 );
1319         vlc_mutex_unlock( &lock );
1320         p_vout = NULL;
1321     }
1322 }
1323
1324 /**
1325  * Register and unregister callback for mouse moving
1326  */
1327 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout, bool b_fs, int i_timeout )
1328 {
1329     vlc_mutex_lock( &lock );
1330     if( b_fs && !b_fullscreen )
1331     {
1332         b_fullscreen = true;
1333         i_hide_timeout = i_timeout;
1334         var_AddCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1335     }
1336     else if( !b_fs && b_fullscreen )
1337     {
1338         b_fullscreen = false;
1339         i_hide_timeout = i_timeout;
1340         var_DelCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1341
1342         /* Force fs hidding */
1343         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1344         QApplication::postEvent( this, static_cast<QEvent *>(eHide) );
1345     }
1346     vlc_mutex_unlock( &lock );
1347 }
1348
1349 /**********************************************************************
1350  * Speed control widget
1351  **********************************************************************/
1352 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
1353                              QFrame( NULL ), p_intf( _p_i )
1354 {
1355     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
1356     sizePolicy.setHorizontalStretch( 0 );
1357     sizePolicy.setVerticalStretch( 0 );
1358
1359     speedSlider = new QSlider;
1360     speedSlider->setSizePolicy( sizePolicy );
1361     speedSlider->setMaximumSize( QSize( 80, 200 ) );
1362     speedSlider->setOrientation( Qt::Vertical );
1363     speedSlider->setTickPosition( QSlider::TicksRight );
1364
1365     speedSlider->setRange( -24, 24 );
1366     speedSlider->setSingleStep( 1 );
1367     speedSlider->setPageStep( 1 );
1368     speedSlider->setTickInterval( 12 );
1369
1370     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
1371
1372     QToolButton *normalSpeedButton = new QToolButton( this );
1373     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
1374     normalSpeedButton->setAutoRaise( true );
1375     normalSpeedButton->setText( "1x" );
1376     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
1377
1378     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
1379
1380     QVBoxLayout *speedControlLayout = new QVBoxLayout;
1381     speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
1382     speedControlLayout->setSpacing( 4 );
1383     speedControlLayout->addWidget( speedSlider );
1384     speedControlLayout->addWidget( normalSpeedButton );
1385     setLayout( speedControlLayout );
1386 }
1387
1388 SpeedControlWidget::~SpeedControlWidget()
1389 {}
1390
1391 void SpeedControlWidget::setEnable( bool b_enable )
1392 {
1393     speedSlider->setEnabled( b_enable );
1394 }
1395
1396 void SpeedControlWidget::updateControls( int rate )
1397 {
1398     if( speedSlider->isSliderDown() )
1399     {
1400         //We don't want to change anything if the user is using the slider
1401         return;
1402     }
1403
1404     double value = 12 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
1405     int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
1406
1407     if( sliderValue < speedSlider->minimum() )
1408     {
1409         sliderValue = speedSlider->minimum();
1410     }
1411     else if( sliderValue > speedSlider->maximum() )
1412     {
1413         sliderValue = speedSlider->maximum();
1414     }
1415
1416     //Block signals to avoid feedback loop
1417     speedSlider->blockSignals( true );
1418     speedSlider->setValue( sliderValue );
1419     speedSlider->blockSignals( false );
1420 }
1421
1422 void SpeedControlWidget::updateRate( int sliderValue )
1423 {
1424     double speed = pow( 2, (double)sliderValue / 12 );
1425     int rate = INPUT_RATE_DEFAULT / speed;
1426
1427     THEMIM->getIM()->setRate(rate);
1428 }
1429
1430 void SpeedControlWidget::resetRate()
1431 {
1432     THEMIM->getIM()->setRate(INPUT_RATE_DEFAULT);
1433 }