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 *****************************************************************************/
27 #include "util/input_slider.hpp"
29 #include <QPaintEvent>
34 InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent )
36 InputSlider::InputSlider( Qt::Horizontal, _parent );
39 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
44 setMouseTracking(true);
49 secstotimestr( psz_length, 0 );
50 CONNECT( this, valueChanged(int), this, userDrag( int ) );
53 void InputSlider::setPosition( float pos, int a, int b )
61 setValue( (int)(pos * 1000.0 ) );
65 void InputSlider::userDrag( int new_value )
69 float f_pos = (float)(new_value)/1000.0;
70 emit sliderDragged( f_pos );
74 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
79 void InputSlider::mousePressEvent(QMouseEvent* event)
82 if( event->button() != Qt::LeftButton &&
83 event->button() != Qt::MidButton )
85 QSlider::mousePressEvent( event );
89 QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
90 Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
91 Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
93 QSlider::mousePressEvent( &newEvent );
96 void InputSlider::mouseMoveEvent(QMouseEvent *event)
100 QSlider::mouseMoveEvent( event );
103 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
104 setToolTip( psz_length );
107 /* This work is derived from Amarok's work under GPLv2+
111 #define WLENGTH 90 // px
112 #define WHEIGHT 25 // px
113 #define SOUNDMIN 0 // %
114 #define SOUNDMAX 200 // % OR 400 ?
116 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
117 : QAbstractSlider( _parent )
122 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
123 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
124 setMouseTracking( true );
127 pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
129 const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
130 const QBitmap mask( temp.createHeuristicMask() );
132 setMinimumSize( pixOutside.size() );
134 pixGradient = QPixmap( mask.size() );
136 QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
137 gradient.setColorAt( 0.0, Qt::white );
138 gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
139 gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
140 gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
142 QPainter painter( &pixGradient );
143 painter.setPen( Qt::NoPen );
144 painter.setBrush( gradient );
145 painter.drawRect( pixGradient.rect() );
148 pixGradient.setMask( mask );
151 void SoundSlider::wheelEvent( QWheelEvent *event )
153 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
154 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
156 emit sliderReleased();
159 void SoundSlider::mousePressEvent( QMouseEvent *event )
161 if( event->button() != Qt::RightButton )
163 /* We enter the sliding mode */
165 i_oldvalue = value();
166 emit sliderPressed();
167 changeValue( event->x() - paddingL );
171 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
173 if( event->button() != Qt::RightButton )
175 if( !b_outside && value() != i_oldvalue )
177 emit sliderReleased();
185 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
189 QRect rect( paddingL - 15, -1,
190 WLENGTH + 15 * 2, WHEIGHT + 4 );
191 if( !rect.contains( event->pos() ) )
192 { /* We are outside */
194 setValue( i_oldvalue );
198 { /* We are inside */
200 changeValue( event->x() - paddingL );
201 emit sliderMoved( value() );
206 int i = ( event->x() - paddingL ) * maximum() / WLENGTH;
207 i = __MIN( __MAX( 0, i ), maximum() );
208 setToolTip( QString("%1 \%" ).arg( i ) );
212 void SoundSlider::changeValue( int x )
214 setValue( x * maximum() / WLENGTH );
217 void SoundSlider::paintEvent(QPaintEvent *e)
219 QPainter painter( this );
220 const int offset = int(
221 double( WLENGTH * value() ) /
222 double( maximum() ) ) + paddingL;
224 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
225 painter.drawPixmap( boundsG, pixGradient, boundsG );
227 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
228 painter.drawPixmap( boundsO, pixOutside, boundsO );
230 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
231 QFont font; font.setPixelSize( 9 );
232 painter.setFont( font );
233 const QRect rect( 0, 0, 34, 15 );
234 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
235 QString::number( value() ) + '%' );