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;
171 pixOutside = QPixmap( ":/toolbar/volslide-outside" );
173 const QPixmap temp( ":/toolbar/volslide-inside" );
174 const QBitmap mask( temp.createHeuristicMask() );
176 setMinimumSize( pixOutside.size() );
178 pixGradient = QPixmap( mask.size() );
179 pixGradient2 = QPixmap( mask.size() );
181 /* Gradient building from the preferences */
182 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
183 QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
185 QStringList colorList = qfu( psz_colors ).split( ";" );
188 /* Fill with 255 if the list is too short */
189 if( colorList.size() < 12 )
190 for( int i = colorList.size(); i < 12; i++)
191 colorList.append( "255" );
194 #define c(i) colorList.at(i).toInt()
195 #define add_color(gradient, range, c1, c2, c3) \
196 gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
198 /* Desaturated colors */
199 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
200 #define add_desaturated_color(gradient, range, c1, c2, c3) \
201 foo = new QColor( c(c1), c(c2), c(c3) );\
202 desaturate( foo ); gradient.setColorAt( range, *foo );\
205 /* combine the two helpers */
206 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
207 add_color( gradient1, range, c1, c2, c3 ); \
208 add_desaturated_color( gradient2, range, c1, c2, c3 );
211 add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
212 add_colors( gradient, gradient2, 0.22, 3, 4, 5 );
213 add_colors( gradient, gradient2, 0.5, 6, 7, 8 );
214 add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
216 QPainter painter( &pixGradient );
217 painter.setPen( Qt::NoPen );
218 painter.setBrush( gradient );
219 painter.drawRect( pixGradient.rect() );
222 painter.begin( &pixGradient2 );
223 painter.setPen( Qt::NoPen );
224 painter.setBrush( gradient2 );
225 painter.drawRect( pixGradient2.rect() );
228 pixGradient.setMask( mask );
229 pixGradient2.setMask( mask );
232 void SoundSlider::wheelEvent( QWheelEvent *event )
234 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
235 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
237 emit sliderReleased();
240 void SoundSlider::mousePressEvent( QMouseEvent *event )
242 if( event->button() != Qt::RightButton )
244 /* We enter the sliding mode */
246 i_oldvalue = value();
247 emit sliderPressed();
248 changeValue( event->x() - paddingL );
252 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
254 if( event->button() != Qt::RightButton )
256 if( !b_mouseOutside && value() != i_oldvalue )
258 emit sliderReleased();
262 b_mouseOutside = false;
266 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
270 QRect rect( paddingL - 15, -1,
271 WLENGTH + 15 * 2 , WHEIGHT + 5 );
272 if( !rect.contains( event->pos() ) )
273 { /* We are outside */
274 if ( !b_mouseOutside )
275 setValue( i_oldvalue );
276 b_mouseOutside = true;
279 { /* We are inside */
280 b_mouseOutside = false;
281 changeValue( event->x() - paddingL );
282 emit sliderMoved( value() );
287 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
288 i = __MIN( __MAX( 0, i ), maximum() );
289 setToolTip( QString("%1 \%" ).arg( i ) );
293 void SoundSlider::changeValue( int x )
295 setValue( (x * maximum() + 40 ) / WLENGTH );
298 void SoundSlider::setMuted( bool m )
304 void SoundSlider::paintEvent( QPaintEvent *e )
306 QPainter painter( this );
307 QPixmap *pixGradient;
309 pixGradient = &this->pixGradient2;
311 pixGradient = &this->pixGradient;
313 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
315 const QRectF boundsG( 0, 0, offset , pixGradient->height() );
316 painter.drawPixmap( boundsG, *pixGradient, boundsG );
318 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
319 painter.drawPixmap( boundsO, pixOutside, boundsO );
321 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
322 QFont font; font.setPixelSize( 9 );
323 painter.setFont( font );
324 const QRect rect( 0, 0, 34, 15 );
325 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
326 QString::number( value() ) + '%' );