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 )
59 : QSlider( q, _parent )
66 /* Timer used to fire intermediate updatePos() when sliding */
67 seekLimitTimer = new QTimer( this );
68 seekLimitTimer->setSingleShot( true );
71 mTimeTooltip = new TimeTooltip( this );
72 mTimeTooltip->setMouseTracking( true );
75 setRange( MINIMUM, MAXIMUM );
78 setMouseTracking( true );
80 setFocusPolicy( Qt::NoFocus );
83 setPosition( -1.0, 0, 0 );
84 secstotimestr( psz_length, 0 );
86 animHandle = new QPropertyAnimation( this, "handleOpacity", this );
87 animHandle->setDuration( FADEDURATION );
88 animHandle->setStartValue( 0.0 );
89 animHandle->setEndValue( 1.0 );
91 hideHandleTimer = new QTimer( this );
92 hideHandleTimer->setSingleShot( true );
93 hideHandleTimer->setInterval( FADEOUTDELAY );
95 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
96 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
97 CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
98 mTimeTooltip->installEventFilter( this );
101 SeekSlider::~SeekSlider()
107 * \brief Sets the chapters seekpoints adapter
109 * \params SeekPoints initilized with current intf thread
111 void SeekSlider::setChapters( SeekPoints *chapters_ )
114 chapters = chapters_;
115 chapters->setParent( this );
119 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
121 * \param pos Position, between 0 and 1. -1 disables the slider
122 * \param time Elapsed time. Unused
123 * \param legnth Duration time.
125 void SeekSlider::setPosition( float pos, int64_t time, int length )
137 setValue( (int)( pos * 1000.0 ) );
139 inputLength = length;
142 void SeekSlider::startSeekTimer()
144 /* Only fire one update, when sliding, every 150ms */
145 if( b_isSliding && !seekLimitTimer->isActive() )
146 seekLimitTimer->start( 150 );
149 void SeekSlider::updatePos()
151 float f_pos = (float)( value() ) / 1000.0;
152 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
155 void SeekSlider::updateBuffering( float f_buffering_ )
157 f_buffering = f_buffering_;
161 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
165 bool b_seekPending = seekLimitTimer->isActive();
166 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
169 b_is_jumping = false;
172 QSlider::mouseReleaseEvent( event );
177 void SeekSlider::mousePressEvent( QMouseEvent* event )
180 if( event->button() != Qt::LeftButton &&
181 event->button() != Qt::MidButton )
183 QSlider::mousePressEvent( event );
187 b_is_jumping = false;
188 /* handle chapter clicks */
189 int i_width = size().width();
190 if ( chapters && inputLength && i_width)
192 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
194 /* only on chapters zone */
195 if ( event->y() < CHAPTERSSPOTSIZE ||
196 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
198 QList<SeekPoint> points = chapters->getPoints();
200 bool b_startsnonzero = false; /* as we always starts at 1 */
201 if ( points.count() > 0 ) /* do we need an extra offset ? */
202 b_startsnonzero = ( points.at(0).time > 0 );
203 int i_min_diff = i_width + 1;
204 for( int i = 0 ; i < points.count() ; i++ )
206 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
207 int diff_x = abs( x - event->x() );
208 if ( diff_x < i_min_diff )
211 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
214 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
216 chapters->jumpTo( i_selected );
226 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
227 emit sliderMoved( value() );
231 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
235 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
236 emit sliderMoved( value() );
240 if ( inputLength > 0 )
242 int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
244 QString chapterLabel;
246 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
248 QList<SeekPoint> points = chapters->getPoints();
250 bool b_startsnonzero = false;
251 if ( points.count() > 0 )
252 b_startsnonzero = ( points.at(0).time > 0 );
253 for( int i = 0 ; i < points.count() ; i++ )
255 int x = points.at(i).time / 1000000.0 / inputLength * size().width();
256 if ( event->x() >= x )
257 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
259 if ( i_selected >= 0 && i_selected < points.size() )
260 chapterLabel = points.at( i_selected ).name;
263 QPoint target( event->globalX() - ( event->x() - posX ),
264 QWidget::mapToGlobal( pos() ).y() );
265 secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
266 mTimeTooltip->setTip( target, psz_length, chapterLabel );
271 void SeekSlider::wheelEvent( QWheelEvent *event )
273 /* Don't do anything if we are for somehow reason sliding */
276 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
277 Since delta is in 1/8 of ° and mouse have steps of 15 °
278 and that our slider is in 0.1% and we want one step to be a 1%
279 increment of position */
280 emit sliderDragged( value() / 1000.0 );
285 void SeekSlider::enterEvent( QEvent * )
287 /* Cancel the fade-out timer */
288 hideHandleTimer->stop();
289 /* Only start the fade-in if needed */
290 if( animHandle->direction() != QAbstractAnimation::Forward )
292 /* If pause is called while not running Qt will complain */
293 if( animHandle->state() == QAbstractAnimation::Running )
295 animHandle->setDirection( QAbstractAnimation::Forward );
298 /* Don't show the tooltip if the slider is disabled or a menu is open */
299 if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
300 mTimeTooltip->show();
303 void SeekSlider::leaveEvent( QEvent * )
305 hideHandleTimer->start();
307 - if the mouse leave the slider rect (Note: it can still be
309 - if another window is on the way of the cursor */
310 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
311 ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
313 mTimeTooltip->hide();
317 void SeekSlider::hideEvent( QHideEvent * )
319 mTimeTooltip->hide();
322 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
324 if( obj == mTimeTooltip )
326 if( event->type() == QEvent::Leave ||
327 event->type() == QEvent::MouseMove )
329 QMouseEvent *e = static_cast<QMouseEvent*>( event );
330 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
331 mTimeTooltip->hide();
336 return QSlider::eventFilter( obj, event );
339 QSize SeekSlider::sizeHint() const
341 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
345 QSize SeekSlider::handleSize() const
347 const int size = ( orientation() == Qt::Horizontal ? height() : width() );
348 return QSize( size, size );
351 void SeekSlider::paintEvent( QPaintEvent *event )
355 QStyleOptionSlider option;
356 initStyleOption( &option );
359 QPainter painter( this );
360 painter.setRenderHints( QPainter::Antialiasing );
363 const int barCorner = 3;
364 qreal sliderPos = -1;
366 QRect barRect = rect();
368 // adjust positions based on the current orientation
369 if ( option.sliderPosition != 0 )
371 switch ( orientation() )
374 sliderPos = ( ( (qreal)width() ) / (qreal)range )
375 * (qreal)option.sliderPosition;
378 sliderPos = ( ( (qreal)height() ) / (qreal)range )
379 * (qreal)option.sliderPosition;
384 switch ( orientation() )
387 barRect.setHeight( height() /2 );
390 barRect.setWidth( width() /2 );
394 barRect.moveCenter( rect().center() );
396 // set the background color and gradient
397 QColor backgroundBase( palette().window().color() );
398 QLinearGradient backgroundGradient( 0, 0, 0, height() );
399 backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
400 backgroundGradient.setColorAt( 1.0, backgroundBase );
402 // set the foreground color and gradient
403 QColor foregroundBase( 50, 156, 255 );
404 QLinearGradient foregroundGradient( 0, 0, 0, height() );
405 foregroundGradient.setColorAt( 0.0, foregroundBase );
406 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
408 // draw a slight 3d effect on the bottom
409 painter.setPen( QColor( 230, 230, 230 ) );
410 painter.setBrush( Qt::NoBrush );
411 painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
414 painter.setPen( Qt::NoPen );
415 painter.setBrush( backgroundGradient );
416 painter.drawRoundedRect( barRect, barCorner, barCorner );
418 // adjusted foreground rectangle
419 QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
421 switch ( orientation() )
424 valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
427 valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
428 valueRect.moveBottom( rect().bottom() );
432 if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
435 painter.setPen( Qt::NoPen );
436 painter.setBrush( foregroundGradient );
437 painter.drawRoundedRect( valueRect, barCorner, barCorner );
440 // draw buffering overlay
441 if ( f_buffering < 1.0 )
443 QRect innerRect = barRect.adjusted( 1, 1,
444 barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
445 QColor overlayColor = QColor( "Orange" );
446 overlayColor.setAlpha( 128 );
447 painter.setBrush( overlayColor );
448 painter.drawRoundedRect( innerRect, barCorner, barCorner );
451 if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
453 /* draw chapters tickpoints */
454 if ( chapters && inputLength && size().width() )
456 QColor background = palette().color( QPalette::Active, QPalette::Background );
457 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
458 foreground.setHsv( foreground.hue(),
459 ( background.saturation() + foreground.saturation() ) / 2,
460 ( background.value() + foreground.value() ) / 2 );
461 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
463 QList<SeekPoint> points = chapters->getPoints();
464 foreach( SeekPoint point, points )
466 int x = point.time / 1000000.0 / inputLength * size().width();
467 painter.setPen( foreground );
468 painter.setBrush( Qt::NoBrush );
469 painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
475 if ( sliderPos != -1 )
477 const int margin = 0;
478 QSize hSize = handleSize() - QSize( 6, 6 );
481 switch ( orientation() )
484 pos = QPoint( sliderPos - ( hSize.width() / 2 ), 2 );
485 pos.rx() = qMax( margin, pos.x() );
486 pos.rx() = qMin( width() - hSize.width() - margin, pos.x() );
489 pos = QPoint( 2, height() - ( sliderPos + ( hSize.height() / 2 ) ) );
490 pos.ry() = qMax( margin, pos.y() );
491 pos.ry() = qMin( height() - hSize.height() - margin, pos.y() );
496 QPoint shadowPos( pos - QPoint( 2, 2 ) );
497 QSize sSize( handleSize() - QSize( 2, 2 ) );
499 // prepare the handle's gradient
500 QLinearGradient handleGradient( 0, 0, 0, hSize.height() );
501 handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
502 handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
504 // prepare the handle's shadow gradient
505 QColor shadowBase = p.shadow().color();
506 if( shadowBase.lightness() > 100 )
507 shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
508 QColor shadowDark( shadowBase.darker( 150 ) );
509 QColor shadowLight( shadowBase.lighter( 180 ) );
510 shadowLight.setAlpha( 50 );
512 QRadialGradient shadowGradient( shadowPos.x() + ( sSize.width() / 2 ),
513 shadowPos.y() + ( sSize.height() / 2 ),
514 qMax( sSize.width(), sSize.height() ) / 2 );
515 shadowGradient.setColorAt( 0.4, shadowDark );
516 shadowGradient.setColorAt( 1.0, shadowLight );
518 painter.setPen( Qt::NoPen );
519 painter.setOpacity( mHandleOpacity );
521 // draw the handle's shadow
522 painter.setBrush( shadowGradient );
523 painter.drawEllipse( shadowPos.x(), shadowPos.y() + 1, sSize.width(), sSize.height() );
525 // finally draw the handle
526 painter.setBrush( handleGradient );
527 painter.drawEllipse( pos.x(), pos.y(), hSize.width(), hSize.height() );
532 qreal SeekSlider::handleOpacity() const
534 return mHandleOpacity;
537 void SeekSlider::setHandleOpacity(qreal opacity)
539 mHandleOpacity = opacity;
540 /* Request a new paintevent */
544 void SeekSlider::hideHandle()
546 /* If pause is called while not running Qt will complain */
547 if( animHandle->state() == QAbstractAnimation::Running )
549 /* Play the animation backward */
550 animHandle->setDirection( QAbstractAnimation::Backward );
554 bool SeekSlider::isAnimationRunning() const
556 return animHandle->state() == QAbstractAnimation::Running
557 || hideHandleTimer->isActive();
560 /* This work is derived from Amarok's work under GPLv2+
564 #define WLENGTH 80 // px
565 #define WHEIGHT 22 // px
566 #define SOUNDMIN 0 // %
567 #define SOUNDMAX 200 // % OR 400 ?
569 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
571 : QAbstractSlider( _parent )
573 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
574 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
575 setMouseTracking( true );
577 b_mouseOutside = true;
580 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
582 const QPixmap temp( ":/toolbar/volslide-inside" );
583 const QBitmap mask( temp.createHeuristicMask() );
585 setFixedSize( pixOutside.size() );
587 pixGradient = QPixmap( mask.size() );
588 pixGradient2 = QPixmap( mask.size() );
590 /* Gradient building from the preferences */
591 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
592 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
594 QStringList colorList = qfu( psz_colors ).split( ";" );
597 /* Fill with 255 if the list is too short */
598 if( colorList.count() < 12 )
599 for( int i = colorList.count(); i < 12; i++)
600 colorList.append( "255" );
603 #define c(i) colorList.at(i).toInt()
604 #define add_color(gradient, range, c1, c2, c3) \
605 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
607 /* Desaturated colors */
608 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
609 #define add_desaturated_color(gradient, range, c1, c2, c3) \
610 foo = new QColor( c(c1), c(c2), c(c3) );\
611 desaturate( foo ); gradient.setColorAt( range, *foo );\
614 /* combine the two helpers */
615 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
616 add_color( gradient1, range, c1, c2, c3 ); \
617 add_desaturated_color( gradient2, range, c1, c2, c3 );
620 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
621 add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
622 add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
623 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
625 QPainter painter( &pixGradient );
626 painter.setPen( Qt::NoPen );
627 painter.setBrush( gradient );
628 painter.drawRect( pixGradient.rect() );
631 painter.begin( &pixGradient2 );
632 painter.setPen( Qt::NoPen );
633 painter.setBrush( gradient2 );
634 painter.drawRect( pixGradient2.rect() );
637 pixGradient.setMask( mask );
638 pixGradient2.setMask( mask );
641 void SoundSlider::wheelEvent( QWheelEvent *event )
643 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
644 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
646 emit sliderReleased();
647 emit sliderMoved( value() );
650 void SoundSlider::mousePressEvent( QMouseEvent *event )
652 if( event->button() != Qt::RightButton )
654 /* We enter the sliding mode */
656 i_oldvalue = value();
657 emit sliderPressed();
658 changeValue( event->x() - paddingL );
659 emit sliderMoved( value() );
663 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
665 if( event->button() != Qt::RightButton )
667 if( !b_mouseOutside && value() != i_oldvalue )
669 emit sliderReleased();
671 emit sliderMoved( value() );
674 b_mouseOutside = false;
678 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
682 QRect rect( paddingL - 15, -1,
683 WLENGTH + 15 * 2 , WHEIGHT + 5 );
684 if( !rect.contains( event->pos() ) )
685 { /* We are outside */
686 if ( !b_mouseOutside )
687 setValue( i_oldvalue );
688 b_mouseOutside = true;
691 { /* We are inside */
692 b_mouseOutside = false;
693 changeValue( event->x() - paddingL );
694 emit sliderMoved( value() );
699 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
700 i = __MIN( __MAX( 0, i ), maximum() );
701 setToolTip( QString("%1 \%" ).arg( i ) );
705 void SoundSlider::changeValue( int x )
707 setValue( (x * maximum() + 40 ) / WLENGTH );
710 void SoundSlider::setMuted( bool m )
716 void SoundSlider::paintEvent( QPaintEvent *e )
718 QPainter painter( this );
719 QPixmap *pixGradient;
721 pixGradient = &this->pixGradient2;
723 pixGradient = &this->pixGradient;
725 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
727 const QRectF boundsG( 0, 0, offset , pixGradient->height() );
728 painter.drawPixmap( boundsG, *pixGradient, boundsG );
730 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
731 painter.drawPixmap( boundsO, pixOutside, boundsO );
733 QColor background = palette().color( QPalette::Active, QPalette::Background );
734 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
735 foreground.setHsv( foreground.hue(),
736 ( background.saturation() + foreground.saturation() ) / 2,
737 ( background.value() + foreground.value() ) / 2 );
738 painter.setPen( foreground );
739 QFont font; font.setPixelSize( 9 );
740 painter.setFont( font );
741 const QRect rect( 0, 0, 34, 15 );
742 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
743 QString::number( value() ) + '%' );