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 ) :
45 setMouseTracking(true);
50 setPosition( -1.0, 0, 0 );
51 secstotimestr( psz_length, 0 );
52 setFocusPolicy( Qt::NoFocus );
53 CONNECT( this, valueChanged(int), this, userDrag( int ) );
56 void InputSlider::setPosition( float pos, int a, int b )
64 setValue( (int)(pos * 1000.0 ) );
69 void InputSlider::userDrag( int new_value )
73 float f_pos = (float)(new_value)/1000.0;
74 emit sliderDragged( f_pos );
78 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
82 QSlider::mouseReleaseEvent( event );
85 void InputSlider::mousePressEvent(QMouseEvent* event)
88 if( event->button() != Qt::LeftButton &&
89 event->button() != Qt::MidButton )
91 QSlider::mousePressEvent( event );
95 QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
96 Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
97 Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
99 QSlider::mousePressEvent( &newEvent );
102 void InputSlider::mouseMoveEvent(QMouseEvent *event)
106 QSlider::mouseMoveEvent( event );
109 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
110 setToolTip( psz_length );
114 void InputSlider::wheelEvent( QWheelEvent *event)
116 /* Don't do anything if we are for somehow reason sliding */
119 setValue( value() + event->delta()/12 ); /* 12 = 8 * 15 / 10
120 Since delta is in 1/8 of ° and mouse have steps of 15 °
121 and that our slider is in 0.1% and we want one step to be a 1%
122 increment of position */
123 emit sliderDragged( value()/1000.0 );
125 /* We do accept because for we don't want the parent to change the sound
130 /* This work is derived from Amarok's work under GPLv2+
134 #define WLENGTH 80 // px
135 #define WHEIGHT 22 // px
136 #define SOUNDMIN 0 // %
137 #define SOUNDMAX 200 // % OR 400 ?
139 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
141 : QAbstractSlider( _parent )
143 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
144 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
145 setMouseTracking( true );
147 b_mouseOutside = true;
149 pixOutside = QPixmap( ":/volslide-outside" );
151 const QPixmap temp( ":/volslide-inside" );
152 const QBitmap mask( temp.createHeuristicMask() );
154 setMinimumSize( pixOutside.size() );
156 pixGradient = QPixmap( mask.size() );
158 /* Gradient building from the preferences */
159 QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
161 QStringList colorList = qfu( psz_colors ).split( ";" );
164 /* Fill with 255 if the list is too short */
165 if( colorList.size() < 12 )
166 for( int i = colorList.size(); i < 12; i++)
167 colorList.append( "255" );
169 #define c(i) colorList.at(i).toInt()
170 gradient.setColorAt( 0.0, QColor( c(0), c(1), c(2) ) );
171 gradient.setColorAt( 0.22, QColor( c(3), c(4), c(5) ) );
172 gradient.setColorAt( 0.5, QColor( c(6), c(7), c(8) ) );
173 gradient.setColorAt( 1.0, QColor( c(9), c(10), c(11) ) );
175 QPainter painter( &pixGradient );
176 painter.setPen( Qt::NoPen );
177 painter.setBrush( gradient );
178 painter.drawRect( pixGradient.rect() );
181 pixGradient.setMask( mask );
184 void SoundSlider::wheelEvent( QWheelEvent *event )
186 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
187 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
189 emit sliderReleased();
192 void SoundSlider::mousePressEvent( QMouseEvent *event )
194 if( event->button() != Qt::RightButton )
196 /* We enter the sliding mode */
198 i_oldvalue = value();
199 emit sliderPressed();
200 changeValue( event->x() - paddingL );
204 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
206 if( event->button() != Qt::RightButton )
208 if( !b_mouseOutside && value() != i_oldvalue )
210 emit sliderReleased();
214 b_mouseOutside = false;
218 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
222 QRect rect( paddingL - 15, -1,
223 WLENGTH + 15 * 2 , WHEIGHT + 5 );
224 if( !rect.contains( event->pos() ) )
225 { /* We are outside */
226 if ( !b_mouseOutside )
227 setValue( i_oldvalue );
228 b_mouseOutside = true;
231 { /* We are inside */
232 b_mouseOutside = false;
233 changeValue( event->x() - paddingL );
234 emit sliderMoved( value() );
239 int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
240 i = __MIN( __MAX( 0, i ), maximum() );
241 setToolTip( QString("%1 \%" ).arg( i ) );
245 void SoundSlider::changeValue( int x )
247 setValue( (x * maximum() + 40 ) / WLENGTH );
250 void SoundSlider::paintEvent( QPaintEvent *e )
252 QPainter painter( this );
253 const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
255 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
256 painter.drawPixmap( boundsG, pixGradient, boundsG );
258 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
259 painter.drawPixmap( boundsO, pixOutside, boundsO );
261 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
262 QFont font; font.setPixelSize( 9 );
263 painter.setFont( font );
264 const QRect rect( 0, 0, 34, 15 );
265 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
266 QString::number( value() ) + '%' );