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