1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #include "util/input_slider.hpp"
27 #include <QPaintEvent>
32 InputSlider::InputSlider( QWidget *_parent ) : DirectSlider( _parent )
34 InputSlider::InputSlider( Qt::Horizontal, _parent );
37 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
38 DirectSlider( q, _parent )
42 setMouseTracking(true);
47 CONNECT( this, valueChanged(int), this, userDrag( int ) );
50 void InputSlider::setPosition( float pos, int a, int b )
57 setValue( (int)(pos * 1000.0 ) );
62 void InputSlider::userDrag( int new_value )
64 float f_pos = (float)(new_value)/1000.0;
67 emit sliderDragged( f_pos );
71 void InputSlider::mouseMoveEvent(QMouseEvent *event)
73 char psz_length[MSTRTIME_MAX_SIZE];
74 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
75 setToolTip( psz_length );
78 #define WLENGTH 100 // px
79 #define WHEIGHT 28 // px
80 #define SOUNDMIN 0 // %
81 #define SOUNDMAX 200 // % OR 400 ?
83 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
84 : QAbstractSlider( _parent )
87 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
88 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
90 pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
92 const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
93 const QBitmap mask( temp.createHeuristicMask() );
95 pixGradient = QPixmap( mask.size() );
97 QPainter p( &pixGradient );
98 QLinearGradient gradient( 0, padding, WLENGTH + 2 * padding, padding );
99 gradient.setColorAt( 0.0, Qt::white );
100 gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
101 gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
102 gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
103 p.setPen( Qt::NoPen );
104 p.setBrush( gradient );
106 p.drawRect( pixGradient.rect() );
109 pixGradient.setMask( mask );
112 void SoundSlider::wheelEvent( QWheelEvent *event )
114 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
115 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
117 emit sliderReleased();
120 void SoundSlider::mousePressEvent( QMouseEvent *event )
122 if( event->button() != Qt::RightButton )
124 /* We enter the sliding mode */
126 i_oldvalue = value();
127 emit sliderPressed();
131 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
133 if( event->button() != Qt::RightButton )
135 if( !b_outside && value() != i_oldvalue )
137 emit sliderReleased();
145 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
149 QRect rect( padding - 15, padding - 1,
150 WLENGTH + 15 * 2, WHEIGHT + 4 );
151 if( !rect.contains( event->pos() ) )
152 { /* We are outside */
154 setValue( i_oldvalue );
158 { /* We are inside */
160 changeValue( event->x() - padding );
161 emit sliderMoved( value() );
168 void SoundSlider::changeValue( int x )
170 setValue( QStyle::sliderValueFromPosition(
171 minimum(), maximum(), x, width() - 2 * padding ) );
174 void SoundSlider::paintEvent(QPaintEvent *e)
176 QPainter painter( this );
177 const int offset = int( double( ( width() - 2 * padding ) * value() ) / maximum() );
178 const QRectF boundsG( padding, padding, offset , pixGradient.height() );
179 painter.drawPixmap( boundsG, pixGradient, boundsG );
181 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
182 painter.drawPixmap( boundsO, pixOutside, boundsO );