]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt4 - Volume slider, move the slider when only clicked on it as ivoire requested.
[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 ) : DirectSlider( _parent )
33 {
34     InputSlider::InputSlider( Qt::Horizontal, _parent );
35 }
36
37 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
38                                  DirectSlider( 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::mouseMoveEvent(QMouseEvent *event)
72 {
73     char psz_length[MSTRTIME_MAX_SIZE];
74     secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
75     setToolTip( psz_length );
76 }
77
78 #define WLENGTH   100 // px
79 #define WHEIGHT   28  // px
80 #define SOUNDMIN  0   // %
81 #define SOUNDMAX  200 // % OR 400 ?
82
83 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
84                         : QAbstractSlider( _parent )
85 {
86     padding = 3;
87
88     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
89     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
90
91     pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
92
93     const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
94     const QBitmap mask( temp.createHeuristicMask() );
95
96     setMinimumSize( pixOutside.size() );
97
98     pixGradient = QPixmap( mask.size() );
99
100     QLinearGradient gradient( padding, 2, WLENGTH , 2 );
101     gradient.setColorAt( 0.0, Qt::white );
102     gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
103     gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
104     gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
105
106     QPainter painter( &pixGradient );
107     painter.setPen( Qt::NoPen );
108     painter.setBrush( gradient );
109     painter.drawRect( pixGradient.rect() );
110     painter.end();
111
112     pixGradient.setMask( mask );
113 }
114
115 void SoundSlider::wheelEvent( QWheelEvent *event )
116 {
117     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
118     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
119
120     emit sliderReleased();
121 }
122
123 void SoundSlider::mousePressEvent( QMouseEvent *event )
124 {
125     if( event->button() != Qt::RightButton )
126     {
127         /* We enter the sliding mode */
128         b_sliding = true;
129         i_oldvalue = value();
130         emit sliderPressed();
131         changeValue( event->x() - padding );
132     }
133 }
134
135 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
136 {
137     if( event->button() != Qt::RightButton )
138     {
139         if( !b_outside && value() != i_oldvalue )
140         {
141             emit sliderReleased();
142             setValue( value() );
143         }
144         b_sliding = false;
145         b_outside = false;
146     }
147 }
148
149 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
150 {
151     if( b_sliding )
152     {
153         QRect rect( padding - 15,     padding - 1,
154                     WLENGTH + 15 * 2, WHEIGHT + 4 );
155         if( !rect.contains( event->pos() ) )
156         { /* We are outside */
157             if ( !b_outside )
158                 setValue( i_oldvalue );
159             b_outside = true;
160         }
161         else
162         { /* We are inside */
163             b_outside = false;
164             changeValue( event->x() - padding );
165             emit sliderMoved( value() );
166         }
167     }
168     else
169         event->ignore();
170 }
171
172 void SoundSlider::changeValue( int x )
173 {
174     setValue( QStyle::sliderValueFromPosition(
175                 minimum(), maximum(), x, width() - 2 * padding ) );
176 }
177
178 void SoundSlider::paintEvent(QPaintEvent *e)
179 {
180     QPainter painter( this );
181     const int offset = int( double( ( width() - 2 * padding ) * value() ) / maximum() );
182
183     const QRectF boundsG( 0, 0, offset , pixGradient.height() );
184     painter.drawPixmap( boundsG, pixGradient, boundsG );
185
186     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
187     painter.drawPixmap( boundsO, pixOutside, boundsO );
188
189     painter.end();
190 }
191