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>
35 #include <QPaintEvent>
39 #include <QStyleOptionSlider>
40 #include <QLinearGradient>
42 #include <QRadialGradient>
46 #define CHAPTERSSPOTSIZE 3
48 SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent )
50 SeekSlider( Qt::Horizontal, _parent );
53 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
54 : QSlider( q, _parent )
60 /* Timer used to fire intermediate updatePos() when sliding */
61 seekLimitTimer = new QTimer( this );
62 seekLimitTimer->setSingleShot( true );
65 mTimeTooltip = new TimeTooltip( this );
66 mTimeTooltip->setMouseTracking( true );
69 setRange( MINIMUM, MAXIMUM );
72 setMouseTracking( true );
74 setFocusPolicy( Qt::NoFocus );
77 setPosition( -1.0, 0, 0 );
78 secstotimestr( psz_length, 0 );
80 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
81 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
82 mTimeTooltip->installEventFilter( this );
85 SeekSlider::~SeekSlider()
91 * \brief Sets the chapters seekpoints adapter
93 * \params SeekPoints initilized with current intf thread
95 void SeekSlider::setChapters( SeekPoints *chapters_ )
99 chapters->setParent( this );
103 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
105 * \param pos Position, between 0 and 1. -1 disables the slider
106 * \param time Elapsed time. Unused
107 * \param legnth Duration time.
109 void SeekSlider::setPosition( float pos, int64_t time, int length )
121 setValue( (int)( pos * 1000.0 ) );
123 inputLength = length;
126 void SeekSlider::startSeekTimer()
128 /* Only fire one update, when sliding, every 150ms */
129 if( b_isSliding && !seekLimitTimer->isActive() )
130 seekLimitTimer->start( 150 );
133 void SeekSlider::updatePos()
135 float f_pos = (float)( value() ) / 1000.0;
136 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
139 void SeekSlider::updateBuffering( float f_buffering_ )
141 f_buffering = f_buffering_;
145 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
149 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
152 b_is_jumping = false;
155 QSlider::mouseReleaseEvent( event );
159 void SeekSlider::mousePressEvent( QMouseEvent* event )
162 if( event->button() != Qt::LeftButton &&
163 event->button() != Qt::MidButton )
165 QSlider::mousePressEvent( event );
169 b_is_jumping = false;
170 /* handle chapter clicks */
171 int i_width = size().width();
172 if ( chapters && inputLength && i_width)
174 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
176 /* only on chapters zone */
177 if ( event->y() < CHAPTERSSPOTSIZE ||
178 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
180 QList<SeekPoint> points = chapters->getPoints();
182 int i_min_diff = i_width + 1;
183 for( int i = 0 ; i < points.count() ; i++ )
185 int x = points.at(i).time / 1000000.0 / inputLength * i_width;
186 int diff_x = abs( x - event->x() );
187 if ( diff_x < i_min_diff )
193 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
195 chapters->jumpTo( i_selected );
205 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
209 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
213 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
214 emit sliderMoved( value() );
218 if ( inputLength > 0 )
220 int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
222 QString chapterLabel;
223 QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
224 QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
226 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
228 QList<SeekPoint> points = chapters->getPoints();
230 for( int i = 0 ; i < points.count() ; i++ )
232 int x = points.at(i).time / 1000000.0 / inputLength * size().width();
233 if ( event->x() >= x )
236 if ( i_selected >= 0 )
237 chapterLabel = points.at( i_selected ).name;
240 secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
241 mTimeTooltip->setText( psz_length, chapterLabel );
242 mTimeTooltip->move( p );
247 void SeekSlider::wheelEvent( QWheelEvent *event )
249 /* Don't do anything if we are for somehow reason sliding */
252 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
253 Since delta is in 1/8 of ° and mouse have steps of 15 °
254 and that our slider is in 0.1% and we want one step to be a 1%
255 increment of position */
256 emit sliderDragged( value() / 1000.0 );
261 void SeekSlider::enterEvent( QEvent * )
263 /* Don't show the tooltip if the slider is disabled */
264 if( isEnabled() && inputLength > 0 )
265 mTimeTooltip->show();
268 void SeekSlider::leaveEvent( QEvent * )
270 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) )
271 mTimeTooltip->hide();
274 void SeekSlider::hideEvent( QHideEvent * )
276 mTimeTooltip->hide();
279 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
281 if( obj == mTimeTooltip )
283 if( event->type() == QEvent::Leave ||
284 event->type() == QEvent::MouseMove )
286 QMouseEvent *e = static_cast<QMouseEvent*>( event );
287 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
288 mTimeTooltip->hide();
293 return QSlider::eventFilter( obj, event );
296 QSize SeekSlider::sizeHint() const
298 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
302 QSize SeekSlider::handleSize() const
304 const int size = ( orientation() == Qt::Horizontal ? height() : width() );
305 return QSize( size, size );
308 void SeekSlider::paintEvent( QPaintEvent *event )
312 QStyleOptionSlider option;
313 initStyleOption( &option );
316 QPainter painter( this );
317 painter.setRenderHints( QPainter::Antialiasing );
320 const int barCorner = 3;
321 qreal sliderPos = -1;
323 QRect barRect = rect();
325 // adjust positions based on the current orientation
326 if ( option.sliderPosition != 0 )
328 switch ( orientation() )
331 sliderPos = ( ( (qreal)width() ) / (qreal)range )
332 * (qreal)option.sliderPosition;
335 sliderPos = ( ( (qreal)height() ) / (qreal)range )
336 * (qreal)option.sliderPosition;
341 switch ( orientation() )
344 barRect.setHeight( handleSize().height() /2 );
347 barRect.setWidth( handleSize().width() /2 );
351 barRect.moveCenter( rect().center() );
353 // set the background color and gradient
354 QColor backgroundBase( 135, 135, 135 );
355 QLinearGradient backgroundGradient( 0, 0, 0, height() );
356 backgroundGradient.setColorAt( 0.0, backgroundBase );
357 backgroundGradient.setColorAt( 1.0, backgroundBase.lighter( 150 ) );
359 // set the foreground color and gradient
360 QColor foregroundBase( 50, 156, 255 );
361 QLinearGradient foregroundGradient( 0, 0, 0, height() );
362 foregroundGradient.setColorAt( 0.0, foregroundBase );
363 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
365 // draw a slight 3d effect on the bottom
366 painter.setPen( QColor( 230, 230, 230 ) );
367 painter.setBrush( Qt::NoBrush );
368 painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
371 painter.setPen( Qt::NoPen );
372 painter.setBrush( backgroundGradient );
373 painter.drawRoundedRect( barRect, barCorner, barCorner );
375 // adjusted foreground rectangle
376 QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
378 switch ( orientation() )
381 valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
384 valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
385 valueRect.moveBottom( rect().bottom() );
389 if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
392 painter.setPen( Qt::NoPen );
393 painter.setBrush( foregroundGradient );
394 painter.drawRoundedRect( valueRect, barCorner, barCorner );
397 // draw buffering overlay
398 if ( f_buffering < 1.0 )
400 QRect innerRect = barRect.adjusted( 1, 1,
401 barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
402 QColor overlayColor = QColor( "Orange" );
403 overlayColor.setAlpha( 128 );
404 painter.setBrush( overlayColor );
405 painter.drawRoundedRect( innerRect, barCorner, barCorner );
408 if ( option.state & QStyle::State_MouseOver )
410 /* draw chapters tickpoints */
411 if ( chapters && inputLength && size().width() )
413 QColor background = palette().color( QPalette::Active, QPalette::Background );
414 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
415 foreground.setHsv( foreground.hue(),
416 ( background.saturation() + foreground.saturation() ) / 2,
417 ( background.value() + foreground.value() ) / 2 );
418 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
420 QList<SeekPoint> points = chapters->getPoints();
421 foreach( SeekPoint point, points )
423 int x = point.time / 1000000.0 / inputLength * size().width();
424 painter.setPen( foreground );
425 painter.setBrush( Qt::NoBrush );
426 painter.drawLine( x, 0, x, CHAPTERSSPOTSIZE );
427 painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
433 if ( sliderPos != -1 )
435 const int margin = 0;
436 QSize hs = handleSize() - QSize( 5, 5 );
439 switch ( orientation() )
442 pos = QPoint( sliderPos - ( hs.width() / 2 ), 2 );
443 pos.rx() = qMax( margin, pos.x() );
444 pos.rx() = qMin( width() - hs.width() - margin, pos.x() );
447 pos = QPoint( 2, height() - ( sliderPos + ( hs.height() / 2 ) ) );
448 pos.ry() = qMax( margin, pos.y() );
449 pos.ry() = qMin( height() - hs.height() - margin, pos.y() );
453 QRadialGradient buttonGradient( pos.x() + ( hs.width() / 2 ) - 2,
454 pos.y() + ( hs.height() / 2 ) - 2,
455 qMax( hs.width(), hs.height() ) );
456 buttonGradient.setColorAt( 0.0, QColor( 0, 0, 0 ) );
457 buttonGradient.setColorAt( 1.0, QColor( 80, 80, 80 ) );
459 painter.setPen( Qt::NoPen );
460 painter.setBrush( buttonGradient );
461 painter.drawEllipse( pos.x(), pos.y(), hs.width(), hs.height() );
467 /* This work is derived from Amarok's work under GPLv2+
471 #define WLENGTH 80 // px
472 #define WHEIGHT 22 // px
473 #define SOUNDMIN 0 // %
474 #define SOUNDMAX 200 // % OR 400 ?
476 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
478 : QAbstractSlider( _parent )
480 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
481 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
482 setMouseTracking( true );
484 b_mouseOutside = true;
487 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
489 const QPixmap temp( ":/toolbar/volslide-inside" );
490 const QBitmap mask( temp.createHeuristicMask() );
492 setFixedSize( pixOutside.size() );
494 pixGradient = QPixmap( mask.size() );
495 pixGradient2 = QPixmap( mask.size() );
497 /* Gradient building from the preferences */
498 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
499 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
501 QStringList colorList = qfu( psz_colors ).split( ";" );
504 /* Fill with 255 if the list is too short */
505 if( colorList.count() < 12 )
506 for( int i = colorList.count(); i < 12; i++)
507 colorList.append( "255" );
510 #define c(i) colorList.at(i).toInt()
511 #define add_color(gradient, range, c1, c2, c3) \
512 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
514 /* Desaturated colors */
515 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
516 #define add_desaturated_color(gradient, range, c1, c2, c3) \
517 foo = new QColor( c(c1), c(c2), c(c3) );\
518 desaturate( foo ); gradient.setColorAt( range, *foo );\
521 /* combine the two helpers */
522 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
523 add_color( gradient1, range, c1, c2, c3 ); \
524 add_desaturated_color( gradient2, range, c1, c2, c3 );
527 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
528 add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
529 add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
530 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
532 QPainter painter( &pixGradient );
533 painter.setPen( Qt::NoPen );
534 painter.setBrush( gradient );
535 painter.drawRect( pixGradient.rect() );
538 painter.begin( &pixGradient2 );
539 painter.setPen( Qt::NoPen );
540 painter.setBrush( gradient2 );
541 painter.drawRect( pixGradient2.rect() );
544 pixGradient.setMask( mask );
545 pixGradient2.setMask( mask );
548 void SoundSlider::wheelEvent( QWheelEvent *event )
550 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
551 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
553 emit sliderReleased();
554 emit sliderMoved( value() );
557 void SoundSlider::mousePressEvent( QMouseEvent *event )
559 if( event->button() != Qt::RightButton )
561 /* We enter the sliding mode */
563 i_oldvalue = value();
564 emit sliderPressed();
565 changeValue( event->x() - paddingL );
566 emit sliderMoved( value() );
570 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
572 if( event->button() != Qt::RightButton )
574 if( !b_mouseOutside && value() != i_oldvalue )
576 emit sliderReleased();
578 emit sliderMoved( value() );
581 b_mouseOutside = false;
585 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
589 QRect rect( paddingL - 15, -1,
590 WLENGTH + 15 * 2 , WHEIGHT + 5 );
591 if( !rect.contains( event->pos() ) )
592 { /* We are outside */
593 if ( !b_mouseOutside )
594 setValue( i_oldvalue );
595 b_mouseOutside = true;
598 { /* We are inside */
599 b_mouseOutside = false;
600 changeValue( event->x() - paddingL );
601 emit sliderMoved( value() );
606 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
607 i = __MIN( __MAX( 0, i ), maximum() );
608 setToolTip( QString("%1 \%" ).arg( i ) );
612 void SoundSlider::changeValue( int x )
614 setValue( (x * maximum() + 40 ) / WLENGTH );
617 void SoundSlider::setMuted( bool m )
623 void SoundSlider::paintEvent( QPaintEvent *e )
625 QPainter painter( this );
626 QPixmap *pixGradient;
628 pixGradient = &this->pixGradient2;
630 pixGradient = &this->pixGradient;
632 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
634 const QRectF boundsG( 0, 0, offset , pixGradient->height() );
635 painter.drawPixmap( boundsG, *pixGradient, boundsG );
637 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
638 painter.drawPixmap( boundsO, pixOutside, boundsO );
640 QColor background = palette().color( QPalette::Active, QPalette::Background );
641 QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
642 foreground.setHsv( foreground.hue(),
643 ( background.saturation() + foreground.saturation() ) / 2,
644 ( background.value() + foreground.value() ) / 2 );
645 painter.setPen( foreground );
646 QFont font; font.setPixelSize( 9 );
647 painter.setFont( font );
648 const QRect rect( 0, 0, 34, 15 );
649 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
650 QString::number( value() ) + '%' );