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