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