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