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 )
65 alternativeStyle = NULL;
67 // prepare some static colors
68 QPalette p = palette();
69 QColor background = p.color( QPalette::Active, QPalette::Window );
70 tickpointForeground = p.color( QPalette::Active, QPalette::WindowText );
71 tickpointForeground.setHsv( tickpointForeground.hue(),
72 ( background.saturation() + tickpointForeground.saturation() ) / 2,
73 ( background.value() + tickpointForeground.value() ) / 2 );
75 // set the background color and gradient
76 QColor backgroundBase( p.window().color() );
77 backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
78 backgroundGradient.setColorAt( 1.0, backgroundBase );
80 // set the foreground color and gradient
81 QColor foregroundBase( 50, 156, 255 );
82 foregroundGradient.setColorAt( 0.0, foregroundBase );
83 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
85 // prepare the handle's gradient
86 handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
87 handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
89 // prepare the handle's shadow gradient
90 QColor shadowBase = p.shadow().color();
91 if( shadowBase.lightness() > 100 )
92 shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
93 shadowDark = shadowBase.darker( 150 );
94 shadowLight = shadowBase.lighter( 180 );
95 shadowLight.setAlpha( 50 );
97 /* Timer used to fire intermediate updatePos() when sliding */
98 seekLimitTimer = new QTimer( this );
99 seekLimitTimer->setSingleShot( true );
102 mTimeTooltip = new TimeTooltip( this );
103 mTimeTooltip->setMouseTracking( true );
106 setRange( MINIMUM, MAXIMUM );
109 setMouseTracking( true );
111 setFocusPolicy( Qt::NoFocus );
113 /* Use the new/classic style */
116 alternativeStyle = new SeekStyle;
117 setStyle( alternativeStyle );
121 setPosition( -1.0, 0, 0 );
122 secstotimestr( psz_length, 0 );
124 animHandle = new QPropertyAnimation( this, "handleOpacity", this );
125 animHandle->setDuration( FADEDURATION );
126 animHandle->setStartValue( 0.0 );
127 animHandle->setEndValue( 1.0 );
129 hideHandleTimer = new QTimer( this );
130 hideHandleTimer->setSingleShot( true );
131 hideHandleTimer->setInterval( FADEOUTDELAY );
133 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
134 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
135 CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
136 mTimeTooltip->installEventFilter( this );
139 SeekSlider::~SeekSlider()
142 if ( alternativeStyle )
143 delete alternativeStyle;
147 * \brief Sets the chapters seekpoints adapter
149 * \params SeekPoints initilized with current intf thread
151 void SeekSlider::setChapters( SeekPoints *chapters_ )
154 chapters = chapters_;
155 chapters->setParent( this );
159 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
161 * \param pos Position, between 0 and 1. -1 disables the slider
162 * \param time Elapsed time. Unused
163 * \param legnth Duration time.
165 void SeekSlider::setPosition( float pos, int64_t time, int length )
171 mTimeTooltip->hide();
175 setEnabled( b_seekable );
178 setValue( (int)( pos * 1000.0 ) );
180 inputLength = length;
183 void SeekSlider::startSeekTimer()
185 /* Only fire one update, when sliding, every 150ms */
186 if( isSliding && !seekLimitTimer->isActive() )
187 seekLimitTimer->start( 150 );
190 void SeekSlider::updatePos()
192 float f_pos = (float)( value() ) / 1000.0;
193 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
196 void SeekSlider::updateBuffering( float f_buffering_ )
198 f_buffering = f_buffering_;
202 void SeekSlider::processReleasedButton()
204 if ( !isSliding && !isJumping ) return;
206 bool b_seekPending = seekLimitTimer->isActive();
207 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
213 if( b_seekPending && isEnabled() )
217 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
219 if ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
221 QSlider::mouseReleaseEvent( event );
225 processReleasedButton();
228 void SeekSlider::mousePressEvent( QMouseEvent* event )
232 ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
235 QSlider::mousePressEvent( event );
240 /* handle chapter clicks */
241 int i_width = size().width();
242 if ( chapters && inputLength && i_width)
244 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
246 /* only on chapters zone */
247 if ( event->y() < CHAPTERSSPOTSIZE ||
248 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
250 QList<SeekPoint> points = chapters->getPoints();
252 bool b_startsnonzero = false; /* as we always starts at 1 */
253 if ( points.count() > 0 ) /* do we need an extra offset ? */
254 b_startsnonzero = ( points.at(0).time > 0 );
255 int i_min_diff = i_width + 1;
256 for( int i = 0 ; i < points.count() ; i++ )
258 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
259 int diff_x = abs( x - event->x() );
260 if ( diff_x < i_min_diff )
263 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
266 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
268 chapters->jumpTo( i_selected );
279 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false ) );
280 emit sliderMoved( value() );
284 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
286 if ( ! ( event->buttons() & ( Qt::LeftButton | Qt::MidButton ) ) )
288 /* Handle button release when mouserelease has been hijacked by popup */
289 processReleasedButton();
292 if ( !isEnabled() ) return event->accept();
296 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false) );
297 emit sliderMoved( value() );
301 if ( inputLength > 0 )
303 int margin = handleLength() / 2;
304 int posX = qMax( rect().left() + margin, qMin( rect().right() - margin, event->x() ) );
306 QString chapterLabel;
308 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
310 QList<SeekPoint> points = chapters->getPoints();
312 bool b_startsnonzero = false;
313 if ( points.count() > 0 )
314 b_startsnonzero = ( points.at(0).time > 0 );
315 for( int i = 0 ; i < points.count() ; i++ )
317 int x = points.at(i).time / 1000000.0 / inputLength * size().width();
318 if ( event->x() >= x )
319 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
321 if ( i_selected >= 0 && i_selected < points.size() )
322 chapterLabel = points.at( i_selected ).name;
325 QPoint target( event->globalX() - ( event->x() - posX ),
326 QWidget::mapToGlobal( QPoint( 0, 0 ) ).y() );
327 if( likely( size().width() > handleLength() ) ) {
328 secstotimestr( psz_length, ( ( posX - margin ) * inputLength ) / ( size().width() - handleLength() ) );
329 mTimeTooltip->setTip( target, psz_length, chapterLabel );
335 void SeekSlider::wheelEvent( QWheelEvent *event )
337 /* Don't do anything if we are for somehow reason sliding */
338 if( !isSliding && isEnabled() )
340 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
341 Since delta is in 1/8 of ° and mouse have steps of 15 °
342 and that our slider is in 0.1% and we want one step to be a 1%
343 increment of position */
344 emit sliderDragged( value() / 1000.0 );
349 void SeekSlider::enterEvent( QEvent * )
351 /* Cancel the fade-out timer */
352 hideHandleTimer->stop();
353 /* Only start the fade-in if needed */
354 if( isEnabled() && animHandle->direction() != QAbstractAnimation::Forward )
356 /* If pause is called while not running Qt will complain */
357 if( animHandle->state() == QAbstractAnimation::Running )
359 animHandle->setDirection( QAbstractAnimation::Forward );
362 /* Don't show the tooltip if the slider is disabled or a menu is open */
363 if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
364 mTimeTooltip->show();
367 void SeekSlider::leaveEvent( QEvent * )
369 hideHandleTimer->start();
371 - if the mouse leave the slider rect (Note: it can still be
373 - if another window is on the way of the cursor */
374 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
375 ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
377 mTimeTooltip->hide();
381 void SeekSlider::hideEvent( QHideEvent * )
383 mTimeTooltip->hide();
386 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
388 if( obj == mTimeTooltip )
390 if( event->type() == QEvent::Leave ||
391 event->type() == QEvent::MouseMove )
393 QMouseEvent *e = static_cast<QMouseEvent*>( event );
394 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
395 mTimeTooltip->hide();
400 return QSlider::eventFilter( obj, event );
403 QSize SeekSlider::sizeHint() const
406 return QSlider::sizeHint();
407 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
411 qreal SeekSlider::handleOpacity() const
413 return mHandleOpacity;
416 void SeekSlider::setHandleOpacity(qreal opacity)
418 mHandleOpacity = opacity;
419 /* Request a new paintevent */
423 inline int SeekSlider::handleLength()
425 if ( mHandleLength > 0 )
426 return mHandleLength;
428 /* Ask for the length of the handle to the underlying style */
429 QStyleOptionSlider option;
430 initStyleOption( &option );
431 mHandleLength = style()->pixelMetric( QStyle::PM_SliderLength, &option );
432 return mHandleLength;
435 void SeekSlider::hideHandle()
437 /* If pause is called while not running Qt will complain */
438 if( animHandle->state() == QAbstractAnimation::Running )
440 /* Play the animation backward */
441 animHandle->setDirection( QAbstractAnimation::Backward );
445 bool SeekSlider::isAnimationRunning() const
447 return animHandle->state() == QAbstractAnimation::Running
448 || hideHandleTimer->isActive();
452 /* This work is derived from Amarok's work under GPLv2+
456 #define WLENGTH 80 // px
457 #define WHEIGHT 22 // px
458 #define SOUNDMIN 0 // %
460 SoundSlider::SoundSlider( QWidget *_parent, float _i_step,
461 char *psz_colors, int max )
462 : QAbstractSlider( _parent )
464 f_step = (float)(_i_step * 10000)
465 / (float)((max - SOUNDMIN) * AOUT_VOLUME_DEFAULT);
466 setRange( SOUNDMIN, max);
467 setMouseTracking( true );
469 b_mouseOutside = true;
472 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
474 const QPixmap temp( ":/toolbar/volslide-inside" );
475 const QBitmap mask( temp.createHeuristicMask() );
477 setFixedSize( pixOutside.size() );
479 pixGradient = QPixmap( mask.size() );
480 pixGradient2 = QPixmap( mask.size() );
482 /* Gradient building from the preferences */
483 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
484 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
486 QStringList colorList = qfu( psz_colors ).split( ";" );
489 /* Fill with 255 if the list is too short */
490 if( colorList.count() < 12 )
491 for( int i = colorList.count(); i < 12; i++)
492 colorList.append( "255" );
494 background = palette().color( QPalette::Active, QPalette::Window );
495 foreground = palette().color( QPalette::Active, QPalette::WindowText );
496 foreground.setHsv( foreground.hue(),
497 ( background.saturation() + foreground.saturation() ) / 2,
498 ( background.value() + foreground.value() ) / 2 );
500 textfont.setPixelSize( 9 );
501 textrect.setRect( 0, 0, 34, 15 );
504 #define c(i) colorList.at(i).toInt()
505 #define add_color(gradient, range, c1, c2, c3) \
506 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
508 /* Desaturated colors */
509 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
510 #define add_desaturated_color(gradient, range, c1, c2, c3) \
511 foo = new QColor( c(c1), c(c2), c(c3) );\
512 desaturate( foo ); gradient.setColorAt( range, *foo );\
515 /* combine the two helpers */
516 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
517 add_color( gradient1, range, c1, c2, c3 ); \
518 add_desaturated_color( gradient2, range, c1, c2, c3 );
520 float f_mid_point = ( 100.0 / maximum() );
522 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
523 add_colors( gradient, gradient2, f_mid_point - 0.05, 3, 4, 5 );
524 add_colors( gradient, gradient2, f_mid_point + 0.05, 6, 7, 8 );
525 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
527 painter.begin( &pixGradient );
528 painter.setPen( Qt::NoPen );
529 painter.setBrush( gradient );
530 painter.drawRect( pixGradient.rect() );
533 painter.begin( &pixGradient2 );
534 painter.setPen( Qt::NoPen );
535 painter.setBrush( gradient2 );
536 painter.drawRect( pixGradient2.rect() );
539 pixGradient.setMask( mask );
540 pixGradient2.setMask( mask );
543 void SoundSlider::wheelEvent( QWheelEvent *event )
545 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
546 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
548 emit sliderReleased();
549 emit sliderMoved( value() );
552 void SoundSlider::mousePressEvent( QMouseEvent *event )
554 if( event->button() != Qt::RightButton )
556 /* We enter the sliding mode */
558 i_oldvalue = value();
559 emit sliderPressed();
560 changeValue( event->x() - paddingL );
561 emit sliderMoved( value() );
565 void SoundSlider::processReleasedButton()
567 if( !b_mouseOutside && value() != i_oldvalue )
569 emit sliderReleased();
571 emit sliderMoved( value() );
574 b_mouseOutside = false;
577 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
579 if( event->button() != Qt::RightButton )
580 processReleasedButton();
583 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
585 /* handle mouserelease hijacking */
586 if ( isSliding && ( event->buttons() & ~Qt::RightButton ) == Qt::NoButton )
587 processReleasedButton();
591 QRect rect( paddingL - 15, -1,
592 WLENGTH + 15 * 2 , WHEIGHT + 5 );
593 if( !rect.contains( event->pos() ) )
594 { /* We are outside */
595 if ( !b_mouseOutside )
596 setValue( i_oldvalue );
597 b_mouseOutside = true;
600 { /* We are inside */
601 b_mouseOutside = false;
602 changeValue( event->x() - paddingL );
603 emit sliderMoved( value() );
608 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
609 i = __MIN( __MAX( 0, i ), maximum() );
610 setToolTip( QString("%1 %" ).arg( i ) );
614 void SoundSlider::changeValue( int x )
616 setValue( (x * maximum() + 40 ) / WLENGTH );
619 void SoundSlider::setMuted( bool m )
625 void SoundSlider::paintEvent( QPaintEvent *e )
627 QPixmap *paintGradient;
629 paintGradient = &this->pixGradient2;
631 paintGradient = &this->pixGradient;
633 painter.begin( this );
635 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
637 const QRectF boundsG( 0, 0, offset , paintGradient->height() );
638 painter.drawPixmap( boundsG, *paintGradient, boundsG );
640 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
641 painter.drawPixmap( boundsO, pixOutside, boundsO );
643 painter.setPen( foreground );
644 painter.setFont( textfont );
645 painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
646 QString::number( value() ) + '%' );