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