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>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
31 #include "util/input_slider.hpp"
32 #include "adapters/seekpoints.hpp"
36 #include <QPaintEvent>
40 #include <QStyleOptionSlider>
41 #include <QLinearGradient>
43 #include <QRadialGradient>
47 #define CHAPTERSSPOTSIZE 3
49 SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent )
51 SeekSlider( Qt::Horizontal, _parent );
54 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
55 : QSlider( q, _parent )
61 /* Timer used to fire intermediate updatePos() when sliding */
62 seekLimitTimer = new QTimer( this );
63 seekLimitTimer->setSingleShot( true );
66 mTimeTooltip = new TimeTooltip( this );
67 mTimeTooltip->setMouseTracking( true );
70 setRange( MINIMUM, MAXIMUM );
73 setMouseTracking( true );
75 setFocusPolicy( Qt::NoFocus );
78 setPosition( -1.0, 0, 0 );
79 secstotimestr( psz_length, 0 );
81 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
82 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
83 mTimeTooltip->installEventFilter( this );
86 SeekSlider::~SeekSlider()
92 * \brief Sets the chapters seekpoints adapter
94 * \params SeekPoints initilized with current intf thread
96 void SeekSlider::setChapters( SeekPoints *chapters_ )
100 chapters->setParent( this );
104 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
106 * \param pos Position, between 0 and 1. -1 disables the slider
107 * \param time Elapsed time. Unused
108 * \param legnth Duration time.
110 void SeekSlider::setPosition( float pos, int64_t time, int length )
122 setValue( (int)( pos * 1000.0 ) );
124 inputLength = length;
127 void SeekSlider::startSeekTimer()
129 /* Only fire one update, when sliding, every 150ms */
130 if( b_isSliding && !seekLimitTimer->isActive() )
131 seekLimitTimer->start( 150 );
134 void SeekSlider::updatePos()
136 float f_pos = (float)( value() ) / 1000.0;
137 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
140 void SeekSlider::updateBuffering( float f_buffering_ )
142 f_buffering = f_buffering_;
146 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
150 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
153 b_is_jumping = false;
156 QSlider::mouseReleaseEvent( event );
160 void SeekSlider::mousePressEvent( QMouseEvent* event )
163 if( event->button() != Qt::LeftButton &&
164 event->button() != Qt::MidButton )
166 QSlider::mousePressEvent( event );
170 b_is_jumping = false;
171 /* handle chapter clicks */
172 int i_width = size().width();
173 if ( chapters && inputLength && i_width)
175 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
177 /* only on chapters zone */
178 if ( event->y() < CHAPTERSSPOTSIZE ||
179 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
181 QList<SeekPoint> points = chapters->getPoints();
183 int i_min_diff = i_width + 1;
184 for( int i = 0 ; i < points.count() ; i++ )
186 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
187 int diff_x = abs( x - event->x() );
188 if ( diff_x < i_min_diff )
194 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
196 chapters->jumpTo( i_selected );
206 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
210 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
214 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
215 emit sliderMoved( value() );
219 if ( inputLength > 0 )
221 int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
223 QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
224 QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
227 secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
228 mTimeTooltip->setTime( psz_length );
229 mTimeTooltip->move( p );
234 void SeekSlider::wheelEvent( QWheelEvent *event )
236 /* Don't do anything if we are for somehow reason sliding */
239 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
240 Since delta is in 1/8 of ° and mouse have steps of 15 °
241 and that our slider is in 0.1% and we want one step to be a 1%
242 increment of position */
243 emit sliderDragged( value() / 1000.0 );
248 void SeekSlider::enterEvent( QEvent * )
250 /* Don't show the tooltip if the slider is disabled */
251 if( isEnabled() && inputLength > 0 )
252 mTimeTooltip->show();
255 void SeekSlider::leaveEvent( QEvent * )
257 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) )
258 mTimeTooltip->hide();
261 void SeekSlider::hideEvent( QHideEvent * )
263 mTimeTooltip->hide();
266 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
268 if( obj == mTimeTooltip )
270 if( event->type() == QEvent::Leave ||
271 event->type() == QEvent::MouseMove )
273 QMouseEvent *e = static_cast<QMouseEvent*>( event );
274 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
275 mTimeTooltip->hide();
280 return QSlider::eventFilter( obj, event );
283 QSize SeekSlider::sizeHint() const
285 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
289 QSize SeekSlider::handleSize() const
291 const int size = ( orientation() == Qt::Horizontal ? height() : width() );
292 return QSize( size, size );
295 void SeekSlider::paintEvent( QPaintEvent *event )
299 QStyleOptionSlider option;
300 initStyleOption( &option );
303 QPainter painter( this );
304 painter.setRenderHints( QPainter::Antialiasing );
307 const int barCorner = 3;
308 qreal sliderPos = -1;
310 QRect barRect = rect();
312 // adjust positions based on the current orientation
313 if ( option.sliderPosition != 0 )
315 switch ( orientation() )
318 sliderPos = ( ( (qreal)width() ) / (qreal)range )
319 * (qreal)option.sliderPosition;
322 sliderPos = ( ( (qreal)height() ) / (qreal)range )
323 * (qreal)option.sliderPosition;
328 switch ( orientation() )
331 barRect.setHeight( handleSize().height() /2 );
334 barRect.setWidth( handleSize().width() /2 );
338 barRect.moveCenter( rect().center() );
340 // set the background color and gradient
341 QColor backgroundBase( 135, 135, 135 );
342 QLinearGradient backgroundGradient( 0, 0, 0, height() );
343 backgroundGradient.setColorAt( 0.0, backgroundBase );
344 backgroundGradient.setColorAt( 1.0, backgroundBase.lighter( 150 ) );
346 // set the foreground color and gradient
347 QColor foregroundBase( 50, 156, 255 );
348 QLinearGradient foregroundGradient( 0, 0, 0, height() );
349 foregroundGradient.setColorAt( 0.0, foregroundBase );
350 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
352 // draw a slight 3d effect on the bottom
353 painter.setPen( QColor( 230, 230, 230 ) );
354 painter.setBrush( Qt::NoBrush );
355 painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
358 painter.setPen( Qt::NoPen );
359 painter.setBrush( backgroundGradient );
360 painter.drawRoundedRect( barRect, barCorner, barCorner );
362 // adjusted foreground rectangle
363 QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
365 switch ( orientation() )
368 valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
371 valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
372 valueRect.moveBottom( rect().bottom() );
376 if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
379 painter.setPen( Qt::NoPen );
380 painter.setBrush( foregroundGradient );
381 painter.drawRoundedRect( valueRect, barCorner, barCorner );
384 // draw buffering overlay
385 if ( f_buffering < 1.0 )
387 QRect innerRect = barRect.adjusted( 1, 1,
388 barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
389 QColor overlayColor = QColor( "Orange" );
390 overlayColor.setAlpha( 128 );
391 painter.setBrush( overlayColor );
392 painter.drawRoundedRect( innerRect, barCorner, barCorner );
395 if ( option.state & QStyle::State_MouseOver )
397 /* draw chapters tickpoints */
398 if ( chapters && inputLength && size().width() )
400 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
402 QList<SeekPoint> points = chapters->getPoints();
403 foreach( SeekPoint point, points )
405 int x = point.time / 1000000.0 / inputLength * size().width();
406 painter.setPen( QColor( 80, 80, 80 ) );
407 painter.setBrush( Qt::NoBrush );
408 painter.drawLine( x, 0, x, CHAPTERSSPOTSIZE );
409 painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
415 if ( sliderPos != -1 )
417 const int margin = 0;
418 QSize hs = handleSize() - QSize( 5, 5 );
421 switch ( orientation() )
424 pos = QPoint( sliderPos - ( hs.width() / 2 ), 2 );
425 pos.rx() = qMax( margin, pos.x() );
426 pos.rx() = qMin( width() - hs.width() - margin, pos.x() );
429 pos = QPoint( 2, height() - ( sliderPos + ( hs.height() / 2 ) ) );
430 pos.ry() = qMax( margin, pos.y() );
431 pos.ry() = qMin( height() - hs.height() - margin, pos.y() );
435 QRadialGradient buttonGradient( pos.x() + ( hs.width() / 2 ) - 2,
436 pos.y() + ( hs.height() / 2 ) - 2,
437 qMax( hs.width(), hs.height() ) );
438 buttonGradient.setColorAt( 0.0, QColor( 0, 0, 0 ) );
439 buttonGradient.setColorAt( 1.0, QColor( 80, 80, 80 ) );
441 painter.setPen( Qt::NoPen );
442 painter.setBrush( buttonGradient );
443 painter.drawEllipse( pos.x(), pos.y(), hs.width(), hs.height() );
449 /* This work is derived from Amarok's work under GPLv2+
453 #define WLENGTH 80 // px
454 #define WHEIGHT 22 // px
455 #define SOUNDMIN 0 // %
456 #define SOUNDMAX 200 // % OR 400 ?
458 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
460 : QAbstractSlider( _parent )
462 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
463 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
464 setMouseTracking( true );
466 b_mouseOutside = true;
469 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
471 const QPixmap temp( ":/toolbar/volslide-inside" );
472 const QBitmap mask( temp.createHeuristicMask() );
474 setFixedSize( pixOutside.size() );
476 pixGradient = QPixmap( mask.size() );
477 pixGradient2 = QPixmap( mask.size() );
479 /* Gradient building from the preferences */
480 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
481 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
483 QStringList colorList = qfu( psz_colors ).split( ";" );
486 /* Fill with 255 if the list is too short */
487 if( colorList.size() < 12 )
488 for( int i = colorList.size(); i < 12; i++)
489 colorList.append( "255" );
492 #define c(i) colorList.at(i).toInt()
493 #define add_color(gradient, range, c1, c2, c3) \
494 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
496 /* Desaturated colors */
497 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
498 #define add_desaturated_color(gradient, range, c1, c2, c3) \
499 foo = new QColor( c(c1), c(c2), c(c3) );\
500 desaturate( foo ); gradient.setColorAt( range, *foo );\
503 /* combine the two helpers */
504 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
505 add_color( gradient1, range, c1, c2, c3 ); \
506 add_desaturated_color( gradient2, range, c1, c2, c3 );
509 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
510 add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
511 add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
512 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
514 QPainter painter( &pixGradient );
515 painter.setPen( Qt::NoPen );
516 painter.setBrush( gradient );
517 painter.drawRect( pixGradient.rect() );
520 painter.begin( &pixGradient2 );
521 painter.setPen( Qt::NoPen );
522 painter.setBrush( gradient2 );
523 painter.drawRect( pixGradient2.rect() );
526 pixGradient.setMask( mask );
527 pixGradient2.setMask( mask );
530 void SoundSlider::wheelEvent( QWheelEvent *event )
532 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
533 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
535 emit sliderReleased();
536 emit sliderMoved( value() );
539 void SoundSlider::mousePressEvent( QMouseEvent *event )
541 if( event->button() != Qt::RightButton )
543 /* We enter the sliding mode */
545 i_oldvalue = value();
546 emit sliderPressed();
547 changeValue( event->x() - paddingL );
548 emit sliderMoved( value() );
552 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
554 if( event->button() != Qt::RightButton )
556 if( !b_mouseOutside && value() != i_oldvalue )
558 emit sliderReleased();
560 emit sliderMoved( value() );
563 b_mouseOutside = false;
567 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
571 QRect rect( paddingL - 15, -1,
572 WLENGTH + 15 * 2 , WHEIGHT + 5 );
573 if( !rect.contains( event->pos() ) )
574 { /* We are outside */
575 if ( !b_mouseOutside )
576 setValue( i_oldvalue );
577 b_mouseOutside = true;
580 { /* We are inside */
581 b_mouseOutside = false;
582 changeValue( event->x() - paddingL );
583 emit sliderMoved( value() );
588 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
589 i = __MIN( __MAX( 0, i ), maximum() );
590 setToolTip( QString("%1 \%" ).arg( i ) );
594 void SoundSlider::changeValue( int x )
596 setValue( (x * maximum() + 40 ) / WLENGTH );
599 void SoundSlider::setMuted( bool m )
605 void SoundSlider::paintEvent( QPaintEvent *e )
607 QPainter painter( this );
608 QPixmap *pixGradient;
610 pixGradient = &this->pixGradient2;
612 pixGradient = &this->pixGradient;
614 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
616 const QRectF boundsG( 0, 0, offset , pixGradient->height() );
617 painter.drawPixmap( boundsG, *pixGradient, boundsG );
619 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
620 painter.drawPixmap( boundsO, pixOutside, boundsO );
622 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
623 QFont font; font.setPixelSize( 9 );
624 painter.setFont( font );
625 const QRect rect( 0, 0, 34, 15 );
626 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
627 QString::number( value() ) + '%' );