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"
33 #include <vlc_aout_intf.h>
37 #include <QPaintEvent>
41 #include <QStyleOptionSlider>
42 #include <QLinearGradient>
44 #include <QRadialGradient>
48 #define CHAPTERSSPOTSIZE 3
50 SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent )
52 SeekSlider( Qt::Horizontal, _parent );
55 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
56 : QSlider( q, _parent )
62 /* Timer used to fire intermediate updatePos() when sliding */
63 seekLimitTimer = new QTimer( this );
64 seekLimitTimer->setSingleShot( true );
67 mTimeTooltip = new TimeTooltip( this );
68 mTimeTooltip->setMouseTracking( true );
71 setRange( MINIMUM, MAXIMUM );
74 setMouseTracking( true );
76 setFocusPolicy( Qt::NoFocus );
79 setPosition( -1.0, 0, 0 );
80 secstotimestr( psz_length, 0 );
82 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
83 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
84 mTimeTooltip->installEventFilter( this );
87 SeekSlider::~SeekSlider()
93 * \brief Sets the chapters seekpoints adapter
95 * \params SeekPoints initilized with current intf thread
97 void SeekSlider::setChapters( SeekPoints *chapters_ )
100 chapters = chapters_;
101 chapters->setParent( this );
105 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
107 * \param pos Position, between 0 and 1. -1 disables the slider
108 * \param time Elapsed time. Unused
109 * \param legnth Duration time.
111 void SeekSlider::setPosition( float pos, int64_t time, int length )
123 setValue( (int)( pos * 1000.0 ) );
125 inputLength = length;
128 void SeekSlider::startSeekTimer()
130 /* Only fire one update, when sliding, every 150ms */
131 if( b_isSliding && !seekLimitTimer->isActive() )
132 seekLimitTimer->start( 150 );
135 void SeekSlider::updatePos()
137 float f_pos = (float)( value() ) / 1000.0;
138 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
141 void SeekSlider::updateBuffering( float f_buffering_ )
143 f_buffering = f_buffering_;
147 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
151 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
154 b_is_jumping = false;
157 QSlider::mouseReleaseEvent( event );
161 void SeekSlider::mousePressEvent( QMouseEvent* event )
164 if( event->button() != Qt::LeftButton &&
165 event->button() != Qt::MidButton )
167 QSlider::mousePressEvent( event );
171 b_is_jumping = false;
172 /* handle chapter clicks */
173 int i_width = size().width();
174 if ( chapters && inputLength && i_width)
176 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
178 /* only on chapters zone */
179 if ( event->y() < CHAPTERSSPOTSIZE ||
180 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
182 QList<SeekPoint> points = chapters->getPoints();
184 int i_min_diff = i_width + 1;
185 for( int i = 0 ; i < points.count() ; i++ )
187 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
188 int diff_x = abs( x - event->x() );
189 if ( diff_x < i_min_diff )
195 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
197 chapters->jumpTo( i_selected );
207 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
211 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
215 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
216 emit sliderMoved( value() );
220 if ( inputLength > 0 )
222 int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
224 QString chapterLabel;
225 QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
226 QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
228 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
230 QList<SeekPoint> points = chapters->getPoints();
232 for( int i = 0 ; i < points.count() ; i++ )
234 int x = points.at(i).time / 1000000.0 / inputLength * size().width();
235 if ( event->x() >= x )
238 if ( i_selected >= 0 )
239 chapterLabel = points.at( i_selected ).name;
242 secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
243 mTimeTooltip->setText( psz_length, chapterLabel );
244 mTimeTooltip->move( p );
249 void SeekSlider::wheelEvent( QWheelEvent *event )
251 /* Don't do anything if we are for somehow reason sliding */
254 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
255 Since delta is in 1/8 of ° and mouse have steps of 15 °
256 and that our slider is in 0.1% and we want one step to be a 1%
257 increment of position */
258 emit sliderDragged( value() / 1000.0 );
263 void SeekSlider::enterEvent( QEvent * )
265 /* Don't show the tooltip if the slider is disabled */
266 if( isEnabled() && inputLength > 0 )
267 mTimeTooltip->show();
270 void SeekSlider::leaveEvent( QEvent * )
272 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) )
273 mTimeTooltip->hide();
276 void SeekSlider::hideEvent( QHideEvent * )
278 mTimeTooltip->hide();
281 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
283 if( obj == mTimeTooltip )
285 if( event->type() == QEvent::Leave ||
286 event->type() == QEvent::MouseMove )
288 QMouseEvent *e = static_cast<QMouseEvent*>( event );
289 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
290 mTimeTooltip->hide();
295 return QSlider::eventFilter( obj, event );
298 QSize SeekSlider::sizeHint() const
300 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
304 QSize SeekSlider::handleSize() const
306 const int size = ( orientation() == Qt::Horizontal ? height() : width() );
307 return QSize( size, size );
310 void SeekSlider::paintEvent( QPaintEvent *event )
314 QStyleOptionSlider option;
315 initStyleOption( &option );
318 QPainter painter( this );
319 painter.setRenderHints( QPainter::Antialiasing );
322 const int barCorner = 3;
323 qreal sliderPos = -1;
325 QRect barRect = rect();
327 // adjust positions based on the current orientation
328 if ( option.sliderPosition != 0 )
330 switch ( orientation() )
333 sliderPos = ( ( (qreal)width() ) / (qreal)range )
334 * (qreal)option.sliderPosition;
337 sliderPos = ( ( (qreal)height() ) / (qreal)range )
338 * (qreal)option.sliderPosition;
343 switch ( orientation() )
346 barRect.setHeight( handleSize().height() /2 );
349 barRect.setWidth( handleSize().width() /2 );
353 barRect.moveCenter( rect().center() );
355 // set the background color and gradient
356 QColor backgroundBase( 135, 135, 135 );
357 QLinearGradient backgroundGradient( 0, 0, 0, height() );
358 backgroundGradient.setColorAt( 0.0, backgroundBase );
359 backgroundGradient.setColorAt( 1.0, backgroundBase.lighter( 150 ) );
361 // set the foreground color and gradient
362 QColor foregroundBase( 50, 156, 255 );
363 QLinearGradient foregroundGradient( 0, 0, 0, height() );
364 foregroundGradient.setColorAt( 0.0, foregroundBase );
365 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
367 // draw a slight 3d effect on the bottom
368 painter.setPen( QColor( 230, 230, 230 ) );
369 painter.setBrush( Qt::NoBrush );
370 painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
373 painter.setPen( Qt::NoPen );
374 painter.setBrush( backgroundGradient );
375 painter.drawRoundedRect( barRect, barCorner, barCorner );
377 // adjusted foreground rectangle
378 QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
380 switch ( orientation() )
383 valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
386 valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
387 valueRect.moveBottom( rect().bottom() );
391 if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
394 painter.setPen( Qt::NoPen );
395 painter.setBrush( foregroundGradient );
396 painter.drawRoundedRect( valueRect, barCorner, barCorner );
399 // draw buffering overlay
400 if ( f_buffering < 1.0 )
402 QRect innerRect = barRect.adjusted( 1, 1,
403 barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
404 QColor overlayColor = QColor( "Orange" );
405 overlayColor.setAlpha( 128 );
406 painter.setBrush( overlayColor );
407 painter.drawRoundedRect( innerRect, barCorner, barCorner );
410 if ( option.state & QStyle::State_MouseOver )
412 /* draw chapters tickpoints */
413 if ( chapters && inputLength && size().width() )
415 QColor background = palette().color( QPalette::Active, QPalette::Background );
416 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
417 foreground.setHsv( foreground.hue(),
418 ( background.saturation() + foreground.saturation() ) / 2,
419 ( background.value() + foreground.value() ) / 2 );
420 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
422 QList<SeekPoint> points = chapters->getPoints();
423 foreach( SeekPoint point, points )
425 int x = point.time / 1000000.0 / inputLength * size().width();
426 painter.setPen( foreground );
427 painter.setBrush( Qt::NoBrush );
428 painter.drawLine( x, 0, x, CHAPTERSSPOTSIZE );
429 painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
435 if ( sliderPos != -1 )
437 const int margin = 0;
438 QSize hs = handleSize() - QSize( 5, 5 );
441 switch ( orientation() )
444 pos = QPoint( sliderPos - ( hs.width() / 2 ), 2 );
445 pos.rx() = qMax( margin, pos.x() );
446 pos.rx() = qMin( width() - hs.width() - margin, pos.x() );
449 pos = QPoint( 2, height() - ( sliderPos + ( hs.height() / 2 ) ) );
450 pos.ry() = qMax( margin, pos.y() );
451 pos.ry() = qMin( height() - hs.height() - margin, pos.y() );
455 QRadialGradient buttonGradient( pos.x() + ( hs.width() / 2 ) - 2,
456 pos.y() + ( hs.height() / 2 ) - 2,
457 qMax( hs.width(), hs.height() ) );
458 buttonGradient.setColorAt( 0.0, QColor( 0, 0, 0 ) );
459 buttonGradient.setColorAt( 1.0, QColor( 80, 80, 80 ) );
461 painter.setPen( Qt::NoPen );
462 painter.setBrush( buttonGradient );
463 painter.drawEllipse( pos.x(), pos.y(), hs.width(), hs.height() );
469 /* This work is derived from Amarok's work under GPLv2+
473 #define WLENGTH 80 // px
474 #define WHEIGHT 22 // px
475 #define SOUNDMIN 0 // %
476 #define SOUNDMAX 200 // % OR 400 ?
478 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
480 : QAbstractSlider( _parent )
482 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
483 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
484 setMouseTracking( true );
486 b_mouseOutside = true;
489 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
491 const QPixmap temp( ":/toolbar/volslide-inside" );
492 const QBitmap mask( temp.createHeuristicMask() );
494 setFixedSize( pixOutside.size() );
496 pixGradient = QPixmap( mask.size() );
497 pixGradient2 = QPixmap( mask.size() );
499 /* Gradient building from the preferences */
500 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
501 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
503 QStringList colorList = qfu( psz_colors ).split( ";" );
506 /* Fill with 255 if the list is too short */
507 if( colorList.count() < 12 )
508 for( int i = colorList.count(); i < 12; i++)
509 colorList.append( "255" );
512 #define c(i) colorList.at(i).toInt()
513 #define add_color(gradient, range, c1, c2, c3) \
514 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
516 /* Desaturated colors */
517 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
518 #define add_desaturated_color(gradient, range, c1, c2, c3) \
519 foo = new QColor( c(c1), c(c2), c(c3) );\
520 desaturate( foo ); gradient.setColorAt( range, *foo );\
523 /* combine the two helpers */
524 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
525 add_color( gradient1, range, c1, c2, c3 ); \
526 add_desaturated_color( gradient2, range, c1, c2, c3 );
529 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
530 add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
531 add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
532 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
534 QPainter painter( &pixGradient );
535 painter.setPen( Qt::NoPen );
536 painter.setBrush( gradient );
537 painter.drawRect( pixGradient.rect() );
540 painter.begin( &pixGradient2 );
541 painter.setPen( Qt::NoPen );
542 painter.setBrush( gradient2 );
543 painter.drawRect( pixGradient2.rect() );
546 pixGradient.setMask( mask );
547 pixGradient2.setMask( mask );
550 void SoundSlider::wheelEvent( QWheelEvent *event )
552 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
553 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
555 emit sliderReleased();
556 emit sliderMoved( value() );
559 void SoundSlider::mousePressEvent( QMouseEvent *event )
561 if( event->button() != Qt::RightButton )
563 /* We enter the sliding mode */
565 i_oldvalue = value();
566 emit sliderPressed();
567 changeValue( event->x() - paddingL );
568 emit sliderMoved( value() );
572 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
574 if( event->button() != Qt::RightButton )
576 if( !b_mouseOutside && value() != i_oldvalue )
578 emit sliderReleased();
580 emit sliderMoved( value() );
583 b_mouseOutside = false;
587 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
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 QPainter painter( this );
628 QPixmap *pixGradient;
630 pixGradient = &this->pixGradient2;
632 pixGradient = &this->pixGradient;
634 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
636 const QRectF boundsG( 0, 0, offset , pixGradient->height() );
637 painter.drawPixmap( boundsG, *pixGradient, boundsG );
639 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
640 painter.drawPixmap( boundsO, pixOutside, boundsO );
642 QColor background = palette().color( QPalette::Active, QPalette::Background );
643 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
644 foreground.setHsv( foreground.hue(),
645 ( background.saturation() + foreground.saturation() ) / 2,
646 ( background.value() + foreground.value() ) / 2 );
647 painter.setPen( foreground );
648 QFont font; font.setPixelSize( 9 );
649 painter.setFont( font );
650 const QRect rect( 0, 0, 34, 15 );
651 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
652 QString::number( value() ) + '%' );