]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt: SeekSlider: don't show buffering < 1s
[vlc] / modules / gui / qt4 / util / input_slider.cpp
1 /*****************************************************************************
2  * input_slider.cpp : VolumeSlider and SeekSlider
3  ****************************************************************************
4  * Copyright (C) 2006-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ludovic Fauvet <etix@videolan.org>
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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31
32 #include "util/input_slider.hpp"
33 #include "adapters/seekpoints.hpp"
34
35 #include <QPaintEvent>
36 #include <QPainter>
37 #include <QBitmap>
38 #include <QStyleOptionSlider>
39 #include <QLinearGradient>
40 #include <QTimer>
41 #include <QRadialGradient>
42 #include <QLinearGradient>
43 #include <QSize>
44 #include <QPalette>
45 #include <QColor>
46 #include <QPoint>
47 #include <QPropertyAnimation>
48 #include <QApplication>
49
50 #define MINIMUM 0
51 #define MAXIMUM 1000
52 #define CHAPTERSSPOTSIZE 3
53 #define FADEDURATION 300
54 #define FADEOUTDELAY 2000
55
56 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
57           : QSlider( q, _parent ), b_classic( _static )
58 {
59     isSliding = false;
60     isJumping = false;
61     f_buffering = 1.0;
62     mHandleOpacity = 1.0;
63     chapters = NULL;
64     mHandleLength = -1;
65     b_seekable = true;
66     alternativeStyle = NULL;
67
68     // prepare some static colors
69     QPalette p = palette();
70     QColor background = p.color( QPalette::Active, QPalette::Window );
71     tickpointForeground = p.color( QPalette::Active, QPalette::WindowText );
72     tickpointForeground.setHsv( tickpointForeground.hue(),
73             ( background.saturation() + tickpointForeground.saturation() ) / 2,
74             ( background.value() + tickpointForeground.value() ) / 2 );
75
76     // set the background color and gradient
77     QColor backgroundBase( p.window().color() );
78     backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
79     backgroundGradient.setColorAt( 1.0, backgroundBase );
80
81     // set the foreground color and gradient
82     QColor foregroundBase( 50, 156, 255 );
83     foregroundGradient.setColorAt( 0.0,  foregroundBase );
84     foregroundGradient.setColorAt( 1.0,  foregroundBase.darker( 140 ) );
85
86     // prepare the handle's gradient
87     handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
88     handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
89
90     // prepare the handle's shadow gradient
91     QColor shadowBase = p.shadow().color();
92     if( shadowBase.lightness() > 100 )
93         shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
94     shadowDark = shadowBase.darker( 150 );
95     shadowLight = shadowBase.lighter( 180 );
96     shadowLight.setAlpha( 50 );
97
98     /* Timer used to fire intermediate updatePos() when sliding */
99     seekLimitTimer = new QTimer( this );
100     seekLimitTimer->setSingleShot( true );
101
102     /* Tooltip bubble */
103     mTimeTooltip = new TimeTooltip( this );
104     mTimeTooltip->setMouseTracking( true );
105
106     /* Properties */
107     setRange( MINIMUM, MAXIMUM );
108     setSingleStep( 2 );
109     setPageStep( 10 );
110     setMouseTracking( true );
111     setTracking( true );
112     setFocusPolicy( Qt::NoFocus );
113
114     /* Use the new/classic style */
115     setMinimumHeight( 18 );
116     if( !b_classic )
117     {
118         alternativeStyle = new SeekStyle;
119         setStyle( alternativeStyle );
120     }
121
122     /* Init to 0 */
123     setPosition( -1.0, 0, 0 );
124     secstotimestr( psz_length, 0 );
125
126     animHandle = new QPropertyAnimation( this, "handleOpacity", this );
127     animHandle->setDuration( FADEDURATION );
128     animHandle->setStartValue( 0.0 );
129     animHandle->setEndValue( 1.0 );
130
131     hideHandleTimer = new QTimer( this );
132     hideHandleTimer->setSingleShot( true );
133     hideHandleTimer->setInterval( FADEOUTDELAY );
134
135     CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
136     CONNECT( seekLimitTimer, timeout(), this, updatePos() );
137     CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
138     mTimeTooltip->installEventFilter( this );
139 }
140
141 SeekSlider::~SeekSlider()
142 {
143     delete chapters;
144     if ( alternativeStyle )
145         delete alternativeStyle;
146 }
147
148 /***
149  * \brief Sets the chapters seekpoints adapter
150  *
151  * \params SeekPoints initilized with current intf thread
152 ***/
153 void SeekSlider::setChapters( SeekPoints *chapters_ )
154 {
155     delete chapters;
156     chapters = chapters_;
157     chapters->setParent( this );
158 }
159
160 /***
161  * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
162  *
163  * \param pos Position, between 0 and 1. -1 disables the slider
164  * \param time Elapsed time. Unused
165  * \param legnth Duration time.
166  ***/
167 void SeekSlider::setPosition( float pos, int64_t time, int length )
168 {
169     VLC_UNUSED(time);
170     if( pos == -1.0 )
171     {
172         setEnabled( false );
173         mTimeTooltip->hide();
174         isSliding = false;
175     }
176     else
177         setEnabled( b_seekable );
178
179     if( !isSliding )
180         setValue( (int)( pos * 1000.0 ) );
181
182     inputLength = length;
183 }
184
185 void SeekSlider::startSeekTimer()
186 {
187     /* Only fire one update, when sliding, every 150ms */
188     if( isSliding && !seekLimitTimer->isActive() )
189         seekLimitTimer->start( 150 );
190 }
191
192 void SeekSlider::updatePos()
193 {
194     float f_pos = (float)( value() ) / 1000.0;
195     emit sliderDragged( f_pos ); /* Send new position to VLC's core */
196 }
197
198 void SeekSlider::updateBuffering( float f_buffering_ )
199 {
200     if ( f_buffering_ < f_buffering )
201         bufferingStart = QTime::currentTime();
202     f_buffering = f_buffering_;
203     repaint();
204 }
205
206 void SeekSlider::processReleasedButton()
207 {
208     if ( !isSliding && !isJumping ) return;
209     isSliding = false;
210     bool b_seekPending = seekLimitTimer->isActive();
211     seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
212     if ( isJumping )
213     {
214         isJumping = false;
215         return;
216     }
217     if( b_seekPending && isEnabled() )
218         updatePos();
219 }
220
221 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
222 {
223     if ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
224     {
225         QSlider::mouseReleaseEvent( event );
226         return;
227     }
228     event->accept();
229     processReleasedButton();
230 }
231
232 void SeekSlider::mousePressEvent( QMouseEvent* event )
233 {
234     /* Right-click */
235     if ( !isEnabled() ||
236          ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
237        )
238     {
239         QSlider::mousePressEvent( event );
240         return;
241     }
242
243     isJumping = false;
244     /* handle chapter clicks */
245     int i_width = size().width();
246     if ( chapters && inputLength && i_width)
247     {
248         if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
249         {
250              /* only on chapters zone */
251             if ( event->y() < CHAPTERSSPOTSIZE ||
252                  event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
253             {
254                 QList<SeekPoint> points = chapters->getPoints();
255                 int i_selected = -1;
256                 bool b_startsnonzero = false; /* as we always starts at 1 */
257                 if ( points.count() > 0 ) /* do we need an extra offset ? */
258                     b_startsnonzero = ( points.at(0).time > 0 );
259                 int i_min_diff = i_width + 1;
260                 for( int i = 0 ; i < points.count() ; i++ )
261                 {
262                     int x = points.at(i).time / 1000000.0 / inputLength * i_width;
263                     int diff_x = abs( x - event->x() );
264                     if ( diff_x < i_min_diff )
265                     {
266                         i_min_diff = diff_x;
267                         i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
268                     } else break;
269                 }
270                 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
271                 {
272                     chapters->jumpTo( i_selected );
273                     event->accept();
274                     isJumping = true;
275                     return;
276                 }
277             }
278         }
279     }
280
281     isSliding = true ;
282
283     setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false ) );
284     emit sliderMoved( value() );
285     event->accept();
286 }
287
288 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
289 {
290     if ( ! ( event->buttons() & ( Qt::LeftButton | Qt::MidButton ) ) )
291     {
292         /* Handle button release when mouserelease has been hijacked by popup */
293         processReleasedButton();
294     }
295
296     if ( !isEnabled() ) return event->accept();
297
298     if( isSliding )
299     {
300         setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false) );
301         emit sliderMoved( value() );
302     }
303
304     /* Tooltip */
305     if ( inputLength > 0 )
306     {
307         int margin = handleLength() / 2;
308         int posX = qMax( rect().left() + margin, qMin( rect().right() - margin, event->x() ) );
309
310         QString chapterLabel;
311
312         if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
313         {
314                 QList<SeekPoint> points = chapters->getPoints();
315                 int i_selected = -1;
316                 bool b_startsnonzero = false;
317                 if ( points.count() > 0 )
318                     b_startsnonzero = ( points.at(0).time > 0 );
319                 for( int i = 0 ; i < points.count() ; i++ )
320                 {
321                     int x = points.at(i).time / 1000000.0 / inputLength * size().width();
322                     if ( event->x() >= x )
323                         i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
324                 }
325                 if ( i_selected >= 0 && i_selected < points.size() )
326                     chapterLabel = points.at( i_selected ).name;
327         }
328
329         QPoint target( event->globalX() - ( event->x() - posX ),
330                   QWidget::mapToGlobal( QPoint( 0, 0 ) ).y() );
331         if( likely( size().width() > handleLength() ) ) {
332             secstotimestr( psz_length, ( ( posX - margin ) * inputLength ) / ( size().width() - handleLength() ) );
333             mTimeTooltip->setTip( target, psz_length, chapterLabel );
334         }
335     }
336     event->accept();
337 }
338
339 void SeekSlider::wheelEvent( QWheelEvent *event )
340 {
341     /* Don't do anything if we are for somehow reason sliding */
342     if( !isSliding && isEnabled() )
343     {
344         setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
345          Since delta is in 1/8 of ° and mouse have steps of 15 °
346          and that our slider is in 0.1% and we want one step to be a 1%
347          increment of position */
348         emit sliderDragged( value() / 1000.0 );
349     }
350     event->accept();
351 }
352
353 void SeekSlider::enterEvent( QEvent * )
354 {
355     /* Cancel the fade-out timer */
356     hideHandleTimer->stop();
357     /* Only start the fade-in if needed */
358     if( isEnabled() && animHandle->direction() != QAbstractAnimation::Forward )
359     {
360         /* If pause is called while not running Qt will complain */
361         if( animHandle->state() == QAbstractAnimation::Running )
362             animHandle->pause();
363         animHandle->setDirection( QAbstractAnimation::Forward );
364         animHandle->start();
365     }
366     /* Don't show the tooltip if the slider is disabled or a menu is open */
367     if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
368         mTimeTooltip->show();
369 }
370
371 void SeekSlider::leaveEvent( QEvent * )
372 {
373     hideHandleTimer->start();
374     /* Hide the tooltip
375        - if the mouse leave the slider rect (Note: it can still be
376          over the tooltip!)
377        - if another window is on the way of the cursor */
378     if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
379       ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
380     {
381         mTimeTooltip->hide();
382     }
383 }
384
385 void SeekSlider::paintEvent( QPaintEvent *ev )
386 {
387     if ( alternativeStyle )
388     {
389         SeekStyle::SeekStyleOption option;
390         option.initFrom( this );
391         if ( QTime::currentTime() > bufferingStart.addSecs( 1 ) )
392             option.buffering = f_buffering;
393         else
394             option.buffering = 1.0;
395         option.length = inputLength;
396         option.animate = ( animHandle->state() == QAbstractAnimation::Running
397                            || hideHandleTimer->isActive() );
398         option.animationopacity = mHandleOpacity;
399         option.sliderPosition = sliderPosition();
400         option.sliderValue = value();
401         option.maximum = maximum();
402         option.minimum = minimum();
403         foreach( const SeekPoint &point, chapters->getPoints() )
404             option.points << point.time;
405         QPainter painter( this );
406         style()->drawComplexControl( QStyle::CC_Slider, &option, &painter, this );
407     }
408     else
409         QSlider::paintEvent( ev );
410 }
411
412 void SeekSlider::hideEvent( QHideEvent * )
413 {
414     mTimeTooltip->hide();
415 }
416
417 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
418 {
419     if( obj == mTimeTooltip )
420     {
421         if( event->type() == QEvent::Leave ||
422             event->type() == QEvent::MouseMove )
423         {
424             QMouseEvent *e = static_cast<QMouseEvent*>( event );
425             if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
426                 mTimeTooltip->hide();
427         }
428         return false;
429     }
430     else
431         return QSlider::eventFilter( obj, event );
432 }
433
434 QSize SeekSlider::sizeHint() const
435 {
436     if ( b_classic )
437         return QSlider::sizeHint();
438     return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
439                                                : QSize( 18, 100 );
440 }
441
442 qreal SeekSlider::handleOpacity() const
443 {
444     return mHandleOpacity;
445 }
446
447 void SeekSlider::setHandleOpacity(qreal opacity)
448 {
449     mHandleOpacity = opacity;
450     /* Request a new paintevent */
451     update();
452 }
453
454 inline int SeekSlider::handleLength()
455 {
456     if ( mHandleLength > 0 )
457         return mHandleLength;
458
459     /* Ask for the length of the handle to the underlying style */
460     QStyleOptionSlider option;
461     initStyleOption( &option );
462     mHandleLength = style()->pixelMetric( QStyle::PM_SliderLength, &option );
463     return mHandleLength;
464 }
465
466 void SeekSlider::hideHandle()
467 {
468     /* If pause is called while not running Qt will complain */
469     if( animHandle->state() == QAbstractAnimation::Running )
470         animHandle->pause();
471     /* Play the animation backward */
472     animHandle->setDirection( QAbstractAnimation::Backward );
473     animHandle->start();
474 }
475
476
477 /* This work is derived from Amarok's work under GPLv2+
478     - Mark Kretschmann
479     - Gábor Lehel
480    */
481 #define WLENGTH   80 // px
482 #define WHEIGHT   22  // px
483 #define SOUNDMIN  0   // %
484
485 SoundSlider::SoundSlider( QWidget *_parent, float _i_step,
486                           char *psz_colors, int max )
487                         : QAbstractSlider( _parent )
488 {
489     f_step = (float)(_i_step * 10000)
490            / (float)((max - SOUNDMIN) * AOUT_VOLUME_DEFAULT);
491     setRange( SOUNDMIN, max);
492     setMouseTracking( true );
493     isSliding = false;
494     b_mouseOutside = true;
495     b_isMuted = false;
496
497     pixOutside = QPixmap( ":/toolbar/volslide-outside" );
498
499     const QPixmap temp( ":/toolbar/volslide-inside" );
500     const QBitmap mask( temp.createHeuristicMask() );
501
502     setFixedSize( pixOutside.size() );
503
504     pixGradient = QPixmap( mask.size() );
505     pixGradient2 = QPixmap( mask.size() );
506
507     /* Gradient building from the preferences */
508     QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
509     QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
510
511     QStringList colorList = qfu( psz_colors ).split( ";" );
512     free( psz_colors );
513
514     /* Fill with 255 if the list is too short */
515     if( colorList.count() < 12 )
516         for( int i = colorList.count(); i < 12; i++)
517             colorList.append( "255" );
518
519     background = palette().color( QPalette::Active, QPalette::Window );
520     foreground = palette().color( QPalette::Active, QPalette::WindowText );
521     foreground.setHsv( foreground.hue(),
522                     ( background.saturation() + foreground.saturation() ) / 2,
523                     ( background.value() + foreground.value() ) / 2 );
524
525     textfont.setPixelSize( 9 );
526     textrect.setRect( 0, 0, 34, 15 );
527
528     /* Regular colors */
529 #define c(i) colorList.at(i).toInt()
530 #define add_color(gradient, range, c1, c2, c3) \
531     gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
532
533     /* Desaturated colors */
534 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
535 #define add_desaturated_color(gradient, range, c1, c2, c3) \
536     foo = new QColor( c(c1), c(c2), c(c3) );\
537     desaturate( foo ); gradient.setColorAt( range, *foo );\
538     delete foo;
539
540     /* combine the two helpers */
541 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
542     add_color( gradient1, range, c1, c2, c3 ); \
543     add_desaturated_color( gradient2, range, c1, c2, c3 );
544
545     float f_mid_point = ( 100.0 / maximum() );
546     QColor * foo;
547     add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
548     add_colors( gradient, gradient2, f_mid_point - 0.05, 3, 4, 5 );
549     add_colors( gradient, gradient2, f_mid_point + 0.05, 6, 7, 8 );
550     add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
551
552     painter.begin( &pixGradient );
553     painter.setPen( Qt::NoPen );
554     painter.setBrush( gradient );
555     painter.drawRect( pixGradient.rect() );
556     painter.end();
557
558     painter.begin( &pixGradient2 );
559     painter.setPen( Qt::NoPen );
560     painter.setBrush( gradient2 );
561     painter.drawRect( pixGradient2.rect() );
562     painter.end();
563
564     pixGradient.setMask( mask );
565     pixGradient2.setMask( mask );
566 }
567
568 void SoundSlider::wheelEvent( QWheelEvent *event )
569 {
570     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
571     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
572
573     emit sliderReleased();
574     emit sliderMoved( value() );
575 }
576
577 void SoundSlider::mousePressEvent( QMouseEvent *event )
578 {
579     if( event->button() != Qt::RightButton )
580     {
581         /* We enter the sliding mode */
582         isSliding = true;
583         i_oldvalue = value();
584         emit sliderPressed();
585         changeValue( event->x() - paddingL );
586         emit sliderMoved( value() );
587     }
588 }
589
590 void SoundSlider::processReleasedButton()
591 {
592     if( !b_mouseOutside && value() != i_oldvalue )
593     {
594         emit sliderReleased();
595         setValue( value() );
596         emit sliderMoved( value() );
597     }
598     isSliding = false;
599     b_mouseOutside = false;
600 }
601
602 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
603 {
604     if( event->button() != Qt::RightButton )
605         processReleasedButton();
606 }
607
608 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
609 {
610     /* handle mouserelease hijacking */
611     if ( isSliding && ( event->buttons() & ~Qt::RightButton ) == Qt::NoButton )
612         processReleasedButton();
613
614     if( isSliding )
615     {
616         QRect rect( paddingL - 15,    -1,
617                     WLENGTH + 15 * 2 , WHEIGHT + 5 );
618         if( !rect.contains( event->pos() ) )
619         { /* We are outside */
620             if ( !b_mouseOutside )
621                 setValue( i_oldvalue );
622             b_mouseOutside = true;
623         }
624         else
625         { /* We are inside */
626             b_mouseOutside = false;
627             changeValue( event->x() - paddingL );
628             emit sliderMoved( value() );
629         }
630     }
631     else
632     {
633         int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
634         i = __MIN( __MAX( 0, i ), maximum() );
635         setToolTip( QString("%1  %" ).arg( i ) );
636     }
637 }
638
639 void SoundSlider::changeValue( int x )
640 {
641     setValue( (x * maximum() + 40 ) / WLENGTH );
642 }
643
644 void SoundSlider::setMuted( bool m )
645 {
646     b_isMuted = m;
647     update();
648 }
649
650 void SoundSlider::paintEvent( QPaintEvent *e )
651 {
652     QPixmap *paintGradient;
653     if (b_isMuted)
654         paintGradient = &this->pixGradient2;
655     else
656         paintGradient = &this->pixGradient;
657
658     painter.begin( this );
659
660     const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
661
662     const QRectF boundsG( 0, 0, offset , paintGradient->height() );
663     painter.drawPixmap( boundsG, *paintGradient, boundsG );
664
665     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
666     painter.drawPixmap( boundsO, pixOutside, boundsO );
667
668     painter.setPen( foreground );
669     painter.setFont( textfont );
670     painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
671                       QString::number( value() ) + '%' );
672
673     painter.end();
674     e->accept();
675 }