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