]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/util/input_slider.cpp
Qt4 - Make the gradient fit INSIDE the boundaries...
[vlc] / modules / gui / qt4 / util / input_slider.cpp
index 019ce0e00d2f679d76d6922519220e6832a42daa..59f205f3f40f16d0c42673641c366ad051d1d4d1 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * input_manager.cpp : Manage an input and interact with its GUI elements
  ****************************************************************************
- * Copyright (C) 2000-2005 the VideoLAN team
- * $Id: wxwidgets.cpp 15731 2006-05-25 14:43:53Z zorglub $
+ * Copyright (C) 2006 the VideoLAN team
+ * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  *
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#include "qt4.hpp"
 #include "util/input_slider.hpp"
 
+#include <QPaintEvent>
+#include <QPainter>
+#include <QBitmap>
+#include <QStyle>
+
 InputSlider::InputSlider( QWidget *_parent ) : DirectSlider( _parent )
 {
     InputSlider::InputSlider( Qt::Horizontal, _parent );
@@ -33,26 +39,24 @@ InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
 {
     mymove = false;
     setMinimum( 0 );
+    setMouseTracking(true);
     setMaximum( 1000 );
     setSingleStep( 2 );
-    setPageStep( 1000 );
+    setPageStep( 10 );
     setTracking( true );
-    connect( this, SIGNAL( valueChanged(int) ), this, SLOT( userDrag( int ) ) );
+    CONNECT( this, valueChanged(int), this, userDrag( int ) );
 }
 
 void InputSlider::setPosition( float pos, int a, int b )
 {
     if( pos == 0.0 )
-    {
         setEnabled( false );
-    }
     else
-    {
         setEnabled( true );
-        mymove = true;
-        setValue( (int)(pos * 1000.0 ) );
-        mymove = false;
-    }
+    mymove = true;
+    setValue( (int)(pos * 1000.0 ) );
+    mymove = false;
+    inputLength = b;
 }
 
 void InputSlider::userDrag( int new_value )
@@ -63,3 +67,124 @@ void InputSlider::userDrag( int new_value )
         emit sliderDragged( f_pos );
     }
 }
+
+void InputSlider::mouseMoveEvent(QMouseEvent *event)
+{
+    char psz_length[MSTRTIME_MAX_SIZE];
+    secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
+    setToolTip( psz_length );
+}
+
+#define WLENGTH   100 // px
+#define WHEIGHT   28  // px
+#define SOUNDMIN  0   // %
+#define SOUNDMAX  200 // % OR 400 ?
+
+SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
+                        : QAbstractSlider( _parent )
+{
+    padding = 3;
+    
+    f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
+    setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
+
+    pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
+
+    const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
+    const QBitmap mask( temp.createHeuristicMask() );
+    
+    setMinimumSize( pixOutside.size() );
+
+    pixGradient = QPixmap( mask.size() );
+
+    QPainter p( &pixGradient );
+    QLinearGradient gradient( padding, 2, WLENGTH , 2 );
+    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 );
+
+    p.drawRect( pixGradient.rect() );
+    p.end();
+
+   pixGradient.setMask( mask );
+}
+
+void SoundSlider::wheelEvent( QWheelEvent *event )
+{
+    int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
+    setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
+
+    emit sliderReleased();
+}
+
+void SoundSlider::mousePressEvent( QMouseEvent *event )
+{
+    if( event->button() != Qt::RightButton )
+    {
+        /* We enter the sliding mode */
+        b_sliding = true;
+        i_oldvalue = value();
+        emit sliderPressed();
+    }
+}
+
+void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
+{
+    if( event->button() != Qt::RightButton )
+    {
+        if( !b_outside && value() != i_oldvalue )
+        {
+            emit sliderReleased();
+            setValue( value() );
+        }
+        b_sliding = false;
+        b_outside = false;
+    }
+}
+
+void SoundSlider::mouseMoveEvent( QMouseEvent *event )
+{
+    if( b_sliding )
+    {
+        QRect rect( padding - 15,     padding - 1,
+                    WLENGTH + 15 * 2, WHEIGHT + 4 );
+        if( !rect.contains( event->pos() ) )
+        { /* We are outside */
+            if ( !b_outside )
+                setValue( i_oldvalue );
+            b_outside = true;
+        }
+        else
+        { /* We are inside */
+            b_outside = false;
+            changeValue( event->x() - padding );
+            emit sliderMoved( value() );
+        }
+    }
+    else
+        event->ignore();
+}
+
+void SoundSlider::changeValue( int x )
+{
+    setValue( QStyle::sliderValueFromPosition(
+                minimum(), maximum(), x, width() - 2 * padding ) );
+}
+
+void SoundSlider::paintEvent(QPaintEvent *e)
+{
+    QPainter painter( this );
+    const int offset = int( double( ( width() - 2 * padding ) * value() ) / maximum() );
+        
+    const QRectF boundsG( 0, 0, offset , pixGradient.height() );
+    painter.drawPixmap( boundsG, pixGradient, boundsG );
+
+    const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
+    painter.drawPixmap( boundsO, pixOutside, boundsO );
+
+    painter.end();
+}
+