]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
c9540cbc95c25201f30fc459ba8cf6a48e042740
[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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "dialogs_provider.hpp"
31 #include "components/interface_widgets.hpp"
32 #include "main_interface.hpp"
33 #include "input_manager.hpp"
34 #include "menus.hpp"
35 #include "util/input_slider.hpp"
36 #include "util/customwidgets.hpp"
37 #include <vlc_vout.h>
38
39 #include <QLabel>
40 #include <QSpacerItem>
41 #include <QCursor>
42 #include <QPushButton>
43 #include <QToolButton>
44 #include <QHBoxLayout>
45 #include <QMenu>
46 #include <QPalette>
47 #include <QResizeEvent>
48 #include <QDate>
49
50 /**********************************************************************
51  * Video Widget. A simple frame on which video is drawn
52  * This class handles resize issues
53  **********************************************************************/
54 static void *DoRequest( intf_thread_t *, vout_thread_t *, int*,int*,
55                         unsigned int *, unsigned int * );
56 static void DoRelease( intf_thread_t *, void * );
57 static int DoControl( intf_thread_t *, void *, int, va_list );
58
59 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
60 {
61     /* Init */
62     vlc_mutex_init( p_intf, &lock );
63     p_vout = NULL;
64     hide(); setMinimumSize( 16, 16 );
65     videoSize.rwidth() = -1;
66     videoSize.rheight() = -1;
67
68     /* Black background is more coherent for a Video Widget IMVHO */
69     QPalette plt =  palette();
70     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
71     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
72     setPalette( plt );
73
74     /* The core can ask through a callback to show the video */
75     CONNECT( this, askVideoWidgetToShow(), this, show() );
76     /* The core can ask through a callback to resize the video */
77    // CONNECT( this, askResize( int, int ), this, SetSizing( int, int ) );
78 }
79
80 VideoWidget::~VideoWidget()
81 {
82     vlc_mutex_lock( &lock );
83     if( p_vout )
84     {
85         if( !p_intf->psz_switch_intf )
86         {
87             if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
88                 vout_Control( p_vout, VOUT_REPARENT );
89         }
90         else
91         {
92             if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
93                 vout_Control( p_vout, VOUT_CLOSE );
94         }
95     }
96     vlc_mutex_unlock( &lock );
97     vlc_mutex_destroy( &lock );
98 }
99
100 /**
101  * Request the video to avoid the conflicts
102  **/
103 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
104                            unsigned int *pi_width, unsigned int *pi_height )
105 {
106     msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
107     emit askVideoWidgetToShow();
108     if( p_vout )
109     {
110         msg_Dbg( p_intf, "embedded video already in use" );
111         return NULL;
112     }
113     p_vout = p_nvout;
114     return ( void* )winId();
115 }
116
117 /* Set the Widget to the correct Size */
118 /* Function has to be called by the parent
119    Parent has to care about resizing himself*/
120 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
121 {
122     msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
123     videoSize.rwidth() = w;
124     videoSize.rheight() = h;
125     updateGeometry(); // Needed for deinterlace
126 }
127
128 void VideoWidget::release( void *p_win )
129 {
130     msg_Dbg( p_intf, "Video is non needed anymore" );
131     p_vout = NULL;
132     videoSize.rwidth() = 0;
133     videoSize.rheight() = 0;
134     hide();
135     updateGeometry(); // Needed for deinterlace
136 }
137
138 QSize VideoWidget::sizeHint() const
139 {
140     return videoSize;
141 }
142
143 /**********************************************************************
144  * Background Widget. Show a simple image background. Currently,
145  * it's album art if present or cone.
146  **********************************************************************/
147 #define ICON_SIZE 128
148 #define MAX_BG_SIZE 400
149 #define MIN_BG_SIZE 64
150
151 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
152                  :QWidget( NULL ), p_intf( _p_i )
153 {
154     /* We should use that one to take the more size it can */
155 //    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
156
157     /* A dark background */
158     setAutoFillBackground( true );
159     plt =  palette();
160     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
161     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
162     setPalette( plt );
163
164     /* A cone in the middle */
165     label = new QLabel;
166     label->setMargin( 5 );
167     label->setMaximumHeight( MAX_BG_SIZE );
168     label->setMaximumWidth( MAX_BG_SIZE );
169     label->setMinimumHeight( MIN_BG_SIZE );
170     label->setMinimumWidth( MIN_BG_SIZE );
171     if( QDate::currentDate().dayOfYear() >= 354 )
172         label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
173     else
174         label->setPixmap( QPixmap( ":/vlc128.png" ) );
175
176     QGridLayout *backgroundLayout = new QGridLayout( this );
177     backgroundLayout->addWidget( label, 0, 1 );
178     backgroundLayout->setColumnStretch( 0, 1 );
179     backgroundLayout->setColumnStretch( 2, 1 );
180
181     CONNECT( THEMIM->getIM(), artChanged( QString ), this, updateArt( QString ) );
182 }
183
184 BackgroundWidget::~BackgroundWidget()
185 {
186 }
187
188 void BackgroundWidget::resizeEvent( QResizeEvent * event )
189 {
190     if( event->size().height() <= MIN_BG_SIZE )
191         label->hide();
192     else
193         label->show();
194 }
195
196 void BackgroundWidget::updateArt( QString url )
197 {
198     if( url.isEmpty() )
199     {
200         if( QDate::currentDate().dayOfYear() >= 354 )
201             label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
202         else
203             label->setPixmap( QPixmap( ":/vlc128.png" ) );
204         return;
205     }
206     else
207     {
208         label->setPixmap( QPixmap( url ) );
209         msg_Dbg( p_intf, "changing input b_need_update done ");
210     }
211 }
212
213 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
214 {
215     QVLCMenu::PopupMenu( p_intf, true );
216 }
217
218 /**********************************************************************
219  * Visualization selector panel
220  **********************************************************************/
221 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
222                                 QFrame( NULL ), p_intf( _p_i )
223 {
224     QHBoxLayout *layout = new QHBoxLayout( this );
225     layout->setMargin( 0 );
226     QPushButton *prevButton = new QPushButton( "Prev" );
227     QPushButton *nextButton = new QPushButton( "Next" );
228     layout->addWidget( prevButton );
229     layout->addWidget( nextButton );
230
231     layout->addItem( new QSpacerItem( 40,20,
232                               QSizePolicy::Expanding, QSizePolicy::Minimum ) );
233     layout->addWidget( new QLabel( qtr( "Current visualization:" ) ) );
234
235     current = new QLabel( qtr( "None" ) );
236     layout->addWidget( current );
237
238     BUTTONACT( prevButton, prev() );
239     BUTTONACT( nextButton, next() );
240
241     setLayout( layout );
242     setMaximumHeight( 35 );
243 }
244
245 VisualSelector::~VisualSelector()
246 {
247 }
248
249 void VisualSelector::prev()
250 {
251     char *psz_new = aout_VisualPrev( p_intf );
252     if( psz_new )
253     {
254         current->setText( qfu( psz_new ) );
255         free( psz_new );
256     }
257 }
258
259 void VisualSelector::next()
260 {
261     char *psz_new = aout_VisualNext( p_intf );
262     if( psz_new )
263     {
264         current->setText( qfu( psz_new ) );
265         free( psz_new );
266     }
267 }
268
269 /**********************************************************************
270  * TEH controls
271  **********************************************************************/
272
273 #define setupSmallButton( aButton ){  \
274     aButton->setMaximumSize( QSize( 26, 26 ) ); \
275     aButton->setMinimumSize( QSize( 26, 26 ) ); \
276     aButton->setIconSize( QSize( 20, 20 ) ); }
277
278 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i ) :
279                                            QFrame( NULL ), p_intf( _p_i )
280 {
281     QHBoxLayout *advLayout = new QHBoxLayout( this );
282     advLayout->setMargin( 0 );
283     advLayout->setSpacing( 0 );
284
285     /* A to B Button */
286     ABButton = new QPushButton( "AB" );
287     ABButton->setMaximumSize( QSize( 26, 26 ) );
288     ABButton->setIconSize( QSize( 20, 20 ) );
289     advLayout->addWidget( ABButton );
290     BUTTON_SET_ACT( ABButton, "AB", qtr( "A to B" ), fromAtoB() );
291     timeA = timeB = 0;
292     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
293              this, AtoBLoop( float, int, int ) );
294 #if 0
295     frameButton = new QPushButton( "Fr" );
296     frameButton->setMaximumSize( QSize( 26, 26 ) );
297     frameButton->setIconSize( QSize( 20, 20 ) );
298     advLayout->addWidget( frameButton );
299     BUTTON_SET_ACT( frameButton, "Fr", qtr( "Frame by Frame" ), frame() );
300 #endif
301
302     recordButton = new QPushButton( "R" );
303     recordButton->setMaximumSize( QSize( 26, 26 ) );
304     recordButton->setIconSize( QSize( 20, 20 ) );
305     advLayout->addWidget( recordButton );
306     BUTTON_SET_ACT_I( recordButton, "", record_16px.png,
307             qtr( "Record" ), record() );
308
309     /* Snapshot Button */
310     snapshotButton = new QPushButton( "S" );
311     snapshotButton->setMaximumSize( QSize( 26, 26 ) );
312     snapshotButton->setIconSize( QSize( 20, 20 ) );
313     advLayout->addWidget( snapshotButton );
314     BUTTON_SET_ACT( snapshotButton, "S", qtr( "Take a snapshot" ), snapshot() );
315 }
316
317 AdvControlsWidget::~AdvControlsWidget()
318 {}
319
320 void AdvControlsWidget::enableInput( bool enable )
321 {
322     ABButton->setEnabled( enable );
323     recordButton->setEnabled( enable );
324 }
325
326 void AdvControlsWidget::enableVideo( bool enable )
327 {
328     snapshotButton->setEnabled( enable );
329 #if 0
330     frameButton->setEnabled( enable );
331 #endif
332 }
333
334 void AdvControlsWidget::snapshot()
335 {
336     vout_thread_t *p_vout =
337         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
338     if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
339 }
340
341 /* Function called when the button is clicked() */
342 void AdvControlsWidget::fromAtoB()
343 {
344     if( !timeA )
345     {
346         timeA = var_GetTime( THEMIM->getInput(), "time"  );
347         ABButton->setText( "A->..." );
348         return;
349     }
350     if( !timeB )
351     {
352         timeB = var_GetTime( THEMIM->getInput(), "time"  );
353         var_SetTime( THEMIM->getInput(), "time" , timeA );
354         ABButton->setText( "A<=>B" );
355         return;
356     }
357     timeA = 0;
358     timeB = 0;
359     ABButton->setText( "AB" );
360 }
361
362 /* Function called regularly when in an AtoB loop */
363 void AdvControlsWidget::AtoBLoop( float f_pos, int i_time, int i_length )
364 {
365     if( timeB )
366     {
367         if( i_time >= (int)(timeB/1000000) )
368             var_SetTime( THEMIM->getInput(), "time" , timeA );
369     }
370 }
371
372 /* FIXME Record function */
373 void AdvControlsWidget::record(){}
374
375 #if 0
376 //FIXME Frame by frame function
377 void AdvControlsWidget::frame(){}
378 #endif
379
380 /*****************************
381  * DA Control Widget !
382  *****************************/
383 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
384                                 MainInterface *_p_mi,
385                                 bool b_advControls,
386                                 bool b_shiny ) :
387                                 QFrame( NULL ), p_intf( _p_i )
388 {
389     controlLayout = new QGridLayout( this );
390     controlLayout->setSpacing( 0 );
391 #if QT43
392     controlLayout->setContentsMargins( 9, 6, 9, 6 );
393 #else
394     controlLayout->setMargin( 6 );
395 #endif
396
397     setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Maximum );
398
399     /** The main Slider **/
400     slider = new InputSlider( Qt::Horizontal, NULL );
401     controlLayout->addWidget( slider, 0, 1, 1, 16 );
402     /* Update the position when the IM has changed */
403     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
404              slider, setPosition( float, int, int ) );
405     /* And update the IM, when the position has changed */
406     CONNECT( slider, sliderDragged( float ),
407              THEMIM->getIM(), sliderUpdate( float ) );
408
409     /** Slower and faster Buttons **/
410     slowerButton = new QToolButton;
411     slowerButton->setAutoRaise( true );
412     slowerButton->setMaximumSize( QSize( 26, 20 ) );
413
414     BUTTON_SET_ACT( slowerButton, "-", qtr( "Slower" ), slower() );
415     controlLayout->addWidget( slowerButton, 0, 0 );
416
417     fasterButton = new QToolButton;
418     fasterButton->setAutoRaise( true );
419     fasterButton->setMaximumSize( QSize( 26, 20 ) );
420
421     BUTTON_SET_ACT( fasterButton, "+", qtr( "Faster" ), faster() );
422     controlLayout->addWidget( fasterButton, 0, 17 );
423
424     /* advanced Controls handling */
425     b_advancedVisible = b_advControls;
426
427     advControls = new AdvControlsWidget( p_intf );
428     controlLayout->addWidget( advControls, 1, 3, 2, 4, Qt::AlignBottom );
429     if( !b_advancedVisible ) advControls->hide();
430
431     /** Disc and Menus handling */
432     discFrame = new QWidget( this );
433
434     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
435     discLayout->setSpacing( 0 );
436     discLayout->setMargin( 0 );
437
438     prevSectionButton = new QPushButton( discFrame );
439     setupSmallButton( prevSectionButton );
440     discLayout->addWidget( prevSectionButton );
441
442     menuButton = new QPushButton( discFrame );
443     setupSmallButton( menuButton );
444     discLayout->addWidget( menuButton );
445
446     nextSectionButton = new QPushButton( discFrame );
447     setupSmallButton( nextSectionButton );
448     discLayout->addWidget( nextSectionButton );
449
450     controlLayout->addWidget( discFrame, 1, 10, 2, 3, Qt::AlignBottom );
451
452     BUTTON_SET_IMG( prevSectionButton, "", previous.png, "" );
453     BUTTON_SET_IMG( nextSectionButton, "", next.png, "" );
454     BUTTON_SET_IMG( menuButton, "", previous.png, "" );
455
456     discFrame->hide();
457
458     /* Change the navigation button display when the IM navigation changes */
459     CONNECT( THEMIM->getIM(), navigationChanged( int ),
460              this, setNavigation( int ) );
461     /* Changes the IM navigation when triggered on the nav buttons */
462     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
463              sectionPrev() );
464     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
465              sectionNext() );
466     CONNECT( menuButton, clicked(), THEMIM->getIM(),
467              sectionMenu() );
468     /**
469      * Telextext QFrame
470      * TODO: Merge with upper menu in a StackLayout
471      **/
472 #ifdef ZVBI_COMPILED
473     telexFrame = new QWidget( this );
474     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
475     telexLayout->setSpacing( 0 );
476     telexLayout->setMargin( 0 );
477
478     QToolButton *telexOn = new QToolButton;
479     telexOn->setText( qtr( "On" ) );
480     setupSmallButton( telexOn );
481     telexLayout->addWidget( telexOn );
482
483     QToolButton *telexTransparent = new QToolButton;
484     telexTransparent->setText( qtr( "Transparent" ) );
485     setupSmallButton( telexTransparent );
486     telexLayout->addWidget( telexTransparent );
487
488     QSpinBox *telexPage = new QSpinBox;
489     telexPage->setRange( 0, 999 );
490     telexPage->setValue( 100 );
491     telexPage->setAlignment( Qt::AlignRight );
492     telexLayout->addWidget( telexPage );
493
494     controlLayout->addWidget( telexFrame, 1, 10, 2, 3, Qt::AlignBottom );
495     telexFrame->hide();
496
497     CONNECT( telexPage, valueChanged( int ), THEMIM->getIM(),
498              telexGotoPage( int ) );
499     CONNECT( telexOn, clicked( bool ), THEMIM->getIM(),
500              telexToggle( bool ) );
501     CONNECT( telexTransparent, clicked( bool ),
502              THEMIM->getIM(), telexSetTransparency( bool ) );
503     CONNECT( THEMIM->getIM(), teletextEnabled( bool ),
504              telexFrame, setVisible( bool ) );
505 #endif
506
507     /** Play Buttons **/
508     QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
509     sizePolicy.setHorizontalStretch( 0 );
510     sizePolicy.setVerticalStretch( 0 );
511
512     /* Play */
513     playButton = new QPushButton;
514     playButton->setSizePolicy( sizePolicy );
515     playButton->setMaximumSize( QSize( 36, 36 ) );
516     playButton->setMinimumSize( QSize( 36, 36 ) );
517     playButton->setIconSize( QSize( 30, 30 ) );
518
519     controlLayout->addWidget( playButton, 2, 0, 2, 2 );
520
521     controlLayout->setColumnMinimumWidth( 2, 20 );
522     controlLayout->setColumnStretch( 2, 0 );
523
524     /** Prev + Stop + Next Block **/
525     QHBoxLayout *controlButLayout = new QHBoxLayout;
526     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
527
528     /* Prev */
529     QPushButton *prevButton = new QPushButton;
530     prevButton->setSizePolicy( sizePolicy );
531     setupSmallButton( prevButton );
532
533     controlButLayout->addWidget( prevButton );
534
535     /* Stop */
536     QPushButton *stopButton = new QPushButton;
537     stopButton->setSizePolicy( sizePolicy );
538     setupSmallButton( stopButton );
539
540     controlButLayout->addWidget( stopButton );
541
542     /* next */
543     QPushButton *nextButton = new QPushButton;
544     nextButton->setSizePolicy( sizePolicy );
545     setupSmallButton( nextButton );
546
547     controlButLayout->addWidget( nextButton );
548
549     /* Add this block to the main layout */
550     controlLayout->addLayout( controlButLayout, 3, 3, 1, 3 );
551
552     BUTTON_SET_ACT_I( playButton, "", play.png, qtr( "Play" ), play() );
553     BUTTON_SET_ACT_I( prevButton, "" , previous.png,
554                       qtr( "Previous" ), prev() );
555     BUTTON_SET_ACT_I( nextButton, "", next.png, qtr( "Next" ), next() );
556     BUTTON_SET_ACT_I( stopButton, "", stop.png, qtr( "Stop" ), stop() );
557
558     controlLayout->setColumnMinimumWidth( 7, 20 );
559     controlLayout->setColumnStretch( 7, 0 );
560     controlLayout->setColumnStretch( 8, 0 );
561     controlLayout->setColumnStretch( 9, 0 );
562
563     /*
564      * Other first Line buttons
565      */
566     /** Fullscreen/Visualisation **/
567     fullscreenButton = new QPushButton( "F" );
568     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
569     setupSmallButton( fullscreenButton );
570     controlLayout->addWidget( fullscreenButton, 3, 10, Qt::AlignBottom );
571
572     /** Playlist Button **/
573     playlistButton = new QPushButton;
574     setupSmallButton( playlistButton );
575     controlLayout->addWidget( playlistButton, 3, 11, Qt::AlignBottom );
576     BUTTON_SET_IMG( playlistButton, "" , playlist.png, qtr( "Show playlist" ) );
577     CONNECT( playlistButton, clicked(), _p_mi, togglePlaylist() );
578
579     /** extended Settings **/
580     QPushButton *extSettingsButton = new QPushButton( "F" );
581     BUTTON_SET_ACT( extSettingsButton, "Ex", qtr( "Extended Settings" ),
582             extSettings() );
583     setupSmallButton( extSettingsButton );
584     controlLayout->addWidget( extSettingsButton, 3, 12, Qt::AlignBottom );
585
586     controlLayout->setColumnStretch( 13, 0 );
587     controlLayout->setColumnMinimumWidth( 13, 24 );
588     controlLayout->setColumnStretch( 14, 5 );
589
590     /* Volume */
591     VolumeClickHandler *hVolLabel = new VolumeClickHandler( p_intf, this );
592
593     volMuteLabel = new QLabel;
594     volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-medium.png" ) );
595     volMuteLabel->setToolTip( qtr( "Mute" ) );
596     volMuteLabel->installEventFilter( hVolLabel );
597     controlLayout->addWidget( volMuteLabel, 3, 15, Qt::AlignBottom );
598
599     if( b_shiny )
600     {
601         volumeSlider = new SoundSlider( this,
602             config_GetInt( p_intf, "volume-step" ),
603             config_GetInt( p_intf, "qt-volume-complete" ) );
604     }
605     else
606     {
607         volumeSlider = new QSlider( this );
608         volumeSlider->setOrientation( Qt::Horizontal );
609     }
610     volumeSlider->setMaximumSize( QSize( 200, 40 ) );
611     volumeSlider->setMinimumSize( QSize( 106, 30 ) );
612     volumeSlider->setFocusPolicy( Qt::NoFocus );
613     controlLayout->addWidget( volumeSlider, 2, 16, 2 , 2, Qt::AlignBottom );
614
615     /* Set the volume from the config */
616     volumeSlider->setValue( ( config_GetInt( p_intf, "volume" ) ) *
617                               VOLUME_MAX / (AOUT_VOLUME_MAX/2) );
618
619     /* Force the update at build time in order to have a muted icon if needed */
620     updateVolume( volumeSlider->value() );
621
622     /* Volume control connection */
623     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
624     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
625
626     updateInput();
627 }
628
629 ControlsWidget::~ControlsWidget()
630 {}
631
632 void ControlsWidget::stop()
633 {
634     THEMIM->stop();
635 }
636
637 void ControlsWidget::play()
638 {
639     if( THEPL->current.i_size == 0 )
640     {
641         /* The playlist is empty, open a file requester */
642         THEDP->openFileDialog();
643         setStatus( 0 );
644         return;
645     }
646     THEMIM->togglePlayPause();
647 }
648
649 void ControlsWidget::prev()
650 {
651     THEMIM->prev();
652 }
653
654 void ControlsWidget::next()
655 {
656     THEMIM->next();
657 }
658
659 void ControlsWidget::setNavigation( int navigation )
660 {
661 #define HELP_MENU N_( "Menu" )
662 #define HELP_PCH N_( "Previous chapter" )
663 #define HELP_NCH N_( "Next chapter" )
664 #define HELP_PTR N_( "Previous track" )
665 #define HELP_NTR N_( "Next track" )
666
667     // 1 = chapter, 2 = title, 0 = no
668     if( navigation == 0 )
669     {
670         discFrame->hide();
671     } else if( navigation == 1 ) {
672         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
673         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
674         menuButton->show();
675         discFrame->show();
676     } else {
677         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
678         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
679         menuButton->hide();
680         discFrame->show();
681     }
682 }
683
684 static bool b_my_volume;
685 void ControlsWidget::updateVolume( int i_sliderVolume )
686 {
687     if( !b_my_volume )
688     {
689         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
690         aout_VolumeSet( p_intf, i_res );
691     }
692     if( i_sliderVolume == 0 )
693         volMuteLabel->setPixmap( QPixmap(":/pixmaps/volume-muted.png" ) );
694     else if( i_sliderVolume < VOLUME_MAX / 3 )
695         volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
696     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
697         volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
698     else volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-medium.png" ) );
699 }
700
701 void ControlsWidget::updateVolume()
702 {
703     /* Audio part */
704     audio_volume_t i_volume;
705     aout_VolumeGet( p_intf, &i_volume );
706     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
707     int i_gauge = volumeSlider->value();
708     b_my_volume = false;
709     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
710     {
711         b_my_volume = true;
712         volumeSlider->setValue( i_volume );
713         b_my_volume = false;
714     }
715 }
716
717 void ControlsWidget::updateInput()
718 {
719     /* Activate the interface buttons according to the presence of the input */
720     enableInput( THEMIM->getIM()->hasInput() );
721     enableVideo( THEMIM->getIM()->hasVideo() && THEMIM->getIM()->hasInput() );
722 }
723
724 void ControlsWidget::setStatus( int status )
725 {
726     if( status == PLAYING_S ) /* Playing */
727     {
728         playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
729         playButton->setToolTip( qtr( "Pause" ) );
730     }
731     else
732     {
733         playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
734         playButton->setToolTip( qtr( "Play" ) );
735     }
736 }
737
738 /**
739  * TODO
740  * This functions toggle the fullscreen mode
741  * If there is no video, it should first activate Visualisations...
742  *  This has also to be fixed in enableVideo()
743  */
744 void ControlsWidget::fullscreen()
745 {
746     vout_thread_t *p_vout =
747         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
748     if( p_vout)
749     {
750         var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
751         vlc_object_release( p_vout );
752     }
753 }
754
755 void ControlsWidget::extSettings()
756 {
757     THEDP->extendedDialog();
758 }
759
760 void ControlsWidget::slower()
761 {
762     THEMIM->getIM()->slower();
763 }
764
765 void ControlsWidget::faster()
766 {
767     THEMIM->getIM()->faster();
768 }
769
770 void ControlsWidget::enableInput( bool enable )
771 {
772     slowerButton->setEnabled( enable );
773     slider->setEnabled( enable );
774     fasterButton->setEnabled( enable );
775
776     /* Advanced Buttons too */
777     advControls->enableInput( enable );
778 }
779
780 void ControlsWidget::enableVideo( bool enable )
781 {
782     // TODO Later make the fullscreenButton toggle Visualisation and so on.
783     fullscreenButton->setEnabled( enable );
784
785     /* Advanced Buttons too */
786     advControls->enableVideo( enable );
787 }
788
789 void ControlsWidget::toggleAdvanced()
790 {
791     if( !VISIBLE( advControls ) )
792     {
793         advControls->show();
794         b_advancedVisible = true;
795     }
796     else
797     {
798         advControls->hide();
799         b_advancedVisible = false;
800     }
801     emit advancedControlsToggled( b_advancedVisible );
802 }
803
804
805 /**********************************************************************
806  * Speed control widget
807  **********************************************************************/
808 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
809                              QFrame( NULL ), p_intf( _p_i )
810 {
811     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
812     sizePolicy.setHorizontalStretch( 0 );
813     sizePolicy.setVerticalStretch( 0 );
814
815     speedSlider = new QSlider;
816     speedSlider->setSizePolicy( sizePolicy );
817     speedSlider->setMaximumSize( QSize( 80, 200 ) );
818     speedSlider->setOrientation( Qt::Vertical );
819     speedSlider->setTickPosition( QSlider::TicksRight );
820
821     speedSlider->setRange( -100, 100 );
822     speedSlider->setSingleStep( 10 );
823     speedSlider->setPageStep( 20 );
824     speedSlider->setTickInterval( 20 );
825
826     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
827
828     QToolButton *normalSpeedButton = new QToolButton( this );
829     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
830     normalSpeedButton->setAutoRaise( true );
831     normalSpeedButton->setText( "N" );
832     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
833
834     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
835
836     QVBoxLayout *speedControlLayout = new QVBoxLayout;
837     speedControlLayout->addWidget( speedSlider );
838     speedControlLayout->addWidget( normalSpeedButton );
839     setLayout( speedControlLayout );
840 }
841
842 SpeedControlWidget::~SpeedControlWidget()
843 {}
844
845 void SpeedControlWidget::setEnable( bool b_enable )
846 {
847     speedSlider->setEnabled( b_enable );
848 }
849
850 #define RATE_SLIDER_MAXIMUM 3.0
851 #define RATE_SLIDER_MINIMUM 0.3
852 #define RATE_SLIDER_LENGTH 100.0
853
854 void SpeedControlWidget::updateControls( int rate )
855 {
856     if( speedSlider->isSliderDown() )
857     {
858         //We don't want to change anything if the user is using the slider
859         return;
860     }
861
862     int sliderValue;
863     double speed = INPUT_RATE_DEFAULT / (double)rate;
864
865     if( rate >= INPUT_RATE_DEFAULT )
866     {
867         if( speed < RATE_SLIDER_MINIMUM )
868         {
869             sliderValue = speedSlider->minimum();
870         }
871         else
872         {
873             sliderValue = (int)( ( speed - 1.0 ) * RATE_SLIDER_LENGTH
874                                         / ( 1.0 - RATE_SLIDER_MAXIMUM ) );
875         }
876     }
877     else
878     {
879         if( speed > RATE_SLIDER_MAXIMUM )
880         {
881             sliderValue = speedSlider->maximum();
882         }
883         else
884         {
885             sliderValue = (int)( ( speed - 1.0 ) * RATE_SLIDER_LENGTH
886                                         / ( RATE_SLIDER_MAXIMUM - 1.0 ) );
887         }
888     }
889
890     //Block signals to avoid feedback loop
891     speedSlider->blockSignals( true );
892     speedSlider->setValue( sliderValue );
893     speedSlider->blockSignals( false );
894 }
895
896 void SpeedControlWidget::updateRate( int sliderValue )
897 {
898     int rate;
899
900     if( sliderValue < 0.0 )
901     {
902         rate = (int)(INPUT_RATE_DEFAULT* RATE_SLIDER_LENGTH /
903             ( sliderValue * ( 1.0 - RATE_SLIDER_MINIMUM ) + RATE_SLIDER_LENGTH ));
904     }
905     else
906     {
907         rate = (int)(INPUT_RATE_DEFAULT* RATE_SLIDER_LENGTH /
908             ( sliderValue * ( RATE_SLIDER_MAXIMUM - 1.0 ) + RATE_SLIDER_LENGTH ));
909     }
910
911     THEMIM->getIM()->setRate(rate);
912 }
913
914 void SpeedControlWidget::resetRate()
915 {
916     THEMIM->getIM()->setRate(INPUT_RATE_DEFAULT);
917 }