]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/util/input_slider.cpp
Small memory leak fix.
[vlc] / modules / gui / qt4 / util / input_slider.cpp
index af54164aa57691ebf37ed4d26ad4ea3e2fd4f51a..b3a720fd4a63fdd64a2501c5d69f3dc9311d6e95 100644 (file)
@@ -5,6 +5,7 @@
  * $Id$
  *
  * Authors: Clément Stenac <zorglub@videolan.org>
+ *          Jean-Baptiste Kempf <jb@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-#include "qt4.hpp"
 #include "util/input_slider.hpp"
 
 #include <QPaintEvent>
 #include <QPainter>
-
+#include <QBitmap>
 #include <QStyle>
-InputSlider::InputSlider( QWidget *_parent ) : DirectSlider( _parent )
+
+InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent )
 {
     InputSlider::InputSlider( Qt::Horizontal, _parent );
 }
 
 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
-                                 DirectSlider( q, _parent )
+                                 QSlider( q, _parent )
 {
-    mymove = false;
+    b_sliding = false;
     setMinimum( 0 );
     setMouseTracking(true);
     setMaximum( 1000 );
     setSingleStep( 2 );
     setPageStep( 10 );
     setTracking( true );
+    secstotimestr( psz_length, 0 );
     CONNECT( this, valueChanged(int), this, userDrag( int ) );
 }
 
@@ -52,61 +57,108 @@ void InputSlider::setPosition( float pos, int a, int b )
         setEnabled( false );
     else
         setEnabled( true );
-    mymove = true;
-    setValue( (int)(pos * 1000.0 ) );
-    mymove = false;
+
+    if( !b_sliding )
+        setValue( (int)(pos * 1000.0 ) );
     inputLength = b;
 }
 
 void InputSlider::userDrag( int new_value )
 {
-    float f_pos = (float)(new_value)/1000.0;
-    if( !mymove )
+    if( b_sliding )
     {
+        float f_pos = (float)(new_value)/1000.0;
         emit sliderDragged( f_pos );
     }
 }
 
+void InputSlider::mouseReleaseEvent( QMouseEvent *event )
+{
+    b_sliding = false;
+}
+
+void InputSlider::mousePressEvent(QMouseEvent* event)
+{
+    b_sliding = true ;
+    if( event->button() != Qt::LeftButton &&
+        event->button() != Qt::MidButton )
+    {
+        QSlider::mousePressEvent( event );
+        return;
+    }
+
+    QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
+        Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
+        Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
+        event->modifiers() );
+    QSlider::mousePressEvent( &newEvent );
+}
+
 void InputSlider::mouseMoveEvent(QMouseEvent *event)
 {
-    char psz_length[MSTRTIME_MAX_SIZE];
+    if( b_sliding )
+    {
+        QSlider::mouseMoveEvent( event );
+    }
+
     secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
     setToolTip( psz_length );
 }
 
-#define WLENGTH   100 // px
+/* This work is derived from Amarok's work under GPLv2+
+    - Mark Kretschmann
+    - Gábor Lehel
+   */
+#define WLENGTH   90 // px
 #define WHEIGHT   25  // px
 #define SOUNDMIN  0   // %
 #define SOUNDMAX  200 // % OR 400 ?
 
-SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
+SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
+                          char *psz_colors )
                         : QAbstractSlider( _parent )
 {
-    padding = 5;
+    paddingL = 5;
+    paddingR = 3;
+
     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
+    setMouseTracking( true );
+    b_sliding = false;
+
+    pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
+
+    const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
+    const QBitmap mask( temp.createHeuristicMask() );
 
-    pixGradient = QPixmap( QSize( WLENGTH, WHEIGHT ) );
-//    QBixmap mask = QBitmap( QPixmap );
+    setMinimumSize( pixOutside.size() );
 
-    QPainter p( &pixGradient );
-    QLinearGradient gradient( 0, 0, WLENGTH, 0 );
-    gradient.setColorAt( 0.0, Qt::white );
-    gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
-    gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
-    gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
-    p.setPen( Qt::NoPen );
-    p.setBrush( gradient );
+    pixGradient = QPixmap( mask.size() );
 
-//static const QPointF points[3] = { QPointF( 0.0, WHEIGHT ),
-  //        QPointF( WLENGTH, WHEIGHT ),  QPointF( WLENGTH, 0.0 ) };
+    /* Gradient building from the preferences */
+    QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
 
- //   p.drawConvexPolygon( points, 3 );
+    QStringList colorList = qfu( psz_colors ).split( ";" );
+    free( psz_colors );
 
-    p.drawRect( pixGradient.rect() );
-    p.end();
+    /* Fill with 255 if the list is too short */
+    if( colorList.size() < 12 )
+        for( int i = colorList.size(); i < 12; i++)
+            colorList.append( "255" );
+
+#define c(i) colorList.at(i).toInt()
+    gradient.setColorAt( 0.0, QColor( c(0), c(1), c(2) ) );
+    gradient.setColorAt( 0.2, QColor( c(3), c(4), c(5) ) );
+    gradient.setColorAt( 0.5, QColor( c(6), c(7), c(8) ) );
+    gradient.setColorAt( 1.0, QColor( c(9), c(10), c(11) ) );
+
+    QPainter painter( &pixGradient );
+    painter.setPen( Qt::NoPen );
+    painter.setBrush( gradient );
+    painter.drawRect( pixGradient.rect() );
+    painter.end();
 
//   pixGradient.setMask( mask );
+    pixGradient.setMask( mask );
 }
 
 void SoundSlider::wheelEvent( QWheelEvent *event )
@@ -125,6 +177,7 @@ void SoundSlider::mousePressEvent( QMouseEvent *event )
         b_sliding = true;
         i_oldvalue = value();
         emit sliderPressed();
+        changeValue( event->x() - paddingL );
     }
 }
 
@@ -146,8 +199,8 @@ void SoundSlider::mouseMoveEvent( QMouseEvent *event )
 {
     if( b_sliding )
     {
-        QRect rect( padding - 15,     padding - 1,
-                    WLENGTH + 15 * 2, WHEIGHT + 2 );
+        QRect rect( paddingL - 15,    -1,
+                    WLENGTH + 15 * 2, WHEIGHT + 4 );
         if( !rect.contains( event->pos() ) )
         { /* We are outside */
             if ( !b_outside )
@@ -157,39 +210,43 @@ void SoundSlider::mouseMoveEvent( QMouseEvent *event )
         else
         { /* We are inside */
             b_outside = false;
-            changeValue( event->x() - padding );
+            changeValue( event->x() - paddingL );
             emit sliderMoved( value() );
         }
     }
     else
-        event->ignore();
+    {
+        int i = ( event->x() - paddingL ) * maximum() / WLENGTH;
+        i = __MIN( __MAX( 0, i ), maximum() );
+        setToolTip( QString("%1  \%" ).arg( i ) );
+    }
 }
 
 void SoundSlider::changeValue( int x )
 {
-    setValue( QStyle::sliderValueFromPosition(
-                minimum(), maximum(), x, width() - 2 * padding ) );
+    setValue( x * maximum() / WLENGTH );
 }
 
 void SoundSlider::paintEvent(QPaintEvent *e)
 {
     QPainter painter( this );
-    const int offset = int( double( ( width() - 2 * padding ) * value() ) / maximum() );
-    const QRectF boundsG( padding, 0, offset , pixGradient.height() );
-    painter.drawPixmap( boundsG, pixGradient, boundsG );
+    const int offset = int(
+            double( WLENGTH * value() ) /
+            double( maximum() ) ) + paddingL;
 
-    painter.end();
-/*    QPainter painter( this );
-    printf( "%i\n", value() );
+    const QRectF boundsG( 0, 0, offset , pixGradient.height() );
+    painter.drawPixmap( boundsG, pixGradient, boundsG );
 
-    QLinearGradient gradient( 0.0, 0.0, WLENGTH, WHEIGHT );
-    gradient.setColorAt( 0.0, Qt::white );
-    gradient.setColorAt( 1.0, Qt::blue );
+    const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
+    painter.drawPixmap( boundsO, pixOutside, boundsO );
 
-    painter.setPen( QPen( QBrush( Qt::black ), 0, Qt::SolidLine, Qt::RoundCap ) );
-    painter.setBrush( gradient );
-    painter.setRenderHint( QPainter::Antialiasing );
+    painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
+    QFont font; font.setPixelSize( 9 );
+    painter.setFont( font );
+    const QRect rect( 0, 0, 34, 15 );
+    painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
+                      QString::number( value() ) + '%' );
 
-    painter.end();*/
+    painter.end();
 }