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>
8 * Jean-Baptiste Kempf <jb@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
28 #include "util/input_slider.hpp"
30 #include <QPaintEvent>
35 InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent )
37 InputSlider::InputSlider( Qt::Horizontal, _parent );
40 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
45 setMouseTracking(true);
50 secstotimestr( psz_length, 0 );
51 CONNECT( this, valueChanged(int), this, userDrag( int ) );
54 void InputSlider::setPosition( float pos, int a, int b )
62 setValue( (int)(pos * 1000.0 ) );
66 void InputSlider::userDrag( int new_value )
70 float f_pos = (float)(new_value)/1000.0;
71 emit sliderDragged( f_pos );
75 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
80 void InputSlider::mousePressEvent(QMouseEvent* event)
83 if( event->button() != Qt::LeftButton &&
84 event->button() != Qt::MidButton )
86 QSlider::mousePressEvent( event );
90 QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
91 Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
92 Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
94 QSlider::mousePressEvent( &newEvent );
97 void InputSlider::mouseMoveEvent(QMouseEvent *event)
101 QSlider::mouseMoveEvent( event );
104 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
105 setToolTip( psz_length );
108 /* This work is derived from Amarok's work under GPLv2+
112 #define WLENGTH 90 // px
113 #define WHEIGHT 25 // px
114 #define SOUNDMIN 0 // %
115 #define SOUNDMAX 200 // % OR 400 ?
117 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
118 : QAbstractSlider( _parent )
123 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
124 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
125 setMouseTracking( true );
128 pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
130 const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
131 const QBitmap mask( temp.createHeuristicMask() );
133 setMinimumSize( pixOutside.size() );
135 pixGradient = QPixmap( mask.size() );
137 QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
138 gradient.setColorAt( 0.0, QColor( 255, 255, 255 ) );
139 gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
140 gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
141 gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
143 QPainter painter( &pixGradient );
144 painter.setPen( Qt::NoPen );
145 painter.setBrush( gradient );
146 painter.drawRect( pixGradient.rect() );
149 pixGradient.setMask( mask );
152 void SoundSlider::wheelEvent( QWheelEvent *event )
154 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
155 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
157 emit sliderReleased();
160 void SoundSlider::mousePressEvent( QMouseEvent *event )
162 if( event->button() != Qt::RightButton )
164 /* We enter the sliding mode */
166 i_oldvalue = value();
167 emit sliderPressed();
168 changeValue( event->x() - paddingL );
172 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
174 if( event->button() != Qt::RightButton )
176 if( !b_outside && value() != i_oldvalue )
178 emit sliderReleased();
186 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
190 QRect rect( paddingL - 15, -1,
191 WLENGTH + 15 * 2, WHEIGHT + 4 );
192 if( !rect.contains( event->pos() ) )
193 { /* We are outside */
195 setValue( i_oldvalue );
199 { /* We are inside */
201 changeValue( event->x() - paddingL );
202 emit sliderMoved( value() );
207 int i = ( event->x() - paddingL ) * maximum() / WLENGTH;
208 i = __MIN( __MAX( 0, i ), maximum() );
209 setToolTip( QString("%1 \%" ).arg( i ) );
213 void SoundSlider::changeValue( int x )
215 setValue( x * maximum() / WLENGTH );
218 void SoundSlider::paintEvent(QPaintEvent *e)
220 QPainter painter( this );
221 const int offset = int(
222 double( WLENGTH * value() ) /
223 double( maximum() ) ) + paddingL;
225 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
226 painter.drawPixmap( boundsG, pixGradient, boundsG );
228 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
229 painter.drawPixmap( boundsO, pixOutside, boundsO );
231 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
232 QFont font; font.setPixelSize( 9 );
233 painter.setFont( font );
234 const QRect rect( 0, 0, 34, 15 );
235 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
236 QString::number( value() ) + '%' );