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