]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Qt4 : Fix the playlistButton Size.
[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     /** Playlist Button **/
381     playlistButton = new QPushButton;
382     playlistButton->setMaximumSize( QSize( 26, 26 ) );
383
384     controlLayout->addWidget( playlistButton, 3, 10 );
385
386     /** Fullscreen/Visualisation **/
387     QPushButton *fullscreenButton = new QPushButton( "F" );
388     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
389     fullscreenButton->setMaximumSize( QSize( 26, 26 ) );
390     controlLayout->addWidget( fullscreenButton, 3, 11 );
391
392     /** extended Settings **/
393     QPushButton *extSettingsButton = new QPushButton( "F" );
394     BUTTON_SET_ACT( extSettingsButton, "Ex", qtr( "Extended Settings" ),
395             extSettings() );
396     extSettingsButton->setMaximumSize( QSize( 26, 26 ) );
397     controlLayout->addWidget( extSettingsButton, 3, 12 );
398
399     /** Preferences **/
400     QPushButton *prefsButton = new QPushButton( "P" );
401     BUTTON_SET_ACT( prefsButton, "P", qtr( "Preferences / Settings" ), prefs() );
402     prefsButton->setMaximumSize( QSize( 26, 26 ) );
403     controlLayout->addWidget( prefsButton, 3, 13 );
404
405     /* Volume */
406     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
407
408     QLabel *volMuteLabel = new QLabel;
409     volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
410     volMuteLabel->setToolTip( qtr( "Mute" ) );
411     volMuteLabel->installEventFilter( h );
412
413     volumeSlider = new QSlider;
414     volumeSlider->setSizePolicy( sizePolicy );
415     volumeSlider->setMaximumSize( QSize( 80, 200 ) );
416     volumeSlider->setOrientation( Qt::Horizontal );
417
418     volumeSlider->setMaximum( 100 );
419     volumeSlider->setFocusPolicy( Qt::NoFocus );
420     controlLayout->addWidget( volMuteLabel, 3, 14 );
421     controlLayout->addWidget( volumeSlider, 3, 15, 1, 2 );
422
423     /* Volume control connection */
424     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
425
426 }
427 ControlsWidget::~ControlsWidget()
428 {
429 }
430 void ControlsWidget::stop()
431 {
432     THEMIM->stop();
433 }
434
435 void ControlsWidget::play()
436 {
437     if( playlist_IsEmpty( THEPL ) )
438     {
439         /* The playlist is empty, open a file requester */
440         THEDP->openFileDialog();
441         setStatus( 0 );
442         return;
443     }
444     THEMIM->togglePlayPause();
445 }
446
447 void ControlsWidget::prev()
448 {
449     THEMIM->prev();
450 }
451
452 void ControlsWidget::next()
453 {
454     THEMIM->next();
455 }
456
457 void ControlsWidget::setNavigation( int navigation )
458 {
459 #define HELP_MENU N_( "Menu" )
460 #define HELP_PCH N_( "Previous chapter" )
461 #define HELP_NCH N_( "Next chapter" )
462 #define HELP_PTR N_( "Previous track" )
463 #define HELP_NTR N_( "Next track" )
464
465     // 1 = chapter, 2 = title, 0 = no
466     if( navigation == 0 )
467     {
468         discFrame->hide();
469     } else if( navigation == 1 ) {
470         prevSectionButton->show();
471         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
472         nextSectionButton->show();
473         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
474         menuButton->show();
475         discFrame->show();
476     } else {
477         prevSectionButton->show();
478         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
479         nextSectionButton->show();
480         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
481         menuButton->hide();
482         discFrame->show();
483     }
484 }
485
486 static bool b_my_volume;
487 void ControlsWidget::updateVolume( int sliderVolume )
488 {
489     if( !b_my_volume )
490     {
491         int i_res = sliderVolume * AOUT_VOLUME_MAX /
492                             ( 2*volumeSlider->maximum() );
493         aout_VolumeSet( p_intf, i_res );
494     }
495 }
496
497 void ControlsWidget::updateOnTimer()
498 {
499     audio_volume_t i_volume;
500     aout_VolumeGet( p_intf, &i_volume );
501     i_volume = ( i_volume *  200 )/ AOUT_VOLUME_MAX ;
502     int i_gauge = volumeSlider->value();
503     b_my_volume = false;
504     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
505     {
506         b_my_volume = true;
507         volumeSlider->setValue( i_volume );
508         b_my_volume = false;
509     }
510 }
511
512 /* FIXME */
513 void ControlsWidget::setStatus( int status )
514 {
515     if( status == 1 ) // Playing
516     {
517         msg_Dbg( p_intf, "I was here %i", status );
518         // playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
519     }
520     else
521     {
522         msg_Dbg( p_intf, "I was here %i", status );
523         // playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
524     }
525 }
526
527 /*
528  * This functions toggle the fullscreen mode
529  * If there is no video, it should first activate Visualisations... TODO
530  */
531 void ControlsWidget::fullscreen()
532 {
533     msg_Dbg( p_intf, "Not implemented yet" );
534 }
535
536 void ControlsWidget::extSettings()
537 {
538     THEDP->extendedDialog();
539 }
540 void ControlsWidget::prefs()
541 {
542     THEDP->prefsDialog();
543 }
544
545 void ControlsWidget::slower()
546 {
547     THEMIM->getIM()->slower();
548 }
549
550 void ControlsWidget::faster()
551 {
552     THEMIM->getIM()->faster();
553 }
554
555 void ControlsWidget::enableInput( bool enable )
556 {
557     slowerButton->setEnabled( enable );
558     slider->setEnabled( enable );
559     fasterButton->setEnabled( enable );
560 }
561
562 /**********************************************************************
563  * Playlist Widget. The embedded playlist
564  **********************************************************************/
565 #include "components/playlist/panels.hpp"
566 #include "components/playlist/selector.hpp"
567
568 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_intf ) :
569                                 p_intf ( _p_intf )
570 {
571     /* Left Part and design */
572     QWidget *leftW = new QWidget( this );
573     QVBoxLayout *left = new QVBoxLayout( leftW );
574
575     /* Source Selector */
576     selector = new PLSelector( this, p_intf, THEPL );
577     left->addWidget( selector );
578
579     /* Art label */
580     art = new QLabel( "" );
581     art->setMinimumHeight( 128 );
582     art->setMinimumWidth( 128 );
583     art->setMaximumHeight( 128 );
584     art->setMaximumWidth( 128 );
585     art->setScaledContents( true );
586     art->setPixmap( QPixmap( ":/noart.png" ) );
587     left->addWidget( art );
588
589     /* Initialisation of the playlist */
590     playlist_item_t *p_root = playlist_GetPreferredNode( THEPL,
591                                                 THEPL->p_local_category );
592
593     rightPanel = qobject_cast<PLPanel *>( new StandardPLPanel( this,
594                               p_intf, THEPL, p_root ) );
595
596     /* Connects */
597     CONNECT( selector, activated( int ), rightPanel, setRoot( int ) );
598
599     CONNECT( qobject_cast<StandardPLPanel *>( rightPanel )->model,
600              artSet( QString ) , this, setArt( QString ) );
601     /* Forward removal requests from the selector to the main panel */
602     CONNECT( qobject_cast<PLSelector *>( selector )->model,
603              shouldRemove( int ),
604              qobject_cast<StandardPLPanel *>( rightPanel ), removeItem( int ) );
605
606     connect( selector, SIGNAL( activated( int ) ),
607              this, SIGNAL( rootChanged( int ) ) );
608     emit rootChanged( p_root->i_id );
609
610     /* Add the two sides of the QSplitter */
611     addWidget( leftW );
612     addWidget( rightPanel );
613
614     leftW->setMaximumWidth( 250 );
615     setCollapsible( 1, false );
616
617     QList<int> sizeList;
618     sizeList << 180 << 520 ;
619     setSizes( sizeList );
620 }
621
622 void PlaylistWidget::setArt( QString url )
623 {
624     if( url.isNull() )
625         art->setPixmap( QPixmap( ":/noart.png" ) );
626     else if( prevArt != url )
627         art->setPixmap( QPixmap( url ) );
628     prevArt = url;
629     emit artSet( url );
630 }
631
632 PlaylistWidget::~PlaylistWidget()
633 {
634 }
635
636 QSize PlaylistWidget::sizeHint() const
637 {
638     return widgetSize;
639 }
640