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 *****************************************************************************/
28 #include "util/input_slider.hpp"
30 #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( 0.0, 0, 0 );
51 secstotimestr( psz_length, 0 );
52 CONNECT( this, valueChanged(int), this, userDrag( int ) );
55 void InputSlider::setPosition( float pos, int a, int b )
63 setValue( (int)(pos * 1000.0 ) );
67 void InputSlider::userDrag( int new_value )
71 float f_pos = (float)(new_value)/1000.0;
72 emit sliderDragged( f_pos );
76 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
81 void InputSlider::mousePressEvent(QMouseEvent* event)
84 if( event->button() != Qt::LeftButton &&
85 event->button() != Qt::MidButton )
87 QSlider::mousePressEvent( event );
91 QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
92 Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
93 Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
95 QSlider::mousePressEvent( &newEvent );
98 void InputSlider::mouseMoveEvent(QMouseEvent *event)
102 QSlider::mouseMoveEvent( event );
105 secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
106 setToolTip( psz_length );
109 /* This work is derived from Amarok's work under GPLv2+
113 #define WLENGTH 90 // px
114 #define WHEIGHT 25 // px
115 #define SOUNDMIN 0 // %
116 #define SOUNDMAX 200 // % OR 400 ?
118 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
120 : QAbstractSlider( _parent )
125 f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
126 setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX );
127 setMouseTracking( true );
130 pixOutside = QPixmap( ":/volslide-outside" );
132 const QPixmap temp( ":/volslide-inside" );
133 const QBitmap mask( temp.createHeuristicMask() );
135 setMinimumSize( pixOutside.size() );
137 pixGradient = QPixmap( mask.size() );
139 /* Gradient building from the preferences */
140 QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
142 QStringList colorList = qfu( psz_colors ).split( ";" );
145 /* Fill with 255 if the list is too short */
146 if( colorList.size() < 12 )
147 for( int i = colorList.size(); i < 12; i++)
148 colorList.append( "255" );
150 #define c(i) colorList.at(i).toInt()
151 gradient.setColorAt( 0.0, QColor( c(0), c(1), c(2) ) );
152 gradient.setColorAt( 0.2, QColor( c(3), c(4), c(5) ) );
153 gradient.setColorAt( 0.5, QColor( c(6), c(7), c(8) ) );
154 gradient.setColorAt( 1.0, QColor( c(9), c(10), c(11) ) );
156 QPainter painter( &pixGradient );
157 painter.setPen( Qt::NoPen );
158 painter.setBrush( gradient );
159 painter.drawRect( pixGradient.rect() );
162 pixGradient.setMask( mask );
165 void SoundSlider::wheelEvent( QWheelEvent *event )
167 int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
168 setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
170 emit sliderReleased();
173 void SoundSlider::mousePressEvent( QMouseEvent *event )
175 if( event->button() != Qt::RightButton )
177 /* We enter the sliding mode */
179 i_oldvalue = value();
180 emit sliderPressed();
181 changeValue( event->x() - paddingL );
185 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
187 if( event->button() != Qt::RightButton )
189 if( !b_outside && value() != i_oldvalue )
191 emit sliderReleased();
199 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
203 QRect rect( paddingL - 15, -1,
204 WLENGTH + 15 * 2, WHEIGHT + 4 );
205 if( !rect.contains( event->pos() ) )
206 { /* We are outside */
208 setValue( i_oldvalue );
212 { /* We are inside */
214 changeValue( event->x() - paddingL );
215 emit sliderMoved( value() );
220 int i = ( event->x() - paddingL ) * maximum() / WLENGTH;
221 i = __MIN( __MAX( 0, i ), maximum() );
222 setToolTip( QString("%1 \%" ).arg( i ) );
226 void SoundSlider::changeValue( int x )
228 setValue( x * maximum() / WLENGTH );
231 void SoundSlider::paintEvent(QPaintEvent *e)
233 QPainter painter( this );
234 const int offset = int(
235 double( WLENGTH * value() ) /
236 double( maximum() ) ) + paddingL;
238 const QRectF boundsG( 0, 0, offset , pixGradient.height() );
239 painter.drawPixmap( boundsG, pixGradient, boundsG );
241 const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
242 painter.drawPixmap( boundsO, pixOutside, boundsO );
244 painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
245 QFont font; font.setPixelSize( 9 );
246 painter.setFont( font );
247 const QRect rect( 0, 0, 34, 15 );
248 painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
249 QString::number( value() ) + '%' );