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