]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt4 - take care of the volume-step value from the preferences.
[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
30 #include <QStyle>
31 InputSlider::InputSlider( QWidget *_parent ) : DirectSlider( _parent )
32 {
33     InputSlider::InputSlider( Qt::Horizontal, _parent );
34 }
35
36 InputSlider::InputSlider( Qt::Orientation q,QWidget *_parent ) :
37                                  DirectSlider( q, _parent )
38 {
39     mymove = false;
40     setMinimum( 0 );
41     setMouseTracking(true);
42     setMaximum( 1000 );
43     setSingleStep( 2 );
44     setPageStep( 10 );
45     setTracking( true );
46     CONNECT( this, valueChanged(int), this, userDrag( int ) );
47 }
48
49 void InputSlider::setPosition( float pos, int a, int b )
50 {
51     if( pos == 0.0 )
52         setEnabled( false );
53     else
54         setEnabled( true );
55     mymove = true;
56     setValue( (int)(pos * 1000.0 ) );
57     mymove = false;
58     inputLength = b;
59 }
60
61 void InputSlider::userDrag( int new_value )
62 {
63     float f_pos = (float)(new_value)/1000.0;
64     if( !mymove )
65     {
66         emit sliderDragged( f_pos );
67     }
68 }
69
70 void InputSlider::mouseMoveEvent(QMouseEvent *event)
71 {
72     char psz_length[MSTRTIME_MAX_SIZE];
73     secstotimestr( psz_length, ( event->x() * inputLength) / size().width() );
74     setToolTip( psz_length );
75 }
76
77 #define WLENGTH   100 // px
78 #define WHEIGHT   25  // px
79 #define SOUNDMIN  0   // %
80 #define SOUNDMAX  200 // % OR 400 ?
81
82 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard )
83                         : QAbstractSlider( _parent )
84 {
85     padding = 5;
86     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
87     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
88
89     pixGradient = QPixmap( QSize( WLENGTH, WHEIGHT ) );
90 //    QBixmap mask = QBitmap( QPixmap );
91
92     QPainter p( &pixGradient );
93     QLinearGradient gradient( 0, 0, WLENGTH, WHEIGHT );
94     gradient.setColorAt( 0.0, Qt::white );
95     gradient.setColorAt( 1.0, Qt::blue );
96     p.setPen( Qt::NoPen );
97     p.setBrush( gradient );
98
99 //static const QPointF points[3] = { QPointF( 0.0, WHEIGHT ),
100   //        QPointF( WLENGTH, WHEIGHT ),  QPointF( WLENGTH, 0.0 ) };
101
102  //   p.drawConvexPolygon( points, 3 );
103
104     p.drawRect( pixGradient.rect() );
105     p.end();
106
107  //   pixGradient.setMask( mask );
108 }
109
110 void SoundSlider::wheelEvent( QWheelEvent *event )
111 {
112     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
113     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
114
115     emit sliderReleased();
116 }
117
118 void SoundSlider::mousePressEvent( QMouseEvent *event )
119 {
120     if( event->button() != Qt::RightButton )
121     {
122         /* We enter the sliding mode */
123         b_sliding = true;
124         i_oldvalue = value();
125         emit sliderPressed();
126     }
127 }
128
129 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
130 {
131     if( event->button() != Qt::RightButton )
132     {
133         if( !b_outside && value() != i_oldvalue )
134         {
135             emit sliderReleased();
136             setValue( value() );
137         }
138         b_sliding = false;
139         b_outside = false;
140     }
141 }
142
143 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
144 {
145     if( b_sliding )
146     {
147         QRect rect( padding - 15,     padding - 1,
148                     WLENGTH + 15 * 2, WHEIGHT + 2 );
149         if( !rect.contains( event->pos() ) )
150         { /* We are outside */
151             if ( !b_outside )
152                 setValue( i_oldvalue );
153             b_outside = true;
154         }
155         else
156         { /* We are inside */
157             b_outside = false;
158             changeValue( event->x() - padding );
159             emit sliderMoved( value() );
160         }
161     }
162     else
163         event->ignore();
164 }
165
166 void SoundSlider::changeValue( int x )
167 {
168     setValue( QStyle::sliderValueFromPosition(
169                 minimum(), maximum(), x, width() - 2 * padding ) );
170 }
171
172 void SoundSlider::paintEvent(QPaintEvent *e)
173 {
174     QPainter painter( this );
175     const int offset = int( double( ( width() - 2 * padding ) * value() ) / maximum() );
176     const QRectF boundsG( padding, 0, offset , pixGradient.height() );
177     painter.drawPixmap( boundsG, pixGradient, boundsG );
178
179     painter.end();
180 /*    QPainter painter( this );
181     printf( "%i\n", value() );
182
183     QLinearGradient gradient( 0.0, 0.0, WLENGTH, WHEIGHT );
184     gradient.setColorAt( 0.0, Qt::white );
185     gradient.setColorAt( 1.0, Qt::blue );
186
187     painter.setPen( QPen( QBrush( Qt::black ), 0, Qt::SolidLine, Qt::RoundCap ) );
188     painter.setBrush( gradient );
189     painter.setRenderHint( QPainter::Antialiasing );
190
191     painter.end();*/
192 }
193