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