]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
UI: prefix/namespace resources for better maintainability
[vlc] / modules / gui / qt4 / components / interface_widgets.cpp
1 /*****************************************************************************
2  * interface_widgets.cpp : Custom widgets for the main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2008 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  *          Ilkka Ollakka <ileoo@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * ( at your option ) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "components/interface_widgets.hpp"
32
33 #include "menus.hpp"             /* Popup menu on bgWidget */
34
35 #include <vlc_vout.h>
36
37 #include <QLabel>
38 #include <QToolButton>
39 #include <QPalette>
40 #include <QResizeEvent>
41 #include <QDate>
42 #include <QMenu>
43 #include <QWidgetAction>
44
45 #ifdef Q_WS_X11
46 # include <X11/Xlib.h>
47 # include <qx11info_x11.h>
48 #endif
49
50 #include <math.h>
51
52 /**********************************************************************
53  * Video Widget. A simple frame on which video is drawn
54  * This class handles resize issues
55  **********************************************************************/
56
57 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
58 {
59     /* Init */
60     p_vout = NULL;
61     videoSize.rwidth() = -1;
62     videoSize.rheight() = -1;
63
64     hide();
65
66     /* Set the policy to expand in both directions */
67 //    setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
68
69     /* Black background is more coherent for a Video Widget */
70     QPalette plt =  palette();
71     plt.setColor( QPalette::Window, Qt::black );
72     setPalette( plt );
73     setAutoFillBackground(true);
74
75     /* Indicates that the widget wants to draw directly onto the screen.
76        Widgets with this attribute set do not participate in composition
77        management */
78     setAttribute( Qt::WA_PaintOnScreen, true );
79 }
80
81 void VideoWidget::paintEvent(QPaintEvent *ev)
82 {
83     QFrame::paintEvent(ev);
84 #ifdef Q_WS_X11
85     XFlush( QX11Info::display() );
86 #endif
87 }
88
89 VideoWidget::~VideoWidget()
90 {
91     /* Ensure we are not leaking the video output. This would crash. */
92     assert( !p_vout );
93 }
94
95 /**
96  * Request the video to avoid the conflicts
97  **/
98 WId VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
99                           unsigned int *pi_width, unsigned int *pi_height,
100                           bool b_keep_size )
101 {
102     msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
103
104     if( b_keep_size )
105     {
106         *pi_width  = size().width();
107         *pi_height = size().height();
108     }
109
110     if( p_vout )
111     {
112         msg_Dbg( p_intf, "embedded video already in use" );
113         return NULL;
114     }
115     p_vout = p_nvout;
116 #ifndef NDEBUG
117     msg_Dbg( p_intf, "embedded video ready (handle %p)", (void *)winId() );
118 #endif
119     return winId();
120 }
121
122 /* Set the Widget to the correct Size */
123 /* Function has to be called by the parent
124    Parent has to care about resizing itself */
125 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
126 {
127     msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
128     videoSize.rwidth() = w;
129     videoSize.rheight() = h;
130     if( !isVisible() ) show();
131     updateGeometry(); // Needed for deinterlace
132 }
133
134 void VideoWidget::release( void )
135 {
136     msg_Dbg( p_intf, "Video is not needed anymore" );
137     p_vout = NULL;
138     videoSize.rwidth() = 0;
139     videoSize.rheight() = 0;
140     updateGeometry();
141     hide();
142 }
143
144 QSize VideoWidget::sizeHint() const
145 {
146     return videoSize;
147 }
148
149 /**********************************************************************
150  * Background Widget. Show a simple image background. Currently,
151  * it's album art if present or cone.
152  **********************************************************************/
153 #define ICON_SIZE 128
154 #define MAX_BG_SIZE 400
155 #define MIN_BG_SIZE 128
156
157 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
158                  :QWidget( NULL ), p_intf( _p_i )
159 {
160     /* We should use that one to take the more size it can */
161     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
162
163     /* A dark background */
164     setAutoFillBackground( true );
165     plt = palette();
166     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
167     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
168     setPalette( plt );
169
170     /* A cone in the middle */
171     label = new QLabel;
172     label->setMargin( 5 );
173     label->setMaximumHeight( MAX_BG_SIZE );
174     label->setMaximumWidth( MAX_BG_SIZE );
175     label->setMinimumHeight( MIN_BG_SIZE );
176     label->setMinimumWidth( MIN_BG_SIZE );
177     if( QDate::currentDate().dayOfYear() >= 354 )
178         label->setPixmap( QPixmap( ":/logo/vlc128-christmas.png" ) );
179     else
180         label->setPixmap( QPixmap( ":/logo/vlc128.png" ) );
181
182     QGridLayout *backgroundLayout = new QGridLayout( this );
183     backgroundLayout->addWidget( label, 0, 1 );
184     backgroundLayout->setColumnStretch( 0, 1 );
185     backgroundLayout->setColumnStretch( 2, 1 );
186
187     CONNECT( THEMIM->getIM(), artChanged( QString ),
188              this, updateArt( const QString& ) );
189 }
190
191 BackgroundWidget::~BackgroundWidget()
192 {}
193
194 void BackgroundWidget::resizeEvent( QResizeEvent * event )
195 {
196     if( event->size().height() <= MIN_BG_SIZE )
197         label->hide();
198     else
199         label->show();
200 }
201
202 void BackgroundWidget::updateArt( const QString& url )
203 {
204     if( url.isEmpty() )
205     {
206         if( QDate::currentDate().dayOfYear() >= 354 )
207             label->setPixmap( QPixmap( ":/logo/vlc128-christmas.png" ) );
208         else
209             label->setPixmap( QPixmap( ":/logo/vlc128.png" ) );
210     }
211     else
212     {
213         label->setPixmap( QPixmap( url ) );
214     }
215 }
216
217 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
218 {
219     QVLCMenu::PopupMenu( p_intf, true );
220     event->accept();
221 }
222
223 #if 0
224 #include <QPushButton>
225 #include <QHBoxLayout>
226
227 /**********************************************************************
228  * Visualization selector panel
229  **********************************************************************/
230 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
231                                 QFrame( NULL ), p_intf( _p_i )
232 {
233     QHBoxLayout *layout = new QHBoxLayout( this );
234     layout->setMargin( 0 );
235     QPushButton *prevButton = new QPushButton( "Prev" );
236     QPushButton *nextButton = new QPushButton( "Next" );
237     layout->addWidget( prevButton );
238     layout->addWidget( nextButton );
239
240     layout->addStretch( 10 );
241     layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
242
243     current = new QLabel( qtr( "None" ) );
244     layout->addWidget( current );
245
246     BUTTONACT( prevButton, prev() );
247     BUTTONACT( nextButton, next() );
248
249     setLayout( layout );
250     setMaximumHeight( 35 );
251 }
252
253 VisualSelector::~VisualSelector()
254 {}
255
256 void VisualSelector::prev()
257 {
258     char *psz_new = aout_VisualPrev( p_intf );
259     if( psz_new )
260     {
261         current->setText( qfu( psz_new ) );
262         free( psz_new );
263     }
264 }
265
266 void VisualSelector::next()
267 {
268     char *psz_new = aout_VisualNext( p_intf );
269     if( psz_new )
270     {
271         current->setText( qfu( psz_new ) );
272         free( psz_new );
273     }
274 }
275 #endif
276
277 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, const QString& text,
278                         QWidget *parent )
279            : QLabel( text, parent ), p_intf( _p_intf )
280 {
281     setToolTip( qtr( "Current playback speed.\nClick to adjust" ) );
282
283     /* Create the Speed Control Widget */
284     speedControl = new SpeedControlWidget( p_intf, this );
285     speedControlMenu = new QMenu( this );
286
287     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
288     widgetAction->setDefaultWidget( speedControl );
289     speedControlMenu->addAction( widgetAction );
290
291     /* Change the SpeedRate in the Status Bar */
292     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
293
294     CONNECT( THEMIM, inputChanged( input_thread_t * ),
295              speedControl, activateOnState() );
296
297 }
298 SpeedLabel::~SpeedLabel()
299 {
300         delete speedControl;
301         delete speedControlMenu;
302 }
303 /****************************************************************************
304  * Small right-click menu for rate control
305  ****************************************************************************/
306 void SpeedLabel::showSpeedMenu( QPoint pos )
307 {
308     speedControlMenu->exec( QCursor::pos() - pos
309                           + QPoint( 0, height() ) );
310 }
311
312 void SpeedLabel::setRate( int rate )
313 {
314     QString str;
315     str.setNum( ( 1000 / (double)rate ), 'f', 2 );
316     str.append( "x" );
317     setText( str );
318     setToolTip( str );
319     speedControl->updateControls( rate );
320 }
321
322 /**********************************************************************
323  * Speed control widget
324  **********************************************************************/
325 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
326                     : QFrame( _parent ), p_intf( _p_i )
327 {
328     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
329     sizePolicy.setHorizontalStretch( 0 );
330     sizePolicy.setVerticalStretch( 0 );
331
332     speedSlider = new QSlider( this );
333     speedSlider->setSizePolicy( sizePolicy );
334     speedSlider->setMaximumSize( QSize( 80, 200 ) );
335     speedSlider->setOrientation( Qt::Vertical );
336     speedSlider->setTickPosition( QSlider::TicksRight );
337
338     speedSlider->setRange( -34, 34 );
339     speedSlider->setSingleStep( 1 );
340     speedSlider->setPageStep( 1 );
341     speedSlider->setTickInterval( 17 );
342
343     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
344
345     QToolButton *normalSpeedButton = new QToolButton( this );
346     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
347     normalSpeedButton->setAutoRaise( true );
348     normalSpeedButton->setText( "1x" );
349     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
350
351     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
352
353     QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
354     speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
355     speedControlLayout->setSpacing( 4 );
356     speedControlLayout->addWidget( speedSlider );
357     speedControlLayout->addWidget( normalSpeedButton );
358
359     activateOnState();
360 }
361
362 void SpeedControlWidget::activateOnState()
363 {
364     speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
365 }
366
367 void SpeedControlWidget::updateControls( int rate )
368 {
369     if( speedSlider->isSliderDown() )
370     {
371         //We don't want to change anything if the user is using the slider
372         return;
373     }
374
375     double value = 17 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
376     int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
377
378     if( sliderValue < speedSlider->minimum() )
379     {
380         sliderValue = speedSlider->minimum();
381     }
382     else if( sliderValue > speedSlider->maximum() )
383     {
384         sliderValue = speedSlider->maximum();
385     }
386
387     //Block signals to avoid feedback loop
388     speedSlider->blockSignals( true );
389     speedSlider->setValue( sliderValue );
390     speedSlider->blockSignals( false );
391 }
392
393 void SpeedControlWidget::updateRate( int sliderValue )
394 {
395     double speed = pow( 2, (double)sliderValue / 17 );
396     int rate = INPUT_RATE_DEFAULT / speed;
397
398     THEMIM->getIM()->setRate(rate);
399 }
400
401 void SpeedControlWidget::resetRate()
402 {
403     THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
404 }
405
406 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
407               : QLabel( parent ), p_intf( _p_i )
408 {
409     setContextMenuPolicy( Qt::ActionsContextMenu );
410     CONNECT( this, updateRequested(), this, askForUpdate() );
411     CONNECT( THEMIM->getIM(), artChanged( QString ),
412              this, showArtUpdate( const QString& ) );
413
414     setMinimumHeight( 128 );
415     setMinimumWidth( 128 );
416     setMaximumHeight( 128 );
417     setMaximumWidth( 128 );
418     setScaledContents( true );
419
420     QList< QAction* > artActions = actions();
421     QAction *action = new QAction( qtr( "Download cover art" ), this );
422     CONNECT( action, triggered(), this, askForUpdate() );
423     addAction( action );
424
425     showArtUpdate( "" );
426 }
427
428 CoverArtLabel::~CoverArtLabel()
429 {
430     QList< QAction* > artActions = actions();
431     foreach( QAction *act, artActions )
432         removeAction( act );
433 }
434
435 void CoverArtLabel::showArtUpdate( const QString& url )
436 {
437     QPixmap pix;
438     if( !url.isEmpty()  && pix.load( url ) )
439     {
440         setPixmap( pix );
441     }
442     else
443     {
444         setPixmap( QPixmap( ":/noart.png" ) );
445     }
446 }
447
448 void CoverArtLabel::askForUpdate()
449 {
450     THEMIM->getIM()->requestArtUpdate();
451 }
452
453 TimeLabel::TimeLabel( intf_thread_t *_p_intf  ) :QLabel(), p_intf( _p_intf )
454 {
455    b_remainingTime = false;
456    setText( " --:--/--:-- " );
457    setAlignment( Qt::AlignRight | Qt::AlignVCenter );
458    setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
459
460
461    CONNECT( THEMIM->getIM(), cachingChanged( float ),
462             this, setCaching( float ) );
463    CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
464              this, setDisplayPosition( float, int, int ) );
465 }
466
467 void TimeLabel::setDisplayPosition( float pos, int time, int length )
468 {
469     if( pos == -1.f )
470     {
471         setText( " --:--/--:-- " );
472         return;
473     }
474
475     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
476     secstotimestr( psz_length, length );
477     secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
478                                                            : time );
479
480     QString timestr;
481     timestr.sprintf( "%s/%s", psz_time,
482                             ( !length && time ) ? "--:--" : psz_length );
483
484     /* Add a minus to remaining time*/
485     if( b_remainingTime && length ) setText( " -"+timestr+" " );
486     else setText( " "+timestr+" " );
487 }
488
489 void TimeLabel::toggleTimeDisplay()
490 {
491     b_remainingTime = !b_remainingTime;
492 }
493
494 void TimeLabel::setCaching( float f_cache )
495 {
496     QString amount;
497     amount.setNum( (int)(100 * f_cache) );
498     msg_Dbg( p_intf, "New caching: %d", (int)(100*f_cache));
499     setText( "Buff: " + amount + "%" );
500 }
501
502