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 ) : DirectSlider( _parent )
34 InputSlider::InputSlider( Qt::Horizontal, _parent );
37 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
38 DirectSlider( q, _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::mouseMoveEvent(QMouseEvent *event)
73 char psz_length[MSTRTIME_MAX_SIZE];
74 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
75 setToolTip( psz_length );
78 /* This work is derived from Amarok's work under GPLv2+
82 #define WLENGTH 90 // px
83 #define WHEIGHT 25 // px
84 #define SOUNDMIN 0 // %
85 #define SOUNDMAX 200 // % OR 400 ?
87 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
88 : QAbstractSlider( _parent )
93 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
94 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
96 pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
98 const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
99 const QBitmap mask( temp.createHeuristicMask() );
101 setMinimumSize( pixOutside.size() );
103 pixGradient = QPixmap( mask.size() );
105 QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
106 gradient.setColorAt( 0.0, Qt::white );
107 gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
108 gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
109 gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
111 QPainter painter( &pixGradient );
112 painter.setPen( Qt::NoPen );
113 painter.setBrush( gradient );
114 painter.drawRect( pixGradient.rect() );
117 pixGradient.setMask( mask );
120 void SoundSlider::wheelEvent( QWheelEvent *event )
122 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
123 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
125 emit sliderReleased();
128 void SoundSlider::mousePressEvent( QMouseEvent *event )
130 if( event->button() != Qt::RightButton )
132 /* We enter the sliding mode */
134 i_oldvalue = value();
135 emit sliderPressed();
136 changeValue( event->x() - paddingL );
140 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
142 if( event->button() != Qt::RightButton )
144 if( !b_outside && value() != i_oldvalue )
146 emit sliderReleased();
154 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
158 QRect rect( paddingL - 15, -1,
159 WLENGTH + 15 * 2, WHEIGHT + 4 );
160 if( !rect.contains( event->pos() ) )
161 { /* We are outside */
163 setValue( i_oldvalue );
167 { /* We are inside */
169 changeValue( event->x() - paddingL );
170 emit sliderMoved( value() );
177 void SoundSlider::changeValue( int x )
179 setValue( x * maximum() / WLENGTH );
182 void SoundSlider::paintEvent(QPaintEvent *e)
184 QPainter painter( this );
185 const int offset = int(
186 double( WLENGTH * value() ) /
187 double( maximum() ) ) + paddingL;
189 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
190 painter.drawPixmap( boundsG, pixGradient, boundsG );
192 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
193 painter.drawPixmap( boundsO, pixOutside, boundsO );
195 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
196 QFont font; font.setPixelSize( 9 );
197 painter.setFont( font );
198 const QRect rect( 0, 0, 34, 15 );
199 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
200 QString::number( value() ) + '%' );