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