]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/util/input_slider.cpp
Qt: hide the tip when the playback ends (blind fix for #6120)
[vlc] / modules / gui / qt4 / util / input_slider.cpp
index d45a0e43edd8e03537b8c00e514cec9e36814ca7..0e70263303c666a2f8cac8fbf3e4067521dc555d 100644 (file)
@@ -36,7 +36,6 @@
 #include <QPaintEvent>
 #include <QPainter>
 #include <QBitmap>
-#include <QPainter>
 #include <QStyleOptionSlider>
 #include <QLinearGradient>
 #include <QTimer>
@@ -62,6 +61,37 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
     f_buffering = 1.0;
     mHandleOpacity = 1.0;
     chapters = NULL;
+    mHandleLength = -1;
+
+    // prepare some static colors
+    QPalette p = palette();
+    QColor background = p.color( QPalette::Active, QPalette::Background );
+    tickpointForeground = p.color( QPalette::Active, QPalette::WindowText );
+    tickpointForeground.setHsv( tickpointForeground.hue(),
+            ( background.saturation() + tickpointForeground.saturation() ) / 2,
+            ( background.value() + tickpointForeground.value() ) / 2 );
+
+    // set the background color and gradient
+    QColor backgroundBase( p.window().color() );
+    backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
+    backgroundGradient.setColorAt( 1.0, backgroundBase );
+
+    // set the foreground color and gradient
+    QColor foregroundBase( 50, 156, 255 );
+    foregroundGradient.setColorAt( 0.0,  foregroundBase );
+    foregroundGradient.setColorAt( 1.0,  foregroundBase.darker( 140 ) );
+
+    // prepare the handle's gradient
+    handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
+    handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
+
+    // prepare the handle's shadow gradient
+    QColor shadowBase = p.shadow().color();
+    if( shadowBase.lightness() > 100 )
+        shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
+    shadowDark = shadowBase.darker( 150 );
+    shadowLight = shadowBase.lighter( 180 );
+    shadowLight.setAlpha( 50 );
 
     /* Timer used to fire intermediate updatePos() when sliding */
     seekLimitTimer = new QTimer( this );
@@ -79,6 +109,10 @@ SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
     setTracking( true );
     setFocusPolicy( Qt::NoFocus );
 
+    /* Use the new/classic style */
+    if( !b_classic )
+        setStyle( new SeekStyle );
+
     /* Init to 0 */
     setPosition( -1.0, 0, 0 );
     secstotimestr( psz_length, 0 );
@@ -128,6 +162,7 @@ void SeekSlider::setPosition( float pos, int64_t time, int length )
     if( pos == -1.0 )
     {
         setEnabled( false );
+        mTimeTooltip->hide();
         isSliding = false;
     }
     else
@@ -223,7 +258,8 @@ void SeekSlider::mousePressEvent( QMouseEvent* event )
     }
 
     isSliding = true ;
-    setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
+
+    setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false ) );
     emit sliderMoved( value() );
     event->accept();
 }
@@ -232,14 +268,15 @@ void SeekSlider::mouseMoveEvent( QMouseEvent *event )
 {
     if( isSliding )
     {
-        setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
+        setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false) );
         emit sliderMoved( value() );
     }
 
     /* Tooltip */
     if ( inputLength > 0 )
     {
-        int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
+        int margin = handleLength() / 2;
+        int posX = qMax( rect().left() + margin, qMin( rect().right() - margin, event->x() ) );
 
         QString chapterLabel;
 
@@ -262,7 +299,7 @@ void SeekSlider::mouseMoveEvent( QMouseEvent *event )
 
         QPoint target( event->globalX() - ( event->x() - posX ),
                   QWidget::mapToGlobal( pos() ).y() );
-        secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
+        secstotimestr( psz_length, ( ( posX - margin ) * inputLength ) / ( size().width() - handleLength() ) );
         mTimeTooltip->setTip( target, psz_length, chapterLabel );
     }
     event->accept();
@@ -338,198 +375,12 @@ bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
 
 QSize SeekSlider::sizeHint() const
 {
+    if ( b_classic )
+        return QSlider::sizeHint();
     return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
                                                : QSize( 18, 100 );
 }
 
