]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller.cpp
Qt4: Split the controllers from the rest of the interface widgets.
[vlc] / modules / gui / qt4 / components / controller.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 #define I_PLAY_TOOLTIP N_("Play\nIf the playlist is empty, open a media")
53
54 /**********************************************************************
55  * TEH controls
56  **********************************************************************/
57
58 static void setupSmallButton( QPushButton *aButton )
59 {
60     aButton->setMaximumSize( QSize( 26, 26 ) );
61     aButton->setMinimumSize( QSize( 26, 26 ) );
62     aButton->setIconSize( QSize( 20, 20 ) );
63     aButton->setFocusPolicy( Qt::NoFocus );
64 }
65
66 /* init static variables in advanced controls */
67 mtime_t AdvControlsWidget::timeA = 0;
68 mtime_t AdvControlsWidget::timeB = 0;
69
70 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i, bool b_fsCreation = false ) :
71                                            QFrame( NULL ), p_intf( _p_i )
72 {
73     QHBoxLayout *advLayout = new QHBoxLayout( this );
74     advLayout->setMargin( 0 );
75     advLayout->setSpacing( 0 );
76     advLayout->setAlignment( Qt::AlignBottom );
77
78     /* A to B Button */
79     ABButton = new QPushButton;
80     setupSmallButton( ABButton );
81     advLayout->addWidget( ABButton );
82     BUTTON_SET_ACT_I( ABButton, "", atob_nob,
83       qtr( "Loop from point A to point B continuously.\nClick to set point A" ),
84       fromAtoB() );
85     timeA = timeB = 0;
86     i_last_input_id = 0;
87     /* in FS controller we skip this, because we dont want to have it double
88        controlled */
89     if( !b_fsCreation )
90         CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
91                  this, AtoBLoop( float, int, int ) );
92     /* set up synchronization between main controller and fs controller */
93     CONNECT( THEMIM->getIM(), advControlsSetIcon(), this, setIcon() );
94     connect( this, SIGNAL( timeChanged() ),
95         THEMIM->getIM(), SIGNAL( advControlsSetIcon()));
96 #if 0
97     frameButton = new QPushButton( "Fr" );
98     frameButton->setMaximumSize( QSize( 26, 26 ) );
99     frameButton->setIconSize( QSize( 20, 20 ) );
100     advLayout->addWidget( frameButton );
101     BUTTON_SET_ACT( frameButton, "Fr", qtr( "Frame by frame" ), frame() );
102 #endif
103
104     /* Record Button */
105     recordButton = new QPushButton;
106     setupSmallButton( recordButton );
107     advLayout->addWidget( recordButton );
108     BUTTON_SET_ACT_I( recordButton, "", record,
109             qtr( "Record" ), record() );
110
111     /* Snapshot Button */
112     snapshotButton = new QPushButton;
113     setupSmallButton( snapshotButton );
114     advLayout->addWidget( snapshotButton );
115     BUTTON_SET_ACT_I( snapshotButton, "", snapshot,
116             qtr( "Take a snapshot" ), snapshot() );
117 }
118
119 AdvControlsWidget::~AdvControlsWidget()
120 {}
121
122 void AdvControlsWidget::enableInput( bool enable )
123 {
124     int i_input_id = 0;
125     if( THEMIM->getInput() != NULL )
126     {
127         input_item_t *p_item = input_GetItem( THEMIM->getInput() );
128         i_input_id = p_item->i_id;
129
130         recordButton->setVisible( var_GetBool( THEMIM->getInput(), "can-record" ) );
131     }
132     else
133     {
134         recordButton->setVisible( false );
135     }
136
137     ABButton->setEnabled( enable );
138     recordButton->setEnabled( enable );
139
140     if( enable && ( i_last_input_id != i_input_id ) )
141     {
142         timeA = timeB = 0;
143         i_last_input_id = i_input_id;
144         emit timeChanged();
145     }
146 }
147
148 void AdvControlsWidget::enableVideo( bool enable )
149 {
150     snapshotButton->setEnabled( enable );
151 #if 0
152     frameButton->setEnabled( enable );
153 #endif
154 }
155
156 void AdvControlsWidget::snapshot()
157 {
158     vout_thread_t *p_vout =
159         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
160     if( p_vout )
161     {
162         vout_Control( p_vout, VOUT_SNAPSHOT );
163         vlc_object_release( p_vout );
164     }
165 }
166
167 /* Function called when the button is clicked() */
168 void AdvControlsWidget::fromAtoB()
169 {
170     if( !timeA )
171     {
172         timeA = var_GetTime( THEMIM->getInput(), "time"  );
173         emit timeChanged();
174         return;
175     }
176     if( !timeB )
177     {
178         timeB = var_GetTime( THEMIM->getInput(), "time"  );
179         var_SetTime( THEMIM->getInput(), "time" , timeA );
180         emit timeChanged();
181         return;
182     }
183     timeA = 0;
184     timeB = 0;
185     emit timeChanged();
186 }
187
188 /* setting/synchro icons after click on main or fs controller */
189 void AdvControlsWidget::setIcon()
190 {
191     if( !timeA && !timeB)
192     {
193         ABButton->setIcon( QIcon( ":/atob_nob" ) );
194         ABButton->setToolTip( qtr( "Loop from point A to point B continuously\nClick to set point A" ) );
195     }
196     else if( timeA && !timeB )
197     {
198         ABButton->setIcon( QIcon( ":/atob_noa" ) );
199         ABButton->setToolTip( qtr( "Click to set point B" ) );
200     }
201     else if( timeA && timeB )
202     {
203         ABButton->setIcon( QIcon( ":/atob" ) );
204         ABButton->setToolTip( qtr( "Stop the A to B loop" ) );
205     }
206 }
207
208 /* Function called regularly when in an AtoB loop */
209 void AdvControlsWidget::AtoBLoop( float f_pos, int i_time, int i_length )
210 {
211     if( timeB )
212     {
213         if( ( i_time >= (int)( timeB/1000000 ) )
214             || ( i_time < (int)( timeA/1000000 ) ) )
215             var_SetTime( THEMIM->getInput(), "time" , timeA );
216     }
217 }
218
219 void AdvControlsWidget::record()
220 {
221     input_thread_t *p_input = THEMIM->getInput();
222     if( p_input )
223     {
224         /* This method won't work fine if the stream can't be cut anywhere */
225         const bool b_recording = var_GetBool( p_input, "record" );
226         var_SetBool( p_input, "record", !b_recording );
227 #if 0
228         else
229         {
230             /* 'record' access-filter is not loaded, we open Save dialog */
231             input_item_t *p_item = input_GetItem( p_input );
232             if( !p_item )
233                 return;
234
235             char *psz = input_item_GetURI( p_item );
236             if( psz )
237                 THEDP->streamingDialog( NULL, psz, true );
238         }
239 #endif
240     }
241 }
242
243 #if 0
244 //FIXME Frame by frame function
245 void AdvControlsWidget::frame(){}
246 #endif
247
248 /*****************************
249  * DA Control Widget !
250  *****************************/
251 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
252                                 MainInterface *_p_mi,
253                                 bool b_advControls,
254                                 bool b_shiny,
255                                 bool b_fsCreation) :
256                                 QFrame( _p_mi ), p_intf( _p_i )
257 {
258     setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Maximum );
259
260     /** The main Slider **/
261     slider = new InputSlider( Qt::Horizontal, NULL );
262     /* Update the position when the IM has changed */
263     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
264              slider, setPosition( float, int, int ) );
265     /* And update the IM, when the position has changed */
266     CONNECT( slider, sliderDragged( float ),
267              THEMIM->getIM(), sliderUpdate( float ) );
268
269     /** Slower and faster Buttons **/
270     slowerButton = new QToolButton;
271     slowerButton->setAutoRaise( true );
272     slowerButton->setMaximumSize( QSize( 26, 20 ) );
273     slowerButton->setFocusPolicy( Qt::NoFocus );
274
275     BUTTON_SET_ACT_I( slowerButton, "", slower, qtr( "Slower" ), slower() );
276
277     fasterButton = new QToolButton;
278     fasterButton->setAutoRaise( true );
279     fasterButton->setMaximumSize( QSize( 26, 20 ) );
280     fasterButton->setFocusPolicy( Qt::NoFocus );
281
282     BUTTON_SET_ACT_I( fasterButton, "", faster, qtr( "Faster" ), faster() );
283
284     /* advanced Controls handling */
285     b_advancedVisible = b_advControls;
286
287     advControls = new AdvControlsWidget( p_intf, b_fsCreation );
288     if( !b_advancedVisible ) advControls->hide();
289
290     /** Disc and Menus handling */
291     discFrame = new QWidget( this );
292
293     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
294     discLayout->setSpacing( 0 );
295     discLayout->setMargin( 0 );
296
297     prevSectionButton = new QPushButton( discFrame );
298     setupSmallButton( prevSectionButton );
299     discLayout->addWidget( prevSectionButton );
300
301     menuButton = new QPushButton( discFrame );
302     setupSmallButton( menuButton );
303     discLayout->addWidget( menuButton );
304
305     nextSectionButton = new QPushButton( discFrame );
306     setupSmallButton( nextSectionButton );
307     discLayout->addWidget( nextSectionButton );
308
309     BUTTON_SET_IMG( prevSectionButton, "", dvd_prev, "" );
310     BUTTON_SET_IMG( nextSectionButton, "", dvd_next, "" );
311     BUTTON_SET_IMG( menuButton, "", dvd_menu, qtr( "Menu" ) );
312
313     discFrame->hide();
314
315     /* Change the navigation button display when the IM navigation changes */
316     CONNECT( THEMIM->getIM(), navigationChanged( int ),
317              this, setNavigation( int ) );
318     /* Changes the IM navigation when triggered on the nav buttons */
319     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
320              sectionPrev() );
321     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
322              sectionNext() );
323     CONNECT( menuButton, clicked(), THEMIM->getIM(),
324              sectionMenu() );
325
326     /**
327      * Telextext QFrame
328      * TODO: Merge with upper menu in a StackLayout
329      **/
330     telexFrame = new QWidget( this );
331     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
332     telexLayout->setSpacing( 0 );
333     telexLayout->setMargin( 0 );
334
335     telexOn = new QPushButton;
336     setupSmallButton( telexOn );
337     telexLayout->addWidget( telexOn );
338
339     telexTransparent = new QPushButton;
340     setupSmallButton( telexTransparent );
341     telexLayout->addWidget( telexTransparent );
342     b_telexTransparent = false;
343
344     telexPage = new QSpinBox;
345     telexPage->setRange( 0, 999 );
346     telexPage->setValue( 100 );
347     telexPage->setAccelerated( true );
348     telexPage->setWrapping( true );
349     telexPage->setAlignment( Qt::AlignRight );
350     telexPage->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
351     telexLayout->addWidget( telexPage );
352
353     telexFrame->hide(); /* default hidden */
354
355     CONNECT( telexPage, valueChanged( int ), THEMIM->getIM(),
356              telexGotoPage( int ) );
357     CONNECT( THEMIM->getIM(), setNewTelexPage( int ),
358               telexPage, setValue( int ) );
359
360     BUTTON_SET_IMG( telexOn, "", tv, qtr( "Teletext on" ) );
361
362     CONNECT( telexOn, clicked(), THEMIM->getIM(),
363              telexToggleButtons() );
364     CONNECT( telexOn, clicked( bool ), THEMIM->getIM(),
365              telexToggle( bool ) );
366     CONNECT( THEMIM->getIM(), toggleTelexButtons(),
367               this, toggleTeletext() );
368     b_telexEnabled = false;
369     telexTransparent->setEnabled( false );
370     telexPage->setEnabled( false );
371
372     BUTTON_SET_IMG( telexTransparent, "", tvtelx, qtr( "Teletext" ) );
373     CONNECT( telexTransparent, clicked( bool ),
374              THEMIM->getIM(), telexSetTransparency() );
375     CONNECT( THEMIM->getIM(), toggleTelexTransparency(),
376               this, toggleTeletextTransparency() );
377     CONNECT( THEMIM->getIM(), teletextEnabled( bool ),
378              this, enableTeletext( bool ) );
379
380     /** Play Buttons **/
381     QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
382     sizePolicy.setHorizontalStretch( 0 );
383     sizePolicy.setVerticalStretch( 0 );
384
385     /* Play */
386     playButton = new QPushButton;
387     playButton->setSizePolicy( sizePolicy );
388     playButton->setMaximumSize( QSize( 32, 32 ) );
389     playButton->setMinimumSize( QSize( 32, 32 ) );
390     playButton->setIconSize( QSize( 26, 26 ) );
391     playButton->setFocusPolicy( Qt::NoFocus );
392
393     /** Prev + Stop + Next Block **/
394     controlButLayout = new QHBoxLayout;
395     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
396
397     /* Prev */
398     QPushButton *prevButton = new QPushButton;
399     prevButton->setSizePolicy( sizePolicy );
400     setupSmallButton( prevButton );
401
402     controlButLayout->addWidget( prevButton );
403
404     /* Stop */
405     QPushButton *stopButton = new QPushButton;
406     stopButton->setSizePolicy( sizePolicy );
407     setupSmallButton( stopButton );
408
409     controlButLayout->addWidget( stopButton );
410
411     /* next */
412     QPushButton *nextButton = new QPushButton;
413     nextButton->setSizePolicy( sizePolicy );
414     setupSmallButton( nextButton );
415
416     controlButLayout->addWidget( nextButton );
417
418     /* Add this block to the main layout */
419
420     BUTTON_SET_ACT_I( playButton, "", play_b, qtr( I_PLAY_TOOLTIP ), play() );
421     BUTTON_SET_ACT_I( prevButton, "" , previous_b,
422                       qtr( "Previous media in the playlist" ), prev() );
423     BUTTON_SET_ACT_I( nextButton, "", next_b,
424                       qtr( "Next media in the playlist" ), next() );
425     BUTTON_SET_ACT_I( stopButton, "", stop_b, qtr( "Stop playback" ), stop() );
426
427     /*
428      * Other first Line buttons
429      */
430     /* */
431     CONNECT( THEMIM->getIM(), voutChanged(bool), this, enableVideo(bool) );
432
433     /** Fullscreen/Visualisation **/
434     fullscreenButton = new QPushButton;
435     BUTTON_SET_ACT_I( fullscreenButton, "", fullscreen,
436             qtr( "Toggle the video in fullscreen" ), fullscreen() );
437     setupSmallButton( fullscreenButton );
438
439     if( !b_fsCreation )
440     {
441         /** Playlist Button **/
442         playlistButton = new QPushButton;
443         setupSmallButton( playlistButton );
444         BUTTON_SET_IMG( playlistButton, "" , playlist, qtr( "Show playlist" ) );
445         CONNECT( playlistButton, clicked(), _p_mi, togglePlaylist() );
446
447         /** extended Settings **/
448         extSettingsButton = new QPushButton;
449         BUTTON_SET_ACT_I( extSettingsButton, "", extended,
450                 qtr( "Show extended settings" ), extSettings() );
451         setupSmallButton( extSettingsButton );
452     }
453
454     /* Volume */
455     hVolLabel = new VolumeClickHandler( p_intf, this );
456
457     volMuteLabel = new QLabel;
458     volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
459     volMuteLabel->installEventFilter( hVolLabel );
460
461     if( b_shiny )
462     {
463         volumeSlider = new SoundSlider( this,
464             config_GetInt( p_intf, "volume-step" ),
465             config_GetInt( p_intf, "qt-volume-complete" ),
466             config_GetPsz( p_intf, "qt-slider-colours" ) );
467     }
468     else
469     {
470         volumeSlider = new QSlider( this );
471         volumeSlider->setOrientation( Qt::Horizontal );
472     }
473     volumeSlider->setMaximumSize( QSize( 200, 40 ) );
474     volumeSlider->setMinimumSize( QSize( 85, 30 ) );
475     volumeSlider->setFocusPolicy( Qt::NoFocus );
476
477     /* Set the volume from the config */
478     volumeSlider->setValue( ( config_GetInt( p_intf, "volume" ) ) *
479                               VOLUME_MAX / (AOUT_VOLUME_MAX/2) );
480
481     /* Force the update at build time in order to have a muted icon if needed */
482     updateVolume( volumeSlider->value() );
483
484     /* Volume control connection */
485     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
486     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
487
488     if( !b_fsCreation )
489     {
490         controlLayout = new QGridLayout( this );
491
492         controlLayout->setSpacing( 0 );
493         controlLayout->setLayoutMargins( 7, 5, 7, 3, 6 );
494
495         controlLayout->addWidget( slider, 0, 1, 1, 18 );
496         controlLayout->addWidget( slowerButton, 0, 0 );
497         controlLayout->addWidget( fasterButton, 0, 19 );
498
499         controlLayout->addWidget( discFrame, 1, 8, 2, 3, Qt::AlignBottom );
500         controlLayout->addWidget( telexFrame, 1, 8, 2, 5, Qt::AlignBottom );
501
502         controlLayout->addWidget( playButton, 2, 0, 2, 2, Qt::AlignBottom );
503         controlLayout->setColumnMinimumWidth( 2, 10 );
504         controlLayout->setColumnStretch( 2, 0 );
505
506         controlLayout->addLayout( controlButLayout, 3, 3, 1, 3, Qt::AlignBottom );
507         /* Column 6 is unused */
508         controlLayout->setColumnStretch( 6, 0 );
509         controlLayout->setColumnStretch( 7, 0 );
510         controlLayout->setColumnMinimumWidth( 7, 10 );
511
512         controlLayout->addWidget( fullscreenButton, 3, 8, Qt::AlignBottom );
513         controlLayout->addWidget( playlistButton, 3, 9, Qt::AlignBottom );
514         controlLayout->addWidget( extSettingsButton, 3, 10, Qt::AlignBottom );
515         controlLayout->setColumnStretch( 11, 0 ); /* telex alignment */
516
517         controlLayout->setColumnStretch( 12, 0 );
518         controlLayout->setColumnMinimumWidth( 12, 10 );
519
520         controlLayout->addWidget( advControls, 3, 13, 1, 3, Qt::AlignBottom );
521
522         controlLayout->setColumnStretch( 16, 10 );
523         controlLayout->setColumnMinimumWidth( 16, 10 );
524
525         controlLayout->addWidget( volMuteLabel, 3, 17, Qt::AlignBottom );
526         controlLayout->addWidget( volumeSlider, 3, 18, 1 , 2, Qt::AlignBottom );
527     }
528
529     updateInput();
530 }
531
532 ControlsWidget::~ControlsWidget()
533 {}
534
535 void ControlsWidget::toggleTeletext()
536 {
537     bool b_enabled = THEMIM->teletextState();
538     if( b_telexEnabled )
539     {
540         telexTransparent->setEnabled( false );
541         telexPage->setEnabled( false );
542         b_telexEnabled = false;
543     }
544     else if( b_enabled )
545     {
546         telexTransparent->setEnabled( true );
547         telexPage->setEnabled( true );
548         b_telexEnabled = true;
549     }
550 }
551
552 void ControlsWidget::enableTeletext( bool b_enable )
553 {
554     telexFrame->setVisible( b_enable );
555     bool b_on = THEMIM->teletextState();
556
557     telexOn->setChecked( b_on );
558     telexTransparent->setEnabled( b_on );
559     telexPage->setEnabled( b_on );
560     b_telexEnabled = b_on;
561 }
562
563 void ControlsWidget::toggleTeletextTransparency()
564 {
565     if( b_telexTransparent )
566     {
567         telexTransparent->setIcon( QIcon( ":/tvtelx" ) );
568         telexTransparent->setToolTip( qtr( "Teletext" ) );
569         b_telexTransparent = false;
570     }
571     else
572     {
573         telexTransparent->setIcon( QIcon( ":/tvtelx-trans" ) );
574         telexTransparent->setToolTip( qtr( "Transparent" ) );
575         b_telexTransparent = true;
576     }
577 }
578
579 void ControlsWidget::stop()
580 {
581     THEMIM->stop();
582 }
583
584 void ControlsWidget::play()
585 {
586     if( THEPL->current.i_size == 0 )
587     {
588         /* The playlist is empty, open a file requester */
589         THEDP->openFileDialog();
590         setStatus( 0 );
591         return;
592     }
593     THEMIM->togglePlayPause();
594 }
595
596 void ControlsWidget::prev()
597 {
598     THEMIM->prev();
599 }
600
601 void ControlsWidget::next()
602 {
603     THEMIM->next();
604 }
605
606 void ControlsWidget::setNavigation( int navigation )
607 {
608 #define HELP_PCH N_( "Previous chapter" )
609 #define HELP_NCH N_( "Next chapter" )
610
611     // 1 = chapter, 2 = title, 0 = no
612     if( navigation == 0 )
613     {
614         discFrame->hide();
615     } else if( navigation == 1 ) {
616         prevSectionButton->setToolTip( qtr( HELP_PCH ) );
617         nextSectionButton->setToolTip( qtr( HELP_NCH ) );
618         menuButton->show();
619         discFrame->show();
620     } else {
621         prevSectionButton->setToolTip( qtr( HELP_PCH ) );
622         nextSectionButton->setToolTip( qtr( HELP_NCH ) );
623         menuButton->hide();
624         discFrame->show();
625     }
626 }
627
628 static bool b_my_volume;
629 void ControlsWidget::updateVolume( int i_sliderVolume )
630 {
631     if( !b_my_volume )
632     {
633         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
634         aout_VolumeSet( p_intf, i_res );
635     }
636     if( i_sliderVolume == 0 )
637     {
638         volMuteLabel->setPixmap( QPixmap(":/volume-muted" ) );
639         volMuteLabel->setToolTip( qtr( "Unmute" ) );
640         return;
641     }
642
643     if( i_sliderVolume < VOLUME_MAX / 3 )
644         volMuteLabel->setPixmap( QPixmap( ":/volume-low" ) );
645     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
646         volMuteLabel->setPixmap( QPixmap( ":/volume-high" ) );
647     else volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
648     volMuteLabel->setToolTip( qtr( "Mute" ) );
649 }
650
651 void ControlsWidget::updateVolume()
652 {
653     /* Audio part */
654     audio_volume_t i_volume;
655     aout_VolumeGet( p_intf, &i_volume );
656     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
657     int i_gauge = volumeSlider->value();
658     b_my_volume = false;
659     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
660     {
661         b_my_volume = true;
662         volumeSlider->setValue( i_volume );
663         b_my_volume = false;
664     }
665 }
666
667 void ControlsWidget::updateInput()
668 {
669     /* Activate the interface buttons according to the presence of the input */
670     enableInput( THEMIM->getIM()->hasInput() );
671     enableVideo( THEMIM->getIM()->hasVideo() );
672 }
673
674 void ControlsWidget::setStatus( int status )
675 {
676     if( status == PLAYING_S ) /* Playing */
677     {
678         playButton->setIcon( QIcon( ":/pause_b" ) );
679         playButton->setToolTip( qtr( "Pause the playback" ) );
680     }
681     else
682     {
683         playButton->setIcon( QIcon( ":/play_b" ) );
684         playButton->setToolTip( qtr( I_PLAY_TOOLTIP ) );
685     }
686 }
687
688 /**
689  * TODO
690  * This functions toggle the fullscreen mode
691  * If there is no video, it should first activate Visualisations...
692  *  This has also to be fixed in enableVideo()
693  */
694 void ControlsWidget::fullscreen()
695 {
696     vout_thread_t *p_vout =
697         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
698     if( p_vout)
699     {
700         var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
701         vlc_object_release( p_vout );
702     }
703 }
704
705 void ControlsWidget::extSettings()
706 {
707     THEDP->extendedDialog();
708 }
709
710 void ControlsWidget::slower()
711 {
712     THEMIM->getIM()->slower();
713 }
714
715 void ControlsWidget::faster()
716 {
717     THEMIM->getIM()->faster();
718 }
719
720 void ControlsWidget::enableInput( bool enable )
721 {
722     slowerButton->setEnabled( enable );
723     slider->setEnabled( enable );
724     slider->setSliderPosition ( 0 );
725     fasterButton->setEnabled( enable );
726
727     /* Advanced Buttons too */
728     advControls->enableInput( enable );
729 }
730
731 void ControlsWidget::enableVideo( bool enable )
732 {
733     // TODO Later make the fullscreenButton toggle Visualisation and so on.
734     fullscreenButton->setEnabled( enable );
735
736     /* Advanced Buttons too */
737     advControls->enableVideo( enable );
738 }
739
740 void ControlsWidget::toggleAdvanced()
741 {
742     if( advControls && !b_advancedVisible )
743     {
744         advControls->show();
745         b_advancedVisible = true;
746     }
747     else
748     {
749         advControls->hide();
750         b_advancedVisible = false;
751     }
752     emit advancedControlsToggled( b_advancedVisible );
753 }
754
755 /**********************************************************************
756  * Fullscrenn control widget
757  **********************************************************************/
758 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i,
759         MainInterface *_p_mi, bool b_advControls, bool b_shiny )
760         : ControlsWidget( _p_i, _p_mi, b_advControls, b_shiny, true ),
761           i_mouse_last_x( -1 ), i_mouse_last_y( -1 ), b_mouse_over(false),
762           b_slow_hide_begin(false), i_slow_hide_timeout(1),
763           b_fullscreen( false ), i_hide_timeout( 1 ), p_vout(NULL)
764 {
765     i_mouse_last_move_x = -1;
766     i_mouse_last_move_y = -1;
767
768     setWindowFlags( Qt::ToolTip );
769     setMinimumWidth( 600 );
770
771     setFrameShape( QFrame::StyledPanel );
772     setFrameStyle( QFrame::Sunken );
773     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
774
775     QGridLayout *fsLayout = new QGridLayout( this );
776     fsLayout->setLayoutMargins( 5, 2, 5, 2, 5 );
777
778     /* First line */
779     slider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum);
780     fsLayout->addWidget( slowerButton, 0, 0 );
781     fsLayout->addWidget( slider, 0, 1, 1, 11 );
782     fsLayout->addWidget( fasterButton, 0, 12 );
783
784     /* Second line */
785     fsLayout->addWidget( playButton, 1, 0, 1, 2 );
786     fsLayout->addLayout( controlButLayout, 1, 2 );
787
788     fsLayout->addWidget( discFrame, 1, 3 );
789     fsLayout->addWidget( telexFrame, 1, 4 );
790     fsLayout->addWidget( fullscreenButton, 1, 5 );
791     fsLayout->addWidget( advControls, 1, 6, Qt::AlignVCenter );
792
793     fsLayout->setColumnStretch( 7, 10 );
794
795     TimeLabel *timeLabel = new TimeLabel( p_intf );
796
797     fsLayout->addWidget( timeLabel, 1, 8 );
798     fsLayout->addWidget( volMuteLabel, 1, 9 );
799     fsLayout->addWidget( volumeSlider, 1, 10, 1, 2 );
800
801     /* hiding timer */
802     p_hideTimer = new QTimer( this );
803     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
804     p_hideTimer->setSingleShot( true );
805
806     /* slow hiding timer */
807 #if HAVE_TRANSPARENCY
808     p_slowHideTimer = new QTimer( this );
809     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
810 #endif
811
812     adjustSize ();  /* need to get real width and height for moving */
813
814     /* center down */
815     QWidget * p_desktop = QApplication::desktop()->screen(
816                 QApplication::desktop()->screenNumber( _p_mi ) );
817
818     QPoint pos = QPoint( p_desktop->width() / 2 - width() / 2,
819           p_desktop->height() - height() );
820
821     getSettings()->beginGroup( "FullScreen" );
822     move( getSettings()->value( "pos", pos ).toPoint() );
823     getSettings()->endGroup();
824
825 #ifdef WIN32TRICK
826     setWindowOpacity( 0.0 );
827     b_fscHidden = true;
828     adjustSize();
829     show();
830 #endif
831
832     fullscreenButton->setIcon( QIcon( ":/defullscreen" ) );
833
834     vlc_mutex_init_recursive( &lock );
835 }
836
837 FullscreenControllerWidget::~FullscreenControllerWidget()
838 {
839     getSettings()->beginGroup( "FullScreen" );
840     getSettings()->setValue( "pos", pos() );
841     getSettings()->endGroup();
842     detachVout();
843     vlc_mutex_destroy( &lock );
844 }
845
846 /**
847  * Show fullscreen controller
848  */
849 void FullscreenControllerWidget::showFSC()
850 {
851     adjustSize();
852 #ifdef WIN32TRICK
853     // after quiting and going to fs, we need to call show()
854     if( isHidden() )
855         show();
856
857     if( b_fscHidden )
858     {
859         b_fscHidden = false;
860         setWindowOpacity( 1.0 );
861     }
862 #else
863     show();
864 #endif
865
866 #if HAVE_TRANSPARENCY
867     setWindowOpacity( DEFAULT_OPACITY );
868 #endif
869 }
870
871 /**
872  * Hide fullscreen controller
873  * FIXME: under windows it have to be done by moving out of screen
874  *        because hide() doesnt work
875  */
876 void FullscreenControllerWidget::hideFSC()
877 {
878 #ifdef WIN32TRICK
879     b_fscHidden = true;
880     setWindowOpacity( 0.0 );    // simulate hidding
881 #else
882     hide();
883 #endif
884 }
885
886 /**
887  * Plane to hide fullscreen controller
888  */
889 void FullscreenControllerWidget::planHideFSC()
890 {
891     vlc_mutex_lock( &lock );
892     int i_timeout = i_hide_timeout;
893     vlc_mutex_unlock( &lock );
894
895     p_hideTimer->start( i_timeout );
896
897 #if HAVE_TRANSPARENCY
898     b_slow_hide_begin = true;
899     i_slow_hide_timeout = i_timeout;
900     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
901 #endif
902 }
903
904 /**
905  * Hidding fullscreen controller slowly
906  * Linux: need composite manager
907  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
908  */
909 void FullscreenControllerWidget::slowHideFSC()
910 {
911 #if HAVE_TRANSPARENCY
912     if( b_slow_hide_begin )
913     {
914         b_slow_hide_begin = false;
915
916         p_slowHideTimer->stop();
917         /* the last part of time divided to 100 pieces */
918         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
919
920     }
921     else
922     {
923 #ifdef WIN32TRICK
924          if ( windowOpacity() > 0.0 && !b_fscHidden )
925 #else
926          if ( windowOpacity() > 0.0 )
927 #endif
928          {
929              /* we should use 0.01 because of 100 pieces ^^^
930                 but than it cannt be done in time */
931              setWindowOpacity( windowOpacity() - 0.02 );
932          }
933
934          if ( windowOpacity() <= 0.0 )
935              p_slowHideTimer->stop();
936     }
937 #endif
938 }
939
940 /**
941  * event handling
942  * events: show, hide, start timer for hidding
943  */
944 void FullscreenControllerWidget::customEvent( QEvent *event )
945 {
946     bool b_fs;
947
948     switch( event->type() )
949     {
950         case FullscreenControlToggle_Type:
951             vlc_mutex_lock( &lock );
952             b_fs = b_fullscreen;
953             vlc_mutex_unlock( &lock );
954             if( b_fs )
955 #ifdef WIN32TRICK
956                 if( b_fscHidden )
957 #else
958                 if( isHidden() )
959 #endif
960                 {
961                     p_hideTimer->stop();
962                     showFSC();
963                 }
964                 else
965                     hideFSC();
966             break;
967         case FullscreenControlShow_Type:
968             vlc_mutex_lock( &lock );
969             b_fs = b_fullscreen;
970             vlc_mutex_unlock( &lock );
971
972 #ifdef WIN32TRICK
973             if( b_fs && b_fscHidden )  // FIXME I am not sure about that one
974 #else
975             if( b_fs && !isVisible() )  // FIXME I am not sure about that one
976 #endif
977                 showFSC();
978             break;
979         case FullscreenControlHide_Type:
980             hideFSC();
981             break;
982         case FullscreenControlPlanHide_Type:
983             if( !b_mouse_over ) // Only if the mouse is not over FSC
984                 planHideFSC();
985             break;
986     }
987 }
988
989 /**
990  * On mouse move
991  * moving with FSC
992  */
993 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
994 {
995     if ( event->buttons() == Qt::LeftButton )
996     {
997         int i_moveX = event->globalX() - i_mouse_last_x;
998         int i_moveY = event->globalY() - i_mouse_last_y;
999
1000         move( x() + i_moveX, y() + i_moveY );
1001
1002         i_mouse_last_x = event->globalX();
1003         i_mouse_last_y = event->globalY();
1004     }
1005 }
1006
1007 /**
1008  * On mouse press
1009  * store position of cursor
1010  */
1011 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
1012 {
1013     i_mouse_last_x = event->globalX();
1014     i_mouse_last_y = event->globalY();
1015 }
1016
1017 /**
1018  * On mouse go above FSC
1019  */
1020 void FullscreenControllerWidget::enterEvent( QEvent *event )
1021 {
1022     b_mouse_over = true;
1023
1024     p_hideTimer->stop();
1025 #if HAVE_TRANSPARENCY
1026     p_slowHideTimer->stop();
1027 #endif
1028 }
1029
1030 /**
1031  * On mouse go out from FSC
1032  */
1033 void FullscreenControllerWidget::leaveEvent( QEvent *event )
1034 {
1035     planHideFSC();
1036
1037     b_mouse_over = false;
1038 }
1039
1040 /**
1041  * When you get pressed key, send it to video output
1042  * FIXME: clearing focus by clearFocus() to not getting
1043  * key press events didnt work
1044  */
1045 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
1046 {
1047     int i_vlck = qtEventToVLCKey( event );
1048     if( i_vlck > 0 )
1049     {
1050         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1051         event->accept();
1052     }
1053     else
1054         event->ignore();
1055 }
1056
1057 /* */
1058 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object,
1059                 const char *variable, vlc_value_t old_val,
1060                 vlc_value_t new_val,  void *data )
1061 {
1062     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
1063     msg_Dbg( p_vout, "Qt4: Fullscreen state changed" );
1064     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1065
1066     p_fs->fullscreenChanged( p_vout, new_val.b_bool,
1067             var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1068
1069     return VLC_SUCCESS;
1070 }
1071 /* */
1072 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
1073                                                  vlc_value_t old_val, vlc_value_t new_val,
1074                                                  void *data )
1075 {
1076     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1077
1078     int i_mousex, i_mousey;
1079     bool b_toShow = false;
1080
1081     /* Get the value from the Vout - Trust the vout more than Qt */
1082     i_mousex = var_GetInteger( p_fs->p_vout, "mouse-x" );
1083     i_mousey = var_GetInteger( p_fs->p_vout, "mouse-y" );
1084
1085     /* First time */
1086     if( p_fs->i_mouse_last_move_x == -1 || p_fs->i_mouse_last_move_y == -1 )
1087     {
1088         p_fs->i_mouse_last_move_x = i_mousex;
1089         p_fs->i_mouse_last_move_y = i_mousey;
1090         b_toShow = true;
1091     }
1092     /* All other times */
1093     else
1094     {
1095         /* Trigger only if move > 3 px dans une direction */
1096         if( abs( p_fs->i_mouse_last_move_x - i_mousex ) > 2 ||
1097             abs( p_fs->i_mouse_last_move_y - i_mousey ) > 2 )
1098         {
1099             b_toShow = true;
1100             p_fs->i_mouse_last_move_x = i_mousex;
1101             p_fs->i_mouse_last_move_y = i_mousey;
1102         }
1103     }
1104
1105     if( b_toShow )
1106     {
1107         /* Show event */
1108         IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1109         QApplication::postEvent( p_fs, static_cast<QEvent *>(eShow) );
1110
1111         /* Plan hide event */
1112         IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1113         QApplication::postEvent( p_fs, static_cast<QEvent *>(eHide) );
1114     }
1115
1116     return VLC_SUCCESS;
1117 }
1118
1119
1120 /**
1121  * It is called when video start
1122  */
1123 void FullscreenControllerWidget::attachVout( vout_thread_t *p_nvout )
1124 {
1125     assert( p_nvout && !p_vout );
1126
1127     p_vout = p_nvout;
1128
1129     msg_Dbg( p_vout, "Qt FS: Attaching Vout" );
1130     vlc_mutex_lock( &lock );
1131
1132     var_AddCallback( p_vout, "fullscreen",
1133             FullscreenControllerWidgetFullscreenChanged, this );
1134             /* I miss a add and fire */
1135     fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ),
1136                        var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1137     vlc_mutex_unlock( &lock );
1138 }
1139 /**
1140  * It is called after turn off video.
1141  */
1142 void FullscreenControllerWidget::detachVout()
1143 {
1144     if( p_vout )
1145     {
1146         msg_Dbg( p_vout, "Qt FS: Detaching Vout" );
1147         var_DelCallback( p_vout, "fullscreen",
1148                 FullscreenControllerWidgetFullscreenChanged, this );
1149         vlc_mutex_lock( &lock );
1150         fullscreenChanged( p_vout, false, 0 );
1151         vlc_mutex_unlock( &lock );
1152         p_vout = NULL;
1153     }
1154 }
1155
1156 /**
1157  * Register and unregister callback for mouse moving
1158  */
1159 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout,
1160         bool b_fs, int i_timeout )
1161 {
1162     msg_Dbg( p_vout, "Qt: Entering Fullscreen" );
1163
1164     vlc_mutex_lock( &lock );
1165     /* Entering fullscreen, register callback */
1166     if( b_fs && !b_fullscreen )
1167     {
1168         b_fullscreen = true;
1169         i_hide_timeout = i_timeout;
1170         var_AddCallback( p_vout, "mouse-moved",
1171                 FullscreenControllerWidgetMouseMoved, this );
1172     }
1173     /* Quitting fullscreen, unregistering callback */
1174     else if( !b_fs && b_fullscreen )
1175     {
1176         b_fullscreen = false;
1177         i_hide_timeout = i_timeout;
1178         var_DelCallback( p_vout, "mouse-moved",
1179                 FullscreenControllerWidgetMouseMoved, this );
1180
1181         /* Force fs hidding */
1182         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1183         QApplication::postEvent( this, static_cast<QEvent *>(eHide) );
1184     }
1185     vlc_mutex_unlock( &lock );
1186 }
1187