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