-QSize SeekSlider::handleSize() const
-{
-    const int size = ( orientation() == Qt::Horizontal ? height() : width() );
-    return QSize( size, size );
-}
-
-void SeekSlider::paintEvent( QPaintEvent *event )
-{
-    if( b_classic )
-        return QSlider::paintEvent( event );
-
-    QStyleOptionSlider option;
-    initStyleOption( &option );
-
-    /* */
-    QPainter painter( this );
-    painter.setRenderHints( QPainter::Antialiasing );
-
-    // draw bar
-    const int barCorner = 3;
-    qreal sliderPos     = -1;
-    int range           = MAXIMUM;
-    QRect barRect       = rect();
-
-    // adjust positions based on the current orientation
-    if ( option.sliderPosition != 0 )
-    {
-        switch ( orientation() )
-        {
-            case Qt::Horizontal:
-                sliderPos = ( ( (qreal)width() ) / (qreal)range )
-                        * (qreal)option.sliderPosition;
-                break;
-            case Qt::Vertical:
-                sliderPos = ( ( (qreal)height() ) / (qreal)range )
-                        * (qreal)option.sliderPosition;
-                break;
-        }
-    }
-
-    switch ( orientation() )
-    {
-        case Qt::Horizontal:
-            barRect.setHeight( height() /2 );
-            break;
-        case Qt::Vertical:
-            barRect.setWidth( width() /2 );
-            break;
-    }
-
-    barRect.moveCenter( rect().center() );
-
-    // set the background color and gradient
-    QColor backgroundBase( palette().window().color() );
-    QLinearGradient backgroundGradient( 0, 0, 0, height() );
-    backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
-    backgroundGradient.setColorAt( 1.0, backgroundBase );
-
-    // set the foreground color and gradient
-    QColor foregroundBase( 50, 156, 255 );
-    QLinearGradient foregroundGradient( 0, 0, 0, height() );
-    foregroundGradient.setColorAt( 0.0,  foregroundBase );
-    foregroundGradient.setColorAt( 1.0,  foregroundBase.darker( 140 ) );
-
-    // draw a slight 3d effect on the bottom
-    painter.setPen( QColor( 230, 230, 230 ) );
-    painter.setBrush( Qt::NoBrush );
-    painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
-
-    // draw background
-    painter.setPen( Qt::NoPen );
-    painter.setBrush( backgroundGradient );
-    painter.drawRoundedRect( barRect, barCorner, barCorner );
-
-    // adjusted foreground rectangle
-    QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
-
-    switch ( orientation() )
-    {
-        case Qt::Horizontal:
-            valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
-            break;
-        case Qt::Vertical:
-            valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
-            valueRect.moveBottom( rect().bottom() );
-            break;
-    }
-
-    if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
-    {
-        // draw foreground
-        painter.setPen( Qt::NoPen );
-        painter.setBrush( foregroundGradient );
-        painter.drawRoundedRect( valueRect, barCorner, barCorner );
-    }
-
-    // draw buffering overlay
-    if ( f_buffering < 1.0 )
-    {
-        QRect innerRect = barRect.adjusted( 1, 1,
-                            barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
-        QColor overlayColor = QColor( "Orange" );
-        overlayColor.setAlpha( 128 );
-        painter.setBrush( overlayColor );
-        painter.drawRoundedRect( innerRect, barCorner, barCorner );
-    }
-
-    if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
-    {
-        /* draw chapters tickpoints */
-        if ( chapters && inputLength && size().width() )
-        {
-            QColor background = palette().color( QPalette::Active, QPalette::Background );
-            QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
-            foreground.setHsv( foreground.hue(),
-                            ( background.saturation() + foreground.saturation() ) / 2,
-                            ( background.value() + foreground.value() ) / 2 );
-            if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
-            {
-                QList<SeekPoint> points = chapters->getPoints();
-                foreach( SeekPoint point, points )
-                {
-                    int x = point.time / 1000000.0 / inputLength * size().width();
-                    painter.setPen( foreground );
-                    painter.setBrush( Qt::NoBrush );
-                    painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
-                }
-            }
-        }
-
-        // draw handle
-        if ( sliderPos != -1 )
-        {
-            const int margin = 0;
-            QSize hSize = handleSize() - QSize( 6, 6 );
-            QPoint pos;
-
-            switch ( orientation() )
-            {
-                case Qt::Horizontal:
-                    pos = QPoint( sliderPos - ( hSize.width() / 2 ), 2 );
-                    pos.rx() = qMax( margin, pos.x() );
-                    pos.rx() = qMin( width() - hSize.width() - margin, pos.x() );
-                    break;
-                case Qt::Vertical:
-                    pos = QPoint( 2, height() - ( sliderPos + ( hSize.height() / 2 ) ) );
-                    pos.ry() = qMax( margin, pos.y() );
-                    pos.ry() = qMin( height() - hSize.height() - margin, pos.y() );
-                    break;
-            }
-
-            QPalette p;
-            QPoint shadowPos( pos - QPoint( 2, 2 ) );
-            QSize sSize( handleSize() - QSize( 2, 2 ) );
-
-            // prepare the handle's gradient
-            QLinearGradient handleGradient( 0, 0, 0, hSize.height() );
-            handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
-            handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
-
-            // prepare the handle's shadow gradient
-            QColor shadowBase = p.shadow().color();
-            if( shadowBase.lightness() > 100 )
-                shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
-            QColor shadowDark( shadowBase.darker( 150 ) );
-            QColor shadowLight( shadowBase.lighter( 180 ) );
-            shadowLight.setAlpha( 50 );
-
-            QRadialGradient shadowGradient( shadowPos.x() + ( sSize.width() / 2 ),
-                                            shadowPos.y() + ( sSize.height() / 2 ),
-                                            qMax( sSize.width(), sSize.height() ) / 2 );
-            shadowGradient.setColorAt( 0.4, shadowDark );
-            shadowGradient.setColorAt( 1.0, shadowLight );
-
-            painter.setPen( Qt::NoPen );
-            painter.setOpacity( mHandleOpacity );
-
-            // draw the handle's shadow
-            painter.setBrush( shadowGradient );
-            painter.drawEllipse( shadowPos.x(), shadowPos.y() + 1, sSize.width(), sSize.height() );
-
-            // finally draw the handle
-            painter.setBrush( handleGradient );
-            painter.drawEllipse( pos.x(), pos.y(), hSize.width(), hSize.height() );
-        }
-    }
-}
-
 qreal SeekSlider::handleOpacity() const
 {
     return mHandleOpacity;
@@ -542,6 +393,18 @@ void SeekSlider::setHandleOpacity(qreal opacity)
     update();
 }
 
+inline int SeekSlider::handleLength()
+{
+    if ( mHandleLength > 0 )
+        return mHandleLength;
+
+    /* Ask for the length of the handle to the underlying style */
+    QStyleOptionSlider option;
+    initStyleOption( &option );
+    mHandleLength = style()->pixelMetric( QStyle::PM_SliderLength, &option );
+    return mHandleLength;
+}
+
 void SeekSlider::hideHandle()
 {
     /* If pause is called while not running Qt will complain */
@@ -558,6 +421,7 @@ bool SeekSlider::isAnimationRunning() const
             || hideHandleTimer->isActive();
 }
 
+
 /* This work is derived from Amarok's work under GPLv2+
     - Mark Kretschmann
     - Gábor Lehel
@@ -600,6 +464,15 @@ SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
         for( int i = colorList.count(); i < 12; i++)
             colorList.append( "255" );
 
+    background = palette().color( QPalette::Active, QPalette::Background );
+    foreground = palette().color( QPalette::Active, QPalette::WindowText );
+    foreground.setHsv( foreground.hue(),
+                    ( background.saturation() + foreground.saturation() ) / 2,
+                    ( background.value() + foreground.value() ) / 2 );
+
+    textfont.setPixelSize( 9 );
+    textrect.setRect( 0, 0, 34, 15 );
+
     /* Regular colors */
 #define c(i) colorList.at(i).toInt()
 #define add_color(gradient, range, c1, c2, c3) \
@@ -617,13 +490,14 @@ SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
     add_color( gradient1, range, c1, c2, c3 ); \
     add_desaturated_color( gradient2, range, c1, c2, c3 );
 
+    float f_mid_point = ( 100.0 / maximum() );
     QColor * foo;
     add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
-    add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
-    add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
+    add_colors( gradient, gradient2, f_mid_point - 0.05, 3, 4, 5 );
+    add_colors( gradient, gradient2, f_mid_point + 0.05, 6, 7, 8 );
     add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
 
-    QPainter painter( &pixGradient );
+    painter.begin( &pixGradient );
     painter.setPen( Qt::NoPen );
     painter.setBrush( gradient );
     painter.drawRect( pixGradient.rect() );
@@ -716,13 +590,14 @@ void SoundSlider::setMuted( bool m )
 
 void SoundSlider::paintEvent( QPaintEvent *e )
 {
-    QPainter painter( this );
     QPixmap *paintGradient;
     if (b_isMuted)
         paintGradient = &this->pixGradient2;
     else
         paintGradient = &this->pixGradient;
 
+    painter.begin( this );
+
     const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
 
     const QRectF boundsG( 0, 0, offset , paintGradient->height() );
@@ -731,16 +606,9 @@ void SoundSlider::paintEvent( QPaintEvent *e )
     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
     painter.drawPixmap( boundsO, pixOutside, boundsO );
 
-    QColor background = palette().color( QPalette::Active, QPalette::Background );
-    QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
-    foreground.setHsv( foreground.hue(),
-                    ( background.saturation() + foreground.saturation() ) / 2,
-                    ( background.value() + foreground.value() ) / 2 );
     painter.setPen( foreground );
-    QFont font; font.setPixelSize( 9 );
-    painter.setFont( font );
-    const QRect rect( 0, 0, 34, 15 );
-    painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
+    painter.setFont( textfont );
+    painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
                       QString::number( value() ) + '%' );
 
     painter.end();