]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller.cpp
16639c4bdc559c863ebc3902632fec2c67184482
[vlc] / modules / gui / qt4 / components / controller.cpp
1 /*****************************************************************************
2  * Controller.cpp : Controller for the main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8  *          Ilkka Ollakka <ileoo@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * ( at your option ) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_vout.h>
30 #include <vlc_keys.h>
31
32 #include "components/controller.hpp"
33 #include "components/controller_widget.hpp"
34 #include "components/interface_widgets.hpp"
35
36 #include "dialogs_provider.hpp" /* Opening Dialogs */
37 #include "input_manager.hpp"
38 #include "actions_manager.hpp"
39
40 #include "util/input_slider.hpp" /* InputSlider */
41 #include "util/customwidgets.hpp" /* qEventToKey */
42
43 #include <QSpacerItem>
44 #include <QToolButton>
45 #include <QHBoxLayout>
46 #include <QSignalMapper>
47 #include <QTimer>
48
49 /**********************************************************************
50  * TEH controls
51  **********************************************************************/
52
53 /******
54  * This is an abstract Toolbar/Controller
55  * This has helper to create any toolbar, any buttons and to manage the actions
56  *
57  *****/
58 AbstractController::AbstractController( intf_thread_t * _p_i, QWidget *_parent )
59                    : QFrame( _parent )
60 {
61     p_intf = _p_i;
62     advControls = NULL;
63
64     /* Main action provider */
65     toolbarActionsMapper = new QSignalMapper( this );
66     CONNECT( toolbarActionsMapper, mapped( int ),
67              ActionsManager::getInstance( p_intf  ), doAction( int ) );
68     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
69 }
70
71 /* Reemit some signals on status Change to activate some buttons */
72 void AbstractController::setStatus( int status )
73 {
74     bool b_hasInput = THEMIM->getIM()->hasInput();
75     /* Activate the interface buttons according to the presence of the input */
76     emit inputExists( b_hasInput );
77
78     emit inputPlaying( status == PLAYING_S );
79
80     emit inputIsRecordable( b_hasInput &&
81                             var_GetBool( THEMIM->getInput(), "can-record" ) );
82 }
83
84 /* Generic button setup */
85 void AbstractController::setupButton( QAbstractButton *aButton )
86 {
87     static QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
88     sizePolicy.setHorizontalStretch( 0 );
89     sizePolicy.setVerticalStretch( 0 );
90
91     aButton->setSizePolicy( sizePolicy );
92     aButton->setFixedSize( QSize( 26, 26 ) );
93     aButton->setIconSize( QSize( 20, 20 ) );
94     aButton->setFocusPolicy( Qt::NoFocus );
95 }
96
97 /* Open the generic config line for the toolbar, parse it
98  * and create the widgets accordingly */
99 void AbstractController::parseAndCreate( QString config,
100                                          QBoxLayout *controlLayout )
101 {
102     QStringList list = config.split( ";", QString::SkipEmptyParts ) ;
103     for( int i = 0; i < list.size(); i++ )
104     {
105         QStringList list2 = list.at( i ).split( "-" );
106         if( list2.size() < 1 )
107         {
108             msg_Warn( p_intf, "Parsing error. Report this" );
109             continue;
110         }
111
112         bool ok;
113         int i_option = WIDGET_NORMAL;
114         buttonType_e i_type = (buttonType_e)list2.at( 0 ).toInt( &ok );
115         if( !ok )
116         {
117             msg_Warn( p_intf, "Parsing error 0. Please report this" );
118             continue;
119         }
120
121         if( list2.size() > 1 )
122         {
123             i_option = list2.at( 1 ).toInt( &ok );
124             if( !ok )
125             {
126                 msg_Warn( p_intf, "Parsing error 1. Please report this" );
127                 continue;
128             }
129         }
130
131         createAndAddWidget( controlLayout, -1, i_type, i_option );
132     }
133 }
134
135 void AbstractController::createAndAddWidget( QBoxLayout *controlLayout,
136                                              int i_index,
137                                              buttonType_e i_type,
138                                              int i_option )
139 {
140     /* Special case for SPACERS, who aren't QWidgets */
141     if( i_type == WIDGET_SPACER )
142     {
143         controlLayout->insertSpacing( i_index, 16 );
144         return;
145     }
146
147     if(  i_type == WIDGET_SPACER_EXTEND )
148     {
149         controlLayout->insertStretch( i_index, 16 );
150         return;
151     }
152
153     QWidget *widg = createWidget( i_type, i_option );
154     if( !widg ) return;
155
156     controlLayout->insertWidget( i_index, widg );
157 }
158
159
160 #define CONNECT_MAP( a ) CONNECT( a, clicked(),  toolbarActionsMapper, map() )
161 #define SET_MAPPING( a, b ) toolbarActionsMapper->setMapping( a , b )
162 #define CONNECT_MAP_SET( a, b ) \
163     CONNECT_MAP( a ); \
164     SET_MAPPING( a, b );
165 #define BUTTON_SET_BAR( a_button ) \
166     a_button->setToolTip( tooltipL[button] );          \
167     a_button->setIcon( QIcon( iconL[button] ) );
168 #define BUTTON_SET_BAR2( button, image, tooltip ) \
169     button->setToolTip( tooltip );          \
170     button->setIcon( QIcon( ":/"#image ) );
171
172
173 #define ENABLE_ON_VIDEO( a ) \
174     CONNECT( THEMIM->getIM(), voutChanged( bool ), a, setEnabled( bool ) ); \
175     a->setEnabled( THEMIM->getIM()->hasVideo() ); /* TODO: is this necessary? when input is started before the interface? */
176
177 #define ENABLE_ON_INPUT( a ) \
178     CONNECT( this, inputExists( bool ), a, setEnabled( bool ) ); \
179     a->setEnabled( THEMIM->getIM()->hasInput() ); /* TODO: is this necessary? when input is started before the interface? */
180
181 QWidget *AbstractController::createWidget( buttonType_e button, int options )
182 {
183
184     bool b_flat = options & WIDGET_FLAT;
185     bool b_big = options & WIDGET_BIG;
186     bool b_shiny = options & WIDGET_SHINY;
187
188     QWidget *widget = NULL;
189     switch( button )
190     {
191     case PLAY_BUTTON: {
192         PlayButton *playButton = new PlayButton;
193         setupButton( playButton );
194         BUTTON_SET_BAR(  playButton );
195         CONNECT_MAP_SET( playButton, PLAY_ACTION );
196         CONNECT( this, inputPlaying( bool ),
197                  playButton, updateButton( bool ));
198         widget = playButton;
199         }
200         break;
201     case STOP_BUTTON:{
202         QToolButton *stopButton = new QToolButton;
203         setupButton( stopButton );
204         CONNECT_MAP_SET( stopButton, STOP_ACTION );
205         BUTTON_SET_BAR(  stopButton );
206         widget = stopButton;
207         }
208         break;
209     case OPEN_BUTTON:{
210         QToolButton *openButton = new QToolButton;
211         setupButton( openButton );
212         CONNECT_MAP_SET( openButton, OPEN_ACTION );
213         BUTTON_SET_BAR( openButton );
214         widget = openButton;
215         }
216         break;
217     case PREVIOUS_BUTTON:{
218         QToolButton *prevButton = new QToolButton;
219         setupButton( prevButton );
220         CONNECT_MAP_SET( prevButton, PREVIOUS_ACTION );
221         BUTTON_SET_BAR( prevButton );
222         widget = prevButton;
223         }
224         break;
225     case NEXT_BUTTON:
226         {
227         QToolButton *nextButton = new QToolButton;
228         setupButton( nextButton );
229         CONNECT_MAP_SET( nextButton, NEXT_ACTION );
230         BUTTON_SET_BAR( nextButton );
231         widget = nextButton;
232         }
233         break;
234     case SLOWER_BUTTON:{
235         QToolButton *slowerButton = new QToolButton;
236         setupButton( slowerButton );
237         CONNECT_MAP_SET( slowerButton, SLOWER_ACTION );
238         BUTTON_SET_BAR(  slowerButton );
239         ENABLE_ON_INPUT( slowerButton );
240         widget = slowerButton;
241         }
242         break;
243     case FASTER_BUTTON:{
244         QToolButton *fasterButton = new QToolButton;
245         setupButton( fasterButton );
246         CONNECT_MAP_SET( fasterButton, FASTER_ACTION );
247         BUTTON_SET_BAR(  fasterButton );
248         ENABLE_ON_INPUT( fasterButton );
249         widget = fasterButton;
250         }
251         break;
252     case FRAME_BUTTON: {
253         QToolButton *frameButton = new QToolButton;
254         setupButton( frameButton );
255         CONNECT_MAP_SET( frameButton, FRAME_ACTION );
256         BUTTON_SET_BAR(  frameButton );
257         ENABLE_ON_VIDEO( frameButton );
258         widget = frameButton;
259         }
260         break;
261     case FULLSCREEN_BUTTON:{
262         QToolButton *fullscreenButton = new QToolButton;
263         setupButton( fullscreenButton );
264         CONNECT_MAP_SET( fullscreenButton, FULLSCREEN_ACTION );
265         BUTTON_SET_BAR( fullscreenButton );
266         ENABLE_ON_VIDEO( fullscreenButton );
267         widget = fullscreenButton;
268         }
269         break;
270     case DEFULLSCREEN_BUTTON:{
271         QToolButton *fullscreenButton = new QToolButton;
272         setupButton( fullscreenButton );
273         CONNECT_MAP_SET( fullscreenButton, FULLSCREEN_ACTION );
274         BUTTON_SET_BAR( fullscreenButton )
275         ENABLE_ON_VIDEO( fullscreenButton );
276         widget = fullscreenButton;
277         }
278         break;
279     case EXTENDED_BUTTON:{
280         QToolButton *extSettingsButton = new QToolButton;
281         setupButton( extSettingsButton );
282         CONNECT_MAP_SET( extSettingsButton, EXTENDED_ACTION );
283         BUTTON_SET_BAR( extSettingsButton )
284         widget = extSettingsButton;
285         }
286         break;
287     case PLAYLIST_BUTTON:{
288         QToolButton *playlistButton = new QToolButton;
289         setupButton( playlistButton );
290         CONNECT_MAP_SET( playlistButton, PLAYLIST_ACTION );
291         BUTTON_SET_BAR( playlistButton );
292         widget = playlistButton;
293         }
294         break;
295     case SNAPSHOT_BUTTON:{
296         QToolButton *snapshotButton = new QToolButton;
297         setupButton( snapshotButton );
298         CONNECT_MAP_SET( snapshotButton, SNAPSHOT_ACTION );
299         BUTTON_SET_BAR(  snapshotButton );
300         ENABLE_ON_VIDEO( snapshotButton );
301         widget = snapshotButton;
302         }
303         break;
304     case RECORD_BUTTON:{
305         QToolButton *recordButton = new QToolButton;
306         setupButton( recordButton );
307         CONNECT_MAP_SET( recordButton, RECORD_ACTION );
308         BUTTON_SET_BAR(  recordButton );
309         ENABLE_ON_INPUT( recordButton );
310         recordButton->setCheckable( true );
311         CONNECT( THEMIM->getIM(), recordingStateChanged( bool ),
312                  recordButton, setChecked( bool ) );
313         widget = recordButton;
314         }
315         break;
316     case ATOB_BUTTON: {
317         AtoB_Button *ABButton = new AtoB_Button;
318         setupButton( ABButton );
319         BUTTON_SET_BAR( ABButton );
320         ENABLE_ON_INPUT( ABButton );
321         CONNECT_MAP_SET( ABButton, ATOB_ACTION );
322         CONNECT( THEMIM->getIM(), AtoBchanged( bool, bool),
323                  ABButton, setIcons( bool, bool ) );
324         widget = ABButton;
325         }
326         break;
327     case INPUT_SLIDER: {
328         InputSlider *slider = new InputSlider( Qt::Horizontal, NULL );
329
330         /* Update the position when the IM has changed */
331         CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
332                 slider, setPosition( float, int, int ) );
333         /* And update the IM, when the position has changed */
334         CONNECT( slider, sliderDragged( float ),
335                  THEMIM->getIM(), sliderUpdate( float ) );
336         widget = slider;
337         }
338         break;
339     case MENU_BUTTONS:
340         widget = discFrame();
341         widget->hide();
342         break;
343     case TELETEXT_BUTTONS:
344         widget = telexFrame();
345         widget->hide();
346         break;
347     case VOLUME:
348         {
349             SoundWidget *snd = new SoundWidget( this, p_intf, b_shiny );
350             widget = snd;
351         }
352         break;
353     case TIME_LABEL:
354         {
355             TimeLabel *timeLabel = new TimeLabel( p_intf );
356             widget = timeLabel;
357         }
358         break;
359     case SPLITTER:
360         {
361             QFrame *line = new QFrame;
362             line->setFrameShape( QFrame::VLine );
363             line->setFrameShadow( QFrame::Raised );
364             line->setLineWidth( 0 );
365             line->setMidLineWidth( 1 );
366             widget = line;
367         }
368         break;
369     case ADVANCED_CONTROLLER:
370         {
371             advControls = new AdvControlsWidget( p_intf, this );
372             widget = advControls;
373         }
374         break;
375     case REVERSE_BUTTON:{
376         QToolButton *reverseButton = new QToolButton;
377         setupButton( reverseButton );
378         CONNECT_MAP_SET( reverseButton, REVERSE_ACTION );
379         BUTTON_SET_BAR(  reverseButton );
380         ENABLE_ON_INPUT( reverseButton );
381         widget = reverseButton;
382         }
383         break;
384     case SKIP_BACK_BUTTON: {
385         QToolButton *skipBakButton = new QToolButton;
386         setupButton( skipBakButton );
387         CONNECT_MAP_SET( skipBakButton, SKIP_BACK_ACTION );
388         BUTTON_SET_BAR(  skipBakButton );
389         ENABLE_ON_INPUT( skipBakButton );
390         widget = skipBakButton;
391         }
392         break;
393     case SKIP_FW_BUTTON: {
394         QToolButton *skipFwButton = new QToolButton;
395         setupButton( skipFwButton );
396         CONNECT_MAP_SET( skipFwButton, SKIP_FW_ACTION );
397         BUTTON_SET_BAR(  skipFwButton );
398         ENABLE_ON_INPUT( skipFwButton );
399         widget = skipFwButton;
400         }
401         break;
402     case QUIT_BUTTON: {
403         QToolButton *quitButton = new QToolButton;
404         setupButton( quitButton );
405         CONNECT_MAP_SET( quitButton, QUIT_ACTION );
406         BUTTON_SET_BAR(  quitButton );
407         widget = quitButton;
408         }
409         break;
410     default:
411         msg_Warn( p_intf, "This should not happen %i", button );
412         break;
413     }
414
415     /* Customize Buttons */
416     if( b_flat || b_big )
417     {
418         QToolButton *tmpButton = qobject_cast<QToolButton *>(widget);
419         if( tmpButton )
420         {
421             if( b_flat )
422                 tmpButton->setAutoRaise( b_flat );
423             if( b_big )
424             {
425                 tmpButton->setFixedSize( QSize( 32, 32 ) );
426                 tmpButton->setIconSize( QSize( 26, 26 ) );
427             }
428         }
429     }
430     return widget;
431 }
432
433 QFrame *AbstractController::discFrame()
434 {
435     /** Disc and Menus handling */
436     QFrame *discFrame = new QFrame( this );
437
438     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
439     discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
440
441     QToolButton *prevSectionButton = new QToolButton( discFrame );
442     setupButton( prevSectionButton );
443     BUTTON_SET_BAR2( prevSectionButton, dvd_prev,
444             qtr("Previous Chapter/Title" ) );
445     discLayout->addWidget( prevSectionButton );
446
447     QToolButton *menuButton = new QToolButton( discFrame );
448     setupButton( menuButton );
449     discLayout->addWidget( menuButton );
450     BUTTON_SET_BAR2( menuButton, dvd_menu, qtr( "Menu" ) );
451
452     QToolButton *nextSectionButton = new QToolButton( discFrame );
453     setupButton( nextSectionButton );
454     discLayout->addWidget( nextSectionButton );
455     BUTTON_SET_BAR2( nextSectionButton, dvd_next,
456             qtr("Next Chapter/Title" ) );
457
458     /* Change the navigation button display when the IM
459        navigation changes */
460     CONNECT( THEMIM->getIM(), titleChanged( bool ),
461             discFrame, setVisible( bool ) );
462     CONNECT( THEMIM->getIM(), chapterChanged( bool ),
463             menuButton, setVisible( bool ) );
464     /* Changes the IM navigation when triggered on the nav buttons */
465     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
466             sectionPrev() );
467     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
468             sectionNext() );
469     CONNECT( menuButton, clicked(), THEMIM->getIM(),
470             sectionMenu() );
471
472     return discFrame;
473 }
474
475 QFrame *AbstractController::telexFrame()
476 {
477     /**
478      * Telextext QFrame
479      **/
480     TeletextController *telexFrame = new TeletextController;
481     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
482     telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
483     CONNECT( THEMIM->getIM(), teletextPossible( bool ),
484              telexFrame, setVisible( bool ) );
485
486     /* On/Off button */
487     QToolButton *telexOn = new QToolButton;
488     telexFrame->telexOn = telexOn;
489     setupButton( telexOn );
490     BUTTON_SET_BAR2( telexOn, tv, qtr( "Teletext Activation" ) );
491     telexLayout->addWidget( telexOn );
492
493     /* Teletext Activation and set */
494     CONNECT( telexOn, clicked( bool ),
495              THEMIM->getIM(), activateTeletext( bool ) );
496     CONNECT( THEMIM->getIM(), teletextActivated( bool ),
497              telexFrame, enableTeletextButtons( bool ) );
498
499
500     /* Transparency button */
501     QToolButton *telexTransparent = new QToolButton;
502     telexFrame->telexTransparent = telexTransparent;
503     setupButton( telexTransparent );
504     BUTTON_SET_BAR2( telexTransparent, tvtelx,
505                      qtr( "Toggle Transparency " ) );
506     telexTransparent->setEnabled( false );
507     telexLayout->addWidget( telexTransparent );
508
509     /* Transparency change and set */
510     CONNECT( telexTransparent, clicked( bool ),
511             THEMIM->getIM(), telexSetTransparency( bool ) );
512     CONNECT( THEMIM->getIM(), teletextTransparencyActivated( bool ),
513             telexFrame, toggleTeletextTransparency( bool ) );
514
515
516     /* Page setting */
517     QSpinBox *telexPage = new QSpinBox;
518     telexFrame->telexPage = telexPage;
519     telexPage->setRange( 0, 999 );
520     telexPage->setValue( 100 );
521     telexPage->setAccelerated( true );
522     telexPage->setWrapping( true );
523     telexPage->setAlignment( Qt::AlignRight );
524     telexPage->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
525     telexPage->setEnabled( false );
526     telexLayout->addWidget( telexPage );
527
528     /* Page change and set */
529     CONNECT( telexPage, valueChanged( int ),
530             THEMIM->getIM(), telexSetPage( int ) );
531     CONNECT( THEMIM->getIM(), newTelexPageSet( int ),
532             telexPage, setValue( int ) );
533
534     return telexFrame;
535 }
536 #undef CONNECT_MAP
537 #undef SET_MAPPING
538 #undef CONNECT_MAP_SET
539 #undef BUTTON_SET_BAR
540 #undef ENABLE_ON_VIDEO
541 #undef ENABLE_ON_INPUT
542
543 #include <QHBoxLayout>
544 /*****************************
545  * DA Control Widget !
546  *****************************/
547 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
548                                 bool b_advControls,
549                                 QWidget *_parent ) :
550                                 AbstractController( _p_i, _parent )
551 {
552     setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Maximum );
553
554     /* advanced Controls handling */
555     b_advancedVisible = b_advControls;
556
557     QVBoxLayout *controlLayout = new QVBoxLayout( this );
558     controlLayout->setLayoutMargins( 6, 4, 6, 2, 5 );
559     controlLayout->setSpacing( 0 );
560     QHBoxLayout *controlLayout1 = new QHBoxLayout;
561     controlLayout1->setSpacing( 0 );
562
563     QString line1 = getSettings()->value( "MainToolbar1", MAIN_TB1_DEFAULT )
564                                         .toString();
565     parseAndCreate( line1, controlLayout1 );
566
567     QHBoxLayout *controlLayout2 = new QHBoxLayout;
568     controlLayout2->setSpacing( 0 );
569     QString line2 = getSettings()->value( "MainToolbar2", MAIN_TB2_DEFAULT )
570                                         .toString();
571     parseAndCreate( line2, controlLayout2 );
572
573     if( !b_advancedVisible && advControls ) advControls->hide();
574
575     controlLayout->addLayout( controlLayout1 );
576     controlLayout->addLayout( controlLayout2 );
577 }
578
579 ControlsWidget::~ControlsWidget()
580 {}
581
582 void ControlsWidget::toggleAdvanced()
583 {
584     if( !advControls ) return;
585
586     if( !b_advancedVisible )
587     {
588         advControls->show();
589         b_advancedVisible = true;
590     }
591     else
592     {
593         advControls->hide();
594         b_advancedVisible = false;
595     }
596     emit advancedControlsToggled( b_advancedVisible );
597 }
598
599 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i, QWidget *_parent ) :
600                                      AbstractController( _p_i, _parent )
601 {
602     controlLayout = new QHBoxLayout( this );
603     controlLayout->setMargin( 0 );
604     controlLayout->setSpacing( 0 );
605
606     QString line = getSettings()->value( "AdvToolbar", ADV_TB_DEFAULT )
607         .toString();
608     parseAndCreate( line, controlLayout );
609 }
610
611 InputControlsWidget::InputControlsWidget( intf_thread_t *_p_i, QWidget *_parent ) :
612                                      AbstractController( _p_i, _parent )
613 {
614     controlLayout = new QHBoxLayout( this );
615     controlLayout->setMargin( 0 );
616     controlLayout->setSpacing( 0 );
617
618     QString line = getSettings()->value( "InputToolbar", INPT_TB_DEFAULT ).toString();
619     parseAndCreate( line, controlLayout );
620 }
621 /**********************************************************************
622  * Fullscrenn control widget
623  **********************************************************************/
624 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i )
625                            : AbstractController( _p_i )
626 {
627     i_mouse_last_x      = -1;
628     i_mouse_last_y      = -1;
629     b_mouse_over        = false;
630     i_mouse_last_move_x = -1;
631     i_mouse_last_move_y = -1;
632 #if HAVE_TRANSPARENCY
633     b_slow_hide_begin   = false;
634     i_slow_hide_timeout = 1;
635 #endif
636     b_fullscreen        = false;
637     i_hide_timeout      = 1;
638     i_screennumber      = -1;
639
640     vout.clear();
641
642     setWindowFlags( Qt::ToolTip );
643     setMinimumWidth( 600 );
644
645     setFrameShape( QFrame::StyledPanel );
646     setFrameStyle( QFrame::Sunken );
647     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
648
649     QVBoxLayout *controlLayout2 = new QVBoxLayout( this );
650     controlLayout2->setLayoutMargins( 5, 2, 5, 2, 5 );
651
652     /* First line */
653     InputControlsWidget *inputC = new InputControlsWidget( p_intf, this );
654     controlLayout2->addWidget( inputC );
655
656     controlLayout = new QHBoxLayout;
657     QString line = getSettings()->value( "MainWindow/FSCtoolbar", FSC_TB_DEFAULT ).toString();
658     parseAndCreate( line, controlLayout );
659     controlLayout2->addLayout( controlLayout );
660
661     /* hiding timer */
662     p_hideTimer = new QTimer( this );
663     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
664     p_hideTimer->setSingleShot( true );
665
666     /* slow hiding timer */
667 #if HAVE_TRANSPARENCY
668     p_slowHideTimer = new QTimer( this );
669     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
670 #endif
671
672     adjustSize ();  /* need to get real width and height for moving */
673
674 #ifdef WIN32TRICK
675     setWindowOpacity( 0.0 );
676     b_fscHidden = true;
677     adjustSize();
678     show();
679 #endif
680
681     vlc_mutex_init_recursive( &lock );
682
683     CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ), this, setVoutList( vout_thread_t **, int ) );
684 }
685
686 FullscreenControllerWidget::~FullscreenControllerWidget()
687 {
688     getSettings()->setValue( "FullScreen/pos", pos() );
689     setVoutList( NULL, 0 );
690     vlc_mutex_destroy( &lock );
691 }
692
693 /**
694  * Show fullscreen controller
695  */
696 void FullscreenControllerWidget::showFSC()
697 {
698     adjustSize();
699     /* center down */
700     int number = QApplication::desktop()->screenNumber( p_intf->p_sys->p_mi );
701     if( number != i_screennumber )
702     {
703         msg_Dbg( p_intf, "Calculation fullscreen controllers center");
704         /* screen has changed, calculate new position */
705         QRect screenRes = QApplication::desktop()->screenGeometry(number);
706         QPoint pos = QPoint( screenRes.x() + (screenRes.width() / 2) - (width() / 2),
707                              screenRes.y() + screenRes.height() - height());
708         move( pos );
709         i_screennumber = number;
710     }
711 #ifdef WIN32TRICK
712     // after quiting and going to fs, we need to call show()
713     if( isHidden() )
714         show();
715     if( b_fscHidden )
716     {
717         b_fscHidden = false;
718         setWindowOpacity( 1.0 );
719     }
720 #else
721     show();
722 #endif
723
724 #if HAVE_TRANSPARENCY
725     setWindowOpacity( DEFAULT_OPACITY );
726 #endif
727 }
728
729 /**
730  * Hide fullscreen controller
731  * FIXME: under windows it have to be done by moving out of screen
732  *        because hide() doesnt work
733  */
734 void FullscreenControllerWidget::hideFSC()
735 {
736 #ifdef WIN32TRICK
737     b_fscHidden = true;
738     setWindowOpacity( 0.0 );    // simulate hidding
739 #else
740     hide();
741 #endif
742 }
743
744 /**
745  * Plane to hide fullscreen controller
746  */
747 void FullscreenControllerWidget::planHideFSC()
748 {
749     vlc_mutex_lock( &lock );
750     int i_timeout = i_hide_timeout;
751     vlc_mutex_unlock( &lock );
752
753     p_hideTimer->start( i_timeout );
754
755 #if HAVE_TRANSPARENCY
756     b_slow_hide_begin = true;
757     i_slow_hide_timeout = i_timeout;
758     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
759 #endif
760 }
761
762 /**
763  * Hidding fullscreen controller slowly
764  * Linux: need composite manager
765  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
766  */
767 void FullscreenControllerWidget::slowHideFSC()
768 {
769 #if HAVE_TRANSPARENCY
770     if( b_slow_hide_begin )
771     {
772         b_slow_hide_begin = false;
773
774         p_slowHideTimer->stop();
775         /* the last part of time divided to 100 pieces */
776         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
777
778     }
779     else
780     {
781 #ifdef WIN32TRICK
782          if ( windowOpacity() > 0.0 && !b_fscHidden )
783 #else
784          if ( windowOpacity() > 0.0 )
785 #endif
786          {
787              /* we should use 0.01 because of 100 pieces ^^^
788                 but than it cannt be done in time */
789              setWindowOpacity( windowOpacity() - 0.02 );
790          }
791
792          if ( windowOpacity() <= 0.0 )
793              p_slowHideTimer->stop();
794     }
795 #endif
796 }
797
798 /**
799  * event handling
800  * events: show, hide, start timer for hidding
801  */
802 void FullscreenControllerWidget::customEvent( QEvent *event )
803 {
804     bool b_fs;
805
806     switch( event->type() )
807     {
808         case FullscreenControlToggle_Type:
809             vlc_mutex_lock( &lock );
810             b_fs = b_fullscreen;
811             vlc_mutex_unlock( &lock );
812             if( b_fs )
813             {
814 #ifdef WIN32TRICK
815                 if( b_fscHidden )
816 #else
817                 if( isHidden() )
818 #endif
819                 {
820                     p_hideTimer->stop();
821                     showFSC();
822                 }
823                 else
824                     hideFSC();
825             }
826             break;
827         case FullscreenControlShow_Type:
828             vlc_mutex_lock( &lock );
829             b_fs = b_fullscreen;
830             vlc_mutex_unlock( &lock );
831
832 #ifdef WIN32TRICK
833             if( b_fs && b_fscHidden )
834 #else
835             if( b_fs && !isVisible() )
836 #endif
837                 showFSC();
838             break;
839         case FullscreenControlHide_Type:
840             hideFSC();
841             break;
842         case FullscreenControlPlanHide_Type:
843             if( !b_mouse_over ) // Only if the mouse is not over FSC
844                 planHideFSC();
845             break;
846         default:
847             break;
848     }
849 }
850
851 /**
852  * On mouse move
853  * moving with FSC
854  */
855 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
856 {
857     if ( event->buttons() == Qt::LeftButton )
858     {
859         int i_moveX = event->globalX() - i_mouse_last_x;
860         int i_moveY = event->globalY() - i_mouse_last_y;
861
862         move( x() + i_moveX, y() + i_moveY );
863
864         i_mouse_last_x = event->globalX();
865         i_mouse_last_y = event->globalY();
866     }
867 }
868
869 /**
870  * On mouse press
871  * store position of cursor
872  */
873 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
874 {
875     i_mouse_last_x = event->globalX();
876     i_mouse_last_y = event->globalY();
877 }
878
879 /**
880  * On mouse go above FSC
881  */
882 void FullscreenControllerWidget::enterEvent( QEvent *event )
883 {
884     b_mouse_over = true;
885
886     p_hideTimer->stop();
887 #if HAVE_TRANSPARENCY
888     p_slowHideTimer->stop();
889 #endif
890     event->accept();
891 }
892
893 /**
894  * On mouse go out from FSC
895  */
896 void FullscreenControllerWidget::leaveEvent( QEvent *event )
897 {
898     planHideFSC();
899
900     b_mouse_over = false;
901     event->accept();
902 }
903
904 /**
905  * When you get pressed key, send it to video output
906  * FIXME: clearing focus by clearFocus() to not getting
907  * key press events didnt work
908  */
909 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
910 {
911     int i_vlck = qtEventToVLCKey( event );
912     if( i_vlck > 0 )
913     {
914         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
915         event->accept();
916     }
917     else
918         event->ignore();
919 }
920
921 /* */
922 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object,
923                 const char *variable, vlc_value_t old_val,
924                 vlc_value_t new_val,  void *data )
925 {
926     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
927
928     msg_Dbg( p_vout, "Qt4: Fullscreen state changed" );
929     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
930
931     p_fs->fullscreenChanged( p_vout, new_val.b_bool, var_GetInteger( p_vout, "mouse-hide-timeout" ) );
932
933     return VLC_SUCCESS;
934 }
935 /* */
936 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
937                                                  vlc_value_t old_val, vlc_value_t new_val,
938                                                  void *data )
939 {
940     vout_thread_t *p_vout = (vout_thread_t *)vlc_object;
941     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
942
943     /* Get the value from the Vout - Trust the vout more than Qt */
944     const int i_mousex = var_GetInteger( p_vout, "mouse-x" );
945     const int i_mousey = var_GetInteger( p_vout, "mouse-y" );
946
947     p_fs->mouseChanged( p_vout, i_mousex, i_mousey );
948
949     return VLC_SUCCESS;
950 }
951
952 /**
953  * It is call to update the list of vout handled by the fullscreen controller
954  */
955 void FullscreenControllerWidget::setVoutList( vout_thread_t **pp_vout, int i_vout )
956 {
957     QList<vout_thread_t*> del;
958     QList<vout_thread_t*> add;
959
960     QList<vout_thread_t*> set;
961
962     /* */
963     for( int i = 0; i < i_vout; i++ )
964         set += pp_vout[i];
965
966     /* Vout to remove */
967     vlc_mutex_lock( &lock );
968     foreach( vout_thread_t *p_vout, vout )
969     {
970         if( !set.contains( p_vout ) )
971             del += p_vout;
972     }
973     vlc_mutex_unlock( &lock );
974
975     foreach( vout_thread_t *p_vout, del )
976     {
977         var_DelCallback( p_vout, "fullscreen",
978                          FullscreenControllerWidgetFullscreenChanged, this );
979         vlc_mutex_lock( &lock );
980         fullscreenChanged( p_vout, false, 0 );
981         vout.removeAll( p_vout );
982         vlc_mutex_unlock( &lock );
983     }
984
985     /* Vout to track */
986     vlc_mutex_lock( &lock );
987     foreach( vout_thread_t *p_vout, set )
988     {
989         if( !vout.contains( p_vout ) )
990             add += p_vout;
991     }
992     vlc_mutex_unlock( &lock );
993
994     foreach( vout_thread_t *p_vout, add )
995     {
996         vlc_object_hold( (vlc_object_t*)p_vout );
997
998         vlc_mutex_lock( &lock );
999         vout.append( p_vout );
1000         var_AddCallback( p_vout, "fullscreen",
1001                          FullscreenControllerWidgetFullscreenChanged, this );
1002         /* I miss a add and fire */
1003         fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ),
1004                            var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1005         vlc_mutex_unlock( &lock );
1006     }
1007 }
1008 /**
1009  * Register and unregister callback for mouse moving
1010  */
1011 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout,
1012         bool b_fs, int i_timeout )
1013 {
1014     /* FIXME - multiple vout (ie multiple mouse position ?) and thread safety if multiple vout ? */
1015     msg_Dbg( p_vout, "Qt: Entering Fullscreen" );
1016
1017     vlc_mutex_lock( &lock );
1018     /* Entering fullscreen, register callback */
1019     if( b_fs && !b_fullscreen )
1020     {
1021         b_fullscreen = true;
1022         i_hide_timeout = i_timeout;
1023         var_AddCallback( p_vout, "mouse-moved",
1024                 FullscreenControllerWidgetMouseMoved, this );
1025     }
1026     /* Quitting fullscreen, unregistering callback */
1027     else if( !b_fs && b_fullscreen )
1028     {
1029         b_fullscreen = false;
1030         i_hide_timeout = i_timeout;
1031         var_DelCallback( p_vout, "mouse-moved",
1032                 FullscreenControllerWidgetMouseMoved, this );
1033
1034         /* Force fs hidding */
1035         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1036         QApplication::postEvent( this, static_cast<QEvent *>(eHide) );
1037     }
1038     vlc_mutex_unlock( &lock );
1039 }
1040 /**
1041  * Mouse change callback (show/hide the controller on mouse movement)
1042  */
1043 void FullscreenControllerWidget::mouseChanged( vout_thread_t *p_vout, int i_mousex, int i_mousey )
1044 {
1045     bool b_toShow;
1046
1047     /* FIXME - multiple vout (ie multiple mouse position ?) and thread safety if multiple vout ? */
1048
1049     b_toShow = false;
1050     if( ( i_mouse_last_move_x == -1 || i_mouse_last_move_y == -1 ) ||
1051         ( abs( i_mouse_last_move_x - i_mousex ) > 2 ||
1052           abs( i_mouse_last_move_y - i_mousey ) > 2 ) )
1053     {
1054         i_mouse_last_move_x = i_mousex;
1055         i_mouse_last_move_y = i_mousey;
1056         b_toShow = true;
1057     }
1058
1059     if( b_toShow )
1060     {
1061         /* Show event */
1062         IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1063         QApplication::postEvent( this, static_cast<QEvent *>(eShow) );
1064
1065         /* Plan hide event */
1066         IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1067         QApplication::postEvent( this, static_cast<QEvent *>(eHide) );
1068     }
1069 }
1070