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 "util/timetooltip.hpp"
34 #include "adapters/seekpoints.hpp"
35 #include "input_manager.hpp"
36 #include "imagehelper.hpp"
38 #include <QPaintEvent>
41 #include <QStyleOptionSlider>
42 #include <QLinearGradient>
44 #include <QRadialGradient>
45 #include <QLinearGradient>
50 #include <QPropertyAnimation>
51 #include <QApplication>
54 #include <QSequentialAnimationGroup>
57 int const MIN_SLIDER_VALUE
= 0;
58 int const MAX_SLIDER_VALUE
= 10000;
60 int const CHAPTER_SPOT_SIZE
= 3;
62 int const FADE_DURATION
= 300;
63 int const FADEOUT_DELAY
= 2000;
66 SeekSlider::SeekSlider( Qt::Orientation q
, QWidget
*_parent
, bool _static
)
67 : QSlider( q
, _parent
), b_classic( _static
), animLoading( NULL
)
77 alternativeStyle
= NULL
;
79 // prepare some static colors
80 QPalette p
= palette();
81 QColor background
= p
.color( QPalette::Active
, QPalette::Window
);
82 tickpointForeground
= p
.color( QPalette::Active
, QPalette::WindowText
);
83 tickpointForeground
.setHsv( tickpointForeground
.hue(),
84 ( background
.saturation() + tickpointForeground
.saturation() ) / 2,
85 ( background
.value() + tickpointForeground
.value() ) / 2 );
87 // set the background color and gradient
88 QColor
backgroundBase( p
.window().color() );
89 backgroundGradient
.setColorAt( 0.0, backgroundBase
.darker( 140 ) );
90 backgroundGradient
.setColorAt( 1.0, backgroundBase
);
92 // set the foreground color and gradient
93 QColor
foregroundBase( 50, 156, 255 );
94 foregroundGradient
.setColorAt( 0.0, foregroundBase
);
95 foregroundGradient
.setColorAt( 1.0, foregroundBase
.darker( 140 ) );
97 // prepare the handle's gradient
98 handleGradient
.setColorAt( 0.0, p
.window().color().lighter( 120 ) );
99 handleGradient
.setColorAt( 0.9, p
.window().color().darker( 120 ) );
101 // prepare the handle's shadow gradient
102 QColor shadowBase
= p
.shadow().color();
103 if( shadowBase
.lightness() > 100 )
104 shadowBase
= QColor( 60, 60, 60 ); // Palette's shadow is too bright
105 shadowDark
= shadowBase
.darker( 150 );
106 shadowLight
= shadowBase
.lighter( 180 );
107 shadowLight
.setAlpha( 50 );
109 /* Timer used to fire intermediate updatePos() when sliding */
110 seekLimitTimer
= new QTimer( this );
111 seekLimitTimer
->setSingleShot( true );
114 mTimeTooltip
= new TimeTooltip( NULL
);
115 mTimeTooltip
->setMouseTracking( true );
118 setRange( MIN_SLIDER_VALUE
, MAX_SLIDER_VALUE
);
121 setMouseTracking( true );
123 setFocusPolicy( Qt::NoFocus
);
125 /* Use the new/classic style */
126 setMinimumHeight( 18 );
129 alternativeStyle
= new SeekStyle
;
130 setStyle( alternativeStyle
);
134 setPosition( -1.0, 0, 0 );
135 secstotimestr( psz_length
, 0 );
137 animHandle
= new QPropertyAnimation( this, "handleOpacity", this );
138 animHandle
->setDuration( FADE_DURATION
);
139 animHandle
->setStartValue( 0.0 );
140 animHandle
->setEndValue( 1.0 );
142 QPropertyAnimation
*animLoadingIn
= new QPropertyAnimation( this, "loadingProperty", this );
143 animLoadingIn
->setDuration( 2000 );
144 animLoadingIn
->setStartValue( 0.0 );
145 animLoadingIn
->setEndValue( 1.0 );
146 animLoadingIn
->setEasingCurve( QEasingCurve::OutBounce
);
147 QPropertyAnimation
*animLoadingOut
= new QPropertyAnimation( this, "loadingProperty", this );
148 animLoadingOut
->setDuration( 2000 );
149 animLoadingOut
->setStartValue( 1.0 );
150 animLoadingOut
->setEndValue( 0.0 );
151 animLoadingOut
->setEasingCurve( QEasingCurve::OutBounce
);
153 animLoading
= new QSequentialAnimationGroup( this );
154 animLoading
->addAnimation( animLoadingIn
);
155 animLoading
->addAnimation( animLoadingOut
);
156 animLoading
->setLoopCount( -1 );
158 hideHandleTimer
= new QTimer( this );
159 hideHandleTimer
->setSingleShot( true );
160 hideHandleTimer
->setInterval( FADEOUT_DELAY
);
162 startAnimLoadingTimer
= new QTimer( this );
163 startAnimLoadingTimer
->setSingleShot( true );
164 startAnimLoadingTimer
->setInterval( 500 );
166 CONNECT( MainInputManager::getInstance(), inputChanged( bool ), this , inputUpdated( bool ) );
167 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
168 CONNECT( seekLimitTimer
, timeout(), this, updatePos() );
169 CONNECT( hideHandleTimer
, timeout(), this, hideHandle() );
170 CONNECT( startAnimLoadingTimer
, timeout(), this, startAnimLoading() );
171 mTimeTooltip
->installEventFilter( this );
174 SeekSlider::~SeekSlider()
177 if ( alternativeStyle
)
178 delete alternativeStyle
;
183 * \brief Sets the chapters seekpoints adapter
185 * \params SeekPoints initilized with current intf thread
187 void SeekSlider::setChapters( SeekPoints
*chapters_
)
190 chapters
= chapters_
;
191 chapters
->setParent( this );
195 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
197 * \param pos Position, between 0 and 1. -1 disables the slider
198 * \param time Elapsed time. Unused
199 * \param legnth Duration time.
201 void SeekSlider::setPosition( float pos
, int64_t time
, int length
)
207 mTimeTooltip
->hide();
211 setEnabled( b_seekable
);
215 setValue( pos
* static_cast<float>( maximum() ) );
216 if ( animLoading
!= NULL
&& pos
>= 0.0f
&& animLoading
->state() != QAbstractAnimation::Stopped
)
224 inputLength
= length
;
227 void SeekSlider::startSeekTimer()
229 /* Only fire one update, when sliding, every 150ms */
230 if( isSliding
&& !seekLimitTimer
->isActive() )
231 seekLimitTimer
->start( 150 );
234 void SeekSlider::updatePos()
236 float f_pos
= value() / static_cast<float>( maximum() );
237 emit
sliderDragged( f_pos
); /* Send new position to VLC's core */
240 void SeekSlider::updateBuffering( float f_buffering_
)
242 if ( f_buffering_
< f_buffering
)
243 bufferingStart
= QTime::currentTime();
244 f_buffering
= f_buffering_
;
245 if ( f_buffering
> 0.0 || isEnabled() ) {
247 startAnimLoadingTimer
->stop();
253 void SeekSlider::inputUpdated( bool b_has_input
)
255 if ( b_has_input
== false ) {
257 startAnimLoadingTimer
->stop();
261 else if ( f_buffering
== 0.0 && !isEnabled() )
262 startAnimLoadingTimer
->start();
265 void SeekSlider::processReleasedButton()
267 if ( !isSliding
&& !isJumping
) return;
269 bool b_seekPending
= seekLimitTimer
->isActive();
270 seekLimitTimer
->stop(); /* We're not sliding anymore: only last seek on release */
276 if( b_seekPending
&& isEnabled() )
280 void SeekSlider::mouseReleaseEvent( QMouseEvent
*event
)
282 if ( event
->button() != Qt::LeftButton
&& event
->button() != Qt::MidButton
)
284 QSlider::mouseReleaseEvent( event
);
288 processReleasedButton();
291 void SeekSlider::mousePressEvent( QMouseEvent
* event
)
295 ( event
->button() != Qt::LeftButton
&& event
->button() != Qt::MidButton
)
298 QSlider::mousePressEvent( event
);
303 /* handle chapter clicks */
304 int i_width
= size().width();
305 if ( chapters
&& inputLength
&& i_width
)
307 if ( orientation() == Qt::Horizontal
) /* TODO: vertical */
309 /* only on chapters zone */
310 if ( event
->y() < CHAPTER_SPOT_SIZE
||
311 event
->y() > ( size().height() - CHAPTER_SPOT_SIZE
) )
313 QList
<SeekPoint
> points
= chapters
->getPoints();
315 bool b_startsnonzero
= false; /* as we always starts at 1 */
316 if ( points
.count() > 0 ) /* do we need an extra offset ? */
317 b_startsnonzero
= ( points
.at(0).time
> 0 );
318 int i_min_diff
= i_width
+ 1;
319 for( int i
= 0 ; i
< points
.count() ; i
++ )
321 int x
= points
.at(i
).time
/ 1000000.0 / inputLength
* i_width
;
322 int diff_x
= abs( x
- event
->x() );
323 if ( diff_x
< i_min_diff
)
326 i_selected
= i
+ ( ( b_startsnonzero
)? 1 : 0 );
329 if ( i_selected
&& i_min_diff
< 4 ) // max 4px around mark
331 chapters
->jumpTo( i_selected
);
342 setValue( getValueFromXPos( event
->x() ) );
343 emit
sliderMoved( value() );
347 void SeekSlider::mouseMoveEvent( QMouseEvent
*event
)
349 if ( ! ( event
->buttons() & ( Qt::LeftButton
| Qt::MidButton
) ) )
351 /* Handle button release when mouserelease has been hijacked by popup */
352 processReleasedButton();
355 if ( !isEnabled() ) return event
->accept();
359 setValue( getValueFromXPos( event
->x() ) );
360 emit
sliderMoved( value() );
364 if ( inputLength
> 0 )
366 int margin
= handleLength();
367 int posX
= qMax( rect().left() + margin
, qMin( rect().right() - margin
, event
->x() ) );
369 QString chapterLabel
;
371 if ( orientation() == Qt::Horizontal
) /* TODO: vertical */
373 QList
<SeekPoint
> points
= chapters
->getPoints();
375 for( int i
= 0 ; i
< points
.count() ; i
++ )
377 int x
= margin
+ points
.at(i
).time
/ 1000000.0 / inputLength
* (size().width() - 2*margin
);
378 if ( event
->x() >= x
)
381 if ( i_selected
>= 0 && i_selected
< points
.size() )
383 chapterLabel
= points
.at( i_selected
).name
;
387 QPoint
target( event
->globalX() - ( event
->x() - posX
),
388 QWidget::mapToGlobal( QPoint( 0, 0 ) ).y() );
389 if( likely( size().width() > handleLength() ) ) {
390 secstotimestr( psz_length
, getValuePercentageFromXPos( event
->x() ) * inputLength
);
391 mTimeTooltip
->setTip( target
, psz_length
, chapterLabel
);
397 void SeekSlider::wheelEvent( QWheelEvent
*event
)
399 /* Don't do anything if we are for somehow reason sliding */
400 if( !isSliding
&& isEnabled() )
402 setValue( value() + event
->delta() / 12 ); /* 12 = 8 * 15 / 10
403 Since delta is in 1/8 of ° and mouse have steps of 15 °
404 and that our slider is in 0.1% and we want one step to be a 1%
405 increment of position */
406 emit
sliderDragged( value() / static_cast<float>( maximum() ) );
411 void SeekSlider::enterEvent( QEvent
* )
413 /* Cancel the fade-out timer */
414 hideHandleTimer
->stop();
415 /* Only start the fade-in if needed */
416 if( isEnabled() && animHandle
->direction() != QAbstractAnimation::Forward
)
418 /* If pause is called while not running Qt will complain */
419 if( animHandle
->state() == QAbstractAnimation::Running
)
421 animHandle
->setDirection( QAbstractAnimation::Forward
);
424 /* Don't show the tooltip if the slider is disabled or a menu is open */
425 if( isEnabled() && inputLength
> 0 && !qApp
->activePopupWidget() )
426 mTimeTooltip
->show();
429 void SeekSlider::leaveEvent( QEvent
* )
431 hideHandleTimer
->start();
433 - if the mouse leave the slider rect (Note: it can still be
435 - if another window is on the way of the cursor */
436 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
437 ( !isActiveWindow() && !mTimeTooltip
->isActiveWindow() ) )
439 mTimeTooltip
->hide();
443 void SeekSlider::paintEvent( QPaintEvent
*ev
)
445 if ( alternativeStyle
)
447 SeekStyle::SeekStyleOption option
;
448 option
.initFrom( this );
449 if ( QTime::currentTime() > bufferingStart
.addSecs( 1 ) )
450 option
.buffering
= f_buffering
;
452 option
.buffering
= 0.0;
453 option
.length
= inputLength
;
454 option
.animate
= ( animHandle
->state() == QAbstractAnimation::Running
455 || hideHandleTimer
->isActive() );
456 option
.animationopacity
= mHandleOpacity
;
457 option
.animationloading
= mLoading
;
458 option
.sliderPosition
= sliderPosition();
459 option
.sliderValue
= value();
460 option
.maximum
= maximum();
461 option
.minimum
= minimum();
462 if ( chapters
) foreach( const SeekPoint
&point
, chapters
->getPoints() )
463 option
.points
<< point
.time
;
464 QPainter
painter( this );
465 style()->drawComplexControl( QStyle::CC_Slider
, &option
, &painter
, this );
468 QSlider::paintEvent( ev
);
471 void SeekSlider::hideEvent( QHideEvent
* )
473 mTimeTooltip
->hide();
476 bool SeekSlider::eventFilter( QObject
*obj
, QEvent
*event
)
478 if( obj
== mTimeTooltip
)
480 if( event
->type() == QEvent::MouseMove
)
482 QMouseEvent
* mev
= static_cast<QMouseEvent
*>( event
);
484 if( rect().contains( mapFromGlobal( mev
->globalPos() ) ) )
488 if( event
->type() == QEvent::Leave
||
489 event
->type() == QEvent::MouseMove
)
491 mTimeTooltip
->hide();
497 return QSlider::eventFilter( obj
, event
);
500 QSize
SeekSlider::sizeHint() const
503 return QSlider::sizeHint();
504 return ( orientation() == Qt::Horizontal
) ? QSize( 100, 18 )
508 qreal
SeekSlider::handleOpacity() const
510 return mHandleOpacity
;
513 qreal
SeekSlider::loading() const
518 void SeekSlider::setHandleOpacity(qreal opacity
)
520 mHandleOpacity
= opacity
;
521 /* Request a new paintevent */
525 void SeekSlider::setLoading(qreal loading
)
528 /* Request a new paintevent */
532 inline int SeekSlider::handleLength()
534 if ( mHandleLength
> 0 )
535 return mHandleLength
;
537 /* Ask for the length of the handle to the underlying style */
538 QStyleOptionSlider option
;
539 initStyleOption( &option
);
540 mHandleLength
= style()->pixelMetric( QStyle::PM_SliderLength
, &option
);
541 return mHandleLength
;
544 inline int SeekSlider::getValueFromXPos( int posX
)
546 return QStyle::sliderValueFromPosition(
547 minimum(), maximum(),
548 posX
- handleLength() / 2,
549 width() - handleLength(),
554 inline float SeekSlider::getValuePercentageFromXPos( int posX
)
556 return getValueFromXPos( posX
) / static_cast<float>( maximum() );
559 void SeekSlider::hideHandle()
561 /* If pause is called while not running Qt will complain */
562 if( animHandle
->state() == QAbstractAnimation::Running
)
564 /* Play the animation backward */
565 animHandle
->setDirection( QAbstractAnimation::Backward
);
569 void SeekSlider::startAnimLoading()
571 animLoading
->start();
574 /* This work is derived from Amarok's work under GPLv2+
578 #define WLENGTH 85 // px
579 #define WHEIGHT 26 // px
580 #define PADDINGL 6 // px
581 #define PADDINGR 6 // px
582 #define SOUNDMIN 0 // %
584 SoundSlider::SoundSlider( QWidget
*_parent
, float _i_step
,
585 char *psz_colors
, int max
)
586 : QAbstractSlider( _parent
)
588 f_step
= (float)(_i_step
* 10000)
589 / (float)((max
- SOUNDMIN
) * AOUT_VOLUME_DEFAULT
);
590 setRange( SOUNDMIN
, max
);
591 setMouseTracking( true );
593 b_mouseOutside
= true;
596 setFixedSize( WLENGTH
, WHEIGHT
);
598 pixOutside
= ImageHelper::loadSvgToPixmap(":/toolbar/volslide-outside.svg", width(), height() );
600 const QPixmap temp
= ImageHelper::loadSvgToPixmap(":/toolbar/volslide-inside.svg", width(), height() );
601 const QBitmap
mask( temp
.createHeuristicMask() );
603 pixGradient
= QPixmap( pixOutside
.size() );
604 pixGradient2
= QPixmap( pixOutside
.size() );
606 pixGradient
.setDevicePixelRatio(QApplication::primaryScreen()->devicePixelRatio());
607 pixGradient2
.setDevicePixelRatio(QApplication::primaryScreen()->devicePixelRatio());
610 /* Gradient building from the preferences */
611 QLinearGradient
gradient( PADDINGL
, 2, width() - PADDINGR
, 2 );
612 QLinearGradient
gradient2( PADDINGL
, 2, width()- PADDINGR
, 2 );
614 QStringList colorList
= qfu( psz_colors
).split( ";" );
617 /* Fill with 255 if the list is too short */
618 if( colorList
.count() < 12 )
619 for( int i
= colorList
.count(); i
< 12; i
++)
620 colorList
.append( "255" );
622 background
= palette().color( QPalette::Active
, QPalette::Window
);
623 foreground
= palette().color( QPalette::Active
, QPalette::WindowText
);
624 foreground
.setHsv( foreground
.hue(),
625 ( background
.saturation() + foreground
.saturation() ) / 2,
626 ( background
.value() + foreground
.value() ) / 2 );
628 textfont
.setPointSize( 7 );
629 textrect
.setRect( 0, 0, 34, 15 );
632 #define c(i) colorList.at(i).toInt()
633 #define add_color(gradient, range, c1, c2, c3) \
634 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
636 /* Desaturated colors */
637 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
638 #define add_desaturated_color(gradient, range, c1, c2, c3) \
639 foo = new QColor( c(c1), c(c2), c(c3) );\
640 desaturate( foo ); gradient.setColorAt( range, *foo );\
643 /* combine the two helpers */
644 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
645 add_color( gradient1, range, c1, c2, c3 ); \
646 add_desaturated_color( gradient2, range, c1, c2, c3 );
648 float f_mid_point
= ( 100.0 / maximum() );
650 add_colors( gradient
, gradient2
, 0.0, 0, 1, 2 );
651 add_colors( gradient
, gradient2
, f_mid_point
- 0.05, 3, 4, 5 );
652 add_colors( gradient
, gradient2
, f_mid_point
+ 0.05, 6, 7, 8 );
653 add_colors( gradient
, gradient2
, 1.0, 9, 10, 11 );
655 painter
.begin( &pixGradient
);
656 painter
.setPen( Qt::NoPen
);
657 painter
.setBrush( gradient
);
658 painter
.drawRect( pixGradient
.rect() );
661 painter
.begin( &pixGradient2
);
662 painter
.setPen( Qt::NoPen
);
663 painter
.setBrush( gradient2
);
664 painter
.drawRect( pixGradient2
.rect() );
667 pixGradient
.setMask( mask
);
668 pixGradient2
.setMask( mask
);
671 void SoundSlider::wheelEvent( QWheelEvent
*event
)
673 int newvalue
= value() + event
->delta() / ( 8 * 15 ) * f_step
;
674 setValue( __MIN( __MAX( minimum(), newvalue
), maximum() ) );
676 emit
sliderReleased();
677 emit
sliderMoved( value() );
680 void SoundSlider::mousePressEvent( QMouseEvent
*event
)
682 if( event
->button() != Qt::RightButton
)
684 /* We enter the sliding mode */
686 i_oldvalue
= value();
687 emit
sliderPressed();
688 changeValue( event
->x() );
689 emit
sliderMoved( value() );
693 void SoundSlider::processReleasedButton()
695 if( !b_mouseOutside
&& value() != i_oldvalue
)
697 emit
sliderReleased();
699 emit
sliderMoved( value() );
702 b_mouseOutside
= false;
705 void SoundSlider::mouseReleaseEvent( QMouseEvent
*event
)
707 if( event
->button() != Qt::RightButton
)
708 processReleasedButton();
711 void SoundSlider::mouseMoveEvent( QMouseEvent
*event
)
713 /* handle mouserelease hijacking */
714 if ( isSliding
&& ( event
->buttons() & ~Qt::RightButton
) == Qt::NoButton
)
715 processReleasedButton();
719 QRect
rect( PADDINGL
- 15, -1,
720 width() - PADDINGR
+ 15 * 2 , width() + 5 );
721 if( !rect
.contains( event
->pos() ) )
722 { /* We are outside */
723 if ( !b_mouseOutside
)
724 setValue( i_oldvalue
);
725 b_mouseOutside
= true;
728 { /* We are inside */
729 b_mouseOutside
= false;
730 changeValue( event
->x() );
731 emit
sliderMoved( value() );
736 int i
= ( ( event
->x() - PADDINGL
) * maximum() ) / ( width() - ( PADDINGR
+ PADDINGL
) );
737 i
= __MIN( __MAX( 0, i
), maximum() );
738 setToolTip( QString("%1 %" ).arg( i
) );
742 void SoundSlider::changeValue( int x
)
744 setValue( ( ( x
- PADDINGL
) * maximum() ) / ( width() - ( PADDINGR
+ PADDINGL
) ) );
747 void SoundSlider::setMuted( bool m
)
753 void SoundSlider::paintEvent( QPaintEvent
*e
)
755 QPixmap
*paintGradient
;
757 paintGradient
= &this->pixGradient2
;
759 paintGradient
= &this->pixGradient
;
761 painter
.begin( this );
763 float f_scale
= paintGradient
->width() / float( width() );
764 const int offsetDst
= int( ( ( width() - ( PADDINGR
+ PADDINGL
) ) * value() + 100 ) / maximum() ) + PADDINGL
;
765 const int offsetSrc
= int( ( ( paintGradient
->width() - ( PADDINGR
+ PADDINGL
) * f_scale
) * value() + 100 ) / maximum() + PADDINGL
* f_scale
);
767 painter
.drawPixmap( 0, 0, offsetDst
, height(), *paintGradient
, 0, 0, offsetSrc
, paintGradient
->height() );
768 painter
.drawPixmap( 0, 0, width(), height(), pixOutside
, 0, 0, pixOutside
.width(), pixOutside
.height() );
770 painter
.setPen( foreground
);
771 painter
.setFont( textfont
);
772 painter
.drawText( textrect
, Qt::AlignRight
| Qt::AlignVCenter
,
773 QString::number( value() ) + '%' );