]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Qt: force the deletion that doesn't seem to happen correctly
[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     /* The core can ask through a callback to show the video. */
81     connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
82              this, SLOT(SetSizing(unsigned int, unsigned int )),
83              Qt::BlockingQueuedConnection );
84 }
85
86 void VideoWidget::paintEvent(QPaintEvent *ev)
87 {
88     QFrame::paintEvent(ev);
89 #ifdef Q_WS_X11
90     XFlush( QX11Info::display() );
91 #endif
92 }
93
94 VideoWidget::~VideoWidget()
95 {
96     /* Ensure we are not leaking the video output. This would crash. */
97     assert( !p_vout );
98 }
99
100 /**
101  * Request the video to avoid the conflicts
102  **/
103 WId VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
104                           unsigned int *pi_width, unsigned int *pi_height,
105                           bool b_keep_size )
106 {
107     msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
108
109     if( b_keep_size )
110     {
111         *pi_width  = size().width();
112         *pi_height = size().height();
113     }
114
115     emit askVideoWidgetToShow( *pi_width, *pi_height );
116     if( p_vout )
117     {
118         msg_Dbg( p_intf, "embedded video already in use" );
119         return NULL;
120     }
121     p_vout = p_nvout;
122 #ifndef NDEBUG
123     msg_Dbg( p_intf, "embedded video ready (handle %p)", (void *)winId() );
124 #endif
125     return winId();
126 }
127
128 /* Set the Widget to the correct Size */
129 /* Function has to be called by the parent
130    Parent has to care about resizing himself*/
131 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
132 {
133     msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
134     videoSize.rwidth() = w;
135     videoSize.rheight() = h;
136     if( isHidden() ) show();
137     updateGeometry(); // Needed for deinterlace
138 }
139
140 void VideoWidget::release( void )
141 {
142     msg_Dbg( p_intf, "Video is not needed anymore" );
143     p_vout = NULL;
144     videoSize.rwidth() = 0;
145     videoSize.rheight() = 0;
146     updateGeometry();
147     hide();
148 }
149
150 QSize VideoWidget::sizeHint() const
151 {
152     return videoSize;
153 }
154
155 /**********************************************************************
156  * Background Widget. Show a simple image background. Currently,
157  * it's album art if present or cone.
158  **********************************************************************/
159 #define ICON_SIZE 128
160 #define MAX_BG_SIZE 400
161 #define MIN_BG_SIZE 128
162
163 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
164                  :QWidget( NULL ), p_intf( _p_i )
165 {
166     /* We should use that one to take the more size it can */
167     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
168
169     /* A dark background */
170     setAutoFillBackground( true );
171     plt = palette();
172     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
173     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
174     setPalette( plt );
175
176     /* A cone in the middle */
177     label = new QLabel;
178     label->setMargin( 5 );
179     label->setMaximumHeight( MAX_BG_SIZE );
180     label->setMaximumWidth( MAX_BG_SIZE );
181     label->setMinimumHeight( MIN_BG_SIZE );
182     label->setMinimumWidth( MIN_BG_SIZE );
183     if( QDate::currentDate().dayOfYear() >= 354 )
184         label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
185     else
186         label->setPixmap( QPixmap( ":/vlc128.png" ) );
187
188     QGridLayout *backgroundLayout = new QGridLayout( this );
189     backgroundLayout->addWidget( label, 0, 1 );
190     backgroundLayout->setColumnStretch( 0, 1 );
191     backgroundLayout->setColumnStretch( 2, 1 );
192
193     CONNECT( THEMIM->getIM(), artChanged( QString ),
194              this, updateArt( QString ) );
195 }
196
197 BackgroundWidget::~BackgroundWidget()
198 {}
199
200 void BackgroundWidget::resizeEvent( QResizeEvent * event )
201 {
202     if( event->size().height() <= MIN_BG_SIZE )
203         label->hide();
204     else
205         label->show();
206 }
207
208 void BackgroundWidget::updateArt( QString url )
209 {
210     if( url.isEmpty() )
211     {
212         if( QDate::currentDate().dayOfYear() >= 354 )
213             label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
214         else
215             label->setPixmap( QPixmap( ":/vlc128.png" ) );
216     }
217     else
218     {
219         label->setPixmap( QPixmap( url ) );
220     }
221 }
222
223 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
224 {
225     QVLCMenu::PopupMenu( p_intf, true );
226     event->accept();
227 }
228
229 #if 0
230 #include <QPushButton>
231 #include <QHBoxLayout>
232
233 /**********************************************************************
234  * Visualization selector panel
235  **********************************************************************/
236 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
237                                 QFrame( NULL ), p_intf( _p_i )
238 {
239     QHBoxLayout *layout = new QHBoxLayout( this );
240     layout->setMargin( 0 );
241     QPushButton *prevButton = new QPushButton( "Prev" );
242     QPushButton *nextButton = new QPushButton( "Next" );
243     layout->addWidget( prevButton );
244     layout->addWidget( nextButton );
245
246     layout->addStretch( 10 );
247     layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
248
249     current = new QLabel( qtr( "None" ) );
250     layout->addWidget( current );
251
252     BUTTONACT( prevButton, prev() );
253     BUTTONACT( nextButton, next() );
254
255     setLayout( layout );
256     setMaximumHeight( 35 );
257 }
258
259 VisualSelector::~VisualSelector()
260 {}
261
262 void VisualSelector::prev()
263 {
264     char *psz_new = aout_VisualPrev( p_intf );
265     if( psz_new )
266     {
267         current->setText( qfu( psz_new ) );
268         free( psz_new );
269     }
270 }
271
272 void VisualSelector::next()
273 {
274     char *psz_new = aout_VisualNext( p_intf );
275     if( psz_new )
276     {
277         current->setText( qfu( psz_new ) );
278         free( psz_new );
279     }
280 }
281 #endif
282
283 SpeedLabel::SpeedLabel( intf_thread_t *_p_intf, const QString& text,
284                         QWidget *parent )
285            : QLabel( text, parent ), p_intf( _p_intf )
286 {
287     setToolTip( qtr( "Current playback speed.\nRight click to adjust" ) );
288     setContextMenuPolicy ( Qt::CustomContextMenu );
289
290     /* Create the Speed Control Widget */
291     speedControl = new SpeedControlWidget( p_intf, this );
292     speedControlMenu = new QMenu( this );
293
294     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
295     widgetAction->setDefaultWidget( speedControl );
296     speedControlMenu->addAction( widgetAction );
297
298     /* Speed Label behaviour:
299        - right click gives the vertical speed slider */
300     CONNECT( this, customContextMenuRequested( QPoint ),
301              this, showSpeedMenu( QPoint ) );
302
303     /* Change the SpeedRate in the Status Bar */
304     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
305
306     CONNECT( THEMIM, inputChanged( input_thread_t * ),
307              speedControl, activateOnState() );
308 }
309 SpeedLabel::~SpeedLabel()
310 {
311         delete speedControl;
312         delete speedControlMenu;
313 }
314 /****************************************************************************
315  * Small right-click menu for rate control
316  ****************************************************************************/
317 void SpeedLabel::showSpeedMenu( QPoint pos )
318 {
319     speedControlMenu->exec( QCursor::pos() - pos
320                           + QPoint( 0, height() ) );
321 }
322
323 void SpeedLabel::setRate( int rate )
324 {
325     QString str;
326     str.setNum( ( 1000 / (double)rate ), 'f', 2 );
327     str.append( "x" );
328     setText( str );
329     setToolTip( str );
330     speedControl->updateControls( rate );
331 }
332
333 /**********************************************************************
334  * Speed control widget
335  **********************************************************************/
336 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i, QWidget *_parent )
337                     : QFrame( _parent ), p_intf( _p_i )
338 {
339     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
340     sizePolicy.setHorizontalStretch( 0 );
341     sizePolicy.setVerticalStretch( 0 );
342
343     speedSlider = new QSlider( this );
344     speedSlider->setSizePolicy( sizePolicy );
345     speedSlider->setMaximumSize( QSize( 80, 200 ) );
346     speedSlider->setOrientation( Qt::Vertical );
347     speedSlider->setTickPosition( QSlider::TicksRight );
348
349     speedSlider->setRange( -34, 34 );
350     speedSlider->setSingleStep( 1 );
351     speedSlider->setPageStep( 1 );
352     speedSlider->setTickInterval( 17 );
353
354     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
355
356     QToolButton *normalSpeedButton = new QToolButton( this );
357     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
358     normalSpeedButton->setAutoRaise( true );
359     normalSpeedButton->setText( "1x" );
360     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
361
362     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
363
364     QVBoxLayout *speedControlLayout = new QVBoxLayout( this );
365     speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
366     speedControlLayout->setSpacing( 4 );
367     speedControlLayout->addWidget( speedSlider );
368     speedControlLayout->addWidget( normalSpeedButton );
369
370     activateOnState();
371 }
372
373 void SpeedControlWidget::activateOnState()
374 {
375     speedSlider->setEnabled( THEMIM->getIM()->hasInput() );
376 }
377
378 void SpeedControlWidget::updateControls( int rate )
379 {
380     if( speedSlider->isSliderDown() )
381     {
382         //We don't want to change anything if the user is using the slider
383         return;
384     }
385
386     double value = 17 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
387     int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
388
389     if( sliderValue < speedSlider->minimum() )
390     {
391         sliderValue = speedSlider->minimum();
392     }
393     else if( sliderValue > speedSlider->maximum() )
394     {
395         sliderValue = speedSlider->maximum();
396     }
397
398     //Block signals to avoid feedback loop
399     speedSlider->blockSignals( true );
400     speedSlider->setValue( sliderValue );
401     speedSlider->blockSignals( false );
402 }
403
404 void SpeedControlWidget::updateRate( int sliderValue )
405 {
406     double speed = pow( 2, (double)sliderValue / 17 );
407     int rate = INPUT_RATE_DEFAULT / speed;
408
409     THEMIM->getIM()->setRate(rate);
410 }
411
412 void SpeedControlWidget::resetRate()
413 {
414     THEMIM->getIM()->setRate( INPUT_RATE_DEFAULT );
415 }
416
417 CoverArtLabel::CoverArtLabel( QWidget *parent, intf_thread_t *_p_i )
418         : QLabel( parent ), p_intf( _p_i )
419 {
420     setContextMenuPolicy( Qt::ActionsContextMenu );
421     CONNECT( this, updateRequested(), this, doUpdate() );
422     CONNECT( THEMIM->getIM(), artChanged( QString ),
423              this, doUpdate( QString ) );
424
425     setMinimumHeight( 128 );
426     setMinimumWidth( 128 );
427     setMaximumHeight( 128 );
428     setMaximumWidth( 128 );
429     setScaledContents( true );
430     QList< QAction* > artActions = actions();
431     QAction *action = new QAction( qtr( "Download cover art" ), this );
432     addAction( action );
433     CONNECT( action, triggered(), this, doUpdate() );
434
435     doUpdate();
436 }
437
438 CoverArtLabel::~CoverArtLabel()
439 {
440     QList< QAction* > artActions = actions();
441     foreach( QAction *act, artActions )
442         removeAction( act );
443 }
444
445 void CoverArtLabel::doUpdate( QString url )
446 {
447     QPixmap pix;
448     if( !url.isEmpty()  && pix.load( url ) )
449     {
450         setPixmap( pix );
451     }
452     else
453     {
454         setPixmap( QPixmap( ":/noart.png" ) );
455     }
456 }
457
458 void CoverArtLabel::doUpdate()
459 {
460     THEMIM->getIM()->requestArtUpdate();
461 }
462
463 TimeLabel::TimeLabel( intf_thread_t *_p_intf  ) :QLabel(), p_intf( _p_intf )
464 {
465    b_remainingTime = false;
466    setText( " --:--/--:-- " );
467    setAlignment( Qt::AlignRight | Qt::AlignVCenter );
468    setToolTip( qtr( "Toggle between elapsed and remaining time" ) );
469
470
471    CONNECT( THEMIM->getIM(), cachingChanged( float ),
472             this, setCaching( float ) );
473    CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
474              this, setDisplayPosition( float, int, int ) );
475 }
476
477 void TimeLabel::setDisplayPosition( float pos, int time, int length )
478 {
479     if( pos == -1.f )
480     {
481         setText( " --:--/--:-- " );
482         return;
483     }
484
485     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
486     secstotimestr( psz_length, length );
487     secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
488                                                            : time );
489
490     QString timestr;
491     timestr.sprintf( "%s/%s", psz_time,
492                             ( !length && time ) ? "--:--" : psz_length );
493
494     /* Add a minus to remaining time*/
495     if( b_remainingTime && length ) setText( " -"+timestr+" " );
496     else setText( " "+timestr+" " );
497 }
498
499 void TimeLabel::toggleTimeDisplay()
500 {
501     b_remainingTime = !b_remainingTime;
502 }
503
504 void TimeLabel::setCaching( float f_cache )
505 {
506     QString amount;
507     amount.setNum( (int)(100 * f_cache) );
508     msg_Dbg( p_intf, "New caching: %d", (int)(100*f_cache));
509     setText( "Buffering " + amount + "%" );
510 }
511
512