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