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