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