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"
34 #include <QPaintEvent>
38 #include <QStyleOptionSlider>
39 #include <QLinearGradient>
41 #include <QRadialGradient>
46 SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent )
48 SeekSlider( Qt::Horizontal, _parent );
51 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
52 : QSlider( q, _parent )
58 /* Timer used to fire intermediate updatePos() when sliding */
59 seekLimitTimer = new QTimer( this );
60 seekLimitTimer->setSingleShot( true );
63 mTimeTooltip = new TimeTooltip( this );
64 mTimeTooltip->setMouseTracking( true );
67 setRange( MINIMUM, MAXIMUM );
70 setMouseTracking( true );
72 setFocusPolicy( Qt::NoFocus );
75 setPosition( -1.0, 0, 0 );
76 secstotimestr( psz_length, 0 );
78 CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
79 CONNECT( seekLimitTimer, timeout(), this, updatePos() );
80 mTimeTooltip->installEventFilter( this );
83 SeekSlider::~SeekSlider()
89 * \brief Sets the chapters seekpoints adapter
91 * \params SeekPoints initilized with current intf thread
93 void SeekSlider::setChapters( SeekPoints *chapters_ )
97 chapters->setParent( this );
101 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
103 * \param pos Position, between 0 and 1. -1 disables the slider
104 * \param time Elapsed time. Unused
105 * \param legnth Duration time.
107 void SeekSlider::setPosition( float pos, int64_t time, int length )
119 setValue( (int)( pos * 1000.0 ) );
121 inputLength = length;
124 void SeekSlider::startSeekTimer()
126 /* Only fire one update, when sliding, every 150ms */
127 if( b_isSliding && !seekLimitTimer->isActive() )
128 seekLimitTimer->start( 150 );
131 void SeekSlider::updatePos()
133 float f_pos = (float)( value() ) / 1000.0;
134 emit sliderDragged( f_pos ); /* Send new position to VLC's core */
137 void SeekSlider::updateBuffering( float f_buffering_ )
139 f_buffering = f_buffering_;
143 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
147 seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
148 QSlider::mouseReleaseEvent( event );
152 void SeekSlider::mousePressEvent( QMouseEvent* event )
155 if( event->button() != Qt::LeftButton &&
156 event->button() != Qt::MidButton )
158 QSlider::mousePressEvent( event );
163 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
167 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
171 setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
172 emit sliderMoved( value() );
176 if ( inputLength > 0 )
178 int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
180 QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
181 QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
184 secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
185 mTimeTooltip->setTime( psz_length );
186 mTimeTooltip->move( p );
191 void SeekSlider::wheelEvent( QWheelEvent *event )
193 /* Don't do anything if we are for somehow reason sliding */
196 setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
197 Since delta is in 1/8 of ° and mouse have steps of 15 °
198 and that our slider is in 0.1% and we want one step to be a 1%
199 increment of position */
200 emit sliderDragged( value() / 1000.0 );
205 void SeekSlider::enterEvent( QEvent * )
207 /* Don't show the tooltip if the slider is disabled */
208 if( isEnabled() && inputLength > 0 )
209 mTimeTooltip->show();
212 void SeekSlider::leaveEvent( QEvent * )
214 if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) )
215 mTimeTooltip->hide();
218 void SeekSlider::hideEvent( QHideEvent * )
220 mTimeTooltip->hide();
223 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
225 if( obj == mTimeTooltip )
227 if( event->type() == QEvent::Leave ||
228 event->type() == QEvent::MouseMove )
230 QMouseEvent *e = static_cast<QMouseEvent*>( event );
231 if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
232 mTimeTooltip->hide();
237 return QSlider::eventFilter( obj, event );
240 QSize SeekSlider::sizeHint() const
242 return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
246 QSize SeekSlider::handleSize() const
248 const int size = ( orientation() == Qt::Horizontal ? height() : width() );
249 return QSize( size, size );
252 void SeekSlider::paintEvent( QPaintEvent *event )
256 QStyleOptionSlider option;
257 initStyleOption( &option );
260 QPainter painter( this );
261 painter.setRenderHints( QPainter::Antialiasing );
264 const int barCorner = 3;
265 qreal sliderPos = -1;
267 QRect barRect = rect();
269 // adjust positions based on the current orientation
270 if ( option.sliderPosition != 0 )
272 switch ( orientation() )
275 sliderPos = ( ( (qreal)width() ) / (qreal)range )
276 * (qreal)option.sliderPosition;
279 sliderPos = ( ( (qreal)height() ) / (qreal)range )
280 * (qreal)option.sliderPosition;
285 switch ( orientation() )
288 barRect.setHeight( handleSize().height() /2 );
291 barRect.setWidth( handleSize().width() /2 );
295 barRect.moveCenter( rect().center() );
297 // set the background color and gradient
298 QColor backgroundBase( 135, 135, 135 );
299 QLinearGradient backgroundGradient( 0, 0, 0, height() );
300 backgroundGradient.setColorAt( 0.0, backgroundBase );
301 backgroundGradient.setColorAt( 1.0, backgroundBase.lighter( 150 ) );
303 // set the foreground color and gradient
304 QColor foregroundBase( 50, 156, 255 );
305 QLinearGradient foregroundGradient( 0, 0, 0, height() );
306 foregroundGradient.setColorAt( 0.0, foregroundBase );
307 foregroundGradient.setColorAt( 1.0, foregroundBase.darker( 140 ) );
309 // draw a slight 3d effect on the bottom
310 painter.setPen( QColor( 230, 230, 230 ) );
311 painter.setBrush( Qt::NoBrush );
312 painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
315 painter.setPen( Qt::NoPen );
316 painter.setBrush( backgroundGradient );
317 painter.drawRoundedRect( barRect, barCorner, barCorner );
319 // adjusted foreground rectangle
320 QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
322 switch ( orientation() )
325 valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
328 valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
329 valueRect.moveBottom( rect().bottom() );
333 if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
336 painter.setPen( Qt::NoPen );
337 painter.setBrush( foregroundGradient );
338 painter.drawRoundedRect( valueRect, barCorner, barCorner );
341 // draw buffering overlay
342 if ( f_buffering < 1.0 )
344 QRect innerRect = barRect.adjusted( 1, 1,
345 barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
346 QColor overlayColor = QColor( "Orange" );
347 overlayColor.setAlpha( 128 );
348 painter.setBrush( overlayColor );
349 painter.drawRoundedRect( innerRect, barCorner, barCorner );
352 if ( option.state & QStyle::State_MouseOver )
354 /* draw chapters tickpoints */
355 if ( chapters && inputLength && size().width() )
357 if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
359 QList<SeekPoint> points = chapters->getPoints();
360 foreach( SeekPoint point, points )
362 int x = point.time / 1000000.0 / inputLength * size().width();
363 painter.setPen( QColor( 80, 80, 80 ) );
364 painter.setBrush( Qt::NoBrush );
365 painter.drawLine( x, 0, x, 3 );
366 painter.drawLine( x, height(), x, height() - 3 );
372 if ( sliderPos != -1 )
374 const int margin = 0;
375 QSize hs = handleSize() - QSize( 5, 5 );
378 switch ( orientation() )
381 pos = QPoint( sliderPos - ( hs.width() / 2 ), 2 );
382 pos.rx() = qMax( margin, pos.x() );
383 pos.rx() = qMin( width() - hs.width() - margin, pos.x() );
386 pos = QPoint( 2, height() - ( sliderPos + ( hs.height() / 2 ) ) );
387 pos.ry() = qMax( margin, pos.y() );
388 pos.ry() = qMin( height() - hs.height() - margin, pos.y() );
392 QRadialGradient buttonGradient( pos.x() + ( hs.width() / 2 ) - 2,
393 pos.y() + ( hs.height() / 2 ) - 2,
394 qMax( hs.width(), hs.height() ) );
395 buttonGradient.setColorAt( 0.0, QColor( 0, 0, 0 ) );
396 buttonGradient.setColorAt( 1.0, QColor( 80, 80, 80 ) );
398 painter.setPen( Qt::NoPen );
399 painter.setBrush( buttonGradient );
400 painter.drawEllipse( pos.x(), pos.y(), hs.width(), hs.height() );
406 /* This work is derived from Amarok's work under GPLv2+
410 #define WLENGTH 80 // px
411 #define WHEIGHT 22 // px
412 #define SOUNDMIN 0 // %
413 #define SOUNDMAX 200 // % OR 400 ?
415 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
417 : QAbstractSlider( _parent )
419 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
420 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
421 setMouseTracking( true );
423 b_mouseOutside = true;
426 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
428 const QPixmap temp( ":/toolbar/volslide-inside" );
429 const QBitmap mask( temp.createHeuristicMask() );
431 setFixedSize( pixOutside.size() );
433 pixGradient = QPixmap( mask.size() );
434 pixGradient2 = QPixmap( mask.size() );
436 /* Gradient building from the preferences */
437 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
438 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
440 QStringList colorList = qfu( psz_colors ).split( ";" );
443 /* Fill with 255 if the list is too short */
444 if( colorList.size() < 12 )
445 for( int i = colorList.size(); i < 12; i++)
446 colorList.append( "255" );
449 #define c(i) colorList.at(i).toInt()
450 #define add_color(gradient, range, c1, c2, c3) \
451 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
453 /* Desaturated colors */
454 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
455 #define add_desaturated_color(gradient, range, c1, c2, c3) \
456 foo = new QColor( c(c1), c(c2), c(c3) );\
457 desaturate( foo ); gradient.setColorAt( range, *foo );\
460 /* combine the two helpers */
461 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
462 add_color( gradient1, range, c1, c2, c3 ); \
463 add_desaturated_color( gradient2, range, c1, c2, c3 );
466 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
467 add_colors( gradient, gradient2, 0.22, 3, 4, 5 );
468 add_colors( gradient, gradient2, 0.5, 6, 7, 8 );
469 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
471 QPainter painter( &pixGradient );
472 painter.setPen( Qt::NoPen );
473 painter.setBrush( gradient );
474 painter.drawRect( pixGradient.rect() );
477 painter.begin( &pixGradient2 );
478 painter.setPen( Qt::NoPen );
479 painter.setBrush( gradient2 );
480 painter.drawRect( pixGradient2.rect() );
483 pixGradient.setMask( mask );
484 pixGradient2.setMask( mask );
487 void SoundSlider::wheelEvent( QWheelEvent *event )
489 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
490 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
492 emit sliderReleased();
493 emit sliderMoved( value() );
496 void SoundSlider::mousePressEvent( QMouseEvent *event )
498 if( event->button() != Qt::RightButton )
500 /* We enter the sliding mode */
502 i_oldvalue = value();
503 emit sliderPressed();
504 changeValue( event->x() - paddingL );
505 emit sliderMoved( value() );
509 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
511 if( event->button() != Qt::RightButton )
513 if( !b_mouseOutside && value() != i_oldvalue )
515 emit sliderReleased();
517 emit sliderMoved( value() );
520 b_mouseOutside = false;
524 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
528 QRect rect( paddingL - 15, -1,
529 WLENGTH + 15 * 2 , WHEIGHT + 5 );
530 if( !rect.contains( event->pos() ) )
531 { /* We are outside */
532 if ( !b_mouseOutside )
533 setValue( i_oldvalue );
534 b_mouseOutside = true;
537 { /* We are inside */
538 b_mouseOutside = false;
539 changeValue( event->x() - paddingL );
540 emit sliderMoved( value() );
545 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
546 i = __MIN( __MAX( 0, i ), maximum() );
547 setToolTip( QString("%1 \%" ).arg( i ) );
551 void SoundSlider::changeValue( int x )
553 setValue( (x * maximum() + 40 ) / WLENGTH );
556 void SoundSlider::setMuted( bool m )
562 void SoundSlider::paintEvent( QPaintEvent *e )
564 QPainter painter( this );
565 QPixmap *pixGradient;
567 pixGradient = &this->pixGradient2;
569 pixGradient = &this->pixGradient;
571 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
573 const QRectF boundsG( 0, 0, offset , pixGradient->height() );
574 painter.drawPixmap( boundsG, *pixGradient, boundsG );
576 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
577 painter.drawPixmap( boundsO, pixOutside, boundsO );
579 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
580 QFont font; font.setPixelSize( 9 );
581 painter.setFont( font );
582 const QRect rect( 0, 0, 34, 15 );
583 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
584 QString::number( value() ) + '%' );