]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Fullscreen controller button reordering to fix consistency with main toolbar (complai...
[vlc] / modules / gui / qt4 / components / interface_widgets.cpp
1 /*****************************************************************************
2  * interface_widgets.cpp : Custom widgets for the main interface
3  ****************************************************************************
4  * Copyright ( C ) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Rafaël Carré <funman@videolanorg>
10  *          Ilkka Ollakka <ileoo@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * ( at your option ) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_vout.h>
32
33 #include "dialogs_provider.hpp"
34 #include "components/interface_widgets.hpp"
35 #include "main_interface.hpp"
36 #include "input_manager.hpp"
37 #include "menus.hpp"
38 #include "util/input_slider.hpp"
39 #include "util/customwidgets.hpp"
40
41 #include <QLabel>
42 #include <QSpacerItem>
43 #include <QCursor>
44 #include <QPushButton>
45 #include <QToolButton>
46 #include <QHBoxLayout>
47 #include <QMenu>
48 #include <QPalette>
49 #include <QResizeEvent>
50 #include <QDate>
51
52 #ifdef Q_WS_X11
53 # include <X11/Xlib.h>
54 # include <qx11info_x11.h>
55 #endif
56
57 #include <math.h>
58
59 /**********************************************************************
60  * Video Widget. A simple frame on which video is drawn
61  * This class handles resize issues
62  **********************************************************************/
63
64 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
65 {
66     /* Init */
67     i_vout = 0;
68     hide(); setMinimumSize( 16, 16 );
69     videoSize.rwidth() = -1;
70     videoSize.rheight() = -1;
71     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
72
73     /* Black background is more coherent for a Video Widget IMVHO */
74     QPalette plt =  palette();
75     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
76     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
77     setPalette( plt );
78     setAttribute( Qt::WA_PaintOnScreen, true );
79
80     /* The core can ask through a callback to show the video. */
81 #if HAS_QT43
82     connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
83              this, SLOT(SetSizing(unsigned int, unsigned int )),
84              Qt::BlockingQueuedConnection );
85 #else
86 #error This is broken. Fix it with a QEventLoop with a processEvents () 
87     connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
88              this, SLOT(SetSizing(unsigned int, unsigned int )) );
89 #endif
90 }
91
92 void VideoWidget::paintEvent(QPaintEvent *ev)
93 {
94     QFrame::paintEvent(ev);
95 #ifdef Q_WS_X11
96     XFlush( QX11Info::display() );
97 #endif
98 }
99
100 VideoWidget::~VideoWidget()
101 {
102     vout_thread_t *p_vout = i_vout
103         ? (vout_thread_t *)vlc_object_get( i_vout ) : NULL;
104
105     if( p_vout )
106     {
107         if( !p_intf->psz_switch_intf )
108         {
109             if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
110                 vout_Control( p_vout, VOUT_REPARENT );
111         }
112         else
113         {
114             if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
115                 vout_Control( p_vout, VOUT_CLOSE );
116         }
117         vlc_object_release( p_vout );
118     }
119 }
120
121 /**
122  * Request the video to avoid the conflicts
123  **/
124 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
125                             unsigned int *pi_width, unsigned int *pi_height )
126 {
127     msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
128     emit askVideoWidgetToShow( *pi_width, *pi_height );
129     if( i_vout )
130     {
131         msg_Dbg( p_intf, "embedded video already in use" );
132         return NULL;
133     }
134     i_vout = p_nvout->i_object_id;
135     msg_Dbg( p_intf, "embedded video ready (handle %p)", winId() );
136     return ( void* )winId();
137 }
138
139 /* Set the Widget to the correct Size */
140 /* Function has to be called by the parent
141    Parent has to care about resizing himself*/
142 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
143 {
144     msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
145     videoSize.rwidth() = w;
146     videoSize.rheight() = h;
147     if( isHidden() ) show();
148     updateGeometry(); // Needed for deinterlace
149 }
150
151 void VideoWidget::release( void *p_win )
152 {
153     msg_Dbg( p_intf, "Video is not needed anymore" );
154     i_vout = 0;
155     videoSize.rwidth() = 0;
156     videoSize.rheight() = 0;
157     hide();
158     updateGeometry(); // Needed for deinterlace
159 }
160
161 QSize VideoWidget::sizeHint() const
162 {
163     return videoSize;
164 }
165
166 /**********************************************************************
167  * Background Widget. Show a simple image background. Currently,
168  * it's album art if present or cone.
169  **********************************************************************/
170 #define ICON_SIZE 128
171 #define MAX_BG_SIZE 400
172 #define MIN_BG_SIZE 64
173
174 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
175                  :QWidget( NULL ), p_intf( _p_i )
176 {
177     /* We should use that one to take the more size it can */
178     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
179
180     /* A dark background */
181     setAutoFillBackground( true );
182     plt =  palette();
183     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
184     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
185     setPalette( plt );
186
187     /* A cone in the middle */
188     label = new QLabel;
189     label->setMargin( 5 );
190     label->setMaximumHeight( MAX_BG_SIZE );
191     label->setMaximumWidth( MAX_BG_SIZE );
192     label->setMinimumHeight( MIN_BG_SIZE );
193     label->setMinimumWidth( MIN_BG_SIZE );
194     if( QDate::currentDate().dayOfYear() >= 354 )
195         label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
196     else
197         label->setPixmap( QPixmap( ":/vlc128.png" ) );
198
199     QGridLayout *backgroundLayout = new QGridLayout( this );
200     backgroundLayout->addWidget( label, 0, 1 );
201     backgroundLayout->setColumnStretch( 0, 1 );
202     backgroundLayout->setColumnStretch( 2, 1 );
203
204     CONNECT( THEMIM->getIM(), artChanged( QString ), this, updateArt( QString ) );
205 }
206
207 BackgroundWidget::~BackgroundWidget()
208 {}
209
210 void BackgroundWidget::resizeEvent( QResizeEvent * event )
211 {
212     if( event->size().height() <= MIN_BG_SIZE )
213         label->hide();
214     else
215         label->show();
216 }
217
218 void BackgroundWidget::updateArt( QString url )
219 {
220     if( url.isEmpty() )
221     {
222         if( QDate::currentDate().dayOfYear() >= 354 )
223             label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
224         else
225             label->setPixmap( QPixmap( ":/vlc128.png" ) );
226         return;
227     }
228     else
229     {
230         label->setPixmap( QPixmap( url ) );
231     }
232 }
233
234 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
235 {
236     QVLCMenu::PopupMenu( p_intf, true );
237 }
238
239 /**********************************************************************
240  * Visualization selector panel
241  **********************************************************************/
242 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
243                                 QFrame( NULL ), p_intf( _p_i )
244 {
245     QHBoxLayout *layout = new QHBoxLayout( this );
246     layout->setMargin( 0 );
247     QPushButton *prevButton = new QPushButton( "Prev" );
248     QPushButton *nextButton = new QPushButton( "Next" );
249     layout->addWidget( prevButton );
250     layout->addWidget( nextButton );
251
252     layout->addStretch( 10 );
253     layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
254
255     current = new QLabel( qtr( "None" ) );
256     layout->addWidget( current );
257
258     BUTTONACT( prevButton, prev() );
259     BUTTONACT( nextButton, next() );
260
261     setLayout( layout );
262     setMaximumHeight( 35 );
263 }
264
265 VisualSelector::~VisualSelector()
266 {}
267
268 void VisualSelector::prev()
269 {
270     char *psz_new = aout_VisualPrev( p_intf );
271     if( psz_new )
272     {
273         current->setText( qfu( psz_new ) );
274         free( psz_new );
275     }
276 }
277
278 void VisualSelector::next()
279 {
280     char *psz_new = aout_VisualNext( p_intf );
281     if( psz_new )
282     {
283         current->setText( qfu( psz_new ) );
284         free( psz_new );
285     }
286 }
287
288 /**********************************************************************
289  * TEH controls
290  **********************************************************************/
291
292 #define setupSmallButton( aButton ){  \
293     aButton->setMaximumSize( QSize( 26, 26 ) ); \
294     aButton->setMinimumSize( QSize( 26, 26 ) ); \
295     aButton->setIconSize( QSize( 20, 20 ) ); }
296
297 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i ) :
298                                            QFrame( NULL ), p_intf( _p_i )
299 {
300     QHBoxLayout *advLayout = new QHBoxLayout( this );
301     advLayout->setMargin( 0 );
302     advLayout->setSpacing( 0 );
303     advLayout->setAlignment( Qt::AlignBottom );
304
305     /* A to B Button */
306     ABButton = new QPushButton( "AB" );
307     setupSmallButton( ABButton );
308     advLayout->addWidget( ABButton );
309     BUTTON_SET_ACT_I( ABButton, "", atob_nob,
310         qtr( "Loop from point A to point B continuously\nClick to set point A" ), fromAtoB() );
311     timeA = timeB = 0;
312     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
313              this, AtoBLoop( float, int, int ) );
314 #if 0
315     frameButton = new QPushButton( "Fr" );
316     frameButton->setMaximumSize( QSize( 26, 26 ) );
317     frameButton->setIconSize( QSize( 20, 20 ) );
318     advLayout->addWidget( frameButton );
319     BUTTON_SET_ACT( frameButton, "Fr", qtr( "Frame by frame" ), frame() );
320 #endif
321
322     recordButton = new QPushButton( "R" );
323     setupSmallButton( recordButton );
324     advLayout->addWidget( recordButton );
325     BUTTON_SET_ACT_I( recordButton, "", record,
326             qtr( "Record" ), record() );
327
328     /* Snapshot Button */
329     snapshotButton = new QPushButton( "S" );
330     setupSmallButton( snapshotButton );
331     advLayout->addWidget( snapshotButton );
332     BUTTON_SET_ACT_I( snapshotButton, "", snapshot,
333             qtr( "Take a snapshot" ), snapshot() );
334 }
335
336 AdvControlsWidget::~AdvControlsWidget()
337 {}
338
339 void AdvControlsWidget::enableInput( bool enable )
340 {
341     ABButton->setEnabled( enable );
342     recordButton->setEnabled( enable );
343 }
344
345 void AdvControlsWidget::enableVideo( bool enable )
346 {
347     snapshotButton->setEnabled( enable );
348 #if 0
349     frameButton->setEnabled( enable );
350 #endif
351 }
352
353 void AdvControlsWidget::snapshot()
354 {
355     vout_thread_t *p_vout =
356         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
357     if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
358 }
359
360 /* Function called when the button is clicked() */
361 void AdvControlsWidget::fromAtoB()
362 {
363     if( !timeA )
364     {
365         timeA = var_GetTime( THEMIM->getInput(), "time"  );
366         ABButton->setToolTip( "Click to set point B" );
367         ABButton->setIcon( QIcon( ":/atob_noa" ) );
368         return;
369     }
370     if( !timeB )
371     {
372         timeB = var_GetTime( THEMIM->getInput(), "time"  );
373         var_SetTime( THEMIM->getInput(), "time" , timeA );
374         ABButton->setIcon( QIcon( ":/atob" ) );
375         ABButton->setToolTip( "Stop the A to B loop" );
376         return;
377     }
378     timeA = 0;
379     timeB = 0;
380     ABButton->setToolTip( qtr( "Loop from point A to point B continuously\nClick to set point A" ) );
381     ABButton->setIcon( QIcon( ":/atob_nob" ) );
382 }
383
384 /* Function called regularly when in an AtoB loop */
385 void AdvControlsWidget::AtoBLoop( float f_pos, int i_time, int i_length )
386 {
387     if( timeB )
388     {
389         if( i_time >= (int)(timeB/1000000) )
390             var_SetTime( THEMIM->getInput(), "time" , timeA );
391     }
392 }
393
394 /* FIXME Record function */
395 void AdvControlsWidget::record(){}
396
397 #if 0
398 //FIXME Frame by frame function
399 void AdvControlsWidget::frame(){}
400 #endif
401
402 /*****************************
403  * DA Control Widget !
404  *****************************/
405 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
406                                 MainInterface *_p_mi,
407                                 bool b_advControls,
408                                 bool b_shiny,
409                                 bool b_fsCreation) :
410                                 QFrame( _p_mi ), p_intf( _p_i )
411 {
412     setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Maximum );
413
414     /** The main Slider **/
415     slider = new InputSlider( Qt::Horizontal, NULL );
416     /* Update the position when the IM has changed */
417     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
418              slider, setPosition( float, int, int ) );
419     /* And update the IM, when the position has changed */
420     CONNECT( slider, sliderDragged( float ),
421              THEMIM->getIM(), sliderUpdate( float ) );
422
423     /** Slower and faster Buttons **/
424     slowerButton = new QToolButton;
425     slowerButton->setAutoRaise( true );
426     slowerButton->setMaximumSize( QSize( 26, 20 ) );
427
428     BUTTON_SET_ACT( slowerButton, "-", qtr( "Slower" ), slower() );
429
430     fasterButton = new QToolButton;
431     fasterButton->setAutoRaise( true );
432     fasterButton->setMaximumSize( QSize( 26, 20 ) );
433
434     BUTTON_SET_ACT( fasterButton, "+", qtr( "Faster" ), faster() );
435
436     /* advanced Controls handling */
437     b_advancedVisible = b_advControls;
438
439     advControls = new AdvControlsWidget( p_intf );
440     if( !b_advancedVisible ) advControls->hide();
441
442     /** Disc and Menus handling */
443     discFrame = new QWidget( this );
444
445     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
446     discLayout->setSpacing( 0 );
447     discLayout->setMargin( 0 );
448
449     prevSectionButton = new QPushButton( discFrame );
450     setupSmallButton( prevSectionButton );
451     discLayout->addWidget( prevSectionButton );
452
453     menuButton = new QPushButton( discFrame );
454     setupSmallButton( menuButton );
455     discLayout->addWidget( menuButton );
456
457     nextSectionButton = new QPushButton( discFrame );
458     setupSmallButton( nextSectionButton );
459     discLayout->addWidget( nextSectionButton );
460
461     BUTTON_SET_IMG( prevSectionButton, "", dvd_prev, "" );
462     BUTTON_SET_IMG( nextSectionButton, "", dvd_next, "" );
463     BUTTON_SET_IMG( menuButton, "", dvd_menu, qtr( "Menu" ) );
464
465     discFrame->hide();
466
467     /* Change the navigation button display when the IM navigation changes */
468     CONNECT( THEMIM->getIM(), navigationChanged( int ),
469              this, setNavigation( int ) );
470     /* Changes the IM navigation when triggered on the nav buttons */
471     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
472              sectionPrev() );
473     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
474              sectionNext() );
475     CONNECT( menuButton, clicked(), THEMIM->getIM(),
476              sectionMenu() );
477
478     /**
479      * Telextext QFrame
480      * TODO: Merge with upper menu in a StackLayout
481      **/
482     telexFrame = new QWidget( this );
483     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
484     telexLayout->setSpacing( 0 );
485     telexLayout->setMargin( 0 );
486
487     telexOn = new QPushButton;
488     setupSmallButton( telexOn );
489     telexLayout->addWidget( telexOn );
490
491     telexTransparent = new QPushButton;
492     setupSmallButton( telexTransparent );
493     telexLayout->addWidget( telexTransparent );
494     b_telexTransparent = false;
495
496     telexPage = new QSpinBox;
497     telexPage->setRange( 0, 999 );
498     telexPage->setValue( 100 );
499     telexPage->setAccelerated( true );
500     telexPage->setWrapping( true );
501     telexPage->setAlignment( Qt::AlignRight );
502     telexPage->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
503     telexLayout->addWidget( telexPage );
504
505     telexFrame->hide(); /* default hidden */
506
507     CONNECT( telexPage, valueChanged( int ), THEMIM->getIM(),
508              telexGotoPage( int ) );
509     CONNECT( THEMIM->getIM(), setNewTelexPage( int ),
510               telexPage, setValue( int ) );
511
512     BUTTON_SET_IMG( telexOn, "", tv, qtr( "Teletext on" ) );
513
514     CONNECT( telexOn, clicked(), THEMIM->getIM(),
515              telexToggleButtons() );
516     CONNECT( telexOn, clicked( bool ), THEMIM->getIM(),
517              telexToggle( bool ) );
518     CONNECT( THEMIM->getIM(), toggleTelexButtons(),
519               this, toggleTeletext() );
520     b_telexEnabled = false;
521     telexTransparent->setEnabled( false );
522     telexPage->setEnabled( false );
523
524     BUTTON_SET_IMG( telexTransparent, "", tvtelx, qtr( "Teletext" ) );
525     CONNECT( telexTransparent, clicked( bool ),
526              THEMIM->getIM(), telexSetTransparency() );
527     CONNECT( THEMIM->getIM(), toggleTelexTransparency(),
528               this, toggleTeletextTransparency() );
529     CONNECT( THEMIM->getIM(), teletextEnabled( bool ),
530              this, enableTeletext( bool ) );
531
532     /** Play Buttons **/
533     QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
534     sizePolicy.setHorizontalStretch( 0 );
535     sizePolicy.setVerticalStretch( 0 );
536
537     /* Play */
538     playButton = new QPushButton;
539     playButton->setSizePolicy( sizePolicy );
540     playButton->setMaximumSize( QSize( 36, 36 ) );
541     playButton->setMinimumSize( QSize( 36, 36 ) );
542     playButton->setIconSize( QSize( 30, 30 ) );
543
544
545     /** Prev + Stop + Next Block **/
546     controlButLayout = new QHBoxLayout;
547     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
548
549     /* Prev */
550     QPushButton *prevButton = new QPushButton;
551     prevButton->setSizePolicy( sizePolicy );
552     setupSmallButton( prevButton );
553
554     controlButLayout->addWidget( prevButton );
555
556     /* Stop */
557     QPushButton *stopButton = new QPushButton;
558     stopButton->setSizePolicy( sizePolicy );
559     setupSmallButton( stopButton );
560
561     controlButLayout->addWidget( stopButton );
562
563     /* next */
564     QPushButton *nextButton = new QPushButton;
565     nextButton->setSizePolicy( sizePolicy );
566     setupSmallButton( nextButton );
567
568     controlButLayout->addWidget( nextButton );
569
570     /* Add this block to the main layout */
571
572     BUTTON_SET_ACT_I( playButton, "", play_b, qtr( "Play" ), play() );
573     BUTTON_SET_ACT_I( prevButton, "" , previous_b,
574                       qtr( "Previous" ), prev() );
575     BUTTON_SET_ACT_I( nextButton, "", next_b, qtr( "Next" ), next() );
576     BUTTON_SET_ACT_I( stopButton, "", stop_b, qtr( "Stop" ), stop() );
577
578     /*
579      * Other first Line buttons
580      */
581     /** Fullscreen/Visualisation **/
582     fullscreenButton = new QPushButton;
583     BUTTON_SET_ACT_I( fullscreenButton, "", fullscreen, qtr( "Fullscreen" ),
584             fullscreen() );
585     setupSmallButton( fullscreenButton );
586
587     if( !b_fsCreation )
588     {
589         /** Playlist Button **/
590         playlistButton = new QPushButton;
591         setupSmallButton( playlistButton );
592         BUTTON_SET_IMG( playlistButton, "" , playlist, qtr( "Show playlist" ) );
593         CONNECT( playlistButton, clicked(), _p_mi, togglePlaylist() );
594
595         /** extended Settings **/
596         extSettingsButton = new QPushButton;
597         BUTTON_SET_ACT_I( extSettingsButton, "", extended,
598                 qtr( "Extended settings" ), extSettings() );
599         setupSmallButton( extSettingsButton );
600     }
601
602     /* Volume */
603     hVolLabel = new VolumeClickHandler( p_intf, this );
604
605     volMuteLabel = new QLabel;
606     volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
607     volMuteLabel->setToolTip( qtr( "Mute" ) );
608     volMuteLabel->installEventFilter( hVolLabel );
609
610     if( b_shiny )
611     {
612         volumeSlider = new SoundSlider( this,
613             config_GetInt( p_intf, "volume-step" ),
614             config_GetInt( p_intf, "qt-volume-complete" ),
615             config_GetPsz( p_intf, "qt-slider-colours" ) );
616     }
617     else
618     {
619         volumeSlider = new QSlider( this );
620         volumeSlider->setOrientation( Qt::Horizontal );
621     }
622     volumeSlider->setMaximumSize( QSize( 200, 40 ) );
623     volumeSlider->setMinimumSize( QSize( 106, 30 ) );
624     volumeSlider->setFocusPolicy( Qt::NoFocus );
625
626     /* Set the volume from the config */
627     volumeSlider->setValue( ( config_GetInt( p_intf, "volume" ) ) *
628                               VOLUME_MAX / (AOUT_VOLUME_MAX/2) );
629
630     /* Force the update at build time in order to have a muted icon if needed */
631     updateVolume( volumeSlider->value() );
632
633     /* Volume control connection */
634     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
635     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
636
637     if( !b_fsCreation )
638     {
639         controlLayout = new QGridLayout( this );
640
641         controlLayout->setSpacing( 0 );
642         controlLayout->setLayoutMargins( 7, 5, 7, 3, 6 );
643
644         controlLayout->addWidget( slider, 0, 1, 1, 16 );
645         controlLayout->addWidget( slowerButton, 0, 0 );
646         controlLayout->addWidget( fasterButton, 0, 17 );
647
648         controlLayout->addWidget( advControls, 1, 3, 2, 4, Qt::AlignBottom );
649         controlLayout->addWidget( discFrame, 1, 10, 2, 3, Qt::AlignBottom );
650         controlLayout->addWidget( telexFrame, 1, 10, 2, 4, Qt::AlignBottom );
651
652         controlLayout->addWidget( playButton, 2, 0, 2, 2 );
653         controlLayout->setColumnMinimumWidth( 2, 20 );
654         controlLayout->setColumnStretch( 2, 0 );
655
656         controlLayout->addLayout( controlButLayout, 3, 3, 1, 3 );
657         controlLayout->setColumnMinimumWidth( 7, 20 );
658         controlLayout->setColumnStretch( 7, 0 );
659         controlLayout->setColumnStretch( 8, 0 );
660         controlLayout->setColumnStretch( 9, 0 );
661
662         controlLayout->addWidget( fullscreenButton, 3, 10, Qt::AlignBottom );
663         controlLayout->addWidget( playlistButton, 3, 11, Qt::AlignBottom );
664         controlLayout->addWidget( extSettingsButton, 3, 12, Qt::AlignBottom );
665
666         controlLayout->setColumnStretch( 13, 0 );
667         controlLayout->setColumnMinimumWidth( 13, 24 );
668         controlLayout->setColumnStretch( 14, 5 );
669
670         controlLayout->addWidget( volMuteLabel, 3, 15, Qt::AlignBottom );
671         controlLayout->addWidget( volumeSlider, 2, 16, 2 , 2, Qt::AlignBottom );
672     }
673
674     updateInput();
675 }
676
677 ControlsWidget::~ControlsWidget()
678 {}
679
680 void ControlsWidget::toggleTeletext()
681 {
682     bool b_enabled = THEMIM->teletextState();
683     if( b_telexEnabled )
684     {
685         telexTransparent->setEnabled( false );
686         telexPage->setEnabled( false );
687         b_telexEnabled = false;
688     }
689     else if( b_enabled )
690     {
691         telexTransparent->setEnabled( true );
692         telexPage->setEnabled( true );
693         b_telexEnabled = true;
694     }
695 }
696
697 void ControlsWidget::enableTeletext( bool b_enable )
698 {
699     telexFrame->setVisible( b_enable );
700     bool b_on = THEMIM->teletextState();
701
702     telexOn->setChecked( b_on );
703     telexTransparent->setEnabled( b_on );
704     telexPage->setEnabled( b_on );
705     b_telexEnabled = b_on;
706 }
707
708 void ControlsWidget::toggleTeletextTransparency()
709 {
710     if( b_telexTransparent )
711     {
712         telexTransparent->setIcon( QIcon( ":/tvtelx" ) );
713         telexTransparent->setToolTip( qtr( "Teletext" ) );
714         b_telexTransparent = false;
715     }
716     else
717     {
718         telexTransparent->setIcon( QIcon( ":/tvtelx-transparent" ) );
719         telexTransparent->setToolTip( qtr( "Transparent" ) );
720         b_telexTransparent = true;
721     }
722 }
723
724 void ControlsWidget::stop()
725 {
726     THEMIM->stop();
727 }
728
729 void ControlsWidget::play()
730 {
731     if( THEPL->current.i_size == 0 )
732     {
733         /* The playlist is empty, open a file requester */
734         THEDP->openFileDialog();
735         setStatus( 0 );
736         return;
737     }
738     THEMIM->togglePlayPause();
739 }
740
741 void ControlsWidget::prev()
742 {
743     THEMIM->prev();
744 }
745
746 void ControlsWidget::next()
747 {
748     THEMIM->next();
749 }
750
751 void ControlsWidget::setNavigation( int navigation )
752 {
753 #define HELP_PCH N_( "Previous chapter" )
754 #define HELP_NCH N_( "Next chapter" )
755
756     // 1 = chapter, 2 = title, 0 = no
757     if( navigation == 0 )
758     {
759         discFrame->hide();
760     } else if( navigation == 1 ) {
761         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
762         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
763         menuButton->show();
764         discFrame->show();
765     } else {
766         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
767         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
768         menuButton->hide();
769         discFrame->show();
770     }
771 }
772
773 static bool b_my_volume;
774 void ControlsWidget::updateVolume( int i_sliderVolume )
775 {
776     if( !b_my_volume )
777     {
778         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
779         aout_VolumeSet( p_intf, i_res );
780     }
781     if( i_sliderVolume == 0 )
782         volMuteLabel->setPixmap( QPixmap(":/volume-muted" ) );
783     else if( i_sliderVolume < VOLUME_MAX / 3 )
784         volMuteLabel->setPixmap( QPixmap( ":/volume-low" ) );
785     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
786         volMuteLabel->setPixmap( QPixmap( ":/volume-high" ) );
787     else volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
788 }
789
790 void ControlsWidget::updateVolume()
791 {
792     /* Audio part */
793     audio_volume_t i_volume;
794     aout_VolumeGet( p_intf, &i_volume );
795     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
796     int i_gauge = volumeSlider->value();
797     b_my_volume = false;
798     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
799     {
800         b_my_volume = true;
801         volumeSlider->setValue( i_volume );
802         b_my_volume = false;
803     }
804 }
805
806 void ControlsWidget::updateInput()
807 {
808     /* Activate the interface buttons according to the presence of the input */
809     enableInput( THEMIM->getIM()->hasInput() );
810     enableVideo( THEMIM->getIM()->hasVideo() && THEMIM->getIM()->hasInput() );
811 }
812
813 void ControlsWidget::setStatus( int status )
814 {
815     if( status == PLAYING_S ) /* Playing */
816     {
817         playButton->setIcon( QIcon( ":/pause_b" ) );
818         playButton->setToolTip( qtr( "Pause" ) );
819     }
820     else
821     {
822         playButton->setIcon( QIcon( ":/play_b" ) );
823         playButton->setToolTip( qtr( "Play" ) );
824     }
825 }
826
827 /**
828  * TODO
829  * This functions toggle the fullscreen mode
830  * If there is no video, it should first activate Visualisations...
831  *  This has also to be fixed in enableVideo()
832  */
833 void ControlsWidget::fullscreen()
834 {
835     vout_thread_t *p_vout =
836         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
837     if( p_vout)
838     {
839         var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
840         vlc_object_release( p_vout );
841     }
842 }
843
844 void ControlsWidget::extSettings()
845 {
846     THEDP->extendedDialog();
847 }
848
849 void ControlsWidget::slower()
850 {
851     THEMIM->getIM()->slower();
852 }
853
854 void ControlsWidget::faster()
855 {
856     THEMIM->getIM()->faster();
857 }
858
859 void ControlsWidget::enableInput( bool enable )
860 {
861     slowerButton->setEnabled( enable );
862     slider->setEnabled( enable );
863     fasterButton->setEnabled( enable );
864
865     /* Advanced Buttons too */
866     advControls->enableInput( enable );
867 }
868
869 void ControlsWidget::enableVideo( bool enable )
870 {
871     // TODO Later make the fullscreenButton toggle Visualisation and so on.
872     fullscreenButton->setEnabled( enable );
873
874     /* Advanced Buttons too */
875     advControls->enableVideo( enable );
876 }
877
878 void ControlsWidget::toggleAdvanced()
879 {
880     if( !VISIBLE( advControls ) )
881     {
882         advControls->show();
883         b_advancedVisible = true;
884     }
885     else
886     {
887         advControls->hide();
888         b_advancedVisible = false;
889     }
890     emit advancedControlsToggled( b_advancedVisible );
891 }
892
893
894 /**********************************************************************
895  * Fullscrenn control widget
896  **********************************************************************/
897 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i,
898         MainInterface *_p_mi, bool b_advControls, bool b_shiny )
899         : ControlsWidget( _p_i, _p_mi, b_advControls, b_shiny, true ),
900           i_mouse_last_x( -1 ), i_mouse_last_y( -1 ), b_mouse_over(false),
901           b_slow_hide_begin(false), i_slow_hide_timeout(1),
902           b_fullscreen( false ), i_hide_timeout( 1 )
903 {
904     setWindowFlags( Qt::ToolTip );
905
906     setFrameShape( QFrame::StyledPanel );
907     setFrameStyle( QFrame::Sunken );
908     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
909
910     QGridLayout *fsLayout = new QGridLayout( this );
911     fsLayout->setLayoutMargins( 5, 1, 5, 1, 5 );
912
913     /* First line */
914     slider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum);
915     fsLayout->addWidget( slowerButton, 0, 0 );
916     fsLayout->addWidget( slider, 0, 1, 1, 6 );
917     fsLayout->addWidget( fasterButton, 0, 7 );
918
919     fsLayout->addWidget( playButton, 1, 0 );
920     fsLayout->addLayout( controlButLayout, 1, 1 );
921
922     fsLayout->addWidget( discFrame, 1, 2 );
923     fsLayout->addWidget( telexFrame, 1, 3 );
924     fsLayout->addWidget( advControls, 1, 4, Qt::AlignVCenter );
925     fsLayout->addWidget( fullscreenButton, 1, 5 );
926
927     fsLayout->addWidget( volMuteLabel, 1, 6);
928     fsLayout->addWidget( volumeSlider, 1, 7 );
929
930     /* hiding timer */
931     p_hideTimer = new QTimer( this );
932     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
933     p_hideTimer->setSingleShot( true );
934
935     /* slow hiding timer */
936 #if HAVE_TRANSPARENCY
937     p_slowHideTimer = new QTimer( this );
938     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
939 #endif
940
941     adjustSize ();  /* need to get real width and height for moving */
942
943     /* center down */
944     QDesktopWidget * p_desktop = QApplication::desktop();
945
946     move( p_desktop->width() / 2 - width() / 2,
947           p_desktop->height() - height() );
948
949 #ifdef WIN32TRICK
950     setWindowOpacity( 0.0 );
951     fscHidden = true;
952     show();
953 #endif
954
955     vlc_mutex_init_recursive( &lock );
956 }
957
958 FullscreenControllerWidget::~FullscreenControllerWidget()
959 {
960     vlc_mutex_destroy( &lock );
961 }
962
963 /**
964  * Show fullscreen controller
965  */
966 void FullscreenControllerWidget::showFSC()
967 {
968 #ifdef WIN32TRICK
969     // after quiting and going to fs, we need to call show()
970     if( isHidden() )
971         show();
972
973     if( fscHidden )
974     {
975         fscHidden = false;
976         setWindowOpacity( 1.0 );
977     }
978 #else
979     show();
980 #endif
981
982 #if HAVE_TRANSPARENCY
983     setWindowOpacity( DEFAULT_OPACITY );
984 #endif
985 }
986
987 /**
988  * Hide fullscreen controller
989  * FIXME: under windows it have to be done by moving out of screen
990  *        because hide() doesnt work
991  */
992 void FullscreenControllerWidget::hideFSC()
993 {
994 #ifdef WIN32TRICK
995     fscHidden = true;
996     setWindowOpacity( 0.0 );    // simulate hidding
997 #else
998     hide();
999 #endif
1000 }
1001
1002 /**
1003  * Plane to hide fullscreen controller
1004  */
1005 void FullscreenControllerWidget::planHideFSC()
1006 {
1007     vlc_mutex_lock( &lock );
1008     int i_timeout = i_hide_timeout;
1009     vlc_mutex_unlock( &lock );
1010
1011     p_hideTimer->start( i_timeout );
1012
1013 #if HAVE_TRANSPARENCY
1014     b_slow_hide_begin = true;
1015     i_slow_hide_timeout = i_timeout;
1016     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
1017 #endif
1018 }
1019
1020 /**
1021  * Hidding fullscreen controller slowly
1022  * Linux: need composite manager
1023  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
1024  */
1025 void FullscreenControllerWidget::slowHideFSC()
1026 {
1027 #if HAVE_TRANSPARENCY
1028     if( b_slow_hide_begin )
1029     {
1030         b_slow_hide_begin = false;
1031
1032         p_slowHideTimer->stop();
1033         /* the last part of time divided to 100 pieces */
1034         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
1035
1036     }
1037     else
1038     {
1039 #ifdef WIN32TRICK
1040          if ( windowOpacity() > 0.0 && !fscHidden )
1041 #else
1042          if ( windowOpacity() > 0.0 )
1043 #endif
1044          {
1045              /* we should use 0.01 because of 100 pieces ^^^
1046                 but than it cannt be done in time */
1047              setWindowOpacity( windowOpacity() - 0.02 );
1048          }
1049
1050          if ( windowOpacity() <= 0.0 )
1051              p_slowHideTimer->stop();
1052     }
1053 #endif
1054 }
1055
1056 /**
1057  * event handling
1058  * events: show, hide, start timer for hidding
1059  */
1060 void FullscreenControllerWidget::customEvent( QEvent *event )
1061 {
1062     bool b_fs;
1063
1064     switch( event->type() )
1065     {
1066     case FullscreenControlShow_Type:
1067         vlc_mutex_lock( &lock );
1068         b_fs = b_fullscreen;
1069         vlc_mutex_unlock( &lock );
1070
1071         if( b_fs )  // FIXME I am not sure about that one
1072             showFSC();
1073         break;
1074     case FullscreenControlHide_Type:
1075         hideFSC();
1076         break;
1077     case FullscreenControlPlanHide_Type:
1078         if( !b_mouse_over ) // Only if the mouse is not over FSC
1079             planHideFSC();
1080         break;
1081     }
1082 }
1083
1084 /**
1085  * On mouse move
1086  * moving with FSC
1087  */
1088 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
1089 {
1090     if ( event->buttons() == Qt::LeftButton )
1091     {
1092         int i_moveX = event->globalX() - i_mouse_last_x;
1093         int i_moveY = event->globalY() - i_mouse_last_y;
1094
1095         move( x() + i_moveX, y() + i_moveY );
1096
1097         i_mouse_last_x = event->globalX();
1098         i_mouse_last_y = event->globalY();
1099     }
1100 }
1101
1102 /**
1103  * On mouse press
1104  * store position of cursor
1105  */
1106 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
1107 {
1108     i_mouse_last_x = event->globalX();
1109     i_mouse_last_y = event->globalY();
1110 }
1111
1112 /**
1113  * On mouse go above FSC
1114  */
1115 void FullscreenControllerWidget::enterEvent( QEvent *event )
1116 {
1117     b_mouse_over = true;
1118
1119     p_hideTimer->stop();
1120 #if HAVE_TRANSPARENCY
1121     p_slowHideTimer->stop();
1122 #endif
1123 }
1124
1125 /**
1126  * On mouse go out from FSC
1127  */
1128 void FullscreenControllerWidget::leaveEvent( QEvent *event )
1129 {
1130     planHideFSC();
1131
1132     b_mouse_over = false;
1133 }
1134
1135 /**
1136  * When you get pressed key, send it to video output
1137  * FIXME: clearing focus by clearFocus() to not getting
1138  * key press events didnt work
1139  */
1140 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
1141 {
1142     int i_vlck = qtEventToVLCKey( event );
1143     if( i_vlck > 0 )
1144     {
1145         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1146         event->accept();
1147     }
1148     else
1149         event->ignore();
1150 }
1151
1152 /* */
1153 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object, const char *variable,
1154                                                         vlc_value_t old_val, vlc_value_t new_val,
1155                                                         void *data )
1156 {
1157     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
1158     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1159
1160     p_fs->fullscreenChanged( p_vout, new_val.b_bool, var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1161
1162     return VLC_SUCCESS;
1163 }
1164 /* */
1165 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
1166                                                  vlc_value_t old_val, vlc_value_t new_val,
1167                                                  void *data )
1168 {
1169     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1170
1171     /* Show event */
1172     IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1173     QApplication::postEvent( p_fs, static_cast<QEvent *>(eShow) );
1174
1175     /* Plan hide event */
1176     IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1177     QApplication::postEvent( p_fs, static_cast<QEvent *>(eHide) );
1178
1179     return VLC_SUCCESS;
1180 }
1181
1182
1183 /**
1184  * It is called when video start
1185  */
1186 void FullscreenControllerWidget::attachVout( vout_thread_t *p_vout )
1187 {
1188     assert( p_vout );
1189
1190     vlc_mutex_lock( &lock );
1191     var_AddCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this ); /* I miss a add and fire */
1192     fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ), var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1193     vlc_mutex_unlock( &lock );
1194 }
1195 /**
1196  * It is called after turn off video.
1197  */
1198 void FullscreenControllerWidget::detachVout( vout_thread_t *p_vout )
1199 {
1200     assert( p_vout );
1201
1202     var_DelCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this );
1203     vlc_mutex_lock( &lock );
1204     fullscreenChanged( p_vout, false, 0 );
1205     vlc_mutex_unlock( &lock );
1206 }
1207
1208 /**
1209  * Register and unregister callback for mouse moving
1210  */
1211 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout, bool b_fs, int i_timeout )
1212 {
1213     vlc_mutex_lock( &lock );
1214     if( b_fs && !b_fullscreen )
1215     {
1216         b_fullscreen = true;
1217         i_hide_timeout = i_timeout;
1218         var_AddCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1219     }
1220     else if( !b_fs && b_fullscreen )
1221     {
1222         b_fullscreen = false;
1223         i_hide_timeout = i_timeout;
1224         var_DelCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1225
1226         /* Force fs hidding */
1227         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1228         QApplication::postEvent( this, static_cast<QEvent *>(eHide) );
1229     }
1230     vlc_mutex_unlock( &lock );
1231 }
1232
1233 /**********************************************************************
1234  * Speed control widget
1235  **********************************************************************/
1236 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
1237                              QFrame( NULL ), p_intf( _p_i )
1238 {
1239     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
1240     sizePolicy.setHorizontalStretch( 0 );
1241     sizePolicy.setVerticalStretch( 0 );
1242
1243     speedSlider = new QSlider;
1244     speedSlider->setSizePolicy( sizePolicy );
1245     speedSlider->setMaximumSize( QSize( 80, 200 ) );
1246     speedSlider->setOrientation( Qt::Vertical );
1247     speedSlider->setTickPosition( QSlider::TicksRight );
1248
1249     speedSlider->setRange( -24, 24 );
1250     speedSlider->setSingleStep( 1 );
1251     speedSlider->setPageStep( 1 );
1252     speedSlider->setTickInterval( 12 );
1253
1254     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
1255
1256     QToolButton *normalSpeedButton = new QToolButton( this );
1257     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
1258     normalSpeedButton->setAutoRaise( true );
1259     normalSpeedButton->setText( "1x" );
1260     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
1261
1262     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
1263
1264     QVBoxLayout *speedControlLayout = new QVBoxLayout;
1265     speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
1266     speedControlLayout->setSpacing( 4 );
1267     speedControlLayout->addWidget( speedSlider );
1268     speedControlLayout->addWidget( normalSpeedButton );
1269     setLayout( speedControlLayout );
1270 }
1271
1272 SpeedControlWidget::~SpeedControlWidget()
1273 {}
1274
1275 void SpeedControlWidget::setEnable( bool b_enable )
1276 {
1277     speedSlider->setEnabled( b_enable );
1278 }
1279
1280 void SpeedControlWidget::updateControls( int rate )
1281 {
1282     if( speedSlider->isSliderDown() )
1283     {
1284         //We don't want to change anything if the user is using the slider
1285         return;
1286     }
1287
1288     double value = 12 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
1289     int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
1290
1291     if( sliderValue < speedSlider->minimum() )
1292     {
1293         sliderValue = speedSlider->minimum();
1294     }
1295     else if( sliderValue > speedSlider->maximum() )
1296     {
1297         sliderValue = speedSlider->maximum();
1298     }
1299
1300     //Block signals to avoid feedback loop
1301     speedSlider->blockSignals( true );
1302     speedSlider->setValue( sliderValue );
1303     speedSlider->blockSignals( false );
1304 }
1305
1306 void SpeedControlWidget::updateRate( int sliderValue )
1307 {
1308     double speed = pow( 2, (double)sliderValue / 12 );
1309     int rate = INPUT_RATE_DEFAULT / speed;
1310
1311     THEMIM->getIM()->setRate(rate);
1312 }
1313
1314 void SpeedControlWidget::resetRate()
1315 {
1316     THEMIM->getIM()->setRate(INPUT_RATE_DEFAULT);
1317 }