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