]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Qt4 : buttons size and various fixes. Remove dead code and avoid duplication.
[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
230 AdvControlsWidget::~AdvControlsWidget()
231 {
232 }
233
234 void AdvControlsWidget::enableInput( bool enable )
235 {
236 //    slowerButton->setEnabled( enable );
237     normalButton->setEnabled( enable );
238 //    fasterButton->setEnabled( enable );
239 }
240 void AdvControlsWidget::enableVideo( bool enable )
241 {
242     snapshotButton->setEnabled( enable );
243     //fullscreenButton->setEnabled( enable );
244 }
245
246 void AdvControlsWidget::normal()
247 {
248     THEMIM->getIM()->normalRate();
249 }
250
251 void AdvControlsWidget::snapshot()
252 {
253 }
254
255 void AdvControlsWidget::fullscreen()
256 {
257 }
258
259 ControlsWidget::ControlsWidget( intf_thread_t *_p_i ) :
260                              QFrame( NULL ), p_intf( _p_i )
261 {
262     //QSize size( 500, 200 );
263     //resize( size );
264     controlLayout = new QGridLayout( this );
265
266     /** The main Slider **/
267     slider = new InputSlider( Qt::Horizontal, NULL );
268     controlLayout->addWidget( slider, 0, 1, 1, 15 );
269     /* Update the position when the IM has changed */
270     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
271              slider, setPosition( float,int, int ) );
272     /* And update the IM, when the position has changed */
273     CONNECT( slider, sliderDragged( float ),
274              THEMIM->getIM(), sliderUpdate( float ) );
275
276     /** Slower and faster Buttons **/
277     slowerButton = new QPushButton( "S" );
278     BUTTON_SET_ACT( slowerButton, "S", qtr( "Slower" ), slower() );
279     controlLayout->addWidget( slowerButton, 0, 0 );
280     slowerButton->setMaximumSize( QSize( 26, 20 ) );
281
282     fasterButton = new QPushButton( "F" );
283     BUTTON_SET_ACT( fasterButton, "F", qtr( "Faster" ), faster() );
284     controlLayout->addWidget( fasterButton, 0, 16 );
285     fasterButton->setMaximumSize( QSize( 26, 20 ) );
286
287     /** Disc and Menus handling */
288     discFrame = new QFrame( this );
289     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
290
291     QPushButton *menuButton = new QPushButton( discFrame );
292     discLayout->addWidget( menuButton );
293
294     QPushButton *prevSectionButton = new QPushButton( discFrame );
295     discLayout->addWidget( prevSectionButton );
296
297     QPushButton *nextSectionButton = new QPushButton( discFrame );
298     discLayout->addWidget( nextSectionButton );
299
300     controlLayout->addWidget( discFrame, 1, 13, 1, 4 );
301
302     BUTTON_SET_IMG( prevSectionButton, "", previous.png, "" );
303     BUTTON_SET_IMG( nextSectionButton, "", next.png, "" );
304     BUTTON_SET_IMG( menuButton, "", previous.png, "" );
305
306     discFrame->hide();
307
308     /* Change the navigation button display when the IM navigation changes */
309     CONNECT( THEMIM->getIM(), navigationChanged( int ),
310              this, setNavigation( int ) );
311     /* Changes the IM navigation when triggered on the nav buttons */
312     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
313              sectionPrev() );
314     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
315              sectionNext() );
316     CONNECT( menuButton, clicked(), THEMIM->getIM(),
317              sectionMenu() );
318
319     /** Play Buttons **/
320     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
321     sizePolicy.setHorizontalStretch( 0 );
322     sizePolicy.setVerticalStretch( 0 );
323 //  sizePolicy.setHeightForWidth( playButton->sizePolicy().hasHeightForWidth() );
324
325     /* Play */
326     QPushButton *playButton = new QPushButton;
327     playButton->setSizePolicy( sizePolicy );
328     playButton->setMaximumSize( QSize( 45, 45 ) );
329     playButton->setIconSize( QSize( 30, 30 ) );
330
331     controlLayout->addWidget( playButton, 2, 0, 2, 2 );
332
333     /** Prev + Stop + Next Block **/
334     QHBoxLayout *controlButLayout = new QHBoxLayout;
335     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
336
337     /* Prev */
338     QPushButton *prevButton = new QPushButton;
339     prevButton->setSizePolicy( sizePolicy );
340     prevButton->setMaximumSize( QSize( 26, 26 ) );
341     prevButton->setIconSize( QSize( 20, 20 ) );
342
343     controlButLayout->addWidget( prevButton );
344
345     /* Stop */
346     QPushButton *stopButton = new QPushButton;
347     stopButton->setSizePolicy( sizePolicy );
348     stopButton->setMaximumSize( QSize( 26, 26 ) );
349     stopButton->setIconSize( QSize( 20, 20 ) );
350
351     controlButLayout->addWidget( stopButton );
352
353     /* next */
354     QPushButton *nextButton = new QPushButton;
355     nextButton->setSizePolicy( sizePolicy );
356     nextButton->setMaximumSize( QSize( 26, 26 ) );
357     nextButton->setIconSize( QSize( 20, 20 ) );
358
359     controlButLayout->addWidget( nextButton );
360
361     /* Add this block to the main layout */
362     controlLayout->addLayout( controlButLayout, 3, 3, 1, 3 );
363
364     BUTTON_SET_ACT_I( playButton, "", play.png, qtr( "Play" ), play() );
365     BUTTON_SET_ACT_I( prevButton, "" , previous.png,
366                       qtr( "Previous" ), prev() );
367     BUTTON_SET_ACT_I( nextButton, "", next.png, qtr( "Next" ), next() );
368     BUTTON_SET_ACT_I( stopButton, "", stop.png, qtr( "Stop" ), stop() );
369
370     /*
371      * Other first Line buttons
372      * Might need to be inside a frame to avoid a few resizing pb
373      * FIXME
374      */
375     /** Fullscreen/Visualisation **/
376     fullscreenButton = new QPushButton( "F" );
377     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
378     fullscreenButton->setMaximumSize( QSize( 26, 26 ) );
379     controlLayout->addWidget( fullscreenButton, 3, 10 );
380
381     /** Playlist Button **/
382     playlistButton = new QPushButton;
383     playlistButton->setMaximumSize( QSize( 26, 26 ) );
384     playlistButton->setIconSize( QSize( 20, 20 ) );
385
386     controlLayout->addWidget( playlistButton, 3, 11 );
387
388     /** extended Settings **/
389     QPushButton *extSettingsButton = new QPushButton( "F" );
390     BUTTON_SET_ACT( extSettingsButton, "Ex", qtr( "Extended Settings" ),
391             extSettings() );
392     extSettingsButton->setMaximumSize( QSize( 26, 26 ) );
393     controlLayout->addWidget( extSettingsButton, 3, 12 );
394
395     /** Preferences **/
396     QPushButton *prefsButton = new QPushButton( "P" );
397     BUTTON_SET_ACT( prefsButton, "P", qtr( "Preferences / Settings" ), prefs() );
398     prefsButton->setMaximumSize( QSize( 26, 26 ) );
399     controlLayout->addWidget( prefsButton, 3, 13 );
400
401     /* Volume */
402     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
403
404     QLabel *volMuteLabel = new QLabel;
405     volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
406     volMuteLabel->setToolTip( qtr( "Mute" ) );
407     volMuteLabel->installEventFilter( h );
408
409     volumeSlider = new QSlider;
410     volumeSlider->setSizePolicy( sizePolicy );
411     volumeSlider->setMaximumSize( QSize( 80, 200 ) );
412     volumeSlider->setOrientation( Qt::Horizontal );
413
414     volumeSlider->setMaximum( 100 );
415     volumeSlider->setFocusPolicy( Qt::NoFocus );
416     controlLayout->addWidget( volMuteLabel, 3, 14 );
417     controlLayout->addWidget( volumeSlider, 3, 15, 1, 2 );
418
419     /* Volume control connection */
420     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
421
422 }
423 ControlsWidget::~ControlsWidget()
424 {
425 }
426 void ControlsWidget::stop()
427 {
428     THEMIM->stop();
429 }
430
431 void ControlsWidget::play()
432 {
433     if( playlist_IsEmpty( THEPL ) )
434     {
435         /* The playlist is empty, open a file requester */
436         THEDP->openFileDialog();
437         setStatus( 0 );
438         return;
439     }
440     THEMIM->togglePlayPause();
441 }
442
443 void ControlsWidget::prev()
444 {
445     THEMIM->prev();
446 }
447
448 void ControlsWidget::next()
449 {
450     THEMIM->next();
451 }
452
453 void ControlsWidget::setNavigation( int navigation )
454 {
455 #define HELP_MENU N_( "Menu" )
456 #define HELP_PCH N_( "Previous chapter" )
457 #define HELP_NCH N_( "Next chapter" )
458 #define HELP_PTR N_( "Previous track" )
459 #define HELP_NTR N_( "Next track" )
460
461     // 1 = chapter, 2 = title, 0 = no
462     if( navigation == 0 )
463     {
464         discFrame->hide();
465     } else if( navigation == 1 ) {
466         prevSectionButton->show();
467         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
468         nextSectionButton->show();
469         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
470         menuButton->show();
471         discFrame->show();
472     } else {
473         prevSectionButton->show();
474         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
475         nextSectionButton->show();
476         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
477         menuButton->hide();
478         discFrame->show();
479     }
480 }
481
482 static bool b_my_volume;
483 void ControlsWidget::updateVolume( int sliderVolume )
484 {
485     if( !b_my_volume )
486     {
487         int i_res = sliderVolume * AOUT_VOLUME_MAX /
488                             ( 2*volumeSlider->maximum() );
489         aout_VolumeSet( p_intf, i_res );
490     }
491 }
492
493 void ControlsWidget::updateOnTimer()
494 {
495     audio_volume_t i_volume;
496     aout_VolumeGet( p_intf, &i_volume );
497     i_volume = ( i_volume *  200 )/ AOUT_VOLUME_MAX ;
498     int i_gauge = volumeSlider->value();
499     b_my_volume = false;
500     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
501     {
502         b_my_volume = true;
503         volumeSlider->setValue( i_volume );
504         b_my_volume = false;
505     }
506 }
507
508 /* FIXME */
509 void ControlsWidget::setStatus( int status )
510 {
511     if( status == 1 ) // Playing
512     {
513         msg_Dbg( p_intf, "I was here %i", status );
514         // playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
515     }
516     else
517     {
518         msg_Dbg( p_intf, "I was here %i", status );
519         // playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
520     }
521 }
522
523 /*
524  * This functions toggle the fullscreen mode
525  * If there is no video, it should first activate Visualisations... TODO
526  */
527 void ControlsWidget::fullscreen()
528 {
529     msg_Dbg( p_intf, "Not implemented yet" );
530 }
531
532 void ControlsWidget::extSettings()
533 {
534     THEDP->extendedDialog();
535 }
536 void ControlsWidget::prefs()
537 {
538     THEDP->prefsDialog();
539 }
540
541 void ControlsWidget::slower()
542 {
543     THEMIM->getIM()->slower();
544 }
545
546 void ControlsWidget::faster()
547 {
548     THEMIM->getIM()->faster();
549 }
550
551 void ControlsWidget::enableInput( bool enable )
552 {
553     slowerButton->setEnabled( enable );
554     slider->setEnabled( enable );
555     fasterButton->setEnabled( enable );
556 }
557
558 void ControlsWidget::enableVideo( bool enable )
559 {
560     // TODO Later make the fullscreenButton toggle Visualisation and so on.
561     fullscreenButton->setEnabled( enable );
562 }
563
564
565 /**********************************************************************
566  * Playlist Widget. The embedded playlist
567  **********************************************************************/
568 #include "components/playlist/panels.hpp"
569 #include "components/playlist/selector.hpp"
570
571 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_intf ) :
572                                 p_intf ( _p_intf )
573 {
574     /* Left Part and design */
575     QWidget *leftW = new QWidget( this );
576     QVBoxLayout *left = new QVBoxLayout( leftW );
577
578     /* Source Selector */
579     selector = new PLSelector( this, p_intf, THEPL );
580     left->addWidget( selector );
581
582     /* Art label */
583     art = new QLabel( "" );
584     art->setMinimumHeight( 128 );
585     art->setMinimumWidth( 128 );
586     art->setMaximumHeight( 128 );
587     art->setMaximumWidth( 128 );
588     art->setScaledContents( true );
589     art->setPixmap( QPixmap( ":/noart.png" ) );
590     left->addWidget( art );
591
592     /* Initialisation of the playlist */
593     playlist_item_t *p_root = playlist_GetPreferredNode( THEPL,
594                                                 THEPL->p_local_category );
595
596     rightPanel = qobject_cast<PLPanel *>( new StandardPLPanel( this,
597                               p_intf, THEPL, p_root ) );
598
599     /* Connects */
600     CONNECT( selector, activated( int ), rightPanel, setRoot( int ) );
601
602     CONNECT( qobject_cast<StandardPLPanel *>( rightPanel )->model,
603              artSet( QString ) , this, setArt( QString ) );
604     /* Forward removal requests from the selector to the main panel */
605     CONNECT( qobject_cast<PLSelector *>( selector )->model,
606              shouldRemove( int ),
607              qobject_cast<StandardPLPanel *>( rightPanel ), removeItem( int ) );
608
609     connect( selector, SIGNAL( activated( int ) ),
610              this, SIGNAL( rootChanged( int ) ) );
611     emit rootChanged( p_root->i_id );
612
613     /* Add the two sides of the QSplitter */
614     addWidget( leftW );
615     addWidget( rightPanel );
616
617     leftW->setMaximumWidth( 250 );
618     setCollapsible( 1, false );
619
620     QList<int> sizeList;
621     sizeList << 180 << 520 ;
622     setSizes( sizeList );
623 }
624
625 void PlaylistWidget::setArt( QString url )
626 {
627     if( url.isNull() )
628         art->setPixmap( QPixmap( ":/noart.png" ) );
629     else if( prevArt != url )
630         art->setPixmap( QPixmap( url ) );
631     prevArt = url;
632     emit artSet( url );
633 }
634
635 PlaylistWidget::~PlaylistWidget()
636 {
637 }
638
639 QSize PlaylistWidget::sizeHint() const
640 {
641     return widgetSize;
642 }
643