]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
libass: upgrade libass contrib with the latest enhancements from Mplayer.
[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     setPosition( 0.0, 0, 0 );
51     secstotimestr( psz_length, 0 );
52     CONNECT( this, valueChanged(int), this, userDrag( int ) );
53 }
54
55 void InputSlider::setPosition( float pos, int a, int b )
56 {
57     if( pos == 0.0 )
58         setEnabled( false );
59     else
60         setEnabled( true );
61
62     if( !b_sliding )
63         setValue( (int)(pos * 1000.0 ) );
64     inputLength = b;
65 }
66
67 void InputSlider::userDrag( int new_value )
68 {
69     if( b_sliding )
70     {
71         float f_pos = (float)(new_value)/1000.0;
72         emit sliderDragged( f_pos );
73     }
74 }
75
76 void InputSlider::mouseReleaseEvent( QMouseEvent *event )
77 {
78     b_sliding = false;
79 }
80
81 void InputSlider::mousePressEvent(QMouseEvent* event)
82 {
83     b_sliding = true ;
84     if( event->button() != Qt::LeftButton &&
85         event->button() != Qt::MidButton )
86     {
87         QSlider::mousePressEvent( event );
88         return;
89     }
90
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 ),
94         event->modifiers() );
95     QSlider::mousePressEvent( &newEvent );
96 }
97
98 void InputSlider::mouseMoveEvent(QMouseEvent *event)
99 {
100     if( b_sliding )
101     {
102         QSlider::mouseMoveEvent( event );
103     }
104
105     secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
106     setToolTip( psz_length );
107 }
108
109 /* This work is derived from Amarok's work under GPLv2+
110     - Mark Kretschmann
111     - Gábor Lehel
112    */
113 #define WLENGTH   90 // px
114 #define WHEIGHT   25  // px
115 #define SOUNDMIN  0   // %
116 #define SOUNDMAX  200 // % OR 400 ?
117
118 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
119                           char *psz_colors )
120                         : QAbstractSlider( _parent )
121 {
122     paddingL = 5;
123     paddingR = 3;
124
125     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
126     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
127     setMouseTracking( true );
128     b_sliding = false;
129
130     pixOutside = QPixmap( ":/volslide-outside" );
131
132     const QPixmap temp( ":/volslide-inside" );
133     const QBitmap mask( temp.createHeuristicMask() );
134
135     setMinimumSize( pixOutside.size() );
136
137     pixGradient = QPixmap( mask.size() );
138
139     /* Gradient building from the preferences */
140     QLinearGradient gradient( paddingL, 4, WLENGTH + paddingL , 4 );
141
142     QStringList colorList = qfu( psz_colors ).split( ";" );
143     free( psz_colors );
144
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" );
149
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) ) );
155
156     QPainter painter( &pixGradient );
157     painter.setPen( Qt::NoPen );
158     painter.setBrush( gradient );
159     painter.drawRect( pixGradient.rect() );
160     painter.end();
161
162     pixGradient.setMask( mask );
163 }
164
165 void SoundSlider::wheelEvent( QWheelEvent *event )
166 {
167     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
168     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
169
170     emit sliderReleased();
171 }
172
173 void SoundSlider::mousePressEvent( QMouseEvent *event )
174 {
175     if( event->button() != Qt::RightButton )
176     {
177         /* We enter the sliding mode */
178         b_sliding = true;
179         i_oldvalue = value();
180         emit sliderPressed();
181         changeValue( event->x() - paddingL );
182     }
183 }
184
185 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
186 {
187     if( event->button() != Qt::RightButton )
188     {
189         if( !b_outside && value() != i_oldvalue )
190         {
191             emit sliderReleased();
192             setValue( value() );
193         }
194         b_sliding = false;
195         b_outside = false;
196     }
197 }
198
199 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
200 {
201     if( b_sliding )
202     {
203         QRect rect( paddingL - 15,    -1,
204                     WLENGTH + 15 * 2, WHEIGHT + 4 );
205         if( !rect.contains( event->pos() ) )
206         { /* We are outside */
207             if ( !b_outside )
208                 setValue( i_oldvalue );
209             b_outside = true;
210         }
211         else
212         { /* We are inside */
213             b_outside = false;
214             changeValue( event->x() - paddingL );
215             emit sliderMoved( value() );
216         }
217     }
218     else
219     {
220         int i = ( event->x() - paddingL ) * maximum() / WLENGTH;
221         i = __MIN( __MAX( 0, i ), maximum() );
222         setToolTip( QString("%1  \%" ).arg( i ) );
223     }
224 }
225
226 void SoundSlider::changeValue( int x )
227 {
228     setValue( x * maximum() / WLENGTH );
229 }
230
231 void SoundSlider::paintEvent(QPaintEvent *e)
232 {
233     QPainter painter( this );
234     const int offset = int(
235             double( WLENGTH * value() ) /
236             double( maximum() ) ) + paddingL;
237
238     const QRectF boundsG( 0, 0, offset , pixGradient.height() );
239     painter.drawPixmap( boundsG, pixGradient, boundsG );
240
241     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
242     painter.drawPixmap( boundsO, pixOutside, boundsO );
243
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() ) + '%' );
250
251     painter.end();
252 }
253