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