1 /*****************************************************************************
2 * input_slider.cpp : VolumeSlider and SeekSlider
3 ****************************************************************************
4 * Copyright (C) 2006-2011 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Jean-Baptiste Kempf <jb@videolan.org>
9 * Ludovic Fauvet <etix@videolan.org>
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.
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.
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 *****************************************************************************/
32 #include "util/input_slider.hpp"
33 #include "adapters/seekpoints.hpp"
34 #include <vlc_aout_intf.h>
36 #include <QPaintEvent>
40 #include <QStyleOptionSlider>
41 #include <QLinearGradient>
43 #include <QRadialGradient>
44 #include <QLinearGradient>
49 #include <QPropertyAnimation>
50 #include <QApplication>
54 #define CHAPTERSSPOTSIZE 3
55 #define FADEDURATION 300
56 #define FADEOUTDELAY 2000
58 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
59 : QSlider( q, _parent ), b_classic( _static )
66 // prepare some static colors
67 QPalette p = palette();
68 QColor background = p.color( QPalette::Active, QPalette::Background );
69 tickpointForeground = p.color( QPalette::Active, QPalette::WindowText );
70 tickpointForeground.setHsv( tickpointForeground.hue(),
71 ( background.saturation() + tickpointForeground.saturation() ) / 2,
72 ( background.value() + tickpointForeground.value() ) / 2 );
74 // set the background color and gradient
75 QColor backgroundBase( p.window().color() );
76 backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
77 backgroundGradient.setColorAt( 1.0, backgroundBase );
79 // set the foreground color and gradient
80 QColor foregroundBase( 50, 156, 255 );
81 foregroundGradient.setColorAt( 0.0, foregroundBase );
82 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
84 // prepare the handle's gradient
85 handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
86 handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
88 // prepare the handle's shadow gradient
89 QColor shadowBase = p.shadow().color();
90 if( shadowBase.lightness() > 100 )
91 shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
92 shadowDark = shadowBase.darker( 150 );
93 shadowLight = shadowBase.lighter( 180 );
94 shadowLight.setAlpha( 50 );
96 /* Timer used to fire intermediate updatePos() when sliding */
97 seekLimitTimer = new QTimer( this );
98 seekLimitTimer->setSingleShot( true );
101 mTimeTooltip = new TimeTooltip( this );
102 mTimeTooltip->setMouseTracking( true );
105 setRange( MINIMUM, MAXIMUM );
108 setMouseTracking( true );
110 setFocusPolicy( Qt::NoFocus );
113 setPosition( -1.0, 0, 0 );
114 secstotimestr( psz_length, 0 );
116 animHandle = new QPropertyAnimation( this, "handleOpacity", this );
117 animHandle->setDuration( FADEDURATION );
118 animHandle->setStartValue( 0.0 );
119 animHandle->setEndValue( 1.0 );
121 hideHandleTimer = new QTimer( this );
122 hideHandleTimer->setSingleShot( true );
123 hideHandleTimer->setInterval( FADEOUTDELAY );
125 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
126 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
127 CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
128 mTimeTooltip->installEventFilter( this );
131 SeekSlider::~SeekSlider()
137 * \brief Sets the chapters seekpoints adapter
139 * \params SeekPoints initilized with current intf thread
141 void SeekSlider::setChapters( SeekPoints *chapters_ )
144 chapters = chapters_;
145 chapters->setParent( this );
149 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
151 * \param pos Position, between 0 and 1. -1 disables the slider
152 * \param time Elapsed time. Unused
153 * \param legnth Duration time.
155 void SeekSlider::setPosition( float pos, int64_t time, int length )
167 setValue( (int)( pos * 1000.0 ) );
169 inputLength = length;
172 void SeekSlider::startSeekTimer()
174 /* Only fire one update, when sliding, every 150ms */
175 if( isSliding && !seekLimitTimer->isActive() )
176 seekLimitTimer->start( 150 );
179 void SeekSlider::updatePos()
181 float f_pos = (float)( value() ) / 1000.0;
182 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
185 void SeekSlider::updateBuffering( float f_buffering_ )
187 f_buffering = f_buffering_;
191 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
195 bool b_seekPending = seekLimitTimer->isActive();
196 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
202 QSlider::mouseReleaseEvent( event );
207 void SeekSlider::mousePressEvent( QMouseEvent* event )
210 if( event->button() != Qt::LeftButton &&
211 event->button() != Qt::MidButton )
213 QSlider::mousePressEvent( event );
218 /* handle chapter clicks */
219 int i_width = size().width();
220 if ( chapters && inputLength && i_width)
222 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
224 /* only on chapters zone */
225 if ( event->y() < CHAPTERSSPOTSIZE ||
226 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
228 QList<SeekPoint> points = chapters->getPoints();
230 bool b_startsnonzero = false; /* as we always starts at 1 */
231 if ( points.count() > 0 ) /* do we need an extra offset ? */
232 b_startsnonzero = ( points.at(0).time > 0 );
233 int i_min_diff = i_width + 1;
234 for( int i = 0 ; i < points.count() ; i++ )
236 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
237 int diff_x = abs( x - event->x() );
238 if ( diff_x < i_min_diff )
241 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
244 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
246 chapters->jumpTo( i_selected );
256 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
257 emit sliderMoved( value() );
261 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
265 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
266 emit sliderMoved( value() );
270 if ( inputLength > 0 )
272 int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
274 QString chapterLabel;
276 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
278 QList<SeekPoint> points = chapters->getPoints();
280 bool b_startsnonzero = false;
281 if ( points.count() > 0 )
282 b_startsnonzero = ( points.at(0).time > 0 );
283 for( int i = 0 ; i < points.count() ; i++ )
285 int x = points.at(i).time / 1000000.0 / inputLength * size().width();
286 if ( event->x() >= x )
287 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
289 if ( i_selected >= 0 && i_selected < points.size() )
290 chapterLabel = points.at( i_selected ).name;
293 QPoint target( event->globalX() - ( event->x() - posX ),
294 QWidget::mapToGlobal( pos() ).y() );
295 secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
296 mTimeTooltip->setTip( target, psz_length, chapterLabel );
301 void SeekSlider::wheelEvent( QWheelEvent *event )
303 /* Don't do anything if we are for somehow reason sliding */
306 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
307 Since delta is in 1/8 of ° and mouse have steps of 15 °
308 and that our slider is in 0.1% and we want one step to be a 1%
309 increment of position */
310 emit sliderDragged( value() / 1000.0 );
315 void SeekSlider::enterEvent( QEvent * )
317 /* Cancel the fade-out timer */
318 hideHandleTimer->stop();
319 /* Only start the fade-in if needed */
320 if( animHandle->direction() != QAbstractAnimation::Forward )
322 /* If pause is called while not running Qt will complain */
323 if( animHandle->state() == QAbstractAnimation::Running )
325 animHandle->setDirection( QAbstractAnimation::Forward );
328 /* Don't show the tooltip if the slider is disabled or a menu is open */
329 if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
330 mTimeTooltip->show();
333 void SeekSlider::leaveEvent( QEvent * )
335 hideHandleTimer->start();
337 - if the mouse leave the slider rect (Note: it can still be
339 - if another window is on the way of the cursor */
340 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
341 ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
343 mTimeTooltip->hide();
347 void SeekSlider::hideEvent( QHideEvent * )
349 mTimeTooltip->hide();
352 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
354 if( obj == mTimeTooltip )
356 if( event->type() == QEvent::Leave ||
357 event->type() == QEvent::MouseMove )
359 QMouseEvent *e = static_cast<QMouseEvent*>( event );
360 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
361 mTimeTooltip->hide();
366 return QSlider::eventFilter( obj, event );
369 QSize SeekSlider::sizeHint() const
371 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
375 QSize SeekSlider::handleSize() const
377 const int size = ( orientation() == Qt::Horizontal ? height() : width() );
378 return QSize( size, size );
381 void SeekSlider::paintEvent( QPaintEvent *event )
384 return QSlider::paintEvent( event );
386 QStyleOptionSlider option;
387 initStyleOption( &option );
390 QPainter painter( this );
391 painter.setRenderHints( QPainter::Antialiasing );
394 const int barCorner = 3;
395 qreal sliderPos = -1;
397 QRect barRect = rect();
399 // adjust positions based on the current orientation
400 if ( option.sliderPosition != 0 )
402 switch ( orientation() )
405 sliderPos = ( ( (qreal)width() ) / (qreal)range )
406 * (qreal)option.sliderPosition;
409 sliderPos = ( ( (qreal)height() ) / (qreal)range )
410 * (qreal)option.sliderPosition;
415 switch ( orientation() )
418 barRect.setHeight( height() /2 );
421 barRect.setWidth( width() /2 );
425 barRect.moveCenter( rect().center() );
427 QSize hSize( handleSize() - QSize( 6, 6 ) );
428 QSize sSize( handleSize() - QSize( 2, 2 ) );
430 if ( gradientsTargetSize != size() )
432 /* Need to fix gradients */
433 gradientsTargetSize = size();
434 backgroundGradient.setFinalStop( 0, height() );
435 foregroundGradient.setFinalStop( 0, height() );
436 handleGradient.setFinalStop( 0, hSize.height() );
439 // draw a slight 3d effect on the bottom
440 painter.setPen( QColor( 230, 230, 230 ) );
441 painter.setBrush( Qt::NoBrush );
442 painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
445 painter.setPen( Qt::NoPen );
446 painter.setBrush( backgroundGradient );
447 painter.drawRoundedRect( barRect, barCorner, barCorner );
449 // adjusted foreground rectangle
450 QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
452 switch ( orientation() )
455 valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
458 valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
459 valueRect.moveBottom( rect().bottom() );
463 if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
466 painter.setPen( Qt::NoPen );
467 painter.setBrush( foregroundGradient );
468 painter.drawRoundedRect( valueRect, barCorner, barCorner );
471 // draw buffering overlay
472 if ( f_buffering < 1.0 )
474 QRect innerRect = barRect.adjusted( 1, 1,
475 barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
476 QColor overlayColor = QColor( "Orange" );
477 overlayColor.setAlpha( 128 );
478 painter.setBrush( overlayColor );
479 painter.drawRoundedRect( innerRect, barCorner, barCorner );
482 if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
484 /* draw chapters tickpoints */
485 if ( chapters && inputLength && size().width() )
487 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
489 QList<SeekPoint> points = chapters->getPoints();
490 painter.setPen( tickpointForeground );
491 painter.setBrush( Qt::NoBrush );
492 foreach( SeekPoint point, points )
494 int x = point.time / 1000000.0 / inputLength * size().width();
495 painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
501 if ( sliderPos != -1 )
503 const int margin = 0;
506 switch ( orientation() )
509 pos = QPoint( sliderPos - ( hSize.width() / 2 ), 2 );
510 pos.rx() = qMax( margin, pos.x() );
511 pos.rx() = qMin( width() - hSize.width() - margin, pos.x() );
514 pos = QPoint( 2, height() - ( sliderPos + ( hSize.height() / 2 ) ) );
515 pos.ry() = qMax( margin, pos.y() );
516 pos.ry() = qMin( height() - hSize.height() - margin, pos.y() );
520 QPoint shadowPos( pos - QPoint( 2, 2 ) );
522 /* FIXME: precompute gradient. Anim Compatible ? */
523 QRadialGradient shadowGradient( shadowPos.x() + ( sSize.width() / 2 ),
524 shadowPos.y() + ( sSize.height() / 2 ),
525 qMax( sSize.width(), sSize.height() ) / 2 );
526 shadowGradient.setColorAt( 0.4, shadowDark );
527 shadowGradient.setColorAt( 1.0, shadowLight );
529 painter.setPen( Qt::NoPen );
530 painter.setOpacity( mHandleOpacity );
532 // draw the handle's shadow
533 painter.setBrush( shadowGradient );
534 painter.drawEllipse( shadowPos.x(), shadowPos.y() + 1, sSize.width(), sSize.height() );
536 // finally draw the handle
537 painter.setBrush( handleGradient );
538 painter.drawEllipse( pos.x(), pos.y(), hSize.width(), hSize.height() );
543 qreal SeekSlider::handleOpacity() const
545 return mHandleOpacity;
548 void SeekSlider::setHandleOpacity(qreal opacity)
550 mHandleOpacity = opacity;
551 /* Request a new paintevent */
555 void SeekSlider::hideHandle()
557 /* If pause is called while not running Qt will complain */
558 if( animHandle->state() == QAbstractAnimation::Running )
560 /* Play the animation backward */
561 animHandle->setDirection( QAbstractAnimation::Backward );
565 bool SeekSlider::isAnimationRunning() const
567 return animHandle->state() == QAbstractAnimation::Running
568 || hideHandleTimer->isActive();
571 /* This work is derived from Amarok's work under GPLv2+
575 #define WLENGTH 80 // px
576 #define WHEIGHT 22 // px
577 #define SOUNDMIN 0 // %
578 #define SOUNDMAX 200 // % OR 400 ?
580 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
582 : QAbstractSlider( _parent )
584 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
585 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
586 setMouseTracking( true );
588 b_mouseOutside = true;
591 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
593 const QPixmap temp( ":/toolbar/volslide-inside" );
594 const QBitmap mask( temp.createHeuristicMask() );
596 setFixedSize( pixOutside.size() );
598 pixGradient = QPixmap( mask.size() );
599 pixGradient2 = QPixmap( mask.size() );
601 /* Gradient building from the preferences */
602 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
603 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
605 QStringList colorList = qfu( psz_colors ).split( ";" );
608 /* Fill with 255 if the list is too short */
609 if( colorList.count() < 12 )
610 for( int i = colorList.count(); i < 12; i++)
611 colorList.append( "255" );
613 background = palette().color( QPalette::Active, QPalette::Background );
614 foreground = palette().color( QPalette::Active, QPalette::WindowText );
615 foreground.setHsv( foreground.hue(),
616 ( background.saturation() + foreground.saturation() ) / 2,
617 ( background.value() + foreground.value() ) / 2 );
619 textfont.setPixelSize( 9 );
620 textrect.setRect( 0, 0, 34, 15 );
623 #define c(i) colorList.at(i).toInt()
624 #define add_color(gradient, range, c1, c2, c3) \
625 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
627 /* Desaturated colors */
628 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
629 #define add_desaturated_color(gradient, range, c1, c2, c3) \
630 foo = new QColor( c(c1), c(c2), c(c3) );\
631 desaturate( foo ); gradient.setColorAt( range, *foo );\
634 /* combine the two helpers */
635 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
636 add_color( gradient1, range, c1, c2, c3 ); \
637 add_desaturated_color( gradient2, range, c1, c2, c3 );
640 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
641 add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
642 add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
643 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
645 painter.begin( &pixGradient );
646 painter.setPen( Qt::NoPen );
647 painter.setBrush( gradient );
648 painter.drawRect( pixGradient.rect() );
651 painter.begin( &pixGradient2 );
652 painter.setPen( Qt::NoPen );
653 painter.setBrush( gradient2 );
654 painter.drawRect( pixGradient2.rect() );
657 pixGradient.setMask( mask );
658 pixGradient2.setMask( mask );
661 void SoundSlider::wheelEvent( QWheelEvent *event )
663 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
664 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
666 emit sliderReleased();
667 emit sliderMoved( value() );
670 void SoundSlider::mousePressEvent( QMouseEvent *event )
672 if( event->button() != Qt::RightButton )
674 /* We enter the sliding mode */
676 i_oldvalue = value();
677 emit sliderPressed();
678 changeValue( event->x() - paddingL );
679 emit sliderMoved( value() );
683 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
685 if( event->button() != Qt::RightButton )
687 if( !b_mouseOutside && value() != i_oldvalue )
689 emit sliderReleased();
691 emit sliderMoved( value() );
694 b_mouseOutside = false;
698 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
702 QRect rect( paddingL - 15, -1,
703 WLENGTH + 15 * 2 , WHEIGHT + 5 );
704 if( !rect.contains( event->pos() ) )
705 { /* We are outside */
706 if ( !b_mouseOutside )
707 setValue( i_oldvalue );
708 b_mouseOutside = true;
711 { /* We are inside */
712 b_mouseOutside = false;
713 changeValue( event->x() - paddingL );
714 emit sliderMoved( value() );
719 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
720 i = __MIN( __MAX( 0, i ), maximum() );
721 setToolTip( QString("%1 %" ).arg( i ) );
725 void SoundSlider::changeValue( int x )
727 setValue( (x * maximum() + 40 ) / WLENGTH );
730 void SoundSlider::setMuted( bool m )
736 void SoundSlider::paintEvent( QPaintEvent *e )
738 QPixmap *paintGradient;
740 paintGradient = &this->pixGradient2;
742 paintGradient = &this->pixGradient;
744 painter.begin( this );
746 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
748 const QRectF boundsG( 0, 0, offset , paintGradient->height() );
749 painter.drawPixmap( boundsG, *paintGradient, boundsG );
751 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
752 painter.drawPixmap( boundsO, pixOutside, boundsO );
754 painter.setPen( foreground );
755 painter.setFont( textfont );
756 painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
757 QString::number( value() ) + '%' );