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