]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt4 - Did you really think I couldn't do more ugly ????
[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     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
88     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
89
90     pixOutside = QPixmap( ":/pixmaps/volume-slider-outside.png" );
91
92     const QPixmap temp( ":/pixmaps/volume-slider-inside.png" );
93     const QBitmap mask( temp.createHeuristicMask() );
94
95     pixGradient = QPixmap( mask.size() );
96
97     QPainter p( &pixGradient );
98     QLinearGradient gradient( 0, padding, WLENGTH + 2 * padding, padding );
99     gradient.setColorAt( 0.0, Qt::white );
100     gradient.setColorAt( 0.2, QColor( 20, 226, 20 ) );
101     gradient.setColorAt( 0.5, QColor( 255, 176, 15 ) );
102     gradient.setColorAt( 1.0, QColor( 235, 30, 20 ) );
103     p.setPen( Qt::NoPen );
104     p.setBrush( gradient );
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 + 4 );
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, padding, offset , pixGradient.height() );
179     painter.drawPixmap( boundsG, pixGradient, boundsG );
180
181     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
182     painter.drawPixmap( boundsO, pixOutside, boundsO );
183
184     painter.end();
185 }
186