]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
qt4: uses setMinimumSize from the good thread using signal/slot
[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     CONNECT( this, askResize(), this, SetMinSize() );
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     emit askResize();
97     return ( void* )winId();
98 }
99
100 void VideoWidget::SetMinSize()
101 {
102     setMinimumSize( 16, 16 );
103 }
104
105 void VideoWidget::release( void *p_win )
106 {
107     p_vout = NULL;
108 }
109 /**********************************************************************
110  * Background Widget. Show a simple image background. Currently,
111  * it's a static cone.
112  **********************************************************************/
113 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i ) :
114                                         QFrame( NULL ), p_intf( _p_i )
115 {
116
117     setAutoFillBackground( true );
118     plt =  palette();
119     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
120     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
121     setPalette( plt );
122
123     label = new QLabel( "" );
124     label->setMaximumHeight( ICON_SIZE );
125     label->setMaximumWidth( ICON_SIZE );
126     label->setScaledContents( true );
127     label->setPixmap( QPixmap( ":/vlc128.png" ) );
128     backgroundLayout = new QHBoxLayout;
129     backgroundLayout->addWidget( label );
130     setLayout( backgroundLayout );
131 }
132
133 BackgroundWidget::~BackgroundWidget()
134 {
135     backgroundLayout->takeAt( 0 );
136     delete backgroundLayout;
137 }
138
139 void BackgroundWidget::setArt( QString url )
140 {
141     if( url.isNull() )
142         label->setPixmap( QPixmap( ":/vlc128.png" ) );
143     else
144         label->setPixmap( QPixmap( url ) );
145 }
146
147 QSize BackgroundWidget::sizeHint() const
148 {
149     return widgetSize;
150 }
151
152 void BackgroundWidget::resizeEvent( QResizeEvent *e )
153 {
154     if( e->size().height() < ICON_SIZE -1 )
155         label->setMaximumWidth( e->size().height() );
156     else
157         label->setMaximumWidth( ICON_SIZE );
158 }
159
160 /**********************************************************************
161  * Visualization selector panel
162  **********************************************************************/
163 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
164                                                 QFrame( NULL ), p_intf( _p_i )
165 {
166     QHBoxLayout *layout = new QHBoxLayout( this );
167     layout->setMargin( 0 );
168     QPushButton *prevButton = new QPushButton( "Prev" );
169     QPushButton *nextButton = new QPushButton( "Next" );
170     layout->addWidget( prevButton );
171     layout->addWidget( nextButton );
172
173     layout->addItem( new QSpacerItem( 40,20,
174                               QSizePolicy::Expanding, QSizePolicy::Minimum ) );
175     layout->addWidget( new QLabel( qtr( "Current visualization:" ) ) );
176
177     current = new QLabel( qtr( "None" ) );
178     layout->addWidget( current );
179
180     BUTTONACT( prevButton, prev() );
181     BUTTONACT( nextButton, next() );
182
183     setLayout( layout );
184     setMaximumHeight( 35 );
185 }
186
187 VisualSelector::~VisualSelector()
188 {
189 }
190
191 void VisualSelector::prev()
192 {
193     char *psz_new = aout_VisualPrev( p_intf );
194     if( psz_new )
195     {
196         current->setText( qfu( psz_new ) );
197         free( psz_new );
198     }
199 }
200
201 void VisualSelector::next()
202 {
203     char *psz_new = aout_VisualNext( p_intf );
204     if( psz_new )
205     {
206         current->setText( qfu( psz_new ) );
207         free( psz_new );
208     }
209 }
210
211 /**********************************************************************
212  * More controls
213  **********************************************************************/
214 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i ) :
215                                            QFrame( NULL ), p_intf( _p_i )
216 {
217     QHBoxLayout *layout = new QHBoxLayout( this );
218     layout->setMargin( 0 );
219
220     normalButton = new QPushButton( "N" );
221     BUTTON_SET_ACT( normalButton, "N", qtr( "Normal rate" ), normal() );
222     layout->addWidget( normalButton );
223     normalButton->setMaximumWidth( 35 );
224
225
226     layout->addItem( new QSpacerItem( 100,20,
227                               QSizePolicy::Expanding, QSizePolicy::Minimum ) );
228
229     snapshotButton = new QPushButton( "S" );
230     BUTTON_SET_ACT( snapshotButton, "S", qtr( "Take a snapshot" ), snapshot() );
231     layout->addWidget( snapshotButton );
232     snapshotButton->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, 20 ) );
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, 20 ) );
291
292     /** TODO: Insert here the AdvControls Widget 
293      * and add - A->B button
294      *         - frame by frame
295      *         - record button
296      * and put the snapshot in the same QFrame 
297      * Then fix all the size issues in main_interface.cpp
298      **/
299     
300     /** Disc and Menus handling */
301     discFrame = new QFrame( this );
302     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
303
304     QPushButton *menuButton = new QPushButton( discFrame );
305     discLayout->addWidget( menuButton );
306
307     QPushButton *prevSectionButton = new QPushButton( discFrame );
308     discLayout->addWidget( prevSectionButton );
309
310     QPushButton *nextSectionButton = new QPushButton( discFrame );
311     discLayout->addWidget( nextSectionButton );
312
313     controlLayout->addWidget( discFrame, 1, 13, 1, 4 );
314
315     BUTTON_SET_IMG( prevSectionButton, "", previous.png, "" );
316     BUTTON_SET_IMG( nextSectionButton, "", next.png, "" );
317     BUTTON_SET_IMG( menuButton, "", previous.png, "" );
318
319     discFrame->hide();
320
321     /* Change the navigation button display when the IM navigation changes */
322     CONNECT( THEMIM->getIM(), navigationChanged( int ),
323              this, setNavigation( int ) );
324     /* Changes the IM navigation when triggered on the nav buttons */
325     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
326              sectionPrev() );
327     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
328              sectionNext() );
329     CONNECT( menuButton, clicked(), THEMIM->getIM(),
330              sectionMenu() );
331
332     /** TODO
333      * Telextext QFrame
334      **/
335
336     /** Play Buttons **/
337     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
338     sizePolicy.setHorizontalStretch( 0 );
339     sizePolicy.setVerticalStretch( 0 );
340 //  sizePolicy.setHeightForWidth( playButton->sizePolicy().hasHeightForWidth() );
341
342     /* Play */
343     QPushButton *playButton = new QPushButton;
344     playButton->setSizePolicy( sizePolicy );
345     playButton->setMaximumSize( QSize( 45, 45 ) );
346     playButton->setIconSize( QSize( 30, 30 ) );
347
348     controlLayout->addWidget( playButton, 2, 0, 2, 2 );
349
350     /** Prev + Stop + Next Block **/
351     QHBoxLayout *controlButLayout = new QHBoxLayout;
352     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
353
354     /* Prev */
355     QPushButton *prevButton = new QPushButton;
356     prevButton->setSizePolicy( sizePolicy );
357     prevButton->setMaximumSize( QSize( 26, 26 ) );
358     prevButton->setIconSize( QSize( 20, 20 ) );
359
360     controlButLayout->addWidget( prevButton );
361
362     /* Stop */
363     QPushButton *stopButton = new QPushButton;
364     stopButton->setSizePolicy( sizePolicy );
365     stopButton->setMaximumSize( QSize( 26, 26 ) );
366     stopButton->setIconSize( QSize( 20, 20 ) );
367
368     controlButLayout->addWidget( stopButton );
369
370     /* next */
371     QPushButton *nextButton = new QPushButton;
372     nextButton->setSizePolicy( sizePolicy );
373     nextButton->setMaximumSize( QSize( 26, 26 ) );
374     nextButton->setIconSize( QSize( 20, 20 ) );
375
376     controlButLayout->addWidget( nextButton );
377
378     /* Add this block to the main layout */
379     controlLayout->addLayout( controlButLayout, 3, 3, 1, 3 );
380
381     BUTTON_SET_ACT_I( playButton, "", play.png, qtr( "Play" ), play() );
382     BUTTON_SET_ACT_I( prevButton, "" , previous.png,
383                       qtr( "Previous" ), prev() );
384     BUTTON_SET_ACT_I( nextButton, "", next.png, qtr( "Next" ), next() );
385     BUTTON_SET_ACT_I( stopButton, "", stop.png, qtr( "Stop" ), stop() );
386
387     /*
388      * Other first Line buttons
389      * Might need to be inside a frame to avoid a few resizing pb
390      * FIXME
391      */
392     /** Fullscreen/Visualisation **/
393     fullscreenButton = new QPushButton( "F" );
394     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
395     fullscreenButton->setMaximumSize( QSize( 26, 26 ) );
396     controlLayout->addWidget( fullscreenButton, 3, 10 );
397
398     /** Playlist Button **/
399     playlistButton = new QPushButton;
400     playlistButton->setMaximumSize( QSize( 26, 26 ) );
401     playlistButton->setIconSize( QSize( 20, 20 ) );
402
403     controlLayout->addWidget( playlistButton, 3, 11 );
404
405     /** extended Settings **/
406     QPushButton *extSettingsButton = new QPushButton( "F" );
407     BUTTON_SET_ACT( extSettingsButton, "Ex", qtr( "Extended Settings" ),
408             extSettings() );
409     extSettingsButton->setMaximumSize( QSize( 26, 26 ) );
410     controlLayout->addWidget( extSettingsButton, 3, 12 );
411
412     /** Preferences **/
413     QPushButton *prefsButton = new QPushButton( "P" );
414     BUTTON_SET_ACT( prefsButton, "P", qtr( "Preferences / Settings" ), prefs() );
415     prefsButton->setMaximumSize( QSize( 26, 26 ) );
416     controlLayout->addWidget( prefsButton, 3, 13 );
417
418     /* Volume */
419     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
420
421     QLabel *volMuteLabel = new QLabel;
422     volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
423     volMuteLabel->setToolTip( qtr( "Mute" ) );
424     volMuteLabel->installEventFilter( h );
425
426     /** TODO: 
427      * Change this slider to use a nice Amarok-like one 
428      * Add a Context menu to change to the most useful %
429      * **/
430     /** FIXME
431      *  THis percerntage thing has to be handled correctly
432      *  This has to match to the OSD
433      **/
434     volumeSlider = new QSlider;
435     volumeSlider->setSizePolicy( sizePolicy );
436     volumeSlider->setMaximumSize( QSize( 80, 200 ) );
437     volumeSlider->setOrientation( Qt::Horizontal );
438
439     volumeSlider->setMaximum( 100 );
440     volumeSlider->setFocusPolicy( Qt::NoFocus );
441     controlLayout->addWidget( volMuteLabel, 3, 14 );
442     controlLayout->addWidget( volumeSlider, 3, 15, 1, 2 );
443
444     /* Volume control connection */
445     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
446
447 }
448 ControlsWidget::~ControlsWidget()
449 {
450 }
451 void ControlsWidget::stop()
452 {
453     THEMIM->stop();
454 }
455
456 void ControlsWidget::play()
457 {
458     if( playlist_IsEmpty( THEPL ) )
459     {
460         /* The playlist is empty, open a file requester */
461         THEDP->openFileDialog();
462         setStatus( 0 );
463         return;
464     }
465     THEMIM->togglePlayPause();
466 }
467
468 void ControlsWidget::prev()
469 {
470     THEMIM->prev();
471 }
472
473 void ControlsWidget::next()
474 {
475     THEMIM->next();
476 }
477
478 void ControlsWidget::setNavigation( int navigation )
479 {
480 #define HELP_MENU N_( "Menu" )
481 #define HELP_PCH N_( "Previous chapter" )
482 #define HELP_NCH N_( "Next chapter" )
483 #define HELP_PTR N_( "Previous track" )
484 #define HELP_NTR N_( "Next track" )
485
486     // 1 = chapter, 2 = title, 0 = no
487     if( navigation == 0 )
488     {
489         discFrame->hide();
490     } else if( navigation == 1 ) {
491         prevSectionButton->show();
492         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
493         nextSectionButton->show();
494         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
495         menuButton->show();
496         discFrame->show();
497     } else {
498         prevSectionButton->show();
499         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
500         nextSectionButton->show();
501         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
502         menuButton->hide();
503         discFrame->show();
504     }
505 }
506
507 static bool b_my_volume;
508 void ControlsWidget::updateVolume( int sliderVolume )
509 {
510     if( !b_my_volume )
511     {
512         int i_res = sliderVolume * AOUT_VOLUME_MAX /
513                             ( 2*volumeSlider->maximum() );
514         aout_VolumeSet( p_intf, i_res );
515     }
516 }
517
518 void ControlsWidget::updateOnTimer()
519 {
520     audio_volume_t i_volume;
521     aout_VolumeGet( p_intf, &i_volume );
522     i_volume = ( i_volume *  200 )/ AOUT_VOLUME_MAX ;
523     int i_gauge = volumeSlider->value();
524     b_my_volume = false;
525     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
526     {
527         b_my_volume = true;
528         volumeSlider->setValue( i_volume );
529         b_my_volume = false;
530     }
531 }
532
533 /* FIXME */
534 void ControlsWidget::setStatus( int status )
535 {
536     if( status == 1 ) // Playing
537     {
538         msg_Dbg( p_intf, "I was here %i", status );
539         // playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
540     }
541     else
542     {
543         msg_Dbg( p_intf, "I was here %i", status );
544         // playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
545     }
546 }
547
548 /**
549  * TODO
550  * This functions toggle the fullscreen mode
551  * If there is no video, it should first activate Visualisations... 
552  *  This has also to be fixed in enableVideo()
553  */
554 void ControlsWidget::fullscreen()
555 {
556     msg_Dbg( p_intf, "Not implemented yet" );
557 }
558
559 void ControlsWidget::extSettings()
560 {
561     THEDP->extendedDialog();
562 }
563 void ControlsWidget::prefs()
564 {
565     THEDP->prefsDialog();
566 }
567
568 void ControlsWidget::slower()
569 {
570     THEMIM->getIM()->slower();
571 }
572
573 void ControlsWidget::faster()
574 {
575     THEMIM->getIM()->faster();
576 }
577
578 void ControlsWidget::enableInput( bool enable )
579 {
580     slowerButton->setEnabled( enable );
581     slider->setEnabled( enable );
582     fasterButton->setEnabled( enable );
583 }
584
585 void ControlsWidget::enableVideo( bool enable )
586 {
587     // TODO Later make the fullscreenButton toggle Visualisation and so on.
588     fullscreenButton->setEnabled( enable );
589 }
590
591
592 /**********************************************************************
593  * Playlist Widget. The embedded playlist
594  **********************************************************************/
595 #include "components/playlist/panels.hpp"
596 #include "components/playlist/selector.hpp"
597
598 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_intf ) :
599                                 p_intf ( _p_intf )
600 {
601     /* Left Part and design */
602     QWidget *leftW = new QWidget( this );
603     QVBoxLayout *left = new QVBoxLayout( leftW );
604
605     /* Source Selector */
606     selector = new PLSelector( this, p_intf, THEPL );
607     left->addWidget( selector );
608
609     /* Art label */
610     art = new QLabel( "" );
611     art->setMinimumHeight( 128 );
612     art->setMinimumWidth( 128 );
613     art->setMaximumHeight( 128 );
614     art->setMaximumWidth( 128 );
615     art->setScaledContents( true );
616     art->setPixmap( QPixmap( ":/noart.png" ) );
617     left->addWidget( art );
618
619     /* Initialisation of the playlist */
620     playlist_item_t *p_root = playlist_GetPreferredNode( THEPL,
621                                                 THEPL->p_local_category );
622
623     rightPanel = qobject_cast<PLPanel *>( new StandardPLPanel( this,
624                               p_intf, THEPL, p_root ) );
625
626     /* Connects */
627     CONNECT( selector, activated( int ), rightPanel, setRoot( int ) );
628
629     CONNECT( qobject_cast<StandardPLPanel *>( rightPanel )->model,
630              artSet( QString ) , this, setArt( QString ) );
631     /* Forward removal requests from the selector to the main panel */
632     CONNECT( qobject_cast<PLSelector *>( selector )->model,
633              shouldRemove( int ),
634              qobject_cast<StandardPLPanel *>( rightPanel ), removeItem( int ) );
635
636     connect( selector, SIGNAL( activated( int ) ),
637              this, SIGNAL( rootChanged( int ) ) );
638     emit rootChanged( p_root->i_id );
639
640     /* Add the two sides of the QSplitter */
641     addWidget( leftW );
642     addWidget( rightPanel );
643
644     leftW->setMaximumWidth( 250 );
645     setCollapsible( 1, false );
646
647     QList<int> sizeList;
648     sizeList << 180 << 520 ;
649     setSizes( sizeList );
650 }
651
652 void PlaylistWidget::setArt( QString url )
653 {
654     if( url.isNull() )
655         art->setPixmap( QPixmap( ":/noart.png" ) );
656     else if( prevArt != url )
657         art->setPixmap( QPixmap( url ) );
658     prevArt = url;
659     emit artSet( url );
660 }
661
662 PlaylistWidget::~PlaylistWidget()
663 {
664 }
665
666 QSize PlaylistWidget::sizeHint() const
667 {
668     return widgetSize;
669 }
670