]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Qt4 - threads assertions fixes, docking playlist and size of main interfaces fixes.
[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  *          Rafaël Carré <funman@videolanorg>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * ( at your option ) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "dialogs_provider.hpp"
27 #include "components/interface_widgets.hpp"
28 #include "main_interface.hpp"
29 #include "input_manager.hpp"
30 #include "menus.hpp"
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 128
44 #define MAX_BG_SIZE 300
45
46 /**********************************************************************
47  * Video Widget. A simple frame on which video is drawn
48  * This class handles resize issues
49  **********************************************************************/
50 static void *DoRequest( intf_thread_t *, vout_thread_t *, int*,int*,
51                         unsigned int *, unsigned int * );
52 static void DoRelease( intf_thread_t *, void * );
53 static int DoControl( intf_thread_t *, void *, int, va_list );
54
55 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
56 {
57     vlc_mutex_init( p_intf, &lock );
58     p_vout = NULL;
59     CONNECT( this, askResize(), this, SetMinSize() );
60     CONNECT( this, askVideoToShow(), this, show() );
61     setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
62 }
63
64 VideoWidget::~VideoWidget()
65 {
66     vlc_mutex_lock( &lock );
67     if( p_vout )
68     {
69         if( !p_intf->psz_switch_intf )
70         {
71             if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
72                 vout_Control( p_vout, VOUT_REPARENT );
73         }
74         else
75         {
76             if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
77                 vout_Control( p_vout, VOUT_CLOSE );
78         }
79     }
80     vlc_mutex_unlock( &lock );
81     vlc_mutex_destroy( &lock );
82 }
83
84 QSize VideoWidget::sizeHint() const
85 {
86     return widgetSize;
87 }
88
89 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
90                            unsigned int *pi_width, unsigned int *pi_height )
91 {
92     emit askVideoToShow();
93     if( p_vout )
94     {
95         msg_Dbg( p_intf, "embedded video already in use" );
96         return NULL;
97     }
98     p_vout = p_nvout;
99     emit askResize();
100     return ( void* )winId();
101 }
102
103 void VideoWidget::SetMinSize()
104 {
105     setMinimumSize( 16, 16 );
106 }
107
108 void VideoWidget::release( void *p_win )
109 {
110     p_vout = NULL;
111 }
112 /**********************************************************************
113  * Background Widget. Show a simple image background. Currently,
114  * it's a static cone.
115  **********************************************************************/
116 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i ) :
117                                         QFrame( NULL ), p_intf( _p_i )
118 {
119
120     setAutoFillBackground( true );
121     plt =  palette();
122     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
123     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
124     setPalette( plt );
125
126     label = new QLabel;
127     label->setMaximumHeight( ICON_SIZE );
128     label->setMaximumWidth( ICON_SIZE );
129     label->setScaledContents( true );
130     label->setPixmap( QPixmap( ":/vlc128.png" ) );
131     backgroundLayout = new QHBoxLayout;
132     backgroundLayout->addWidget( label );
133     setLayout( backgroundLayout );
134 }
135
136 BackgroundWidget::~BackgroundWidget()
137 {
138     backgroundLayout->takeAt( 0 );
139     delete backgroundLayout;
140 }
141
142 void BackgroundWidget::setArt( QString url )
143 {
144     if( url.isNull() )
145         label->setPixmap( QPixmap( ":/vlc128.png" ) );
146     else
147         label->setPixmap( QPixmap( url ) );
148 }
149
150 QSize BackgroundWidget::sizeHint() const
151 {
152     return widgetSize;
153 }
154
155 void BackgroundWidget::resizeEvent( QResizeEvent *e )
156 {
157     if( e->size().height() < ICON_SIZE -1 )
158         label->setMaximumWidth( e->size().height() );
159     else
160         label->setMaximumWidth( ICON_SIZE );
161 }
162
163 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
164 {
165     QVLCMenu::PopupMenu( p_intf, true );
166 }
167 /**********************************************************************
168  * Visualization selector panel
169  **********************************************************************/
170 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
171                                                 QFrame( NULL ), p_intf( _p_i )
172 {
173     QHBoxLayout *layout = new QHBoxLayout( this );
174     layout->setMargin( 0 );
175     QPushButton *prevButton = new QPushButton( "Prev" );
176     QPushButton *nextButton = new QPushButton( "Next" );
177     layout->addWidget( prevButton );
178     layout->addWidget( nextButton );
179
180     layout->addItem( new QSpacerItem( 40,20,
181                               QSizePolicy::Expanding, QSizePolicy::Minimum ) );
182     layout->addWidget( new QLabel( qtr( "Current visualization:" ) ) );
183
184     current = new QLabel( qtr( "None" ) );
185     layout->addWidget( current );
186
187     BUTTONACT( prevButton, prev() );
188     BUTTONACT( nextButton, next() );
189
190     setLayout( layout );
191     setMaximumHeight( 35 );
192 }
193
194 VisualSelector::~VisualSelector()
195 {
196 }
197
198 void VisualSelector::prev()
199 {
200     char *psz_new = aout_VisualPrev( p_intf );
201     if( psz_new )
202     {
203         current->setText( qfu( psz_new ) );
204         free( psz_new );
205     }
206 }
207
208 void VisualSelector::next()
209 {
210     char *psz_new = aout_VisualNext( p_intf );
211     if( psz_new )
212     {
213         current->setText( qfu( psz_new ) );
214         free( psz_new );
215     }
216 }
217
218 /**********************************************************************
219  * TEH controls
220  **********************************************************************/
221
222 #define setupSmallButton( aButton ){  \
223     aButton->setMaximumSize( QSize( 26, 26 ) ); \
224     aButton->setMinimumSize( QSize( 26, 26 ) ); \
225     aButton->setIconSize( QSize( 20, 20 ) ); }
226
227 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i ) :
228                                            QFrame( NULL ), p_intf( _p_i )
229 {
230     QHBoxLayout *advLayout = new QHBoxLayout( this );
231     advLayout->setMargin( 0 );
232     advLayout->setSpacing( 0 );
233
234 /* FIXME A to B function */
235     ABButton = new QPushButton( "AB" );
236     ABButton->setMaximumSize( QSize( 26, 26 ) );
237     ABButton->setIconSize( QSize( 20, 20 ) );
238     advLayout->addWidget( ABButton );
239     BUTTON_SET_ACT( ABButton, "AB", qtr( "A to B" ), fromAtoB() );
240
241 //FIXME Frame by frame function
242     frameButton = new QPushButton( "Fr" );
243     frameButton->setMaximumSize( QSize( 26, 26 ) );
244     frameButton->setIconSize( QSize( 20, 20 ) );
245     advLayout->addWidget( frameButton );
246     BUTTON_SET_ACT( frameButton, "Fr", qtr( "Frame by Frame" ), frame() );
247
248 /* FIXME Record function */
249     recordButton = new QPushButton( "R" );
250     recordButton->setMaximumSize( QSize( 26, 26 ) );
251     recordButton->setIconSize( QSize( 20, 20 ) );
252     advLayout->addWidget( recordButton );
253     BUTTON_SET_ACT_I( recordButton, "", record_16px.png,
254             qtr( "Record" ), record() );
255
256     snapshotButton = new QPushButton( "S" );
257     snapshotButton->setMaximumSize( QSize( 26, 26 ) );
258     snapshotButton->setIconSize( QSize( 20, 20 ) );
259     advLayout->addWidget( snapshotButton );
260     BUTTON_SET_ACT( snapshotButton, "S", qtr( "Take a snapshot" ), snapshot() );
261
262 }
263
264 AdvControlsWidget::~AdvControlsWidget()
265 {
266 }
267
268 void AdvControlsWidget::enableInput( bool enable )
269 {
270     ABButton->setEnabled( enable );
271     recordButton->setEnabled( enable );
272 }
273 void AdvControlsWidget::enableVideo( bool enable )
274 {
275     snapshotButton->setEnabled( enable );
276     frameButton->setEnabled( enable );
277 }
278
279 void AdvControlsWidget::snapshot()
280 {
281     vout_thread_t *p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
282     if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
283 }
284
285 void AdvControlsWidget::frame(){}
286 void AdvControlsWidget::fromAtoB(){}
287 void AdvControlsWidget::record(){}
288
289 /*****************************
290  * DA Control Widget !
291  *****************************/
292 ControlsWidget::ControlsWidget( intf_thread_t *_p_i, bool b_advControls ) :
293                              QFrame( NULL ), p_intf( _p_i )
294 {
295     controlLayout = new QGridLayout( this );
296     controlLayout->setSpacing( 0 );
297     setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
298
299     /** The main Slider **/
300     slider = new InputSlider( Qt::Horizontal, NULL );
301     controlLayout->addWidget( slider, 0, 1, 1, 16 );
302     /* Update the position when the IM has changed */
303     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
304              slider, setPosition( float,int, int ) );
305     /* And update the IM, when the position has changed */
306     CONNECT( slider, sliderDragged( float ),
307              THEMIM->getIM(), sliderUpdate( float ) );
308
309     /** Slower and faster Buttons **/
310     slowerButton = new QPushButton;
311     slowerButton->setFlat( true );
312     slowerButton->setMaximumSize( QSize( 26, 20 ) );
313
314     BUTTON_SET_ACT( slowerButton, "-", qtr( "Slower" ), slower() );
315     controlLayout->addWidget( slowerButton, 0, 0 );
316
317     fasterButton = new QPushButton;
318     fasterButton->setFlat( true );
319     fasterButton->setMaximumSize( QSize( 26, 20 ) );
320
321     BUTTON_SET_ACT( fasterButton, "+", qtr( "Faster" ), faster() );
322     controlLayout->addWidget( fasterButton, 0, 17 );
323
324     /* advanced Controls handling */
325     b_advancedVisible = b_advControls;
326
327     advControls = new AdvControlsWidget( p_intf );
328     controlLayout->addWidget( advControls, 1, 3, 2, 4, Qt::AlignBottom );
329     if( !b_advancedVisible ) advControls->hide();
330
331     /** Disc and Menus handling */
332     discFrame = new QFrame( this );
333
334     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
335     discLayout->setSpacing( 0 );
336     discLayout->setMargin( 0 );
337
338     prevSectionButton = new QPushButton( discFrame );
339     setupSmallButton( prevSectionButton );
340     discLayout->addWidget( prevSectionButton );
341
342     menuButton = new QPushButton( discFrame );
343     setupSmallButton( menuButton );
344     discLayout->addWidget( menuButton );
345
346     nextSectionButton = new QPushButton( discFrame );
347     setupSmallButton( nextSectionButton );
348     discLayout->addWidget( nextSectionButton );
349
350     controlLayout->addWidget( discFrame, 1, 10, 2, 3, Qt::AlignBottom );
351
352     BUTTON_SET_IMG( prevSectionButton, "", previous.png, "" );
353     BUTTON_SET_IMG( nextSectionButton, "", next.png, "" );
354     BUTTON_SET_IMG( menuButton, "", previous.png, "" );
355
356     discFrame->hide();
357
358     /* Change the navigation button display when the IM navigation changes */
359     CONNECT( THEMIM->getIM(), navigationChanged( int ),
360              this, setNavigation( int ) );
361     /* Changes the IM navigation when triggered on the nav buttons */
362     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
363              sectionPrev() );
364     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
365              sectionNext() );
366     CONNECT( menuButton, clicked(), THEMIM->getIM(),
367              sectionMenu() );
368
369     /** TODO
370      * Telextext QFrame
371      * Merge with upper menu in a StackLayout
372      **/
373
374     /** Play Buttons **/
375     QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
376     sizePolicy.setHorizontalStretch( 0 );
377     sizePolicy.setVerticalStretch( 0 );
378
379     /* Play */
380     playButton = new QPushButton;
381     playButton->setSizePolicy( sizePolicy );
382     playButton->setMaximumSize( QSize( 38, 38 ) );
383     playButton->setMinimumSize( QSize( 45, 45 ) );
384     playButton->setIconSize( QSize( 30, 30 ) );
385
386     controlLayout->addWidget( playButton, 2, 0, 2, 2 );
387
388     controlLayout->setColumnMinimumWidth( 2, 20 );
389     controlLayout->setColumnStretch( 2, 0 );
390
391     /** Prev + Stop + Next Block **/
392     QHBoxLayout *controlButLayout = new QHBoxLayout;
393     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
394
395     /* Prev */
396     QPushButton *prevButton = new QPushButton;
397     prevButton->setSizePolicy( sizePolicy );
398     setupSmallButton( prevButton );
399
400     controlButLayout->addWidget( prevButton );
401
402     /* Stop */
403     QPushButton *stopButton = new QPushButton;
404     stopButton->setSizePolicy( sizePolicy );
405     setupSmallButton( stopButton );
406
407     controlButLayout->addWidget( stopButton );
408
409     /* next */
410     QPushButton *nextButton = new QPushButton;
411     nextButton->setSizePolicy( sizePolicy );
412     setupSmallButton( nextButton );
413
414     controlButLayout->addWidget( nextButton );
415
416     /* Add this block to the main layout */
417     controlLayout->addLayout( controlButLayout, 3, 3, 1, 3 );
418
419     BUTTON_SET_ACT_I( playButton, "", play.png, qtr( "Play" ), play() );
420     BUTTON_SET_ACT_I( prevButton, "" , previous.png,
421                       qtr( "Previous" ), prev() );
422     BUTTON_SET_ACT_I( nextButton, "", next.png, qtr( "Next" ), next() );
423     BUTTON_SET_ACT_I( stopButton, "", stop.png, qtr( "Stop" ), stop() );
424
425     controlLayout->setColumnStretch( 8 , 10 );
426     controlLayout->setColumnStretch( 9, 0 );
427
428     /*
429      * Other first Line buttons
430      * Might need to be inside a frame to avoid a few resizing pb
431      * FIXME
432      */
433     /** Fullscreen/Visualisation **/
434     fullscreenButton = new QPushButton( "F" );
435     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
436     setupSmallButton( fullscreenButton );
437     controlLayout->addWidget( fullscreenButton, 3, 10 );
438
439     /** Playlist Button **/
440     playlistButton = new QPushButton;
441     setupSmallButton( playlistButton );
442     controlLayout->addWidget( playlistButton, 3, 11 );
443     BUTTON_SET_IMG( playlistButton, "" , playlist.png, qtr( "Show playlist" ) );
444
445     /** extended Settings **/
446     QPushButton *extSettingsButton = new QPushButton( "F" );
447     BUTTON_SET_ACT( extSettingsButton, "Ex", qtr( "Extended Settings" ),
448             extSettings() );
449     setupSmallButton( extSettingsButton );
450     controlLayout->addWidget( extSettingsButton, 3, 12 );
451
452     controlLayout->setColumnStretch( 14, 5 );
453
454     /* Volume */
455     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
456
457     volMuteLabel = new QLabel;
458     volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
459     volMuteLabel->setToolTip( qtr( "Mute" ) );
460     volMuteLabel->installEventFilter( h );
461
462     /** TODO:
463      * Change this slider to use a nice Amarok-like one
464      * **/
465     /** FIXME
466      *  THis percerntage thing has to be handled correctly
467      *  This has to match to the OSD
468      **/
469     volumeSlider = new QSlider;
470     volumeSlider->setSizePolicy( sizePolicy );
471     volumeSlider->setMaximumSize( QSize( 80, 200 ) );
472     volumeSlider->setOrientation( Qt::Horizontal );
473
474     volumeSlider->setMaximum( VOLUME_MAX );
475     volumeSlider->setFocusPolicy( Qt::NoFocus );
476     controlLayout->addWidget( volMuteLabel, 3, 15 );
477     controlLayout->addWidget( volumeSlider, 3, 16, 1, 2 );
478
479     /* Volume control connection */
480     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
481     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
482 }
483 ControlsWidget::~ControlsWidget()
484 {
485 }
486 void ControlsWidget::stop()
487 {
488     THEMIM->stop();
489 }
490
491 void ControlsWidget::play()
492 {
493     if( THEPL )
494         msg_Dbg( p_intf, "There is %i playlist items", THEPL->items.i_size ); /* FIXME: remove me */
495     if( playlist_IsEmpty( THEPL ) )
496     {
497         /* The playlist is empty, open a file requester */
498         THEDP->openFileDialog();
499         setStatus( 0 );
500         return;
501     }
502     THEMIM->togglePlayPause();
503 }
504
505 void ControlsWidget::prev()
506 {
507     THEMIM->prev();
508 }
509
510 void ControlsWidget::next()
511 {
512     THEMIM->next();
513 }
514
515 void ControlsWidget::setNavigation( int navigation )
516 {
517 #define HELP_MENU N_( "Menu" )
518 #define HELP_PCH N_( "Previous chapter" )
519 #define HELP_NCH N_( "Next chapter" )
520 #define HELP_PTR N_( "Previous track" )
521 #define HELP_NTR N_( "Next track" )
522
523     // 1 = chapter, 2 = title, 0 = no
524     if( navigation == 0 )
525     {
526         discFrame->hide();
527     } else if( navigation == 1 ) {
528         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
529         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
530         menuButton->show();
531         discFrame->show();
532     } else {
533         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
534         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
535         menuButton->hide();
536         discFrame->show();
537     }
538 }
539
540 static bool b_my_volume;
541 void ControlsWidget::updateVolume( int i_sliderVolume )
542 {
543     if( !b_my_volume )
544     {
545         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
546         aout_VolumeSet( p_intf, i_res );
547     }
548     if( i_sliderVolume == 0 )
549         volMuteLabel->setPixmap( QPixmap(":/pixmaps/volume-muted.png" ) );
550     else if( i_sliderVolume < VOLUME_MAX / 2 )
551         volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
552     else volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
553 }
554
555 void ControlsWidget::updateOnTimer()
556 {
557     /* Audio part */
558     audio_volume_t i_volume;
559     aout_VolumeGet( p_intf, &i_volume );
560     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2) ;
561     int i_gauge = volumeSlider->value();
562     b_my_volume = false;
563     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
564     {
565         b_my_volume = true;
566         volumeSlider->setValue( i_volume );
567         b_my_volume = false;
568     }
569
570     /* Activate the interface buttons according to the presence of the input */
571     enableInput( THEMIM->getIM()->hasInput() );
572     //enableVideo( THEMIM->getIM()->hasVideo() );
573     enableVideo( true );
574 }
575
576 void ControlsWidget::setStatus( int status )
577 {
578     if( status == PLAYING_S ) // Playing
579         playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
580     else
581         playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
582 }
583
584 /**
585  * TODO
586  * This functions toggle the fullscreen mode
587  * If there is no video, it should first activate Visualisations...
588  *  This has also to be fixed in enableVideo()
589  */
590 void ControlsWidget::fullscreen()
591 {
592     vout_thread_t *p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
593     if( p_vout)
594     {
595         var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
596         vlc_object_release( p_vout );
597     }
598 }
599
600 void ControlsWidget::extSettings()
601 {
602     THEDP->extendedDialog();
603 }
604
605 void ControlsWidget::slower()
606 {
607     THEMIM->getIM()->slower();
608 }
609
610 void ControlsWidget::faster()
611 {
612     THEMIM->getIM()->faster();
613 }
614
615 void ControlsWidget::enableInput( bool enable )
616 {
617     slowerButton->setEnabled( enable );
618     slider->setEnabled( enable );
619     fasterButton->setEnabled( enable );
620
621     /* Advanced Buttons too */
622     advControls->enableInput( enable );
623 }
624
625 void ControlsWidget::enableVideo( bool enable )
626 {
627     // TODO Later make the fullscreenButton toggle Visualisation and so on.
628     fullscreenButton->setEnabled( enable );
629
630     /* Advanced Buttons too */
631     advControls->enableVideo( enable );
632 }
633
634 void ControlsWidget::toggleAdvanced()
635 {
636     if( !VISIBLE( advControls ) )
637     {
638         advControls->show();
639         b_advancedVisible = true;
640     }
641     else
642     {
643         advControls->hide();
644         b_advancedVisible = false;
645     }
646     //FIXME connect this one :D
647     emit advancedControlsToggled( b_advancedVisible );  //  doComponentsUpdate();
648 }
649
650 /**********************************************************************
651  * Playlist Widget. The embedded playlist
652  **********************************************************************/
653 #include "components/playlist/panels.hpp"
654 #include "components/playlist/selector.hpp"
655
656 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i ) :
657                                 p_intf ( _p_i )
658 {
659     /* Left Part and design */
660     QWidget *leftW = new QWidget( this );
661     QVBoxLayout *left = new QVBoxLayout( leftW );
662
663     /* Source Selector */
664     selector = new PLSelector( this, p_intf, THEPL );
665     left->addWidget( selector );
666
667     /* Art label */
668     art = new QLabel( "" );
669     art->setMinimumHeight( 128 );
670     art->setMinimumWidth( 128 );
671     art->setMaximumHeight( 128 );
672     art->setMaximumWidth( 128 );
673     art->setScaledContents( true );
674     art->setPixmap( QPixmap( ":/noart.png" ) );
675     left->addWidget( art );
676
677     /* Initialisation of the playlist */
678     playlist_item_t *p_root = playlist_GetPreferredNode( THEPL,
679                                                 THEPL->p_local_category );
680
681     rightPanel = qobject_cast<PLPanel *>( new StandardPLPanel( this,
682                               p_intf, THEPL, p_root ) );
683
684     /* Connects */
685     CONNECT( selector, activated( int ), rightPanel, setRoot( int ) );
686
687     CONNECT( qobject_cast<StandardPLPanel *>( rightPanel )->model,
688              artSet( QString ) , this, setArt( QString ) );
689     /* Forward removal requests from the selector to the main panel */
690     CONNECT( qobject_cast<PLSelector *>( selector )->model,
691              shouldRemove( int ),
692              qobject_cast<StandardPLPanel *>( rightPanel ), removeItem( int ) );
693
694     connect( selector, SIGNAL( activated( int ) ),
695              this, SIGNAL( rootChanged( int ) ) );
696     emit rootChanged( p_root->i_id );
697
698     /* Add the two sides of the QSplitter */
699     addWidget( leftW );
700     addWidget( rightPanel );
701
702     leftW->setMaximumWidth( 250 );
703     setCollapsible( 1, false );
704
705     QList<int> sizeList;
706     sizeList << 180 << 520 ;
707     setSizes( sizeList );
708 }
709
710 void PlaylistWidget::setArt( QString url )
711 {
712     if( url.isNull() )
713         art->setPixmap( QPixmap( ":/noart.png" ) );
714     else if( prevArt != url )
715     {
716         art->setPixmap( QPixmap( url ) );
717         prevArt = url;
718         emit artSet( url );
719     }
720 }
721
722 PlaylistWidget::~PlaylistWidget()
723 {
724 }
725
726 /**********************************************************************
727  * Speed control widget
728  **********************************************************************/
729 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
730                              QFrame( NULL ), p_intf( _p_i )
731 {
732     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
733     sizePolicy.setHorizontalStretch( 0 );
734     sizePolicy.setVerticalStretch( 0 );
735
736     speedSlider = new QSlider;
737     speedSlider->setSizePolicy( sizePolicy );
738     speedSlider->setMaximumSize( QSize( 80, 200 ) );
739     speedSlider->setOrientation( Qt::Vertical );
740     speedSlider->setTickPosition( QSlider::TicksRight );
741
742     speedSlider->setRange( -100, 100 );
743     speedSlider->setSingleStep( 10 );
744     speedSlider->setPageStep( 20 );
745     speedSlider->setTickInterval( 20 );
746
747     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
748
749     normalSpeedButton = new QPushButton( "N" );
750     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
751     normalSpeedButton->setFlat( true );
752     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
753
754     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
755
756     QVBoxLayout *speedControlLayout = new QVBoxLayout;
757     speedControlLayout->addWidget(speedSlider);
758     speedControlLayout->addWidget(normalSpeedButton);
759     setLayout(speedControlLayout);
760 }
761
762 SpeedControlWidget::~SpeedControlWidget()
763 {
764 }
765
766 #define RATE_SLIDER_MAXIMUM 3.0
767 #define RATE_SLIDER_MINIMUM 0.3
768 #define RATE_SLIDER_LENGTH 100.0
769
770 void SpeedControlWidget::updateControls( int rate )
771 {
772     if( speedSlider->isSliderDown() )
773     {
774         //We don't want to change anything if the user is using the slider
775         return;
776     }
777
778     int sliderValue;
779     double speed = INPUT_RATE_DEFAULT / (double)rate;
780
781     if( rate >= INPUT_RATE_DEFAULT )
782     {
783         if( speed < RATE_SLIDER_MINIMUM )
784         {
785             sliderValue = speedSlider->minimum();
786         }
787         else
788         {
789             sliderValue = (int)( ( speed - 1.0 ) * RATE_SLIDER_LENGTH
790                                         / ( 1.0 - RATE_SLIDER_MAXIMUM ) );
791         }
792     }
793     else
794     {
795         if( speed > RATE_SLIDER_MAXIMUM )
796         {
797             sliderValue = speedSlider->maximum();
798         }
799         else
800         {
801             sliderValue = (int)( ( speed - 1.0 ) * RATE_SLIDER_LENGTH
802                                         / ( RATE_SLIDER_MAXIMUM - 1.0 ) );
803         }
804     }
805
806     //Block signals to avoid feedback loop
807     speedSlider->blockSignals( true );
808     speedSlider->setValue( sliderValue );
809     speedSlider->blockSignals( false );
810 }
811
812 void SpeedControlWidget::updateRate( int sliderValue )
813 {
814     int rate;
815
816     if( sliderValue < 0.0 )
817     {
818         rate = INPUT_RATE_DEFAULT* RATE_SLIDER_LENGTH /
819                 ( sliderValue * ( 1.0 - RATE_SLIDER_MINIMUM ) + RATE_SLIDER_LENGTH ) ;
820     }
821     else
822     {
823         rate = INPUT_RATE_DEFAULT* RATE_SLIDER_LENGTH /
824                 ( sliderValue * ( RATE_SLIDER_MAXIMUM - 1.0 ) + RATE_SLIDER_LENGTH );
825     }
826
827     THEMIM->getIM()->setRate(rate);
828 }
829
830 void SpeedControlWidget::resetRate()
831 {
832     THEMIM->getIM()->setRate(INPUT_RATE_DEFAULT);
833 }