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