Commit e83c9f51 authored by Ludovic Fauvet's avatar Ludovic Fauvet Committed by Jean-Baptiste Kempf

Qt: animate the handle of the seek slider

Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent 17f422bc
...@@ -46,10 +46,13 @@ ...@@ -46,10 +46,13 @@
#include <QPalette> #include <QPalette>
#include <QColor> #include <QColor>
#include <QPoint> #include <QPoint>
#include <QPropertyAnimation>
#define MINIMUM 0 #define MINIMUM 0
#define MAXIMUM 1000 #define MAXIMUM 1000
#define CHAPTERSSPOTSIZE 3 #define CHAPTERSSPOTSIZE 3
#define FADEDURATION 300
#define FADEOUTDELAY 2000
SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent ) SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent )
{ {
...@@ -61,6 +64,7 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent ) ...@@ -61,6 +64,7 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
{ {
b_isSliding = false; b_isSliding = false;
f_buffering = 1.0; f_buffering = 1.0;
mHandleOpacity = 1.0;
chapters = NULL; chapters = NULL;
/* Timer used to fire intermediate updatePos() when sliding */ /* Timer used to fire intermediate updatePos() when sliding */
...@@ -83,8 +87,18 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent ) ...@@ -83,8 +87,18 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
setPosition( -1.0, 0, 0 ); setPosition( -1.0, 0, 0 );
secstotimestr( psz_length, 0 ); secstotimestr( psz_length, 0 );
animHandle = new QPropertyAnimation( this, "handleOpacity", this );
animHandle->setDuration( FADEDURATION );
animHandle->setStartValue( 0.0 );
animHandle->setEndValue( 1.0 );
hideHandleTimer = new QTimer( this );
hideHandleTimer->setSingleShot( true );
hideHandleTimer->setInterval( FADEOUTDELAY );
CONNECT( this, sliderMoved( int ), this, startSeekTimer() ); CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
CONNECT( seekLimitTimer, timeout(), this, updatePos() ); CONNECT( seekLimitTimer, timeout(), this, updatePos() );
CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
mTimeTooltip->installEventFilter( this ); mTimeTooltip->installEventFilter( this );
} }
...@@ -266,6 +280,17 @@ void SeekSlider::wheelEvent( QWheelEvent *event ) ...@@ -266,6 +280,17 @@ void SeekSlider::wheelEvent( QWheelEvent *event )
void SeekSlider::enterEvent( QEvent * ) void SeekSlider::enterEvent( QEvent * )
{ {
/* Cancel the fade-out timer */
hideHandleTimer->stop();
/* Only start the fade-in if needed */
if( animHandle->direction() != QAbstractAnimation::Forward )
{
/* If pause is called while not running Qt will complain */
if( animHandle->state() == QAbstractAnimation::Running )
animHandle->pause();
animHandle->setDirection( QAbstractAnimation::Forward );
animHandle->start();
}
/* Don't show the tooltip if the slider is disabled */ /* Don't show the tooltip if the slider is disabled */
if( isEnabled() && inputLength > 0 ) if( isEnabled() && inputLength > 0 )
mTimeTooltip->show(); mTimeTooltip->show();
...@@ -273,6 +298,7 @@ void SeekSlider::enterEvent( QEvent * ) ...@@ -273,6 +298,7 @@ void SeekSlider::enterEvent( QEvent * )
void SeekSlider::leaveEvent( QEvent * ) void SeekSlider::leaveEvent( QEvent * )
{ {
hideHandleTimer->start();
if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ) if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) )
mTimeTooltip->hide(); mTimeTooltip->hide();
} }
...@@ -411,7 +437,7 @@ void SeekSlider::paintEvent( QPaintEvent *event ) ...@@ -411,7 +437,7 @@ void SeekSlider::paintEvent( QPaintEvent *event )
painter.drawRoundedRect( innerRect, barCorner, barCorner ); painter.drawRoundedRect( innerRect, barCorner, barCorner );
} }
if ( option.state & QStyle::State_MouseOver ) if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
{ {
/* draw chapters tickpoints */ /* draw chapters tickpoints */
if ( chapters && inputLength && size().width() ) if ( chapters && inputLength && size().width() )
...@@ -477,6 +503,7 @@ void SeekSlider::paintEvent( QPaintEvent *event ) ...@@ -477,6 +503,7 @@ void SeekSlider::paintEvent( QPaintEvent *event )
shadowGradient.setColorAt( 1.0, shadowLight ); shadowGradient.setColorAt( 1.0, shadowLight );
painter.setPen( Qt::NoPen ); painter.setPen( Qt::NoPen );
painter.setOpacity( mHandleOpacity );
// draw the handle's shadow // draw the handle's shadow
painter.setBrush( shadowGradient ); painter.setBrush( shadowGradient );
...@@ -489,6 +516,33 @@ void SeekSlider::paintEvent( QPaintEvent *event ) ...@@ -489,6 +516,33 @@ void SeekSlider::paintEvent( QPaintEvent *event )
} }
} }
qreal SeekSlider::handleOpacity() const
{
return mHandleOpacity;
}
void SeekSlider::setHandleOpacity(qreal opacity)
{
mHandleOpacity = opacity;
/* Request a new paintevent */
update();
}
void SeekSlider::hideHandle()
{
/* If pause is called while not running Qt will complain */
if( animHandle->state() == QAbstractAnimation::Running )
animHandle->pause();
/* Play the animation backward */
animHandle->setDirection( QAbstractAnimation::Backward );
animHandle->start();
}
bool SeekSlider::isAnimationRunning() const
{
return animHandle->state() == QAbstractAnimation::Running
|| hideHandleTimer->isActive();
}
/* This work is derived from Amarok's work under GPLv2+ /* This work is derived from Amarok's work under GPLv2+
- Mark Kretschmann - Mark Kretschmann
...@@ -678,4 +732,3 @@ void SoundSlider::paintEvent( QPaintEvent *e ) ...@@ -678,4 +732,3 @@ void SoundSlider::paintEvent( QPaintEvent *e )
painter.end(); painter.end();
e->accept(); e->accept();
} }
...@@ -38,11 +38,13 @@ class QWheelEvent; ...@@ -38,11 +38,13 @@ class QWheelEvent;
class QHideEvent; class QHideEvent;
class QTimer; class QTimer;
class SeekPoints; class SeekPoints;
class QPropertyAnimation;
/* Input Slider derived from QSlider */ /* Input Slider derived from QSlider */
class SeekSlider : public QSlider class SeekSlider : public QSlider
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(qreal handleOpacity READ handleOpacity WRITE setHandleOpacity)
public: public:
SeekSlider( QWidget *_parent ); SeekSlider( QWidget *_parent );
SeekSlider( Qt::Orientation q, QWidget *_parent ); SeekSlider( Qt::Orientation q, QWidget *_parent );
...@@ -63,6 +65,9 @@ protected: ...@@ -63,6 +65,9 @@ protected:
QSize handleSize() const; QSize handleSize() const;
QSize sizeHint() const; QSize sizeHint() const;
bool isAnimationRunning() const;
qreal handleOpacity() const;
void setHandleOpacity( qreal opacity );
private: private:
bool b_isSliding; /* Whether we are currently sliding by user action */ bool b_isSliding; /* Whether we are currently sliding by user action */
...@@ -74,9 +79,15 @@ private: ...@@ -74,9 +79,15 @@ private:
float f_buffering; float f_buffering;
SeekPoints* chapters; SeekPoints* chapters;
/* Handle's animation */
qreal mHandleOpacity;
QPropertyAnimation *animHandle;
QTimer *hideHandleTimer;
public slots: public slots:
void setPosition( float, int64_t, int ); void setPosition( float, int64_t, int );
void updateBuffering( float ); void updateBuffering( float );
void hideHandle();
private slots: private slots:
void startSeekTimer(); void startSeekTimer();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment