]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt4 - I couldn't do more ugly... If you know how to... please try :D
[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, 0 );
94     gradient.setColorAt( 0.0, Qt::white );
95     gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
96     gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
97     gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
98     p.setPen( Qt::NoPen );
99     p.setBrush( gradient );
100
101 //static const QPointF points[3] = { QPointF( 0.0, WHEIGHT ),
102   //        QPointF( WLENGTH, WHEIGHT ),  QPointF( WLENGTH, 0.0 ) };
103
104  //   p.drawConvexPolygon( points, 3 );
105
106     p.drawRect( pixGradient.rect() );
107     p.end();
108
109  //   pixGradient.setMask( mask );
110 }
111
112 void SoundSlider::wheelEvent( QWheelEvent *event )
113 {
114     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
115     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
116
117     emit sliderReleased();
118 }
119
120 void SoundSlider::mousePressEvent( QMouseEvent *event )
121 {
122     if( event->button() != Qt::RightButton )
123     {
124         /* We enter the sliding mode */
125         b_sliding = true;
126         i_oldvalue = value();
127         emit sliderPressed();
128     }
129 }
130
131 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
132 {
133     if( event->button() != Qt::RightButton )
134     {
135         if( !b_outside && value() != i_oldvalue )
136         {
137             emit sliderReleased();
138             setValue( value() );
139         }
140         b_sliding = false;
141         b_outside = false;
142     }
143 }
144
145 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
146 {
147     if( b_sliding )
148     {
149         QRect rect( padding - 15,     padding - 1,
150                     WLENGTH + 15 * 2, WHEIGHT + 2 );
151         if( !rect.contains( event->pos() ) )
152         { /* We are outside */
153             if ( !b_outside )
154                 setValue( i_oldvalue );
155             b_outside = true;
156         }
157         else
158         { /* We are inside */
159             b_outside = false;
160             changeValue( event->x() - padding );
161             emit sliderMoved( value() );
162         }
163     }
164     else
165         event->ignore();
166 }
167
168 void SoundSlider::changeValue( int x )
169 {
170     setValue( QStyle::sliderValueFromPosition(
171                 minimum(), maximum(), x, width() - 2 * padding ) );
172 }
173
174 void SoundSlider::paintEvent(QPaintEvent *e)
175 {
176     QPainter painter( this );
177     const int offset = int( double( ( width() - 2 * padding ) * value() ) / maximum() );
178     const QRectF boundsG( padding, 0, offset , pixGradient.height() );
179     painter.drawPixmap( boundsG, pixGradient, boundsG );
180
181     painter.end();
182 /*    QPainter painter( this );
183     printf( "%i\n", value() );
184
185     QLinearGradient gradient( 0.0, 0.0, WLENGTH, WHEIGHT );
186     gradient.setColorAt( 0.0, Qt::white );
187     gradient.setColorAt( 1.0, Qt::blue );
188
189     painter.setPen( QPen( QBrush( Qt::black ), 0, Qt::SolidLine, Qt::RoundCap ) );
190     painter.setBrush( gradient );
191     painter.setRenderHint( QPainter::Antialiasing );
192
193     painter.end();*/
194 }
195