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( QWidget *_parent ) : QSlider( _parent )
60 SeekSlider( Qt::Horizontal, _parent );
63 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
64 : QSlider( q, _parent )
71 /* Timer used to fire intermediate updatePos() when sliding */
72 seekLimitTimer = new QTimer( this );
73 seekLimitTimer->setSingleShot( true );
76 mTimeTooltip = new TimeTooltip( this );
77 mTimeTooltip->setMouseTracking( true );
80 setRange( MINIMUM, MAXIMUM );
83 setMouseTracking( true );
85 setFocusPolicy( Qt::NoFocus );
88 setPosition( -1.0, 0, 0 );
89 secstotimestr( psz_length, 0 );
91 animHandle = new QPropertyAnimation( this, "handleOpacity", this );
92 animHandle->setDuration( FADEDURATION );
93 animHandle->setStartValue( 0.0 );
94 animHandle->setEndValue( 1.0 );
96 hideHandleTimer = new QTimer( this );
97 hideHandleTimer->setSingleShot( true );
98 hideHandleTimer->setInterval( FADEOUTDELAY );
100 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
101 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
102 CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
103 mTimeTooltip->installEventFilter( this );
106 SeekSlider::~SeekSlider()
112 * \brief Sets the chapters seekpoints adapter
114 * \params SeekPoints initilized with current intf thread
116 void SeekSlider::setChapters( SeekPoints *chapters_ )
119 chapters = chapters_;
120 chapters->setParent( this );
124 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
126 * \param pos Position, between 0 and 1. -1 disables the slider
127 * \param time Elapsed time. Unused
128 * \param legnth Duration time.
130 void SeekSlider::setPosition( float pos, int64_t time, int length )
142 setValue( (int)( pos * 1000.0 ) );
144 inputLength = length;
147 void SeekSlider::startSeekTimer()
149 /* Only fire one update, when sliding, every 150ms */
150 if( b_isSliding && !seekLimitTimer->isActive() )
151 seekLimitTimer->start( 150 );
154 void SeekSlider::updatePos()
156 float f_pos = (float)( value() ) / 1000.0;
157 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
160 void SeekSlider::updateBuffering( float f_buffering_ )
162 f_buffering = f_buffering_;
166 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
170 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
173 b_is_jumping = false;
176 QSlider::mouseReleaseEvent( event );
180 void SeekSlider::mousePressEvent( QMouseEvent* event )
183 if( event->button() != Qt::LeftButton &&
184 event->button() != Qt::MidButton )
186 QSlider::mousePressEvent( event );
190 b_is_jumping = false;
191 /* handle chapter clicks */
192 int i_width = size().width();
193 if ( chapters && inputLength && i_width)
195 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
197 /* only on chapters zone */
198 if ( event->y() < CHAPTERSSPOTSIZE ||
199 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
201 QList<SeekPoint> points = chapters->getPoints();
203 bool b_startsnonzero = false; /* as we always starts at 1 */
204 if ( points.count() > 0 ) /* do we need an extra offset ? */
205 b_startsnonzero = ( points.at(0).time > 0 );
206 int i_min_diff = i_width + 1;
207 for( int i = 0 ; i < points.count() ; i++ )
209 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
210 int diff_x = abs( x - event->x() );
211 if ( diff_x < i_min_diff )
214 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
217 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
219 chapters->jumpTo( i_selected );
229 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
233 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
237 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
238 emit sliderMoved( value() );
242 if ( inputLength > 0 )
244 int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
246 QString chapterLabel;
247 QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
248 QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
250 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
252 QList<SeekPoint> points = chapters->getPoints();
254 bool b_startsnonzero = false;
255 if ( points.count() > 0 )
256 b_startsnonzero = ( points.at(0).time > 0 );
257 for( int i = 0 ; i < points.count() ; i++ )
259 int x = points.at(i).time / 1000000.0 / inputLength * size().width();
260 if ( event->x() >= x )
261 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
263 if ( i_selected >= 0 )
264 chapterLabel = points.at( i_selected ).name;
267 secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
268 mTimeTooltip->setText( psz_length, chapterLabel );
269 mTimeTooltip->move( p );
274 void SeekSlider::wheelEvent( QWheelEvent *event )
276 /* Don't do anything if we are for somehow reason sliding */
279 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
280 Since delta is in 1/8 of ° and mouse have steps of 15 °
281 and that our slider is in 0.1% and we want one step to be a 1%
282 increment of position */
283 emit sliderDragged( value() / 1000.0 );
288 void SeekSlider::enterEvent( QEvent * )
290 /* Cancel the fade-out timer */
291 hideHandleTimer->stop();
292 /* Only start the fade-in if needed */
293 if( animHandle->direction() != QAbstractAnimation::Forward )
295 /* If pause is called while not running Qt will complain */
296 if( animHandle->state() == QAbstractAnimation::Running )
298 animHandle->setDirection( QAbstractAnimation::Forward );
301 /* Don't show the tooltip if the slider is disabled or a menu is open */
302 if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
303 mTimeTooltip->show();
306 void SeekSlider::leaveEvent( QEvent * )
308 hideHandleTimer->start();
310 - if the mouse leave the slider rect (Note: it can still be
312 - if another window is on the way of the cursor */
313 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
314 ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
316 mTimeTooltip->hide();
320 void SeekSlider::hideEvent( QHideEvent * )
322 mTimeTooltip->hide();
325 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
327 if( obj == mTimeTooltip )
329 if( event->type() == QEvent::Leave ||
330 event->type() == QEvent::MouseMove )
332 QMouseEvent *e = static_cast<QMouseEvent*>( event );
333 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
334 mTimeTooltip->hide();
339 return QSlider::eventFilter( obj, event );
342 QSize SeekSlider::sizeHint() const
344 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
348 QSize SeekSlider::handleSize() const
350 const int size = ( orientation() == Qt::Horizontal ? height() : width() );
351 return QSize( size, size );
354 void SeekSlider::paintEvent( QPaintEvent *event )
358 QStyleOptionSlider option;
359 initStyleOption( &option );
362 QPainter painter( this );
363 painter.setRenderHints( QPainter::Antialiasing );
366 const int barCorner = 3;
367 qreal sliderPos = -1;
369 QRect barRect = rect();
371 // adjust positions based on the current orientation
372 if ( option.sliderPosition != 0 )
374 switch ( orientation() )
377 sliderPos = ( ( (qreal)width() ) / (qreal)range )
378 * (qreal)option.sliderPosition;
381 sliderPos = ( ( (qreal)height() ) / (qreal)range )
382 * (qreal)option.sliderPosition;
387 switch ( orientation() )
390 barRect.setHeight( handleSize().height() /2 );
393 barRect.setWidth( handleSize().width() /2 );
397 barRect.moveCenter( rect().center() );
399 // set the background color and gradient
400 QColor backgroundBase( 135, 135, 135 );
401 QLinearGradient backgroundGradient( 0, 0, 0, height() );
402 backgroundGradient.setColorAt( 0.0, backgroundBase );
403 backgroundGradient.setColorAt( 1.0, backgroundBase.lighter( 150 ) );
405 // set the foreground color and gradient
406 QColor foregroundBase( 50, 156, 255 );
407 QLinearGradient foregroundGradient( 0, 0, 0, height() );
408 foregroundGradient.setColorAt( 0.0, foregroundBase );
409 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
411 // draw a slight 3d effect on the bottom
412 painter.setPen( QColor( 230, 230, 230 ) );
413 painter.setBrush( Qt::NoBrush );
414 painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
417 painter.setPen( Qt::NoPen );
418 painter.setBrush( backgroundGradient );
419 painter.drawRoundedRect( barRect, barCorner, barCorner );
421 // adjusted foreground rectangle
422 QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
424 switch ( orientation() )
427 valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
430 valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
431 valueRect.moveBottom( rect().bottom() );
435 if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
438 painter.setPen( Qt::NoPen );
439 painter.setBrush( foregroundGradient );
440 painter.drawRoundedRect( valueRect, barCorner, barCorner );
443 // draw buffering overlay
444 if ( f_buffering < 1.0 )
446 QRect innerRect = barRect.adjusted( 1, 1,
447 barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
448 QColor overlayColor = QColor( "Orange" );
449 overlayColor.setAlpha( 128 );
450 painter.setBrush( overlayColor );
451 painter.drawRoundedRect( innerRect, barCorner, barCorner );
454 if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
456 /* draw chapters tickpoints */
457 if ( chapters && inputLength && size().width() )
459 QColor background = palette().color( QPalette::Active, QPalette::Background );
460 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
461 foreground.setHsv( foreground.hue(),
462 ( background.saturation() + foreground.saturation() ) / 2,
463 ( background.value() + foreground.value() ) / 2 );
464 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
466 QList<SeekPoint> points = chapters->getPoints();
467 foreach( SeekPoint point, points )
469 int x = point.time / 1000000.0 / inputLength * size().width();
470 painter.setPen( foreground );
471 painter.setBrush( Qt::NoBrush );
472 painter.drawLine( x, 0, x, CHAPTERSSPOTSIZE );
473 painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
479 if ( sliderPos != -1 )
481 const int margin = 0;
482 QSize hSize = handleSize() - QSize( 6, 6 );
485 switch ( orientation() )
488 pos = QPoint( sliderPos - ( hSize.width() / 2 ), 2 );
489 pos.rx() = qMax( margin, pos.x() );
490 pos.rx() = qMin( width() - hSize.width() - margin, pos.x() );
493 pos = QPoint( 2, height() - ( sliderPos + ( hSize.height() / 2 ) ) );
494 pos.ry() = qMax( margin, pos.y() );
495 pos.ry() = qMin( height() - hSize.height() - margin, pos.y() );
500 QPoint shadowPos( pos - QPoint( 2, 2 ) );
501 QSize sSize( handleSize() - QSize( 2, 2 ) );
503 // prepare the handle's gradient
504 QLinearGradient handleGradient( 0, 0, 0, hSize.height() );
505 handleGradient.setColorAt( 0.0, p.midlight().color() );
506 handleGradient.setColorAt( 0.9, p.mid().color() );
508 // prepare the handle's shadow gradient
509 QColor shadowDark( p.shadow().color().darker( 150 ) );
510 QColor shadowLight( p.shadow().color().lighter( 180 ) );
511 shadowLight.setAlpha( 50 );
513 QRadialGradient shadowGradient( shadowPos.x() + ( sSize.width() / 2 ),
514 shadowPos.y() + ( sSize.height() / 2 ),
515 qMax( sSize.width(), sSize.height() ) / 2 );
516 shadowGradient.setColorAt( 0.4, shadowDark );
517 shadowGradient.setColorAt( 1.0, shadowLight );
519 painter.setPen( Qt::NoPen );
520 painter.setOpacity( mHandleOpacity );
522 // draw the handle's shadow
523 painter.setBrush( shadowGradient );
524 painter.drawEllipse( shadowPos.x(), shadowPos.y() + 1, sSize.width(), sSize.height() );
526 // finally draw the handle
527 painter.setBrush( handleGradient );
528 painter.drawEllipse( pos.x(), pos.y(), hSize.width(), hSize.height() );
533 qreal SeekSlider::handleOpacity() const
535 return mHandleOpacity;
538 void SeekSlider::setHandleOpacity(qreal opacity)
540 mHandleOpacity = opacity;
541 /* Request a new paintevent */
545 void SeekSlider::hideHandle()
547 /* If pause is called while not running Qt will complain */
548 if( animHandle->state() == QAbstractAnimation::Running )
550 /* Play the animation backward */
551 animHandle->setDirection( QAbstractAnimation::Backward );
555 bool SeekSlider::isAnimationRunning() const
557 return animHandle->state() == QAbstractAnimation::Running
558 || hideHandleTimer->isActive();
561 /* This work is derived from Amarok's work under GPLv2+
565 #define WLENGTH 80 // px
566 #define WHEIGHT 22 // px
567 #define SOUNDMIN 0 // %
568 #define SOUNDMAX 200 // % OR 400 ?
570 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
572 : QAbstractSlider( _parent )
574 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
575 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
576 setMouseTracking( true );
578 b_mouseOutside = true;
581 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
583 const QPixmap temp( ":/toolbar/volslide-inside" );
584 const QBitmap mask( temp.createHeuristicMask() );
586 setFixedSize( pixOutside.size() );
588 pixGradient = QPixmap( mask.size() );
589 pixGradient2 = QPixmap( mask.size() );
591 /* Gradient building from the preferences */
592 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
593 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
595 QStringList colorList = qfu( psz_colors ).split( ";" );
598 /* Fill with 255 if the list is too short */
599 if( colorList.count() < 12 )
600 for( int i = colorList.count(); i < 12; i++)
601 colorList.append( "255" );
604 #define c(i) colorList.at(i).toInt()
605 #define add_color(gradient, range, c1, c2, c3) \
606 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
608 /* Desaturated colors */
609 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
610 #define add_desaturated_color(gradient, range, c1, c2, c3) \
611 foo = new QColor( c(c1), c(c2), c(c3) );\
612 desaturate( foo ); gradient.setColorAt( range, *foo );\
615 /* combine the two helpers */
616 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
617 add_color( gradient1, range, c1, c2, c3 ); \
618 add_desaturated_color( gradient2, range, c1, c2, c3 );
621 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
622 add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
623 add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
624 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
626 QPainter painter( &pixGradient );
627 painter.setPen( Qt::NoPen );
628 painter.setBrush( gradient );
629 painter.drawRect( pixGradient.rect() );
632 painter.begin( &pixGradient2 );
633 painter.setPen( Qt::NoPen );
634 painter.setBrush( gradient2 );
635 painter.drawRect( pixGradient2.rect() );
638 pixGradient.setMask( mask );
639 pixGradient2.setMask( mask );
642 void SoundSlider::wheelEvent( QWheelEvent *event )
644 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
645 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
647 emit sliderReleased();
648 emit sliderMoved( value() );
651 void SoundSlider::mousePressEvent( QMouseEvent *event )
653 if( event->button() != Qt::RightButton )
655 /* We enter the sliding mode */
657 i_oldvalue = value();
658 emit sliderPressed();
659 changeValue( event->x() - paddingL );
660 emit sliderMoved( value() );
664 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
666 if( event->button() != Qt::RightButton )
668 if( !b_mouseOutside && value() != i_oldvalue )
670 emit sliderReleased();
672 emit sliderMoved( value() );
675 b_mouseOutside = false;
679 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
683 QRect rect( paddingL - 15, -1,
684 WLENGTH + 15 * 2 , WHEIGHT + 5 );
685 if( !rect.contains( event->pos() ) )
686 { /* We are outside */
687 if ( !b_mouseOutside )
688 setValue( i_oldvalue );
689 b_mouseOutside = true;
692 { /* We are inside */
693 b_mouseOutside = false;
694 changeValue( event->x() - paddingL );
695 emit sliderMoved( value() );
700 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
701 i = __MIN( __MAX( 0, i ), maximum() );
702 setToolTip( QString("%1 \%" ).arg( i ) );
706 void SoundSlider::changeValue( int x )
708 setValue( (x * maximum() + 40 ) / WLENGTH );
711 void SoundSlider::setMuted( bool m )
717 void SoundSlider::paintEvent( QPaintEvent *e )
719 QPainter painter( this );
720 QPixmap *pixGradient;
722 pixGradient = &this->pixGradient2;
724 pixGradient = &this->pixGradient;
726 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
728 const QRectF boundsG( 0, 0, offset , pixGradient->height() );
729 painter.drawPixmap( boundsG, *pixGradient, boundsG );
731 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
732 painter.drawPixmap( boundsO, pixOutside, boundsO );
734 QColor background = palette().color( QPalette::Active, QPalette::Background );
735 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
736 foreground.setHsv( foreground.hue(),
737 ( background.saturation() + foreground.saturation() ) / 2,
738 ( background.value() + foreground.value() ) / 2 );
739 painter.setPen( foreground );
740 QFont font; font.setPixelSize( 9 );
741 painter.setFont( font );
742 const QRect rect( 0, 0, 34, 15 );
743 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
744 QString::number( value() ) + '%' );