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