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 );
126 pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
128 const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
129 const QBitmap mask( temp.createHeuristicMask() );
131 setMinimumSize( pixOutside.size() );
133 pixGradient = QPixmap( mask.size() );
135 QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
136 gradient.setColorAt( 0.0, Qt::white );
137 gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
138 gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
139 gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
141 QPainter painter( &pixGradient );
142 painter.setPen( Qt::NoPen );
143 painter.setBrush( gradient );
144 painter.drawRect( pixGradient.rect() );
147 pixGradient.setMask( mask );
150 void SoundSlider::wheelEvent( QWheelEvent *event )
152 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
153 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
155 emit sliderReleased();
158 void SoundSlider::mousePressEvent( QMouseEvent *event )
160 if( event->button() != Qt::RightButton )
162 /* We enter the sliding mode */
164 i_oldvalue = value();
165 emit sliderPressed();
166 changeValue( event->x() - paddingL );
170 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
172 if( event->button() != Qt::RightButton )
174 if( !b_outside && value() != i_oldvalue )
176 emit sliderReleased();
184 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
188 QRect rect( paddingL - 15, -1,
189 WLENGTH + 15 * 2, WHEIGHT + 4 );
190 if( !rect.contains( event->pos() ) )
191 { /* We are outside */
193 setValue( i_oldvalue );
197 { /* We are inside */
199 changeValue( event->x() - paddingL );
200 emit sliderMoved( value() );
205 setToolTip( QString("%1 \%" )
206 .arg( (int)( event->x() - paddingL ) * maximum() / WLENGTH ) );
211 void SoundSlider::changeValue( int x )
213 setValue( x * maximum() / WLENGTH );
216 void SoundSlider::paintEvent(QPaintEvent *e)
218 QPainter painter( this );
219 const int offset = int(
220 double( WLENGTH * value() ) /
221 double( maximum() ) ) + paddingL;
223 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
224 painter.drawPixmap( boundsG, pixGradient, boundsG );
226 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
227 painter.drawPixmap( boundsO, pixOutside, boundsO );
229 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
230 QFont font; font.setPixelSize( 9 );
231 painter.setFont( font );
232 const QRect rect( 0, 0, 34, 15 );
233 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
234 QString::number( value() ) + '%' );