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