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 // prepare some static colors
67 QPalette p = palette();
68 QColor background = p.color( QPalette::Active, QPalette::Background );
69 tickpointForeground = p.color( QPalette::Active, QPalette::WindowText );
70 tickpointForeground.setHsv( tickpointForeground.hue(),
71 ( background.saturation() + tickpointForeground.saturation() ) / 2,
72 ( background.value() + tickpointForeground.value() ) / 2 );
74 // set the background color and gradient
75 QColor backgroundBase( p.window().color() );
76 backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
77 backgroundGradient.setColorAt( 1.0, backgroundBase );
79 // set the foreground color and gradient
80 QColor foregroundBase( 50, 156, 255 );
81 foregroundGradient.setColorAt( 0.0, foregroundBase );
82 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
84 // prepare the handle's gradient
85 handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
86 handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
88 // prepare the handle's shadow gradient
89 QColor shadowBase = p.shadow().color();
90 if( shadowBase.lightness() > 100 )
91 shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
92 shadowDark = shadowBase.darker( 150 );
93 shadowLight = shadowBase.lighter( 180 );
94 shadowLight.setAlpha( 50 );
96 /* Timer used to fire intermediate updatePos() when sliding */
97 seekLimitTimer = new QTimer( this );
98 seekLimitTimer->setSingleShot( true );
101 mTimeTooltip = new TimeTooltip( this );
102 mTimeTooltip->setMouseTracking( true );
105 setRange( MINIMUM, MAXIMUM );
108 setMouseTracking( true );
110 setFocusPolicy( Qt::NoFocus );
112 /* Use the new/classic style */
114 setStyle( new SeekStyle );
117 setPosition( -1.0, 0, 0 );
118 secstotimestr( psz_length, 0 );
120 animHandle = new QPropertyAnimation( this, "handleOpacity", this );
121 animHandle->setDuration( FADEDURATION );
122 animHandle->setStartValue( 0.0 );
123 animHandle->setEndValue( 1.0 );
125 hideHandleTimer = new QTimer( this );
126 hideHandleTimer->setSingleShot( true );
127 hideHandleTimer->setInterval( FADEOUTDELAY );
129 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
130 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
131 CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
132 mTimeTooltip->installEventFilter( this );
135 SeekSlider::~SeekSlider()
141 * \brief Sets the chapters seekpoints adapter
143 * \params SeekPoints initilized with current intf thread
145 void SeekSlider::setChapters( SeekPoints *chapters_ )
148 chapters = chapters_;
149 chapters->setParent( this );
153 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
155 * \param pos Position, between 0 and 1. -1 disables the slider
156 * \param time Elapsed time. Unused
157 * \param legnth Duration time.
159 void SeekSlider::setPosition( float pos, int64_t time, int length )
165 mTimeTooltip->hide();
169 setEnabled( b_seekable );
172 setValue( (int)( pos * 1000.0 ) );
174 inputLength = length;
177 void SeekSlider::startSeekTimer()
179 /* Only fire one update, when sliding, every 150ms */
180 if( isSliding && !seekLimitTimer->isActive() )
181 seekLimitTimer->start( 150 );
184 void SeekSlider::updatePos()
186 float f_pos = (float)( value() ) / 1000.0;
187 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
190 void SeekSlider::updateBuffering( float f_buffering_ )
192 f_buffering = f_buffering_;
196 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
200 bool b_seekPending = seekLimitTimer->isActive();
201 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
207 QSlider::mouseReleaseEvent( event );
208 if( b_seekPending && isEnabled() )
212 void SeekSlider::mousePressEvent( QMouseEvent* event )
216 ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
219 QSlider::mousePressEvent( event );
224 /* handle chapter clicks */
225 int i_width = size().width();
226 if ( chapters && inputLength && i_width)
228 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
230 /* only on chapters zone */
231 if ( event->y() < CHAPTERSSPOTSIZE ||
232 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
234 QList<SeekPoint> points = chapters->getPoints();
236 bool b_startsnonzero = false; /* as we always starts at 1 */
237 if ( points.count() > 0 ) /* do we need an extra offset ? */
238 b_startsnonzero = ( points.at(0).time > 0 );
239 int i_min_diff = i_width + 1;
240 for( int i = 0 ; i < points.count() ; i++ )
242 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
243 int diff_x = abs( x - event->x() );
244 if ( diff_x < i_min_diff )
247 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
250 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
252 chapters->jumpTo( i_selected );
263 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false ) );
264 emit sliderMoved( value() );
268 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
270 if ( !isEnabled() ) return event->accept();
274 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false) );
275 emit sliderMoved( value() );
279 if ( inputLength > 0 )
281 int margin = handleLength() / 2;
282 int posX = qMax( rect().left() + margin, qMin( rect().right() - margin, event->x() ) );
284 QString chapterLabel;
286 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
288 QList<SeekPoint> points = chapters->getPoints();
290 bool b_startsnonzero = false;
291 if ( points.count() > 0 )
292 b_startsnonzero = ( points.at(0).time > 0 );
293 for( int i = 0 ; i < points.count() ; i++ )
295 int x = points.at(i).time / 1000000.0 / inputLength * size().width();
296 if ( event->x() >= x )
297 i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
299 if ( i_selected >= 0 && i_selected < points.size() )
300 chapterLabel = points.at( i_selected ).name;
303 QPoint target( event->globalX() - ( event->x() - posX ),
304 QWidget::mapToGlobal( QPoint( 0, 0 ) ).y() );
305 if( likely( size().width() > handleLength() ) ) {
306 secstotimestr( psz_length, ( ( posX - margin ) * inputLength ) / ( size().width() - handleLength() ) );
307 mTimeTooltip->setTip( target, psz_length, chapterLabel );
313 void SeekSlider::wheelEvent( QWheelEvent *event )
315 /* Don't do anything if we are for somehow reason sliding */
316 if( !isSliding && isEnabled() )
318 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
319 Since delta is in 1/8 of ° and mouse have steps of 15 °
320 and that our slider is in 0.1% and we want one step to be a 1%
321 increment of position */
322 emit sliderDragged( value() / 1000.0 );
327 void SeekSlider::enterEvent( QEvent * )
329 /* Cancel the fade-out timer */
330 hideHandleTimer->stop();
331 /* Only start the fade-in if needed */
332 if( isEnabled() && animHandle->direction() != QAbstractAnimation::Forward )
334 /* If pause is called while not running Qt will complain */
335 if( animHandle->state() == QAbstractAnimation::Running )
337 animHandle->setDirection( QAbstractAnimation::Forward );
340 /* Don't show the tooltip if the slider is disabled or a menu is open */
341 if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
342 mTimeTooltip->show();
345 void SeekSlider::leaveEvent( QEvent * )
347 hideHandleTimer->start();
349 - if the mouse leave the slider rect (Note: it can still be
351 - if another window is on the way of the cursor */
352 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
353 ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
355 mTimeTooltip->hide();
359 void SeekSlider::hideEvent( QHideEvent * )
361 mTimeTooltip->hide();
364 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
366 if( obj == mTimeTooltip )
368 if( event->type() == QEvent::Leave ||
369 event->type() == QEvent::MouseMove )
371 QMouseEvent *e = static_cast<QMouseEvent*>( event );
372 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
373 mTimeTooltip->hide();
378 return QSlider::eventFilter( obj, event );
381 QSize SeekSlider::sizeHint() const
384 return QSlider::sizeHint();
385 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
389 qreal SeekSlider::handleOpacity() const
391 return mHandleOpacity;
394 void SeekSlider::setHandleOpacity(qreal opacity)
396 mHandleOpacity = opacity;
397 /* Request a new paintevent */
401 inline int SeekSlider::handleLength()
403 if ( mHandleLength > 0 )
404 return mHandleLength;
406 /* Ask for the length of the handle to the underlying style */
407 QStyleOptionSlider option;
408 initStyleOption( &option );
409 mHandleLength = style()->pixelMetric( QStyle::PM_SliderLength, &option );
410 return mHandleLength;
413 void SeekSlider::hideHandle()
415 /* If pause is called while not running Qt will complain */
416 if( animHandle->state() == QAbstractAnimation::Running )
418 /* Play the animation backward */
419 animHandle->setDirection( QAbstractAnimation::Backward );
423 bool SeekSlider::isAnimationRunning() const
425 return animHandle->state() == QAbstractAnimation::Running
426 || hideHandleTimer->isActive();
430 /* This work is derived from Amarok's work under GPLv2+
434 #define WLENGTH 80 // px
435 #define WHEIGHT 22 // px
436 #define SOUNDMIN 0 // %
438 SoundSlider::SoundSlider( QWidget *_parent, int _i_step,
439 char *psz_colors, int max )
440 : QAbstractSlider( _parent )
442 f_step = (float)(_i_step * 10000)
443 / (float)((max - SOUNDMIN) * AOUT_VOLUME_DEFAULT);
444 setRange( SOUNDMIN, max);
445 setMouseTracking( true );
447 b_mouseOutside = true;
450 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
452 const QPixmap temp( ":/toolbar/volslide-inside" );
453 const QBitmap mask( temp.createHeuristicMask() );
455 setFixedSize( pixOutside.size() );
457 pixGradient = QPixmap( mask.size() );
458 pixGradient2 = QPixmap( mask.size() );
460 /* Gradient building from the preferences */
461 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
462 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
464 QStringList colorList = qfu( psz_colors ).split( ";" );
467 /* Fill with 255 if the list is too short */
468 if( colorList.count() < 12 )
469 for( int i = colorList.count(); i < 12; i++)
470 colorList.append( "255" );
472 background = palette().color( QPalette::Active, QPalette::Background );
473 foreground = palette().color( QPalette::Active, QPalette::WindowText );
474 foreground.setHsv( foreground.hue(),
475 ( background.saturation() + foreground.saturation() ) / 2,
476 ( background.value() + foreground.value() ) / 2 );
478 textfont.setPixelSize( 9 );
479 textrect.setRect( 0, 0, 34, 15 );
482 #define c(i) colorList.at(i).toInt()
483 #define add_color(gradient, range, c1, c2, c3) \
484 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
486 /* Desaturated colors */
487 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
488 #define add_desaturated_color(gradient, range, c1, c2, c3) \
489 foo = new QColor( c(c1), c(c2), c(c3) );\
490 desaturate( foo ); gradient.setColorAt( range, *foo );\
493 /* combine the two helpers */
494 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
495 add_color( gradient1, range, c1, c2, c3 ); \
496 add_desaturated_color( gradient2, range, c1, c2, c3 );
498 float f_mid_point = ( 100.0 / maximum() );
500 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
501 add_colors( gradient, gradient2, f_mid_point - 0.05, 3, 4, 5 );
502 add_colors( gradient, gradient2, f_mid_point + 0.05, 6, 7, 8 );
503 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
505 painter.begin( &pixGradient );
506 painter.setPen( Qt::NoPen );
507 painter.setBrush( gradient );
508 painter.drawRect( pixGradient.rect() );
511 painter.begin( &pixGradient2 );
512 painter.setPen( Qt::NoPen );
513 painter.setBrush( gradient2 );
514 painter.drawRect( pixGradient2.rect() );
517 pixGradient.setMask( mask );
518 pixGradient2.setMask( mask );
521 void SoundSlider::wheelEvent( QWheelEvent *event )
523 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
524 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
526 emit sliderReleased();
527 emit sliderMoved( value() );
530 void SoundSlider::mousePressEvent( QMouseEvent *event )
532 if( event->button() != Qt::RightButton )
534 /* We enter the sliding mode */
536 i_oldvalue = value();
537 emit sliderPressed();
538 changeValue( event->x() - paddingL );
539 emit sliderMoved( value() );
543 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
545 if( event->button() != Qt::RightButton )
547 if( !b_mouseOutside && value() != i_oldvalue )
549 emit sliderReleased();
551 emit sliderMoved( value() );
554 b_mouseOutside = false;
558 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
562 QRect rect( paddingL - 15, -1,
563 WLENGTH + 15 * 2 , WHEIGHT + 5 );
564 if( !rect.contains( event->pos() ) )
565 { /* We are outside */
566 if ( !b_mouseOutside )
567 setValue( i_oldvalue );
568 b_mouseOutside = true;
571 { /* We are inside */
572 b_mouseOutside = false;
573 changeValue( event->x() - paddingL );
574 emit sliderMoved( value() );
579 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
580 i = __MIN( __MAX( 0, i ), maximum() );
581 setToolTip( QString("%1 %" ).arg( i ) );
585 void SoundSlider::changeValue( int x )
587 setValue( (x * maximum() + 40 ) / WLENGTH );
590 void SoundSlider::setMuted( bool m )
596 void SoundSlider::paintEvent( QPaintEvent *e )
598 QPixmap *paintGradient;
600 paintGradient = &this->pixGradient2;
602 paintGradient = &this->pixGradient;
604 painter.begin( this );
606 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
608 const QRectF boundsG( 0, 0, offset , paintGradient->height() );
609 painter.drawPixmap( boundsG, *paintGradient, boundsG );
611 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
612 painter.drawPixmap( boundsO, pixOutside, boundsO );
614 painter.setPen( foreground );
615 painter.setFont( textfont );
616 painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
617 QString::number( value() ) + '%' );