]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt4 - Stop the stupidity of the input Slider:
[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  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "util/input_slider.hpp"
25
26 #include <QPaintEvent>
27 #include <QPainter>
28 #include <QBitmap>
29 #include <QStyle>
30
31 InputSlider::InputSlider( QWidget *_parent ) : QSlider( _parent )
32 {
33     InputSlider::InputSlider( Qt::Horizontal, _parent );
34 }
35
36 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
37                                  QSlider( q, _parent )
38 {
39     b_sliding = false;
40     setMinimum( 0 );
41     setMouseTracking(true);
42     setMaximum( 1000 );
43     setSingleStep( 2 );
44     setPageStep( 10 );
45     setTracking( true );
46     secstotimestr( psz_length, 0 );
47     CONNECT( this, valueChanged(int), this, userDrag( int ) );
48 }
49
50 void InputSlider::setPosition( float pos, int a, int b )
51 {
52     if( pos == 0.0 )
53         setEnabled( false );
54     else
55         setEnabled( true );
56
57     if( !b_sliding )
58         setValue( (int)(pos * 1000.0 ) );
59     inputLength = b;
60 }
61
62 void InputSlider::userDrag( int new_value )
63 {
64     if( b_sliding )
65     {
66         float f_pos = (float)(new_value)/1000.0;
67         emit sliderDragged( f_pos );
68     }
69 }
70
71 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
72 {
73     b_sliding = false;
74 }
75
76 void InputSlider::mousePressEvent(QMouseEvent* event)
77 {
78     b_sliding = true ;
79     if( event->button() != Qt::LeftButton &&
80         event->button() != Qt::MidButton )
81     {
82         QSlider::mousePressEvent( event );
83         return;
84     }
85
86     QMouseEvent newEvent( event->type(), event->pos(), event->globalPos(),
87         Qt::MouseButton( event->button() ^ Qt::LeftButton ^ Qt::MidButton ),
88         Qt::MouseButtons( event->buttons() ^ Qt::LeftButton ^ Qt::MidButton ),
89         event->modifiers() );
90     QSlider::mousePressEvent( &newEvent );
91 }
92
93 void InputSlider::mouseMoveEvent(QMouseEvent *event)
94 {
95     if( b_sliding )
96     {
97         QSlider::mouseMoveEvent( event );
98     }
99
100     secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
101     setToolTip( psz_length );
102 }
103
104 /* This work is derived from Amarok's work under GPLv2+
105     - Mark Kretschmann
106     - Gábor Lehel
107    */
108 #define WLENGTH   90 // px
109 #define WHEIGHT   25  // px
110 #define SOUNDMIN  0   // %
111 #define SOUNDMAX  200 // % OR 400 ?
112
113 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
114                         : QAbstractSlider( _parent )
115 {
116     paddingL = 5;
117     paddingR = 3;
118
119     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
120     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
121
122     pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
123
124     const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
125     const QBitmap mask( temp.createHeuristicMask() );
126
127     setMinimumSize( pixOutside.size() );
128
129     pixGradient = QPixmap( mask.size() );
130
131     QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
132     gradient.setColorAt( 0.0, Qt::white );
133     gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
134     gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
135     gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
136
137     QPainter painter( &pixGradient );
138     painter.setPen( Qt::NoPen );
139     painter.setBrush( gradient );
140     painter.drawRect( pixGradient.rect() );
141     painter.end();
142
143     pixGradient.setMask( mask );
144 }
145
146 void SoundSlider::wheelEvent( QWheelEvent *event )
147 {
148     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
149     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
150
151     emit sliderReleased();
152 }
153
154 void SoundSlider::mousePressEvent( QMouseEvent *event )
155 {
156     if( event->button() != Qt::RightButton )
157     {
158         /* We enter the sliding mode */
159         b_sliding = true;
160         i_oldvalue = value();
161         emit sliderPressed();
162         changeValue( event->x() - paddingL );
163     }
164 }
165
166 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
167 {
168     if( event->button() != Qt::RightButton )
169     {
170         if( !b_outside && value() != i_oldvalue )
171         {
172             emit sliderReleased();
173             setValue( value() );
174         }
175         b_sliding = false;
176         b_outside = false;
177     }
178 }
179
180 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
181 {
182     if( b_sliding )
183     {
184         QRect rect( paddingL - 15,    -1,
185                     WLENGTH + 15 * 2, WHEIGHT + 4 );
186         if( !rect.contains( event->pos() ) )
187         { /* We are outside */
188             if ( !b_outside )
189                 setValue( i_oldvalue );
190             b_outside = true;
191         }
192         else
193         { /* We are inside */
194             b_outside = false;
195             changeValue( event->x() - paddingL );
196             emit sliderMoved( value() );
197         }
198     }
199     else
200         event->ignore();
201 }
202
203 void SoundSlider::changeValue( int x )
204 {
205     setValue( x * maximum() / WLENGTH );
206 }
207
208 void SoundSlider::paintEvent(QPaintEvent *e)
209 {
210     QPainter painter( this );
211     const int offset = int(
212             double( WLENGTH * value() ) /
213             double( maximum() ) ) + paddingL;
214
215     const QRectF boundsG( 0, 0, offset , pixGradient.height() );
216     painter.drawPixmap( boundsG, pixGradient, boundsG );
217
218     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
219     painter.drawPixmap( boundsO, pixOutside, boundsO );
220
221     painter.setPen( palette().color( QPalette::Active, QPalette::Mid ) );
222     QFont font; font.setPixelSize( 9 );
223     painter.setFont( font );
224     const QRect rect( 0, 0, 34, 15 );
225     painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
226                       QString::number( value() ) + '%' );
227
228     painter.end();
229 }
230