]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Forgotten tooltip :D
[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, qtr( "Menu" ) );
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_PCH N_( "Previous chapter" )
726 #define HELP_NCH N_( "Next chapter" )
727
728     // 1 = chapter, 2 = title, 0 = no
729     if( navigation == 0 )
730     {
731         discFrame->hide();
732     } else if( navigation == 1 ) {
733         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
734         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
735         menuButton->show();
736         discFrame->show();
737     } else {
738         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
739         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
740         menuButton->hide();
741         discFrame->show();
742     }
743 }
744
745 static bool b_my_volume;
746 void ControlsWidget::updateVolume( int i_sliderVolume )
747 {
748     if( !b_my_volume )
749     {
750         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
751         aout_VolumeSet( p_intf, i_res );
752     }
753     if( i_sliderVolume == 0 )
754         volMuteLabel->setPixmap( QPixmap(":/pixmaps/volume-muted.png" ) );
755     else if( i_sliderVolume < VOLUME_MAX / 3 )
756         volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
757     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
758         volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
759     else volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-medium.png" ) );
760 }
761
762 void ControlsWidget::updateVolume()
763 {
764     /* Audio part */
765     audio_volume_t i_volume;
766     aout_VolumeGet( p_intf, &i_volume );
767     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
768     int i_gauge = volumeSlider->value();
769     b_my_volume = false;
770     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
771     {
772         b_my_volume = true;
773         volumeSlider->setValue( i_volume );
774         b_my_volume = false;
775     }
776 }
777
778 void ControlsWidget::updateInput()
779 {
780     /* Activate the interface buttons according to the presence of the input */
781     enableInput( THEMIM->getIM()->hasInput() );
782     enableVideo( THEMIM->getIM()->hasVideo() && THEMIM->getIM()->hasInput() );
783 }
784
785 void ControlsWidget::setStatus( int status )
786 {
787     if( status == PLAYING_S ) /* Playing */
788     {
789         playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
790         playButton->setToolTip( qtr( "Pause" ) );
791     }
792     else
793     {
794         playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
795         playButton->setToolTip( qtr( "Play" ) );
796     }
797 }
798
799 /**
800  * TODO
801  * This functions toggle the fullscreen mode
802  * If there is no video, it should first activate Visualisations...
803  *  This has also to be fixed in enableVideo()
804  */
805 void ControlsWidget::fullscreen()
806 {
807     vout_thread_t *p_vout =
808         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
809     if( p_vout)
810     {
811         var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
812         vlc_object_release( p_vout );
813     }
814 }
815
816 void ControlsWidget::extSettings()
817 {
818     THEDP->extendedDialog();
819 }
820
821 void ControlsWidget::slower()
822 {
823     THEMIM->getIM()->slower();
824 }
825
826 void ControlsWidget::faster()
827 {
828     THEMIM->getIM()->faster();
829 }
830
831 void ControlsWidget::enableInput( bool enable )
832 {
833     slowerButton->setEnabled( enable );
834     slider->setEnabled( enable );
835     fasterButton->setEnabled( enable );
836
837     /* Advanced Buttons too */
838     advControls->enableInput( enable );
839 }
840
841 void ControlsWidget::enableVideo( bool enable )
842 {
843     // TODO Later make the fullscreenButton toggle Visualisation and so on.
844     fullscreenButton->setEnabled( enable );
845
846     /* Advanced Buttons too */
847     advControls->enableVideo( enable );
848 }
849
850 void ControlsWidget::toggleAdvanced()
851 {
852     if( !VISIBLE( advControls ) )
853     {
854         advControls->show();
855         b_advancedVisible = true;
856     }
857     else
858     {
859         advControls->hide();
860         b_advancedVisible = false;
861     }
862     emit advancedControlsToggled( b_advancedVisible );
863 }
864
865
866 /**********************************************************************
867  * Fullscrenn control widget
868  **********************************************************************/
869 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i,
870         MainInterface *_p_mi, bool b_advControls, bool b_shiny )
871         : ControlsWidget( _p_i, _p_mi, b_advControls, b_shiny, true ),
872         i_lastPosX( -1 ), i_lastPosY( -1 ), i_hideTimeout( 1 ),
873         b_mouseIsOver( false )
874 {
875     setWindowFlags( Qt::ToolTip );
876
877     setFrameShape( QFrame::StyledPanel );
878     setFrameStyle( QFrame::Sunken );
879     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
880
881     QGridLayout *fsLayout = new QGridLayout( this );
882     controlLayout->setSpacing( 0 );
883     controlLayout->setLayoutMargins( 5, 1, 5, 1, 5 );
884
885     fsLayout->addWidget( slowerButton, 0, 0 );
886     slider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum);
887     fsLayout->addWidget( slider, 0, 1, 1, 6 );
888     fsLayout->addWidget( fasterButton, 0, 7 );
889
890     fsLayout->addWidget( volMuteLabel, 1, 0);
891     fsLayout->addWidget( volumeSlider, 1, 1 );
892
893     fsLayout->addLayout( controlButLayout, 1, 2 );
894
895     fsLayout->addWidget( playButton, 1, 3 );
896
897     fsLayout->addWidget( discFrame, 1, 4 );
898
899     fsLayout->addWidget( telexFrame, 1, 5 );
900
901     fsLayout->addWidget( advControls, 1, 6, Qt::AlignVCenter );
902
903     fsLayout->addWidget( fullscreenButton, 1, 7 );
904
905     /* hiding timer */
906     p_hideTimer = new QTimer( this );
907     CONNECT( p_hideTimer, timeout(), this, hideFSControllerWidget() );
908     p_hideTimer->setSingleShot( true );
909
910     /* slow hiding timer */
911 #if HAVE_TRANSPARENCY
912     p_slowHideTimer = new QTimer( this );
913     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
914 #endif
915
916     adjustSize ();  /* need to get real width and height for moving */
917
918     /* center down */
919     QDesktopWidget * p_desktop = QApplication::desktop();
920
921     move( p_desktop->width() / 2 - width() / 2,
922           p_desktop->height() - height() );
923
924     #ifdef WIN32TRICK
925     setWindowOpacity( 0.0 );
926     fscHidden = true;
927     show();
928     #endif
929 }
930
931 FullscreenControllerWidget::~FullscreenControllerWidget()
932 {
933 }
934
935 /**
936  * Hide fullscreen controller
937  * FIXME: under windows it have to be done by moving out of screen
938  *        because hide() doesnt work
939  */
940 void FullscreenControllerWidget::hideFSControllerWidget()
941 {
942     #ifdef WIN32TRICK
943     fscHidden = true;
944     setWindowOpacity( 0.0 );    // simulate hidding
945     #else
946     hide();
947     #endif
948 }
949
950 /**
951  * Hidding fullscreen controller slowly
952  * Linux: need composite manager
953  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
954  */
955 void FullscreenControllerWidget::slowHideFSC()
956 {
957 #if HAVE_TRANSPARENCY
958     static bool first_call = true;
959
960     if ( first_call )
961     {
962         first_call = false;
963
964         p_slowHideTimer->stop();
965         /* the last part of time divided to 100 pieces */
966         p_slowHideTimer->start(
967             (int) ( i_hideTimeout / 2 / ( windowOpacity() * 100 ) ) );
968     }
969     else
970     {
971          if ( windowOpacity() > 0.0 )
972          {
973              /* we should use 0.01 because of 100 pieces ^^^
974                 but than it cannt be done in time */
975              setWindowOpacity( windowOpacity() - 0.02 );
976          }
977
978          if ( windowOpacity() == 0.0 )
979          {
980              first_call = true;
981              p_slowHideTimer->stop();
982          }
983     }
984 #endif
985 }
986
987 /**
988  * Get state of visibility of FS controller on screen
989  * On windows control if it is on hidden position
990  */
991 bool FullscreenControllerWidget::isFSCHidden()
992 {
993     #ifdef WIN32TRICK
994     return fscHidden;
995     #endif
996
997     return isHidden();
998 }
999
1000 /**
1001  * event handling
1002  * events: show, hide, start timer for hidding
1003  */
1004 void FullscreenControllerWidget::customEvent( QEvent *event )
1005 {
1006     int type = event->type();
1007
1008     if ( type == FullscreenControlShow_Type )
1009     {
1010         #ifdef WIN32TRICK
1011         // after quiting and going to fs, we need to call show()
1012         if ( isHidden() )
1013             show();
1014
1015         if ( fscHidden )
1016         {
1017             fscHidden = false;
1018             setWindowOpacity( 1.0 );
1019         }
1020         #else
1021         show();
1022         #endif
1023
1024 #if HAVE_TRANSPARENCY
1025         setWindowOpacity( DEFAULT_OPACITY );
1026 #endif
1027     }
1028     else if ( type == FullscreenControlHide_Type )
1029     {
1030         hideFSControllerWidget();
1031     }
1032     else if ( type == FullscreenControlPlanHide_Type && !b_mouseIsOver )
1033     {
1034         p_hideTimer->start( i_hideTimeout );
1035 #if HAVE_TRANSPARENCY
1036         p_slowHideTimer->start( i_hideTimeout / 2 );
1037 #endif
1038     }
1039 }
1040
1041 /**
1042  * On mouse move
1043  * moving with FSC
1044  */
1045 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
1046 {
1047     if ( event->buttons() == Qt::LeftButton )
1048     {
1049         int i_moveX = event->globalX() - i_lastPosX;
1050         int i_moveY = event->globalY() - i_lastPosY;
1051
1052         move( x() + i_moveX, y() + i_moveY );
1053
1054         i_lastPosX = event->globalX();
1055         i_lastPosY = event->globalY();
1056     }
1057 }
1058
1059 /**
1060  * On mouse press
1061  * store position of cursor
1062  */
1063 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
1064 {
1065     i_lastPosX = event->globalX();
1066     i_lastPosY = event->globalY();
1067 }
1068
1069 /**
1070  * On mouse go above FSC
1071  */
1072 void FullscreenControllerWidget::enterEvent( QEvent *event )
1073 {
1074     p_hideTimer->stop();
1075 #if HAVE_TRANSPARENCY
1076     p_slowHideTimer->stop();
1077 #endif
1078     b_mouseIsOver = true;
1079 }
1080
1081 /**
1082  * On mouse go out from FSC
1083  */
1084 void FullscreenControllerWidget::leaveEvent( QEvent *event )
1085 {
1086     p_hideTimer->start( i_hideTimeout );
1087 #if HAVE_TRANSPARENCY
1088     p_slowHideTimer->start( i_hideTimeout / 2 );
1089 #endif
1090     b_mouseIsOver = false;
1091 }
1092
1093 /**
1094  * When you get pressed key, send it to video output
1095  * FIXME: clearing focus by clearFocus() to not getting
1096  * key press events didnt work
1097  */
1098 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
1099 {
1100     int i_vlck = qtEventToVLCKey( event );
1101     if( i_vlck > 0 )
1102     {
1103         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1104         event->accept();
1105     }
1106     else
1107         event->ignore();
1108 }
1109
1110 /**
1111  * It is called when video start
1112  */
1113 void FullscreenControllerWidget::regFullscreenCallback( vout_thread_t *p_vout )
1114 {
1115     if ( p_vout )
1116     {
1117         var_AddCallback( p_vout, "fullscreen", regMouseMoveCallback, this );
1118     }
1119 }
1120
1121 /**
1122  * It is called after turn off video, because p_vout is NULL now
1123  * we cannt delete callback, just hide if FScontroller is visible
1124  */
1125 void FullscreenControllerWidget::unregFullscreenCallback()
1126 {
1127     if ( isVisible() )
1128         hide();
1129 }
1130
1131 /**
1132  * Register and unregister callback for mouse moving
1133  */
1134 static int regMouseMoveCallback( vlc_object_t *vlc_object, const char *variable,
1135                                  vlc_value_t old_val, vlc_value_t new_val,
1136                                  void *data )
1137 {
1138     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
1139
1140     static bool b_registered = false;
1141     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *) data;
1142
1143     if ( var_GetBool( p_vout, "fullscreen" ) && !b_registered )
1144     {
1145         p_fs->SetHideTimeout( var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1146         var_AddCallback( p_vout, "mouse-moved",
1147                         showFullscreenControllCallback, (void *) p_fs );
1148         b_registered = true;
1149     }
1150
1151     if ( !var_GetBool( p_vout, "fullscreen" ) && b_registered )
1152     {
1153         var_DelCallback( p_vout, "mouse-moved",
1154                         showFullscreenControllCallback, (void *) p_fs );
1155         b_registered = false;
1156     }
1157
1158     if ( !var_GetBool( p_vout, "fullscreen" ) )
1159         p_fs->hide();
1160
1161     return VLC_SUCCESS;
1162 }
1163
1164 /**
1165  * Show fullscreen controller after mouse move
1166  * after show immediately plan hide event
1167  */
1168 static int showFullscreenControllCallback( vlc_object_t *vlc_object, const char *variable,
1169                                            vlc_value_t old_val, vlc_value_t new_val,
1170                                            void *data )
1171 {
1172     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *) data;
1173
1174     if ( p_fs->isFSCHidden() || p_fs->windowOpacity() < DEFAULT_OPACITY )
1175     {
1176         IMEvent *event = new IMEvent( FullscreenControlShow_Type, 0 );
1177         QApplication::postEvent( p_fs, static_cast<QEvent *>(event) );
1178     }
1179
1180     IMEvent *e = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1181     QApplication::postEvent( p_fs, static_cast<QEvent *>(e) );
1182
1183     return VLC_SUCCESS;
1184 }
1185
1186 /**********************************************************************
1187  * Speed control widget
1188  **********************************************************************/
1189 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
1190                              QFrame( NULL ), p_intf( _p_i )
1191 {
1192     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
1193     sizePolicy.setHorizontalStretch( 0 );
1194     sizePolicy.setVerticalStretch( 0 );
1195
1196     speedSlider = new QSlider;
1197     speedSlider->setSizePolicy( sizePolicy );
1198     speedSlider->setMaximumSize( QSize( 80, 200 ) );
1199     speedSlider->setOrientation( Qt::Vertical );
1200     speedSlider->setTickPosition( QSlider::TicksRight );
1201
1202     speedSlider->setRange( -100, 100 );
1203     speedSlider->setSingleStep( 10 );
1204     speedSlider->setPageStep( 20 );
1205     speedSlider->setTickInterval( 20 );
1206
1207     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
1208
1209     QToolButton *normalSpeedButton = new QToolButton( this );
1210     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
1211     normalSpeedButton->setAutoRaise( true );
1212     normalSpeedButton->setText( "N" );
1213     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
1214
1215     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
1216
1217     QVBoxLayout *speedControlLayout = new QVBoxLayout;
1218     speedControlLayout->addWidget( speedSlider );
1219     speedControlLayout->addWidget( normalSpeedButton );
1220     setLayout( speedControlLayout );
1221 }
1222
1223 SpeedControlWidget::~SpeedControlWidget()
1224 {}
1225
1226 void SpeedControlWidget::setEnable( bool b_enable )
1227 {
1228     speedSlider->setEnabled( b_enable );
1229 }
1230
1231 #define RATE_SLIDER_MAXIMUM 3.0
1232 #define RATE_SLIDER_MINIMUM 0.3
1233 #define RATE_SLIDER_LENGTH 100.0
1234
1235 void SpeedControlWidget::updateControls( int rate )
1236 {
1237     if( speedSlider->isSliderDown() )
1238     {
1239         //We don't want to change anything if the user is using the slider
1240         return;
1241     }
1242
1243     int sliderValue;
1244     double speed = INPUT_RATE_DEFAULT / (double)rate;
1245
1246     if( rate >= INPUT_RATE_DEFAULT )
1247     {
1248         if( speed < RATE_SLIDER_MINIMUM )
1249         {
1250             sliderValue = speedSlider->minimum();
1251         }
1252         else
1253         {
1254             sliderValue = (int)( ( speed - 1.0 ) * RATE_SLIDER_LENGTH
1255                                         / ( 1.0 - RATE_SLIDER_MAXIMUM ) );
1256         }
1257     }
1258     else
1259     {
1260         if( speed > RATE_SLIDER_MAXIMUM )
1261         {
1262             sliderValue = speedSlider->maximum();
1263         }
1264         else
1265         {
1266             sliderValue = (int)( ( speed - 1.0 ) * RATE_SLIDER_LENGTH
1267                                         / ( RATE_SLIDER_MAXIMUM - 1.0 ) );
1268         }
1269     }
1270
1271     //Block signals to avoid feedback loop
1272     speedSlider->blockSignals( true );
1273     speedSlider->setValue( sliderValue );
1274     speedSlider->blockSignals( false );
1275 }
1276
1277 void SpeedControlWidget::updateRate( int sliderValue )
1278 {
1279     int rate;
1280
1281     if( sliderValue < 0.0 )
1282     {
1283         rate = (int)(INPUT_RATE_DEFAULT* RATE_SLIDER_LENGTH /
1284             ( sliderValue * ( 1.0 - RATE_SLIDER_MINIMUM ) + RATE_SLIDER_LENGTH ));
1285     }
1286     else
1287     {
1288         rate = (int)(INPUT_RATE_DEFAULT* RATE_SLIDER_LENGTH /
1289             ( sliderValue * ( RATE_SLIDER_MAXIMUM - 1.0 ) + RATE_SLIDER_LENGTH ));
1290     }
1291
1292     THEMIM->getIM()->setRate(rate);
1293 }
1294
1295 void SpeedControlWidget::resetRate()
1296 {
1297     THEMIM->getIM()->setRate(INPUT_RATE_DEFAULT);
1298 }