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 *****************************************************************************/
25 #include "util/input_slider.hpp"
27 #include <QPaintEvent>
32 InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent )
34 InputSlider::InputSlider( Qt::Horizontal, _parent );
37 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
42 setMouseTracking(true);
47 CONNECT( this, valueChanged(int), this, userDrag( int ) );
50 void InputSlider::setPosition( float pos, int a, int b )
57 setValue( (int)(pos * 1000.0 ) );
62 void InputSlider::userDrag( int new_value )
64 float f_pos = (float)(new_value)/1000.0;
67 emit sliderDragged( f_pos );
71 void InputSlider::mousePressEvent(QMouseEvent* event)
73 if( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
75 QSlider::mousePressEvent( event );
79 QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
80 Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
81 Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
83 QSlider::mousePressEvent( &newEvent );
86 void InputSlider::mouseMoveEvent(QMouseEvent *event)
88 char psz_length[MSTRTIME_MAX_SIZE];
89 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
90 setToolTip( psz_length );
93 /* This work is derived from Amarok's work under GPLv2+
97 #define WLENGTH 90 // px
98 #define WHEIGHT 25 // px
99 #define SOUNDMIN 0 // %
100 #define SOUNDMAX 200 // % OR 400 ?
102 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
103 : QAbstractSlider( _parent )
108 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
109 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
111 pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
113 const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
114 const QBitmap mask( temp.createHeuristicMask() );
116 setMinimumSize( pixOutside.size() );
118 pixGradient = QPixmap( mask.size() );
120 QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
121 gradient.setColorAt( 0.0, Qt::white );
122 gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
123 gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
124 gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
126 QPainter painter( &pixGradient );
127 painter.setPen( Qt::NoPen );
128 painter.setBrush( gradient );
129 painter.drawRect( pixGradient.rect() );
132 pixGradient.setMask( mask );
135 void SoundSlider::wheelEvent( QWheelEvent *event )
137 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
138 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
140 emit sliderReleased();
143 void SoundSlider::mousePressEvent( QMouseEvent *event )
145 if( event->button() != Qt::RightButton )
147 /* We enter the sliding mode */
149 i_oldvalue = value();
150 emit sliderPressed();
151 changeValue( event->x() - paddingL );
155 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
157 if( event->button() != Qt::RightButton )
159 if( !b_outside && value() != i_oldvalue )
161 emit sliderReleased();
169 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
173 QRect rect( paddingL - 15, -1,
174 WLENGTH + 15 * 2, WHEIGHT + 4 );
175 if( !rect.contains( event->pos() ) )
176 { /* We are outside */
178 setValue( i_oldvalue );
182 { /* We are inside */
184 changeValue( event->x() - paddingL );
185 emit sliderMoved( value() );
192 void SoundSlider::changeValue( int x )
194 setValue( x * maximum() / WLENGTH );
197 void SoundSlider::paintEvent(QPaintEvent *e)
199 QPainter painter( this );
200 const int offset = int(
201 double( WLENGTH * value() ) /
202 double( maximum() ) ) + paddingL;
204 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
205 painter.drawPixmap( boundsG, pixGradient, boundsG );
207 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
208 painter.drawPixmap( boundsO, pixOutside, boundsO );
210 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
211 QFont font; font.setPixelSize( 9 );
212 painter.setFont( font );
213 const QRect rect( 0, 0, 34, 15 );
214 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
215 QString::number( value() ) + '%' );