]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
ed37c94d89e557ec012ef1f91b4fbd7ab4f7de69
[vlc] / modules / gui / qt4 / util / input_slider.cpp
1 /*****************************************************************************
2  * input_manager.cpp : Manage an input and interact with its GUI elements
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "util/input_slider.hpp"
30
31 #include <QPaintEvent>
32 #include <QPainter>
33 #include <QBitmap>
34
35 InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent )
36 {
37     InputSlider::InputSlider( Qt::Horizontal, _parent );
38 }
39
40 InputSlider::InputSlider( Qt::Orientation q, QWidget *_parent ) :
41                                  QSlider( q, _parent )
42 {
43     b_isSliding = false;
44     lastSeeked  = 0;
45
46     timer = new QTimer(this);
47     timer->setSingleShot(true);
48
49     /* Properties */
50     setRange( 0, 1000 );
51     setSingleStep( 2 );
52     setPageStep( 10 );
53     setMouseTracking(true);
54     setTracking( true );
55     setFocusPolicy( Qt::NoFocus );
56
57     /* Init to 0 */
58     setPosition( -1.0, 0, 0 );
59     secstotimestr( psz_length, 0 );
60
61     CONNECT( this, valueChanged(int), this, userDrag( int ) );
62     CONNECT( timer, timeout(), this, seekTick() );
63 }
64
65 void InputSlider::setPosition( float pos, int a, int b )
66 {
67     if( pos == -1.0 )
68     {
69         setEnabled( false );
70         b_isSliding = false;
71     }
72     else
73         setEnabled( true );
74
75     if( !b_isSliding )
76         setValue( (int)(pos * 1000.0 ) );
77
78     inputLength = b;
79 }
80
81 void InputSlider::userDrag( int new_value )
82 {
83     if( b_isSliding && !timer->isActive() )
84         timer->start( 150 );
85 }
86
87 void InputSlider::seekTick()
88 {
89     if( value() != lastSeeked )
90     {
91         lastSeeked = value();
92         float f_pos = (float)(lastSeeked)/1000.0;
93         emit sliderDragged( f_pos );
94     }
95 }
96
97 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
98 {
99     b_isSliding = false;
100     event->accept();
101     QSlider::mouseReleaseEvent( event );
102 }
103
104 void InputSlider::mousePressEvent(QMouseEvent* event)
105 {
106     b_isSliding = true ;
107     if( event->button() != Qt::LeftButton &&
108         event->button() != Qt::MidButton )
109     {
110         QSlider::mousePressEvent( event );
111         return;
112     }
113
114     QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
115         Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
116         Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
117         event->modifiers() );
118     QSlider::mousePressEvent( &newEvent );
119
120     seekTick();
121 }
122
123 void InputSlider::mouseMoveEvent(QMouseEvent *event)
124 {
125     if( b_isSliding )
126     {
127         QSlider::mouseMoveEvent( event );
128     }
129
130     secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
131     setToolTip( psz_length );
132     event->accept();
133 }
134
135 void InputSlider::wheelEvent( QWheelEvent *event)
136 {
137     /* Don't do anything if we are for somehow reason sliding */
138     if( !b_isSliding )
139     {
140         setValue( value() + event->delta()/12 ); /* 12 = 8 * 15 / 10
141          Since delta is in 1/8 of ° and mouse have steps of 15 °
142          and that our slider is in 0.1% and we want one step to be a 1%
143          increment of position */
144         emit sliderDragged( value()/1000.0 );
145     }
146     /* We do accept because for we don't want the parent to change the sound
147        vol */
148     event->accept();
149 }
150
151 /* This work is derived from Amarok's work under GPLv2+
152     - Mark Kretschmann
153     - Gábor Lehel
154    */
155 #define WLENGTH   80 // px
156 #define WHEIGHT   22  // px
157 #define SOUNDMIN  0   // %
158 #define SOUNDMAX  200 // % OR 400 ?
159
160 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
161                           char *psz_colors )
162                         : QAbstractSlider( _parent )
163 {
164     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
165     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
166     setMouseTracking( true );
167     b_isSliding = false;
168     b_mouseOutside = true;
169
170     pixOutside = QPixmap( ":/toolbar/volslide-outside" );
171
172     const QPixmap temp( ":/toolbar/volslide-inside" );
173     const QBitmap mask( temp.createHeuristicMask() );
174
175     setMinimumSize( pixOutside.size() );
176
177     pixGradient = QPixmap( mask.size() );
178
179     /* Gradient building from the preferences */
180     QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
181
182     QStringList colorList = qfu( psz_colors ).split( ";" );
183     free( psz_colors );
184
185     /* Fill with 255 if the list is too short */
186     if( colorList.size() < 12 )
187         for( int i = colorList.size(); i < 12; i++)
188             colorList.append( "255" );
189
190 #define c(i) colorList.at(i).toInt()
191     gradient.setColorAt( 0.0, QColor( c(0), c(1), c(2) ) );
192     gradient.setColorAt( 0.22, QColor( c(3), c(4), c(5) ) );
193     gradient.setColorAt( 0.5, QColor( c(6), c(7), c(8) ) );
194     gradient.setColorAt( 1.0, QColor( c(9), c(10), c(11) ) );
195
196     QPainter painter( &pixGradient );
197     painter.setPen( Qt::NoPen );
198     painter.setBrush( gradient );
199     painter.drawRect( pixGradient.rect() );
200     painter.end();
201
202     pixGradient.setMask( mask );
203 }
204
205 void SoundSlider::wheelEvent( QWheelEvent *event )
206 {
207     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
208     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
209
210     emit sliderReleased();
211 }
212
213 void SoundSlider::mousePressEvent( QMouseEvent *event )
214 {
215     if( event->button() != Qt::RightButton )
216     {
217         /* We enter the sliding mode */
218         b_isSliding = true;
219         i_oldvalue = value();
220         emit sliderPressed();
221         changeValue( event->x() - paddingL );
222     }
223 }
224
225 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
226 {
227     if( event->button() != Qt::RightButton )
228     {
229         if( !b_mouseOutside && value() != i_oldvalue )
230         {
231             emit sliderReleased();
232             setValue( value() );
233         }
234         b_isSliding = false;
235         b_mouseOutside = false;
236     }
237 }
238
239 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
240 {
241     if( b_isSliding )
242     {
243         QRect rect( paddingL - 15,    -1,
244                     WLENGTH + 15 * 2 , WHEIGHT + 5 );
245         if( !rect.contains( event->pos() ) )
246         { /* We are outside */
247             if ( !b_mouseOutside )
248                 setValue( i_oldvalue );
249             b_mouseOutside = true;
250         }
251         else
252         { /* We are inside */
253             b_mouseOutside = false;
254             changeValue( event->x() - paddingL );
255             emit sliderMoved( value() );
256         }
257     }
258     else
259     {
260         int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
261         i = __MIN( __MAX( 0, i ), maximum() );
262         setToolTip( QString("%1  \%" ).arg( i ) );
263     }
264 }
265
266 void SoundSlider::changeValue( int x )
267 {
268     setValue( (x * maximum() + 40 ) / WLENGTH );
269 }
270
271 void SoundSlider::paintEvent( QPaintEvent *e )
272 {
273     QPainter painter( this );
274     const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
275
276     const QRectF boundsG( 0, 0, offset , pixGradient.height() );
277     painter.drawPixmap( boundsG, pixGradient, boundsG );
278
279     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
280     painter.drawPixmap( boundsO, pixOutside, boundsO );
281
282     painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
283     QFont font; font.setPixelSize( 9 );
284     painter.setFont( font );
285     const QRect rect( 0, 0, 34, 15 );
286     painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
287                       QString::number( value() ) + '%' );
288
289     painter.end();
290     e->accept();
291 }
292