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