]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Qt4 : Fix a size in main controlsWidget.
[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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * ( at your option ) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "dialogs_provider.hpp"
26 #include "qt4.hpp"
27 #include "components/interface_widgets.hpp"
28 #include "main_interface.hpp"
29 #include "input_manager.hpp"
30
31 #include "util/input_slider.hpp"
32 #include <vlc_vout.h>
33
34 #include <QLabel>
35 #include <QSpacerItem>
36 #include <QCursor>
37 #include <QPushButton>
38 #include <QHBoxLayout>
39 #include <QMenu>
40 #include <QPalette>
41 #include <QResizeEvent>
42
43 #define ICON_SIZE 300
44
45 /**********************************************************************
46  * Video Widget. A simple frame on which video is drawn
47  * This class handles resize issues
48  **********************************************************************/
49 static void *DoRequest( intf_thread_t *, vout_thread_t *, int*,int*,
50                         unsigned int *, unsigned int * );
51 static void DoRelease( intf_thread_t *, void * );
52 static int DoControl( intf_thread_t *, void *, int, va_list );
53
54 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
55 {
56     vlc_mutex_init( p_intf, &lock );
57     p_vout = NULL;
58
59     setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
60 }
61
62 VideoWidget::~VideoWidget()
63 {
64     vlc_mutex_lock( &lock );
65     if( p_vout )
66     {
67         if( !p_intf->psz_switch_intf )
68         {
69             if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
70                 vout_Control( p_vout, VOUT_REPARENT );
71         }
72         else
73         {
74             if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
75                 vout_Control( p_vout, VOUT_CLOSE );
76         }
77     }
78     vlc_mutex_unlock( &lock );
79     vlc_mutex_destroy( &lock );
80 }
81
82 QSize VideoWidget::sizeHint() const
83 {
84     return widgetSize;
85 }
86
87 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
88                            unsigned int *pi_width, unsigned int *pi_height )
89 {
90     if( p_vout )
91     {
92         msg_Dbg( p_intf, "embedded video already in use" );
93         return NULL;
94     }
95     p_vout = p_nvout;
96     setMinimumSize( 16, 16 );
97     return ( void* )winId();
98 }
99
100 void VideoWidget::release( void *p_win )
101 {
102     p_vout = NULL;
103 }
104 /**********************************************************************
105  * Background Widget. Show a simple image background. Currently,
106  * it's a static cone.
107  **********************************************************************/
108 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i ) :
109                                         QFrame( NULL ), p_intf( _p_i )
110 {
111
112     setAutoFillBackground( true );
113     plt =  palette();
114     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
115     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
116     setPalette( plt );
117
118     label = new QLabel( "" );
119     label->setMaximumHeight( ICON_SIZE );
120     label->setMaximumWidth( ICON_SIZE );
121     label->setScaledContents( true );
122     label->setPixmap( QPixmap( ":/vlc128.png" ) );
123     backgroundLayout = new QHBoxLayout;
124     backgroundLayout->addWidget( label );
125     setLayout( backgroundLayout );
126 }
127
128 BackgroundWidget::~BackgroundWidget()
129 {
130     backgroundLayout->takeAt( 0 );
131     delete backgroundLayout;
132 }
133
134 void BackgroundWidget::setArt( QString url )
135 {
136     if( url.isNull() )
137         label->setPixmap( QPixmap( ":/vlc128.png" ) );
138     else
139         label->setPixmap( QPixmap( url ) );
140 }
141
142 QSize BackgroundWidget::sizeHint() const
143 {
144     return widgetSize;
145 }
146
147 void BackgroundWidget::resizeEvent( QResizeEvent *e )
148 {
149     if( e->size().height() < ICON_SIZE -1 )
150         label->setMaximumWidth( e->size().height() );
151     else
152         label->setMaximumWidth( ICON_SIZE );
153 }
154
155 /**********************************************************************
156  * Visualization selector panel
157  **********************************************************************/
158 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
159                                                 QFrame( NULL ), p_intf( _p_i )
160 {
161     QHBoxLayout *layout = new QHBoxLayout( this );
162     layout->setMargin( 0 );
163     QPushButton *prevButton = new QPushButton( "Prev" );
164     QPushButton *nextButton = new QPushButton( "Next" );
165     layout->addWidget( prevButton );
166     layout->addWidget( nextButton );
167
168     layout->addItem( new QSpacerItem( 40,20,
169                               QSizePolicy::Expanding, QSizePolicy::Minimum ) );
170     layout->addWidget( new QLabel( qtr( "Current visualization:" ) ) );
171
172     current = new QLabel( qtr( "None" ) );
173     layout->addWidget( current );
174
175     BUTTONACT( prevButton, prev() );
176     BUTTONACT( nextButton, next() );
177
178     setLayout( layout );
179     setMaximumHeight( 35 );
180 }
181
182 VisualSelector::~VisualSelector()
183 {
184 }
185
186 void VisualSelector::prev()
187 {
188     char *psz_new = aout_VisualPrev( p_intf );
189     if( psz_new )
190     {
191         current->setText( qfu( psz_new ) );
192         free( psz_new );
193     }
194 }
195
196 void VisualSelector::next()
197 {
198     char *psz_new = aout_VisualNext( p_intf );
199     if( psz_new )
200     {
201         current->setText( qfu( psz_new ) );
202         free( psz_new );
203     }
204 }
205
206 /**********************************************************************
207  * More controls
208  **********************************************************************/
209 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i ) :
210                                            QFrame( NULL ), p_intf( _p_i )
211 {
212     QHBoxLayout *layout = new QHBoxLayout( this );
213     layout->setMargin( 0 );
214
215     normalButton = new QPushButton( "N" );
216     BUTTON_SET_ACT( normalButton, "N", qtr( "Normal rate" ), normal() );
217     layout->addWidget( normalButton );
218     normalButton->setMaximumWidth( 35 );
219
220
221     layout->addItem( new QSpacerItem( 100,20,
222                               QSizePolicy::Expanding, QSizePolicy::Minimum ) );
223
224     snapshotButton = new QPushButton( "S" );
225     BUTTON_SET_ACT( snapshotButton, "S", qtr( "Take a snapshot" ), snapshot() );
226     layout->addWidget( snapshotButton );
227     snapshotButton->setMaximumWidth( 35 );
228
229     fullscreenButton = new QPushButton( "F" );
230     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
231     layout->addWidget( fullscreenButton );
232     fullscreenButton->setMaximumWidth( 35 );
233 }
234
235 AdvControlsWidget::~AdvControlsWidget()
236 {
237 }
238
239 void AdvControlsWidget::enableInput( bool enable )
240 {
241 //    slowerButton->setEnabled( enable );
242     normalButton->setEnabled( enable );
243 //    fasterButton->setEnabled( enable );
244 }
245 void AdvControlsWidget::enableVideo( bool enable )
246 {
247     snapshotButton->setEnabled( enable );
248     fullscreenButton->setEnabled( enable );
249 }
250
251 void AdvControlsWidget::normal()
252 {
253     THEMIM->getIM()->normalRate();
254 }
255
256 void AdvControlsWidget::snapshot()
257 {
258 }
259
260 void AdvControlsWidget::fullscreen()
261 {
262 }
263
264 ControlsWidget::ControlsWidget( intf_thread_t *_p_i ) :
265                              QFrame( NULL ), p_intf( _p_i )
266 {
267     //QSize size( 500, 200 );
268     //resize( size );
269     controlLayout = new QGridLayout( this );
270
271     /** The main Slider **/
272     slider = new InputSlider( Qt::Horizontal, NULL );
273     controlLayout->addWidget( slider, 0, 1, 1, 15 );
274     /* Update the position when the IM has changed */
275     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
276              slider, setPosition( float,int, int ) );
277     /* And update the IM, when the position has changed */
278     CONNECT( slider, sliderDragged( float ),
279              THEMIM->getIM(), sliderUpdate( float ) );
280
281     /** Slower and faster Buttons **/
282     slowerButton = new QPushButton( "S" );
283     BUTTON_SET_ACT( slowerButton, "S", qtr( "Slower" ), slower() );
284     controlLayout->addWidget( slowerButton, 0, 0 );
285     slowerButton->setMaximumSize( QSize( 26, 26 ) );
286
287     fasterButton = new QPushButton( "F" );
288     BUTTON_SET_ACT( fasterButton, "F", qtr( "Faster" ), faster() );
289     controlLayout->addWidget( fasterButton, 0, 16 );
290     fasterButton->setMaximumSize( QSize( 26, 26 ) );
291
292     /** Disc and Menus handling */
293     discFrame = new QFrame( this );
294     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
295
296     QPushButton *menuButton = new QPushButton( discFrame );
297     discLayout->addWidget( menuButton );
298
299     QPushButton *prevSectionButton = new QPushButton( discFrame );
300     discLayout->addWidget( prevSectionButton );
301
302     QPushButton *nextSectionButton = new QPushButton( discFrame );
303     discLayout->addWidget( nextSectionButton );
304
305     controlLayout->addWidget( discFrame, 1, 13, 1, 4 );
306
307     BUTTON_SET_IMG( prevSectionButton, "", previous.png, "" );
308     BUTTON_SET_IMG( nextSectionButton, "", next.png, "" );
309     BUTTON_SET_IMG( menuButton, "", previous.png, "" );
310
311     discFrame->hide();
312
313     /* Change the navigation button display when the IM navigation changes */
314     CONNECT( THEMIM->getIM(), navigationChanged( int ),
315              this, setNavigation( int ) );
316     /* Changes the IM navigation when triggered on the nav buttons */
317     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
318              sectionPrev() );
319     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
320              sectionNext() );
321     CONNECT( menuButton, clicked(), THEMIM->getIM(),
322              sectionMenu() );
323
324     /** Play Buttons **/
325     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
326     sizePolicy.setHorizontalStretch( 0 );
327     sizePolicy.setVerticalStretch( 0 );
328 //  sizePolicy.setHeightForWidth( playButton->sizePolicy().hasHeightForWidth() );
329
330     /* Play */
331     QPushButton *playButton = new QPushButton;
332     playButton->setSizePolicy( sizePolicy );
333     playButton->setMaximumSize( QSize( 26, 26 ) );
334     playButton->setIconSize( QSize( 20, 20 ) );
335
336     controlLayout->addWidget( playButton, 2, 0, 2, 2 );
337
338     /** Prev + Stop + Next Block **/
339     QHBoxLayout *controlButLayout = new QHBoxLayout;
340     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
341
342     /* Prev */
343     QPushButton *prevButton = new QPushButton;
344     prevButton->setSizePolicy( sizePolicy );
345     prevButton->setMaximumSize( QSize( 26, 26 ) );
346     prevButton->setIconSize( QSize( 20, 20 ) );
347
348     controlButLayout->addWidget( prevButton );
349
350     /* Stop */
351     QPushButton *stopButton = new QPushButton;
352     stopButton->setSizePolicy( sizePolicy );
353     stopButton->setMaximumSize( QSize( 26, 26 ) );
354     stopButton->setIconSize( QSize( 20, 20 ) );
355
356     controlButLayout->addWidget( stopButton );
357
358     /* next */
359     QPushButton *nextButton = new QPushButton;
360     nextButton->setSizePolicy( sizePolicy );
361     nextButton->setMaximumSize( QSize( 26, 26 ) );
362     nextButton->setIconSize( QSize( 20, 20 ) );
363
364     controlButLayout->addWidget( nextButton );
365
366     /* Add this block to the main layout */
367     controlLayout->addLayout( controlButLayout, 3, 3, 1, 3 );
368
369     BUTTON_SET_ACT_I( playButton, "", play.png, qtr( "Play" ), play() );
370     BUTTON_SET_ACT_I( prevButton, "" , previous.png,
371                       qtr( "Previous" ), prev() );
372     BUTTON_SET_ACT_I( nextButton, "", next.png, qtr( "Next" ), next() );
373     BUTTON_SET_ACT_I( stopButton, "", stop.png, qtr( "Stop" ), stop() );
374
375     /*
376      * Other first Line buttons
377      * Might need to be inside a frame to avoid a few resizing pb
378      * FIXME
379      */
380     /** Fullscreen/Visualisation **/
381     fullscreenButton = new QPushButton( "F" );
382     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
383     fullscreenButton->setMaximumSize( QSize( 26, 26 ) );
384     controlLayout->addWidget( fullscreenButton, 3, 11 );
385
386     /** Playlist Button **/
387     playlistButton = new QPushButton;
388     playlistButton->setMaximumSize( QSize( 45, 45 ) );
389     playlistButton->setIconSize( QSize( 30, 30 ) );
390
391     controlLayout->addWidget( playlistButton, 3, 10 );
392
393     /** extended Settings **/
394     QPushButton *extSettingsButton = new QPushButton( "F" );
395     BUTTON_SET_ACT( extSettingsButton, "Ex", qtr( "Extended Settings" ),
396             extSettings() );
397     extSettingsButton->setMaximumSize( QSize( 26, 26 ) );
398     controlLayout->addWidget( extSettingsButton, 3, 12 );
399
400     /** Preferences **/
401     QPushButton *prefsButton = new QPushButton( "P" );
402     BUTTON_SET_ACT( prefsButton, "P", qtr( "Preferences / Settings" ), prefs() );
403     prefsButton->setMaximumSize( QSize( 26, 26 ) );
404     controlLayout->addWidget( prefsButton, 3, 13 );
405
406     /* Volume */
407     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
408
409     QLabel *volMuteLabel = new QLabel;
410     volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
411     volMuteLabel->setToolTip( qtr( "Mute" ) );
412     volMuteLabel->installEventFilter( h );
413
414     volumeSlider = new QSlider;
415     volumeSlider->setSizePolicy( sizePolicy );
416     volumeSlider->setMaximumSize( QSize( 80, 200 ) );
417     volumeSlider->setOrientation( Qt::Horizontal );
418
419     volumeSlider->setMaximum( 100 );
420     volumeSlider->setFocusPolicy( Qt::NoFocus );
421     controlLayout->addWidget( volMuteLabel, 3, 14 );
422     controlLayout->addWidget( volumeSlider, 3, 15, 1, 2 );
423
424     /* Volume control connection */
425     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
426
427 }
428 ControlsWidget::~ControlsWidget()
429 {
430 }
431 void ControlsWidget::stop()
432 {
433     THEMIM->stop();
434 }
435
436 void ControlsWidget::play()
437 {
438     if( playlist_IsEmpty( THEPL ) )
439     {
440         /* The playlist is empty, open a file requester */
441         THEDP->openFileDialog();
442         setStatus( 0 );
443         return;
444     }
445     THEMIM->togglePlayPause();
446 }
447
448 void ControlsWidget::prev()
449 {
450     THEMIM->prev();
451 }
452
453 void ControlsWidget::next()
454 {
455     THEMIM->next();
456 }
457
458 void ControlsWidget::setNavigation( int navigation )
459 {
460 #define HELP_MENU N_( "Menu" )
461 #define HELP_PCH N_( "Previous chapter" )
462 #define HELP_NCH N_( "Next chapter" )
463 #define HELP_PTR N_( "Previous track" )
464 #define HELP_NTR N_( "Next track" )
465
466     // 1 = chapter, 2 = title, 0 = no
467     if( navigation == 0 )
468     {
469         discFrame->hide();
470     } else if( navigation == 1 ) {
471         prevSectionButton->show();
472         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
473         nextSectionButton->show();
474         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
475         menuButton->show();
476         discFrame->show();
477     } else {
478         prevSectionButton->show();
479         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
480         nextSectionButton->show();
481         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
482         menuButton->hide();
483         discFrame->show();
484     }
485 }
486
487 static bool b_my_volume;
488 void ControlsWidget::updateVolume( int sliderVolume )
489 {
490     if( !b_my_volume )
491     {
492         int i_res = sliderVolume * AOUT_VOLUME_MAX /
493                             ( 2*volumeSlider->maximum() );
494         aout_VolumeSet( p_intf, i_res );
495     }
496 }
497
498 void ControlsWidget::updateOnTimer()
499 {
500     audio_volume_t i_volume;
501     aout_VolumeGet( p_intf, &i_volume );
502     i_volume = ( i_volume *  200 )/ AOUT_VOLUME_MAX ;
503     int i_gauge = volumeSlider->value();
504     b_my_volume = false;
505     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
506     {
507         b_my_volume = true;
508         volumeSlider->setValue( i_volume );
509         b_my_volume = false;
510     }
511 }
512
513 /* FIXME */
514 void ControlsWidget::setStatus( int status )
515 {
516     if( status == 1 ) // Playing
517     {
518         msg_Dbg( p_intf, "I was here %i", status );
519         // playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
520     }
521     else
522     {
523         msg_Dbg( p_intf, "I was here %i", status );
524         // playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
525     }
526 }
527
528 /*
529  * This functions toggle the fullscreen mode
530  * If there is no video, it should first activate Visualisations... TODO
531  */
532 void ControlsWidget::fullscreen()
533 {
534     msg_Dbg( p_intf, "Not implemented yet" );
535 }
536
537 void ControlsWidget::extSettings()
538 {
539     THEDP->extendedDialog();
540 }
541 void ControlsWidget::prefs()
542 {
543     THEDP->prefsDialog();
544 }
545
546 void ControlsWidget::slower()
547 {
548     THEMIM->getIM()->slower();
549 }
550
551 void ControlsWidget::faster()
552 {
553     THEMIM->getIM()->faster();
554 }
555
556 void ControlsWidget::enableInput( bool enable )
557 {
558     slowerButton->setEnabled( enable );
559     slider->setEnabled( enable );
560     fasterButton->setEnabled( enable );
561 }
562
563 void ControlsWidget::enableVideo( bool enable )
564 {
565     // TODO Later make the fullscreenButton toggle Visualisation and so on.
566     fullscreenButton->setEnabled( enable );
567 }
568
569
570 /**********************************************************************
571  * Playlist Widget. The embedded playlist
572  **********************************************************************/
573 #include "components/playlist/panels.hpp"
574 #include "components/playlist/selector.hpp"
575
576 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_intf ) :
577                                 p_intf ( _p_intf )
578 {
579     /* Left Part and design */
580     QWidget *leftW = new QWidget( this );
581     QVBoxLayout *left = new QVBoxLayout( leftW );
582
583     /* Source Selector */
584     selector = new PLSelector( this, p_intf, THEPL );
585     left->addWidget( selector );
586
587     /* Art label */
588     art = new QLabel( "" );
589     art->setMinimumHeight( 128 );
590     art->setMinimumWidth( 128 );
591     art->setMaximumHeight( 128 );
592     art->setMaximumWidth( 128 );
593     art->setScaledContents( true );
594     art->setPixmap( QPixmap( ":/noart.png" ) );
595     left->addWidget( art );
596
597     /* Initialisation of the playlist */
598     playlist_item_t *p_root = playlist_GetPreferredNode( THEPL,
599                                                 THEPL->p_local_category );
600
601     rightPanel = qobject_cast<PLPanel *>( new StandardPLPanel( this,
602                               p_intf, THEPL, p_root ) );
603
604     /* Connects */
605     CONNECT( selector, activated( int ), rightPanel, setRoot( int ) );
606
607     CONNECT( qobject_cast<StandardPLPanel *>( rightPanel )->model,
608              artSet( QString ) , this, setArt( QString ) );
609     /* Forward removal requests from the selector to the main panel */
610     CONNECT( qobject_cast<PLSelector *>( selector )->model,
611              shouldRemove( int ),
612              qobject_cast<StandardPLPanel *>( rightPanel ), removeItem( int ) );
613
614     connect( selector, SIGNAL( activated( int ) ),
615              this, SIGNAL( rootChanged( int ) ) );
616     emit rootChanged( p_root->i_id );
617
618     /* Add the two sides of the QSplitter */
619     addWidget( leftW );
620     addWidget( rightPanel );
621
622     leftW->setMaximumWidth( 250 );
623     setCollapsible( 1, false );
624
625     QList<int> sizeList;
626     sizeList << 180 << 520 ;
627     setSizes( sizeList );
628 }
629
630 void PlaylistWidget::setArt( QString url )
631 {
632     if( url.isNull() )
633         art->setPixmap( QPixmap( ":/noart.png" ) );
634     else if( prevArt != url )
635         art->setPixmap( QPixmap( url ) );
636     prevArt = url;
637     emit artSet( url );
638 }
639
640 PlaylistWidget::~PlaylistWidget()
641 {
642 }
643
644 QSize PlaylistWidget::sizeHint() const
645 {
646     return widgetSize;
647 }
648