]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller.cpp
Qt4: Add aspect ratio combobox to toolbar editor
[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     case ASPECT_RATIO_COMBOBOX:
469         widget = new AspectRatioComboBox( p_intf );
470         break;
471     default:
472         msg_Warn( p_intf, "This should not happen %i", button );
473         break;
474     }
475
476     /* Customize Buttons */
477     if( b_flat || b_big )
478     {
479         QFrame *frame = qobject_cast<QFrame *>(widget);
480         if( frame )
481         {
482             QList<QToolButton *> allTButtons = frame->findChildren<QToolButton *>();
483             for( int i = 0; i < allTButtons.count(); i++ )
484                 applyAttributes( allTButtons[i], b_flat, b_big );
485         }
486         else
487         {
488             QToolButton *tmpButton = qobject_cast<QToolButton *>(widget);
489             if( tmpButton )
490                 applyAttributes( tmpButton, b_flat, b_big );
491         }
492     }
493     return widget;
494 }
495 #undef NORMAL_BUTTON
496
497 void AbstractController::applyAttributes( QToolButton *tmpButton, bool b_flat, bool b_big )
498 {
499     if( tmpButton )
500     {
501         if( b_flat )
502             tmpButton->setAutoRaise( b_flat );
503         if( b_big )
504         {
505             tmpButton->setFixedSize( QSize( 32, 32 ) );
506             tmpButton->setIconSize( QSize( 26, 26 ) );
507         }
508     }
509 }
510
511 QFrame *AbstractController::discFrame()
512 {
513     /** Disc and Menus handling */
514     QFrame *discFrame = new QFrame( this );
515
516     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
517     discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
518
519     QToolButton *prevSectionButton = new QToolButton( discFrame );
520     setupButton( prevSectionButton );
521     BUTTON_SET_BAR2( prevSectionButton, toolbar/dvd_prev,
522             qtr("Previous Chapter/Title" ) );
523     discLayout->addWidget( prevSectionButton );
524
525     QToolButton *menuButton = new QToolButton( discFrame );
526     setupButton( menuButton );
527     discLayout->addWidget( menuButton );
528     BUTTON_SET_BAR2( menuButton, toolbar/dvd_menu, qtr( "Menu" ) );
529
530     QToolButton *nextSectionButton = new QToolButton( discFrame );
531     setupButton( nextSectionButton );
532     discLayout->addWidget( nextSectionButton );
533     BUTTON_SET_BAR2( nextSectionButton, toolbar/dvd_next,
534             qtr("Next Chapter/Title" ) );
535
536     /* Change the navigation button display when the IM
537        navigation changes */
538     CONNECT( THEMIM->getIM(), titleChanged( bool ),
539             discFrame, setVisible( bool ) );
540     CONNECT( THEMIM->getIM(), chapterChanged( bool ),
541             menuButton, setVisible( bool ) );
542     /* Changes the IM navigation when triggered on the nav buttons */
543     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
544             sectionPrev() );
545     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
546             sectionNext() );
547     CONNECT( menuButton, clicked(), THEMIM->getIM(),
548             sectionMenu() );
549
550     return discFrame;
551 }
552
553 QFrame *AbstractController::telexFrame()
554 {
555     /**
556      * Telextext QFrame
557      **/
558     QFrame *telexFrame = new QFrame( this );
559     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
560     telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
561     CONNECT( THEMIM->getIM(), teletextPossible( bool ),
562              telexFrame, setVisible( bool ) );
563
564     /* On/Off button */
565     QToolButton *telexOn = new QToolButton;
566     setupButton( telexOn );
567     BUTTON_SET_BAR2( telexOn, toolbar/tv, qtr( "Teletext Activation" ) );
568     telexOn->setEnabled( false );
569     telexOn->setCheckable( true );
570
571     telexLayout->addWidget( telexOn );
572
573     /* Teletext Activation and set */
574     CONNECT( telexOn, clicked( bool ),
575              THEMIM->getIM(), activateTeletext( bool ) );
576     CONNECT( THEMIM->getIM(), teletextPossible( bool ),
577              telexOn, setEnabled( bool ) );
578
579     /* Transparency button */
580     QToolButton *telexTransparent = new QToolButton;
581     setupButton( telexTransparent );
582     BUTTON_SET_BAR2( telexTransparent, toolbar/tvtelx,
583                      qtr( "Toggle Transparency " ) );
584     telexTransparent->setEnabled( false );
585     telexTransparent->setCheckable( true );
586     telexLayout->addWidget( telexTransparent );
587
588     /* Transparency change and set */
589     CONNECT( telexTransparent, clicked( bool ),
590             THEMIM->getIM(), telexSetTransparency( bool ) );
591     CONNECT( THEMIM->getIM(), teletextTransparencyActivated( bool ),
592              telexTransparent, setChecked( bool ) );
593
594
595     /* Page setting */
596     QSpinBox *telexPage = new QSpinBox( telexFrame );
597     telexPage->setRange( 0, 999 );
598     telexPage->setValue( 100 );
599     telexPage->setAccelerated( true );
600     telexPage->setWrapping( true );
601     telexPage->setAlignment( Qt::AlignRight );
602     telexPage->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
603     telexPage->setEnabled( false );
604     telexLayout->addWidget( telexPage );
605
606     /* Page change and set */
607     CONNECT( telexPage, valueChanged( int ),
608             THEMIM->getIM(), telexSetPage( int ) );
609     CONNECT( THEMIM->getIM(), newTelexPageSet( int ),
610             telexPage, setValue( int ) );
611
612     CONNECT( THEMIM->getIM(), teletextActivated( bool ), telexPage, setEnabled( bool ) );
613     CONNECT( THEMIM->getIM(), teletextActivated( bool ), telexTransparent, setEnabled( bool ) );
614     CONNECT( THEMIM->getIM(), teletextActivated( bool ), telexOn, setChecked( bool ) );
615     return telexFrame;
616 }
617 #undef CONNECT_MAP
618 #undef SET_MAPPING
619 #undef CONNECT_MAP_SET
620 #undef BUTTON_SET_BAR
621 #undef BUTTON_SET_BAR2
622 #undef ENABLE_ON_VIDEO
623 #undef ENABLE_ON_INPUT
624
625 #include <QHBoxLayout>
626 /*****************************
627  * DA Control Widget !
628  *****************************/
629 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
630                                 bool b_advControls,
631                                 QWidget *_parent ) :
632                                 AbstractController( _p_i, _parent )
633 {
634     RTL_UNAFFECTED_WIDGET
635     /* advanced Controls handling */
636     b_advancedVisible = b_advControls;
637 #ifdef DEBUG_LAYOUT
638     setStyleSheet( "background: red ");
639 #endif
640     setAttribute( Qt::WA_MacBrushedMetal);
641     QVBoxLayout *controlLayout = new QVBoxLayout( this );
642     controlLayout->setContentsMargins( 4, 1, 0, 0 );
643     controlLayout->setSpacing( 0 );
644     QHBoxLayout *controlLayout1 = new QHBoxLayout;
645     controlLayout1->setSpacing( 0 ); controlLayout1->setMargin( 0 );
646
647     QString line1 = getSettings()->value( "MainWindow/MainToolbar1", MAIN_TB1_DEFAULT )
648                                         .toString();
649     parseAndCreate( line1, controlLayout1 );
650
651     QHBoxLayout *controlLayout2 = new QHBoxLayout;
652     controlLayout2->setSpacing( 0 ); controlLayout2->setMargin( 0 );
653     QString line2 = getSettings()->value( "MainWindow/MainToolbar2", MAIN_TB2_DEFAULT )
654                                         .toString();
655     parseAndCreate( line2, controlLayout2 );
656
657     grip = new QSizeGrip( this );
658     controlLayout2->addWidget( grip, 0, Qt::AlignBottom|Qt::AlignRight );
659
660     if( !b_advancedVisible && advControls ) advControls->hide();
661
662     controlLayout->addLayout( controlLayout1 );
663     controlLayout->addLayout( controlLayout2 );
664 }
665
666 void ControlsWidget::toggleAdvanced()
667 {
668     if( !advControls ) return;
669
670     advControls->setVisible( !b_advancedVisible );
671     b_advancedVisible = !b_advancedVisible;
672     emit advancedControlsToggled( b_advancedVisible );
673 }
674
675 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i, QWidget *_parent ) :
676                                      AbstractController( _p_i, _parent )
677 {
678     RTL_UNAFFECTED_WIDGET
679     controlLayout = new QHBoxLayout( this );
680     controlLayout->setMargin( 0 );
681     controlLayout->setSpacing( 0 );
682 #ifdef DEBUG_LAYOUT
683     setStyleSheet( "background: orange ");
684 #endif
685
686
687     QString line = getSettings()->value( "MainWindow/AdvToolbar", ADV_TB_DEFAULT )
688         .toString();
689     parseAndCreate( line, controlLayout );
690 }
691
692 InputControlsWidget::InputControlsWidget( intf_thread_t *_p_i, QWidget *_parent ) :
693                                      AbstractController( _p_i, _parent )
694 {
695     RTL_UNAFFECTED_WIDGET
696     controlLayout = new QHBoxLayout( this );
697     controlLayout->setMargin( 0 );
698     controlLayout->setSpacing( 0 );
699 #ifdef DEBUG_LAYOUT
700     setStyleSheet( "background: green ");
701 #endif
702
703     QString line = getSettings()->value( "MainWindow/InputToolbar", INPT_TB_DEFAULT ).toString();
704     parseAndCreate( line, controlLayout );
705 }
706 /**********************************************************************
707  * Fullscrenn control widget
708  **********************************************************************/
709 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i, QWidget *_parent )
710                            : AbstractController( _p_i, _parent )
711 {
712     RTL_UNAFFECTED_WIDGET
713     i_mouse_last_x      = -1;
714     i_mouse_last_y      = -1;
715     b_mouse_over        = false;
716     i_mouse_last_move_x = -1;
717     i_mouse_last_move_y = -1;
718 #if HAVE_TRANSPARENCY
719     b_slow_hide_begin   = false;
720     i_slow_hide_timeout = 1;
721 #endif
722     b_fullscreen        = false;
723     i_hide_timeout      = 1;
724     i_screennumber      = -1;
725
726     vout.clear();
727
728     setWindowFlags( Qt::ToolTip );
729     setMinimumWidth( FSC_WIDTH );
730     setMinimumHeight( FSC_HEIGHT );
731     isWideFSC = false;
732
733     setFrameShape( QFrame::StyledPanel );
734     setFrameStyle( QFrame::Sunken );
735     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
736
737     QVBoxLayout *controlLayout2 = new QVBoxLayout( this );
738     controlLayout2->setContentsMargins( 4, 6, 4, 2 );
739
740     /* First line */
741     InputControlsWidget *inputC = new InputControlsWidget( p_intf, this );
742     controlLayout2->addWidget( inputC );
743
744     controlLayout = new QHBoxLayout;
745     QString line = getSettings()->value( "MainWindow/FSCtoolbar", FSC_TB_DEFAULT ).toString();
746     parseAndCreate( line, controlLayout );
747     controlLayout2->addLayout( controlLayout );
748
749     /* hiding timer */
750     p_hideTimer = new QTimer( this );
751     p_hideTimer->setSingleShot( true );
752     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
753
754     /* slow hiding timer */
755 #if HAVE_TRANSPARENCY
756     p_slowHideTimer = new QTimer( this );
757     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
758     f_opacity = var_InheritFloat( p_intf, "qt-fs-opacity" );
759 #endif
760
761     vlc_mutex_init_recursive( &lock );
762
763     DCONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ),
764               this, setVoutList( vout_thread_t **, int ) );
765
766     /* First Move */
767     previousPosition = getSettings()->value( "FullScreen/pos" ).toPoint();
768     screenRes = getSettings()->value( "FullScreen/screen" ).toRect();
769     isWideFSC = getSettings()->value( "FullScreen/wide" ).toBool();
770     i_screennumber = var_InheritInteger( p_intf, "qt-fullscreen-screennumber" );
771 }
772
773 FullscreenControllerWidget::~FullscreenControllerWidget()
774 {
775     getSettings()->setValue( "FullScreen/pos", previousPosition );
776     getSettings()->setValue( "FullScreen/screen", screenRes );
777     getSettings()->setValue( "FullScreen/wide", isWideFSC );
778
779     setVoutList( NULL, 0 );
780     vlc_mutex_destroy( &lock );
781 }
782
783 void FullscreenControllerWidget::restoreFSC()
784 {
785     if( !isWideFSC )
786     {
787         /* Restore half-bar and re-centre if needed */
788         setMinimumWidth( FSC_WIDTH );
789         adjustSize();
790
791         QRect currentRes = QApplication::desktop()->screenGeometry( targetScreen() );
792
793         if( currentRes == screenRes &&
794             QApplication::desktop()->screen()->geometry().contains( previousPosition, true ) )
795         {
796             /* Restore to the last known position */
797             move( previousPosition );
798         }
799         else
800         {
801             /* FSC is out of screen or screen resolution changed */
802             msg_Dbg( p_intf, "Recentering the Fullscreen Controller" );
803             centerFSC( targetScreen() );
804             screenRes = currentRes;
805             previousPosition = pos();
806         }
807     }
808     else
809     {
810         /* Dock at the bottom of the screen */
811         updateFullwidthGeometry( targetScreen() );
812     }
813
814 #ifdef Q_WS_X11
815     // Tell kwin that we do not want a shadow around the fscontroller
816     setMask( QRegion( 0, 0, width(), height() ) );
817 #endif
818 }
819
820 void FullscreenControllerWidget::centerFSC( int number )
821 {
822     QRect currentRes = QApplication::desktop()->screenGeometry( number );
823
824     /* screen has changed, calculate new position */
825     QPoint pos = QPoint( currentRes.x() + (currentRes.width() / 2) - (width() / 2),
826             currentRes.y() + currentRes.height() - height());
827     move( pos );
828 }
829
830 /**
831  * Show fullscreen controller
832  */
833 void FullscreenControllerWidget::showFSC()
834 {
835     restoreFSC();
836
837 #if HAVE_TRANSPARENCY
838     setWindowOpacity( f_opacity );
839 #endif
840
841     show();
842 }
843
844 /**
845  * Plane to hide fullscreen controller
846  */
847 void FullscreenControllerWidget::planHideFSC()
848 {
849     vlc_mutex_lock( &lock );
850     int i_timeout = i_hide_timeout;
851     vlc_mutex_unlock( &lock );
852
853     p_hideTimer->start( i_timeout );
854
855 #if HAVE_TRANSPARENCY
856     b_slow_hide_begin = true;
857     i_slow_hide_timeout = i_timeout;
858     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
859 #endif
860 }
861
862 /**
863  * Hidding fullscreen controller slowly
864  * Linux: need composite manager
865  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
866  */
867 void FullscreenControllerWidget::slowHideFSC()
868 {
869 #if HAVE_TRANSPARENCY
870     if( b_slow_hide_begin )
871     {
872         b_slow_hide_begin = false;
873
874         p_slowHideTimer->stop();
875         /* the last part of time divided to 100 pieces */
876         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
877
878     }
879     else
880     {
881          if ( windowOpacity() > 0.0 )
882          {
883              /* we should use 0.01 because of 100 pieces ^^^
884                 but than it cannt be done in time */
885              setWindowOpacity( windowOpacity() - 0.02 );
886          }
887
888          if ( windowOpacity() <= 0.0 )
889              p_slowHideTimer->stop();
890     }
891 #endif
892 }
893
894 void FullscreenControllerWidget::updateFullwidthGeometry( int number )
895 {
896     QRect screenGeometry = QApplication::desktop()->screenGeometry( number );
897     setMinimumWidth( screenGeometry.width() );
898     setGeometry( screenGeometry.x(), screenGeometry.y() + screenGeometry.height() - FSC_HEIGHT, screenGeometry.width(), FSC_HEIGHT );
899     adjustSize();
900 }
901
902 void FullscreenControllerWidget::toggleFullwidth()
903 {
904     /* Toggle isWideFSC switch */
905     isWideFSC = !isWideFSC;
906
907     restoreFSC();
908 }
909
910 int FullscreenControllerWidget::targetScreen()
911 {
912     if( i_screennumber == -1 || i_screennumber > QApplication::desktop()->numScreens() )
913         return QApplication::desktop()->screenNumber( p_intf->p_sys->p_mi );
914     return i_screennumber;
915 }
916
917 /**
918  * event handling
919  * events: show, hide, start timer for hiding
920  */
921 void FullscreenControllerWidget::customEvent( QEvent *event )
922 {
923     bool b_fs;
924
925     switch( (int)event->type() )
926     {
927         /* This is used when the 'i' hotkey is used, to force quick toggle */
928         case FullscreenControlToggle_Type:
929             vlc_mutex_lock( &lock );
930             b_fs = b_fullscreen;
931             vlc_mutex_unlock( &lock );
932
933             if( b_fs )
934             {
935                 if( isHidden() )
936                 {
937                     p_hideTimer->stop();
938                     showFSC();
939                 }
940                 else
941                     hideFSC();
942             }
943             break;
944         /* Event called to Show the FSC on mouseChanged() */
945         case FullscreenControlShow_Type:
946             vlc_mutex_lock( &lock );
947             b_fs = b_fullscreen;
948             vlc_mutex_unlock( &lock );
949
950             if( b_fs )
951                 showFSC();
952
953             break;
954         /* Start the timer to hide later, called usually with above case */
955         case FullscreenControlPlanHide_Type:
956             if( !b_mouse_over ) // Only if the mouse is not over FSC
957                 planHideFSC();
958             break;
959         /* Hide */
960         case FullscreenControlHide_Type:
961             hideFSC();
962             break;
963         default:
964             break;
965     }
966 }
967
968 /**
969  * On mouse move
970  * moving with FSC
971  */
972 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
973 {
974     if( event->buttons() == Qt::LeftButton )
975     {
976         if( i_mouse_last_x == -1 || i_mouse_last_y == -1 )
977             return;
978
979         int i_moveX = event->globalX() - i_mouse_last_x;
980         int i_moveY = event->globalY() - i_mouse_last_y;
981
982         move( x() + i_moveX, y() + i_moveY );
983
984         i_mouse_last_x = event->globalX();
985         i_mouse_last_y = event->globalY();
986     }
987 }
988
989 /**
990  * On mouse press
991  * store position of cursor
992  */
993 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
994 {
995     if( isWideFSC ) return;
996     i_mouse_last_x = event->globalX();
997     i_mouse_last_y = event->globalY();
998     event->accept();
999 }
1000
1001 void FullscreenControllerWidget::mouseReleaseEvent( QMouseEvent *event )
1002 {
1003     if( isWideFSC ) return;
1004     i_mouse_last_x = -1;
1005     i_mouse_last_y = -1;
1006     event->accept();
1007
1008     // Save the new FSC position
1009     previousPosition = pos();
1010 }
1011
1012 /**
1013  * On mouse go above FSC
1014  */
1015 void FullscreenControllerWidget::enterEvent( QEvent *event )
1016 {
1017     b_mouse_over = true;
1018
1019     p_hideTimer->stop();
1020 #if HAVE_TRANSPARENCY
1021     p_slowHideTimer->stop();
1022     setWindowOpacity( f_opacity );
1023 #endif
1024     event->accept();
1025 }
1026
1027 /**
1028  * On mouse go out from FSC
1029  */
1030 void FullscreenControllerWidget::leaveEvent( QEvent *event )
1031 {
1032     planHideFSC();
1033
1034     b_mouse_over = false;
1035     event->accept();
1036 }
1037
1038 /**
1039  * When you get pressed key, send it to video output
1040  */
1041 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
1042 {
1043     emit keyPressed( event );
1044 }
1045
1046 /* */
1047 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object,
1048                 const char *variable, vlc_value_t old_val,
1049                 vlc_value_t new_val,  void *data )
1050 {
1051     VLC_UNUSED( variable ); VLC_UNUSED( old_val );
1052
1053     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
1054
1055     msg_Dbg( p_vout, "Qt4: Fullscreen state changed" );
1056     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1057
1058     p_fs->fullscreenChanged( p_vout, new_val.b_bool, var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1059
1060     return VLC_SUCCESS;
1061 }
1062 /* */
1063 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
1064                                                  vlc_value_t old_val, vlc_value_t new_val,
1065                                                  void *data )
1066 {
1067     VLC_UNUSED( variable ); VLC_UNUSED( old_val );
1068
1069     vout_thread_t *p_vout = (vout_thread_t *)vlc_object;
1070     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1071
1072     /* Get the value from the Vout - Trust the vout more than Qt */
1073     p_fs->mouseChanged( p_vout, new_val.coords.x, new_val.coords.y );
1074
1075     return VLC_SUCCESS;
1076 }
1077
1078 /**
1079  * It is call to update the list of vout handled by the fullscreen controller
1080  */
1081 void FullscreenControllerWidget::setVoutList( vout_thread_t **pp_vout, int i_vout )
1082 {
1083     QList<vout_thread_t*> del;
1084     QList<vout_thread_t*> add;
1085
1086     QList<vout_thread_t*> set;
1087
1088     /* */
1089     for( int i = 0; i < i_vout; i++ )
1090         set += pp_vout[i];
1091
1092     /* Vout to remove */
1093     vlc_mutex_lock( &lock );
1094     foreach( vout_thread_t *p_vout, vout )
1095     {
1096         if( !set.contains( p_vout ) )
1097             del += p_vout;
1098     }
1099     vlc_mutex_unlock( &lock );
1100
1101     foreach( vout_thread_t *p_vout, del )
1102     {
1103         var_DelCallback( p_vout, "fullscreen",
1104                          FullscreenControllerWidgetFullscreenChanged, this );
1105         vlc_mutex_lock( &lock );
1106         fullscreenChanged( p_vout, false, 0 );
1107         vout.removeAll( p_vout );
1108         vlc_mutex_unlock( &lock );
1109
1110         vlc_object_release( VLC_OBJECT(p_vout) );
1111     }
1112
1113     /* Vout to track */
1114     vlc_mutex_lock( &lock );
1115     foreach( vout_thread_t *p_vout, set )
1116     {
1117         if( !vout.contains( p_vout ) )
1118             add += p_vout;
1119     }
1120     vlc_mutex_unlock( &lock );
1121
1122     foreach( vout_thread_t *p_vout, add )
1123     {
1124         vlc_object_hold( VLC_OBJECT(p_vout) );
1125
1126         vlc_mutex_lock( &lock );
1127         vout.append( p_vout );
1128         var_AddCallback( p_vout, "fullscreen",
1129                          FullscreenControllerWidgetFullscreenChanged, this );
1130         /* I miss a add and fire */
1131         fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ),
1132                            var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1133         vlc_mutex_unlock( &lock );
1134     }
1135 }
1136 /**
1137  * Register and unregister callback for mouse moving
1138  */
1139 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout,
1140         bool b_fs, int i_timeout )
1141 {
1142     /* FIXME - multiple vout (ie multiple mouse position ?) and thread safety if multiple vout ? */
1143
1144     vlc_mutex_lock( &lock );
1145     /* Entering fullscreen, register callback */
1146     if( b_fs && !b_fullscreen )
1147     {
1148         msg_Dbg( p_vout, "Qt: Entering Fullscreen" );
1149         b_fullscreen = true;
1150         i_hide_timeout = i_timeout;
1151         var_AddCallback( p_vout, "mouse-moved",
1152                 FullscreenControllerWidgetMouseMoved, this );
1153     }
1154     /* Quitting fullscreen, unregistering callback */
1155     else if( !b_fs && b_fullscreen )
1156     {
1157         msg_Dbg( p_vout, "Qt: Quitting Fullscreen" );
1158         b_fullscreen = false;
1159         i_hide_timeout = i_timeout;
1160         var_DelCallback( p_vout, "mouse-moved",
1161                 FullscreenControllerWidgetMouseMoved, this );
1162
1163         /* Force fs hiding */
1164         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1165         QApplication::postEvent( this, eHide );
1166     }
1167     vlc_mutex_unlock( &lock );
1168 }
1169
1170 /**
1171  * Mouse change callback (show/hide the controller on mouse movement)
1172  */
1173 void FullscreenControllerWidget::mouseChanged( vout_thread_t *, int i_mousex, int i_mousey )
1174 {
1175     bool b_toShow;
1176
1177     /* FIXME - multiple vout (ie multiple mouse position ?) and thread safety if multiple vout ? */
1178
1179     b_toShow = false;
1180     if( ( i_mouse_last_move_x == -1 || i_mouse_last_move_y == -1 ) ||
1181         ( abs( i_mouse_last_move_x - i_mousex ) > 2 ||
1182           abs( i_mouse_last_move_y - i_mousey ) > 2 ) )
1183     {
1184         i_mouse_last_move_x = i_mousex;
1185         i_mouse_last_move_y = i_mousey;
1186         b_toShow = true;
1187     }
1188
1189     if( b_toShow )
1190     {
1191         /* Show event */
1192         IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1193         QApplication::postEvent( this, eShow );
1194
1195         /* Plan hide event */
1196         IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1197         QApplication::postEvent( this, eHide );
1198     }
1199 }
1200