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 *****************************************************************************/
29 #include "util/input_slider.hpp"
31 #include <QPaintEvent>
35 InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent )
37 InputSlider::InputSlider( Qt::Horizontal, _parent );
40 InputSlider::InputSlider( Qt::Orientation q, QWidget *_parent ) :
46 timer = new QTimer(this);
47 timer->setSingleShot(true);
53 setMouseTracking(true);
55 setFocusPolicy( Qt::NoFocus );
58 setPosition( -1.0, 0, 0 );
59 secstotimestr( psz_length, 0 );
61 CONNECT( this, valueChanged(int), this, userDrag( int ) );
62 CONNECT( timer, timeout(), this, seekTick() );
65 void InputSlider::setPosition( float pos, int a, int b )
76 setValue( (int)(pos * 1000.0 ) );
81 void InputSlider::userDrag( int new_value )
83 if( b_isSliding && !timer->isActive() )
87 void InputSlider::seekTick()
89 if( value() != lastSeeked )
92 float f_pos = (float)(lastSeeked)/1000.0;
93 emit sliderDragged( f_pos );
97 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
101 QSlider::mouseReleaseEvent( event );
104 void InputSlider::mousePressEvent(QMouseEvent* event)
107 if( event->button() != Qt::LeftButton &&
108 event->button() != Qt::MidButton )
110 QSlider::mousePressEvent( event );
114 QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
115 Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
116 Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
117 event->modifiers() );
118 QSlider::mousePressEvent( &newEvent );
123 void InputSlider::mouseMoveEvent(QMouseEvent *event)
127 QSlider::mouseMoveEvent( event );
130 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
131 setToolTip( psz_length );
135 void InputSlider::wheelEvent( QWheelEvent *event)
137 /* Don't do anything if we are for somehow reason sliding */
140 setValue( value() + event->delta()/12 ); /* 12 = 8 * 15 / 10
141 Since delta is in 1/8 of ° and mouse have steps of 15 °
142 and that our slider is in 0.1% and we want one step to be a 1%
143 increment of position */
144 emit sliderDragged( value()/1000.0 );
146 /* We do accept because for we don't want the parent to change the sound
151 /* This work is derived from Amarok's work under GPLv2+
155 #define WLENGTH 80 // px
156 #define WHEIGHT 22 // px
157 #define SOUNDMIN 0 // %
158 #define SOUNDMAX 200 // % OR 400 ?
160 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
162 : QAbstractSlider( _parent )
164 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
165 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
166 setMouseTracking( true );
168 b_mouseOutside = true;
170 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
172 const QPixmap temp( ":/toolbar/volslide-inside" );
173 const QBitmap mask( temp.createHeuristicMask() );
175 setMinimumSize( pixOutside.size() );
177 pixGradient = QPixmap( mask.size() );
179 /* Gradient building from the preferences */
180 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
182 QStringList colorList = qfu( psz_colors ).split( ";" );
185 /* Fill with 255 if the list is too short */
186 if( colorList.size() < 12 )
187 for( int i = colorList.size(); i < 12; i++)
188 colorList.append( "255" );
190 #define c(i) colorList.at(i).toInt()
191 gradient.setColorAt( 0.0, QColor( c(0), c(1), c(2) ) );
192 gradient.setColorAt( 0.22, QColor( c(3), c(4), c(5) ) );
193 gradient.setColorAt( 0.5, QColor( c(6), c(7), c(8) ) );
194 gradient.setColorAt( 1.0, QColor( c(9), c(10), c(11) ) );
196 QPainter painter( &pixGradient );
197 painter.setPen( Qt::NoPen );
198 painter.setBrush( gradient );
199 painter.drawRect( pixGradient.rect() );
202 pixGradient.setMask( mask );
205 void SoundSlider::wheelEvent( QWheelEvent *event )
207 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
208 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
210 emit sliderReleased();
213 void SoundSlider::mousePressEvent( QMouseEvent *event )
215 if( event->button() != Qt::RightButton )
217 /* We enter the sliding mode */
219 i_oldvalue = value();
220 emit sliderPressed();
221 changeValue( event->x() - paddingL );
225 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
227 if( event->button() != Qt::RightButton )
229 if( !b_mouseOutside && value() != i_oldvalue )
231 emit sliderReleased();
235 b_mouseOutside = false;
239 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
243 QRect rect( paddingL - 15, -1,
244 WLENGTH + 15 * 2 , WHEIGHT + 5 );
245 if( !rect.contains( event->pos() ) )
246 { /* We are outside */
247 if ( !b_mouseOutside )
248 setValue( i_oldvalue );
249 b_mouseOutside = true;
252 { /* We are inside */
253 b_mouseOutside = false;
254 changeValue( event->x() - paddingL );
255 emit sliderMoved( value() );
260 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
261 i = __MIN( __MAX( 0, i ), maximum() );
262 setToolTip( QString("%1 \%" ).arg( i ) );
266 void SoundSlider::changeValue( int x )
268 setValue( (x * maximum() + 40 ) / WLENGTH );
271 void SoundSlider::paintEvent( QPaintEvent *e )
273 QPainter painter( this );
274 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
276 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
277 painter.drawPixmap( boundsG, pixGradient, boundsG );
279 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
280 painter.drawPixmap( boundsO, pixOutside, boundsO );
282 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
283 QFont font; font.setPixelSize( 9 );
284 painter.setFont( font );
285 const QRect rect( 0, 0, 34, 15 );
286 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
287 QString::number( value() ) + '%' );