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"
35 #include <QPaintEvent>
38 #include <QStyleOptionSlider>
39 #include <QLinearGradient>
41 #include <QRadialGradient>
42 #include <QLinearGradient>
47 #include <QPropertyAnimation>
48 #include <QApplication>
52 #define CHAPTERSSPOTSIZE 3
53 #define FADEDURATION 300
54 #define FADEOUTDELAY 2000
56 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
57 : QSlider( q, _parent ), b_classic( _static )
66 alternativeStyle = NULL;
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 );
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 );
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 ) );
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 ) );
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 );
98 /* Timer used to fire intermediate updatePos() when sliding */
99 seekLimitTimer = new QTimer( this );
100 seekLimitTimer->setSingleShot( true );
103 mTimeTooltip = new TimeTooltip( this );
104 mTimeTooltip->setMouseTracking( true );
107 setRange( MINIMUM, MAXIMUM );
110 setMouseTracking( true );
112 setFocusPolicy( Qt::NoFocus );
114 /* Use the new/classic style */
115 setMinimumHeight( 18 );
118 alternativeStyle = new SeekStyle;
119 setStyle( alternativeStyle );
123 setPosition( -1.0, 0, 0 );
124 secstotimestr( psz_length, 0 );
126 animHandle = new QPropertyAnimation( this, "handleOpacity", this );
127 animHandle->setDuration( FADEDURATION );
128 animHandle->setStartValue( 0.0 );
129 animHandle->setEndValue( 1.0 );
131 hideHandleTimer = new QTimer( this );
132 hideHandleTimer->setSingleShot( true );
133 hideHandleTimer->setInterval( FADEOUTDELAY );
135 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
136 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
137 CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
138 mTimeTooltip->installEventFilter( this );
141 SeekSlider::~SeekSlider()
144 if ( alternativeStyle )
145 delete alternativeStyle;
149 * \brief Sets the chapters seekpoints adapter
151 * \params SeekPoints initilized with current intf thread
153 void SeekSlider::setChapters( SeekPoints *chapters_ )
156 chapters = chapters_;
157 chapters->setParent( this );
161 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
163 * \param pos Position, between 0 and 1. -1 disables the slider
164 * \param time Elapsed time. Unused
165 * \param legnth Duration time.
167 void SeekSlider::setPosition( float pos, int64_t time, int length )
173 mTimeTooltip->hide();
177 setEnabled( b_seekable );
180 setValue( (int)( pos * 1000.0 ) );
182 inputLength = length;
185 void SeekSlider::startSeekTimer()
187 /* Only fire one update, when sliding, every 150ms */
188 if( isSliding && !seekLimitTimer->isActive() )
189 seekLimitTimer->start( 150 );
192 void SeekSlider::updatePos()
194 float f_pos = (float)( value() ) / 1000.0;
195 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
198 void SeekSlider::updateBuffering( float f_buffering_ )
200 if ( f_buffering_ < f_buffering )
201 bufferingStart = QTime::currentTime();
202 f_buffering = f_buffering_;
206 void SeekSlider::processReleasedButton()
208 if ( !isSliding && !isJumping ) return;
210 bool b_seekPending = seekLimitTimer->isActive();
211 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
217 if( b_seekPending && isEnabled() )
221 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
223 if ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
225 QSlider::mouseReleaseEvent( event );
229 processReleasedButton();
232 void SeekSlider::mousePressEvent( QMouseEvent* event )
236 ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
239 QSlider::mousePressEvent( event );
244 /* handle chapter clicks */
245 int i_width = size().width();
246 if ( chapters && inputLength && i_width)
248 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
250 /* only on chapters zone */
251 if ( event->y() < CHAPTERSSPOTSIZE ||
252 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
254 QList<SeekPoint> points = chapters->getPoints();
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++ )
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 )
267 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
270 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
272 chapters->jumpTo( i_selected );
283 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false ) );
284 emit sliderMoved( value() );
288 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
290 if ( ! ( event->buttons() & ( Qt::LeftButton | Qt::MidButton ) ) )
292 /* Handle button release when mouserelease has been hijacked by popup */
293 processReleasedButton();
296 if ( !isEnabled() ) return event->accept();
300 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false) );
301 emit sliderMoved( value() );
305 if ( inputLength > 0 )
307 int margin = handleLength() / 2;
308 int posX = qMax( rect().left() + margin, qMin( rect().right() - margin, event->x() ) );
310 QString chapterLabel;
312 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
314 QList<SeekPoint> points = chapters->getPoints();
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++ )
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 );
325 if ( i_selected >= 0 && i_selected < points.size() )
326 chapterLabel = points.at( i_selected ).name;
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 );
339 void SeekSlider::wheelEvent( QWheelEvent *event )
341 /* Don't do anything if we are for somehow reason sliding */
342 if( !isSliding && isEnabled() )
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 );
353 void SeekSlider::enterEvent( QEvent * )
355 /* Cancel the fade-out timer */
356 hideHandleTimer->stop();
357 /* Only start the fade-in if needed */
358 if( isEnabled() && animHandle->direction() != QAbstractAnimation::Forward )
360 /* If pause is called while not running Qt will complain */
361 if( animHandle->state() == QAbstractAnimation::Running )
363 animHandle->setDirection( QAbstractAnimation::Forward );
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();
371 void SeekSlider::leaveEvent( QEvent * )
373 hideHandleTimer->start();
375 - if the mouse leave the slider rect (Note: it can still be
377 - if another window is on the way of the cursor */
378 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
379 ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
381 mTimeTooltip->hide();
385 void SeekSlider::paintEvent( QPaintEvent *ev )
387 if ( alternativeStyle )
389 SeekStyle::SeekStyleOption option;
390 option.initFrom( this );
391 if ( QTime::currentTime() > bufferingStart.addSecs( 1 ) )
392 option.buffering = f_buffering;
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 );
409 QSlider::paintEvent( ev );
412 void SeekSlider::hideEvent( QHideEvent * )
414 mTimeTooltip->hide();
417 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
419 if( obj == mTimeTooltip )
421 if( event->type() == QEvent::Leave ||
422 event->type() == QEvent::MouseMove )
424 QMouseEvent *e = static_cast<QMouseEvent*>( event );
425 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
426 mTimeTooltip->hide();
431 return QSlider::eventFilter( obj, event );
434 QSize SeekSlider::sizeHint() const
437 return QSlider::sizeHint();
438 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
442 qreal SeekSlider::handleOpacity() const
444 return mHandleOpacity;
447 void SeekSlider::setHandleOpacity(qreal opacity)
449 mHandleOpacity = opacity;
450 /* Request a new paintevent */
454 inline int SeekSlider::handleLength()
456 if ( mHandleLength > 0 )
457 return mHandleLength;
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;
466 void SeekSlider::hideHandle()
468 /* If pause is called while not running Qt will complain */
469 if( animHandle->state() == QAbstractAnimation::Running )
471 /* Play the animation backward */
472 animHandle->setDirection( QAbstractAnimation::Backward );
477 /* This work is derived from Amarok's work under GPLv2+
481 #define WLENGTH 80 // px
482 #define WHEIGHT 22 // px
483 #define SOUNDMIN 0 // %
485 SoundSlider::SoundSlider( QWidget *_parent, float _i_step,
486 char *psz_colors, int max )
487 : QAbstractSlider( _parent )
489 f_step = (float)(_i_step * 10000)
490 / (float)((max - SOUNDMIN) * AOUT_VOLUME_DEFAULT);
491 setRange( SOUNDMIN, max);
492 setMouseTracking( true );
494 b_mouseOutside = true;
497 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
499 const QPixmap temp( ":/toolbar/volslide-inside" );
500 const QBitmap mask( temp.createHeuristicMask() );
502 setFixedSize( pixOutside.size() );
504 pixGradient = QPixmap( mask.size() );
505 pixGradient2 = QPixmap( mask.size() );
507 /* Gradient building from the preferences */
508 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
509 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
511 QStringList colorList = qfu( psz_colors ).split( ";" );
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" );
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 );
525 textfont.setPixelSize( 9 );
526 textrect.setRect( 0, 0, 34, 15 );
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) ) );
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 );\
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 );
545 float f_mid_point = ( 100.0 / maximum() );
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 );
552 painter.begin( &pixGradient );
553 painter.setPen( Qt::NoPen );
554 painter.setBrush( gradient );
555 painter.drawRect( pixGradient.rect() );
558 painter.begin( &pixGradient2 );
559 painter.setPen( Qt::NoPen );
560 painter.setBrush( gradient2 );
561 painter.drawRect( pixGradient2.rect() );
564 pixGradient.setMask( mask );
565 pixGradient2.setMask( mask );
568 void SoundSlider::wheelEvent( QWheelEvent *event )
570 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
571 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
573 emit sliderReleased();
574 emit sliderMoved( value() );
577 void SoundSlider::mousePressEvent( QMouseEvent *event )
579 if( event->button() != Qt::RightButton )
581 /* We enter the sliding mode */
583 i_oldvalue = value();
584 emit sliderPressed();
585 changeValue( event->x() - paddingL );
586 emit sliderMoved( value() );
590 void SoundSlider::processReleasedButton()
592 if( !b_mouseOutside && value() != i_oldvalue )
594 emit sliderReleased();
596 emit sliderMoved( value() );
599 b_mouseOutside = false;
602 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
604 if( event->button() != Qt::RightButton )
605 processReleasedButton();
608 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
610 /* handle mouserelease hijacking */
611 if ( isSliding && ( event->buttons() & ~Qt::RightButton ) == Qt::NoButton )
612 processReleasedButton();
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;
625 { /* We are inside */
626 b_mouseOutside = false;
627 changeValue( event->x() - paddingL );
628 emit sliderMoved( value() );
633 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
634 i = __MIN( __MAX( 0, i ), maximum() );
635 setToolTip( QString("%1 %" ).arg( i ) );
639 void SoundSlider::changeValue( int x )
641 setValue( (x * maximum() + 40 ) / WLENGTH );
644 void SoundSlider::setMuted( bool m )
650 void SoundSlider::paintEvent( QPaintEvent *e )
652 QPixmap *paintGradient;
654 paintGradient = &this->pixGradient2;
656 paintGradient = &this->pixGradient;
658 painter.begin( this );
660 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
662 const QRectF boundsG( 0, 0, offset , paintGradient->height() );
663 painter.drawPixmap( boundsG, *paintGradient, boundsG );
665 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
666 painter.drawPixmap( boundsO, pixOutside, boundsO );
668 painter.setPen( foreground );
669 painter.setFont( textfont );
670 painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
671 QString::number( value() ) + '%' );