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 // %
437 #define SOUNDMAX 125 // % (+6dB)
439 SoundSlider::SoundSlider( QWidget *_parent, int _i_step,
441 : QAbstractSlider( _parent )
443 f_step = (float)(_i_step * 10000)
444 / (float)((SOUNDMAX - SOUNDMIN) * AOUT_VOLUME_DEFAULT);
445 setRange( SOUNDMIN, SOUNDMAX );
446 setMouseTracking( true );
448 b_mouseOutside = true;
451 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
453 const QPixmap temp( ":/toolbar/volslide-inside" );
454 const QBitmap mask( temp.createHeuristicMask() );
456 setFixedSize( pixOutside.size() );
458 pixGradient = QPixmap( mask.size() );
459 pixGradient2 = QPixmap( mask.size() );
461 /* Gradient building from the preferences */
462 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
463 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
465 QStringList colorList = qfu( psz_colors ).split( ";" );
468 /* Fill with 255 if the list is too short */
469 if( colorList.count() < 12 )
470 for( int i = colorList.count(); i < 12; i++)
471 colorList.append( "255" );
473 background = palette().color( QPalette::Active, QPalette::Background );
474 foreground = palette().color( QPalette::Active, QPalette::WindowText );
475 foreground.setHsv( foreground.hue(),
476 ( background.saturation() + foreground.saturation() ) / 2,
477 ( background.value() + foreground.value() ) / 2 );
479 textfont.setPixelSize( 9 );
480 textrect.setRect( 0, 0, 34, 15 );
483 #define c(i) colorList.at(i).toInt()
484 #define add_color(gradient, range, c1, c2, c3) \
485 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
487 /* Desaturated colors */
488 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
489 #define add_desaturated_color(gradient, range, c1, c2, c3) \
490 foo = new QColor( c(c1), c(c2), c(c3) );\
491 desaturate( foo ); gradient.setColorAt( range, *foo );\
494 /* combine the two helpers */
495 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
496 add_color( gradient1, range, c1, c2, c3 ); \
497 add_desaturated_color( gradient2, range, c1, c2, c3 );
499 float f_mid_point = ( 100.0 / maximum() );
501 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
502 add_colors( gradient, gradient2, f_mid_point - 0.05, 3, 4, 5 );
503 add_colors( gradient, gradient2, f_mid_point + 0.05, 6, 7, 8 );
504 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
506 painter.begin( &pixGradient );
507 painter.setPen( Qt::NoPen );
508 painter.setBrush( gradient );
509 painter.drawRect( pixGradient.rect() );
512 painter.begin( &pixGradient2 );
513 painter.setPen( Qt::NoPen );
514 painter.setBrush( gradient2 );
515 painter.drawRect( pixGradient2.rect() );
518 pixGradient.setMask( mask );
519 pixGradient2.setMask( mask );
522 void SoundSlider::wheelEvent( QWheelEvent *event )
524 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
525 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
527 emit sliderReleased();
528 emit sliderMoved( value() );
531 void SoundSlider::mousePressEvent( QMouseEvent *event )
533 if( event->button() != Qt::RightButton )
535 /* We enter the sliding mode */
537 i_oldvalue = value();
538 emit sliderPressed();
539 changeValue( event->x() - paddingL );
540 emit sliderMoved( value() );
544 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
546 if( event->button() != Qt::RightButton )
548 if( !b_mouseOutside && value() != i_oldvalue )
550 emit sliderReleased();
552 emit sliderMoved( value() );
555 b_mouseOutside = false;
559 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
563 QRect rect( paddingL - 15, -1,
564 WLENGTH + 15 * 2 , WHEIGHT + 5 );
565 if( !rect.contains( event->pos() ) )
566 { /* We are outside */
567 if ( !b_mouseOutside )
568 setValue( i_oldvalue );
569 b_mouseOutside = true;
572 { /* We are inside */
573 b_mouseOutside = false;
574 changeValue( event->x() - paddingL );
575 emit sliderMoved( value() );
580 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
581 i = __MIN( __MAX( 0, i ), maximum() );
582 setToolTip( QString("%1 %" ).arg( i ) );
586 void SoundSlider::changeValue( int x )
588 setValue( (x * maximum() + 40 ) / WLENGTH );
591 void SoundSlider::setMuted( bool m )
597 void SoundSlider::paintEvent( QPaintEvent *e )
599 QPixmap *paintGradient;
601 paintGradient = &this->pixGradient2;
603 paintGradient = &this->pixGradient;
605 painter.begin( this );
607 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
609 const QRectF boundsG( 0, 0, offset , paintGradient->height() );
610 painter.drawPixmap( boundsG, *paintGradient, boundsG );
612 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
613 painter.drawPixmap( boundsO, pixOutside, boundsO );
615 painter.setPen( foreground );
616 painter.setFont( textfont );
617 painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
618 QString::number( value() ) + '%' );