]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt: allow a maximum volume on the sound slider
[vlc] / modules / gui / qt4 / util / input_slider.cpp
1 /*****************************************************************************
2  * input_slider.cpp : VolumeSlider and SeekSlider
3  ****************************************************************************
4  * Copyright (C) 2006-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ludovic Fauvet <etix@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31
32 #include "util/input_slider.hpp"
33 #include "adapters/seekpoints.hpp"
34
35 #include <QPaintEvent>
36 #include <QPainter>
37 #include <QBitmap>
38 #include <QStyleOptionSlider>
39 #include <QLinearGradient>
40 #include <QTimer>
41 #include <QRadialGradient>
42 #include <QLinearGradient>
43 #include <QSize>
44 #include <QPalette>
45 #include <QColor>
46 #include <QPoint>
47 #include <QPropertyAnimation>
48 #include <QApplication>
49
50 #define MINIMUM 0
51 #define MAXIMUM 1000
52 #define CHAPTERSSPOTSIZE 3
53 #define FADEDURATION 300
54 #define FADEOUTDELAY 2000
55
56 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent, bool _static )
57           : QSlider( q, _parent ), b_classic( _static )
58 {
59     isSliding = false;
60     f_buffering = 1.0;
61     mHandleOpacity = 1.0;
62     chapters = NULL;
63     mHandleLength = -1;
64     b_seekable = true;
65
66     // prepare some static colors
67     QPalette p = palette();
68     QColor background = p.color( QPalette::Active, QPalette::Background );
69     tickpointForeground = p.color( QPalette::Active, QPalette::WindowText );
70     tickpointForeground.setHsv( tickpointForeground.hue(),
71             ( background.saturation() + tickpointForeground.saturation() ) / 2,
72             ( background.value() + tickpointForeground.value() ) / 2 );
73
74     // set the background color and gradient
75     QColor backgroundBase( p.window().color() );
76     backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
77     backgroundGradient.setColorAt( 1.0, backgroundBase );
78
79     // set the foreground color and gradient
80     QColor foregroundBase( 50, 156, 255 );
81     foregroundGradient.setColorAt( 0.0,  foregroundBase );
82     foregroundGradient.setColorAt( 1.0,  foregroundBase.darker( 140 ) );
83
84     // prepare the handle's gradient
85     handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
86     handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
87
88     // prepare the handle's shadow gradient
89     QColor shadowBase = p.shadow().color();
90     if( shadowBase.lightness() > 100 )
91         shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
92     shadowDark = shadowBase.darker( 150 );
93     shadowLight = shadowBase.lighter( 180 );
94     shadowLight.setAlpha( 50 );
95
96     /* Timer used to fire intermediate updatePos() when sliding */
97     seekLimitTimer = new QTimer( this );
98     seekLimitTimer->setSingleShot( true );
99
100     /* Tooltip bubble */
101     mTimeTooltip = new TimeTooltip( this );
102     mTimeTooltip->setMouseTracking( true );
103
104     /* Properties */
105     setRange( MINIMUM, MAXIMUM );
106     setSingleStep( 2 );
107     setPageStep( 10 );
108     setMouseTracking( true );
109     setTracking( true );
110     setFocusPolicy( Qt::NoFocus );
111
112     /* Use the new/classic style */
113     if( !b_classic )
114         setStyle( new SeekStyle );
115
116     /* Init to 0 */
117     setPosition( -1.0, 0, 0 );
118     secstotimestr( psz_length, 0 );
119
120     animHandle = new QPropertyAnimation( this, "handleOpacity", this );
121     animHandle->setDuration( FADEDURATION );
122     animHandle->setStartValue( 0.0 );
123     animHandle->setEndValue( 1.0 );
124
125     hideHandleTimer = new QTimer( this );
126     hideHandleTimer->setSingleShot( true );
127     hideHandleTimer->setInterval( FADEOUTDELAY );
128
129     CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
130     CONNECT( seekLimitTimer, timeout(), this, updatePos() );
131     CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
132     mTimeTooltip->installEventFilter( this );
133 }
134
135 SeekSlider::~SeekSlider()
136 {
137     delete chapters;
138 }
139
140 /***
141  * \brief Sets the chapters seekpoints adapter
142  *
143  * \params SeekPoints initilized with current intf thread
144 ***/
145 void SeekSlider::setChapters( SeekPoints *chapters_ )
146 {
147     delete chapters;
148     chapters = chapters_;
149     chapters->setParent( this );
150 }
151
152 /***
153  * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
154  *
155  * \param pos Position, between 0 and 1. -1 disables the slider
156  * \param time Elapsed time. Unused
157  * \param legnth Duration time.
158  ***/
159 void SeekSlider::setPosition( float pos, int64_t time, int length )
160 {
161     VLC_UNUSED(time);
162     if( pos == -1.0 )
163     {
164         setEnabled( false );
165         mTimeTooltip->hide();
166         isSliding = false;
167     }
168     else
169         setEnabled( b_seekable );
170
171     if( !isSliding )
172         setValue( (int)( pos * 1000.0 ) );
173
174     inputLength = length;
175 }
176
177 void SeekSlider::startSeekTimer()
178 {
179     /* Only fire one update, when sliding, every 150ms */
180     if( isSliding && !seekLimitTimer->isActive() )
181         seekLimitTimer->start( 150 );
182 }
183
184 void SeekSlider::updatePos()
185 {
186     float f_pos = (float)( value() ) / 1000.0;
187     emit sliderDragged( f_pos ); /* Send new position to VLC's core */
188 }
189
190 void SeekSlider::updateBuffering( float f_buffering_ )
191 {
192     f_buffering = f_buffering_;
193     repaint();
194 }
195
196 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
197 {
198     event->accept();
199     isSliding = false;
200     bool b_seekPending = seekLimitTimer->isActive();
201     seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
202     if ( isJumping )
203     {
204         isJumping = false;
205         return;
206     }
207     QSlider::mouseReleaseEvent( event );
208     if( b_seekPending && isEnabled() )
209         updatePos();
210 }
211
212 void SeekSlider::mousePressEvent( QMouseEvent* event )
213 {
214     /* Right-click */
215     if ( !isEnabled() ||
216          ( event->button() != Qt::LeftButton && event->button() != Qt::MidButton )
217        )
218     {
219         QSlider::mousePressEvent( event );
220         return;
221     }
222
223     isJumping = false;
224     /* handle chapter clicks */
225     int i_width = size().width();
226     if ( chapters && inputLength && i_width)
227     {
228         if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
229         {
230              /* only on chapters zone */
231             if ( event->y() < CHAPTERSSPOTSIZE ||
232                  event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
233             {
234                 QList<SeekPoint> points = chapters->getPoints();
235                 int i_selected = -1;
236                 bool b_startsnonzero = false; /* as we always starts at 1 */
237                 if ( points.count() > 0 ) /* do we need an extra offset ? */
238                     b_startsnonzero = ( points.at(0).time > 0 );
239                 int i_min_diff = i_width + 1;
240                 for( int i = 0 ; i < points.count() ; i++ )
241                 {
242                     int x = points.at(i).time / 1000000.0 / inputLength * i_width;
243                     int diff_x = abs( x - event->x() );
244                     if ( diff_x < i_min_diff )
245                     {
246                         i_min_diff = diff_x;
247                         i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
248                     } else break;
249                 }
250                 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
251                 {
252                     chapters->jumpTo( i_selected );
253                     event->accept();
254                     isJumping = true;
255                     return;
256                 }
257             }
258         }
259     }
260
261     isSliding = true ;
262
263     setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false ) );
264     emit sliderMoved( value() );
265     event->accept();
266 }
267
268 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
269 {
270     if ( !isEnabled() ) return event->accept();
271
272     if( isSliding )
273     {
274         setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x() - handleLength() / 2, width() - handleLength(), false) );
275         emit sliderMoved( value() );
276     }
277
278     /* Tooltip */
279     if ( inputLength > 0 )
280     {
281         int margin = handleLength() / 2;
282         int posX = qMax( rect().left() + margin, qMin( rect().right() - margin, event->x() ) );
283
284         QString chapterLabel;
285
286         if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
287         {
288                 QList<SeekPoint> points = chapters->getPoints();
289                 int i_selected = -1;
290                 bool b_startsnonzero = false;
291                 if ( points.count() > 0 )
292                     b_startsnonzero = ( points.at(0).time > 0 );
293                 for( int i = 0 ; i < points.count() ; i++ )
294                 {
295                     int x = points.at(i).time / 1000000.0 / inputLength * size().width();
296                     if ( event->x() >= x )
297                         i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
298                 }
299                 if ( i_selected >= 0 && i_selected < points.size() )
300                     chapterLabel = points.at( i_selected ).name;
301         }
302
303         QPoint target( event->globalX() - ( event->x() - posX ),
304                   QWidget::mapToGlobal( QPoint( 0, 0 ) ).y() );
305         if( likely( size().width() > handleLength() ) ) {
306             secstotimestr( psz_length, ( ( posX - margin ) * inputLength ) / ( size().width() - handleLength() ) );
307             mTimeTooltip->setTip( target, psz_length, chapterLabel );
308         }
309     }
310     event->accept();
311 }
312
313 void SeekSlider::wheelEvent( QWheelEvent *event )
314 {
315     /* Don't do anything if we are for somehow reason sliding */
316     if( !isSliding && isEnabled() )
317     {
318         setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
319          Since delta is in 1/8 of ° and mouse have steps of 15 °
320          and that our slider is in 0.1% and we want one step to be a 1%
321          increment of position */
322         emit sliderDragged( value() / 1000.0 );
323     }
324     event->accept();
325 }
326
327 void SeekSlider::enterEvent( QEvent * )
328 {
329     /* Cancel the fade-out timer */
330     hideHandleTimer->stop();
331     /* Only start the fade-in if needed */
332     if( isEnabled() && animHandle->direction() != QAbstractAnimation::Forward )
333     {
334         /* If pause is called while not running Qt will complain */
335         if( animHandle->state() == QAbstractAnimation::Running )
336             animHandle->pause();
337         animHandle->setDirection( QAbstractAnimation::Forward );
338         animHandle->start();
339     }
340     /* Don't show the tooltip if the slider is disabled or a menu is open */
341     if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
342         mTimeTooltip->show();
343 }
344
345 void SeekSlider::leaveEvent( QEvent * )
346 {
347     hideHandleTimer->start();
348     /* Hide the tooltip
349        - if the mouse leave the slider rect (Note: it can still be
350          over the tooltip!)
351        - if another window is on the way of the cursor */
352     if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
353       ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
354     {
355         mTimeTooltip->hide();
356     }
357 }
358
359 void SeekSlider::hideEvent( QHideEvent * )
360 {
361     mTimeTooltip->hide();
362 }
363
364 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
365 {
366     if( obj == mTimeTooltip )
367     {
368         if( event->type() == QEvent::Leave ||
369             event->type() == QEvent::MouseMove )
370         {
371             QMouseEvent *e = static_cast<QMouseEvent*>( event );
372             if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
373                 mTimeTooltip->hide();
374         }
375         return false;
376     }
377     else
378         return QSlider::eventFilter( obj, event );
379 }
380
381 QSize SeekSlider::sizeHint() const
382 {
383     if ( b_classic )
384         return QSlider::sizeHint();
385     return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
386                                                : QSize( 18, 100 );
387 }
388
389 qreal SeekSlider::handleOpacity() const
390 {
391     return mHandleOpacity;
392 }
393
394 void SeekSlider::setHandleOpacity(qreal opacity)
395 {
396     mHandleOpacity = opacity;
397     /* Request a new paintevent */
398     update();
399 }
400
401 inline int SeekSlider::handleLength()
402 {
403     if ( mHandleLength > 0 )
404         return mHandleLength;
405
406     /* Ask for the length of the handle to the underlying style */
407     QStyleOptionSlider option;
408     initStyleOption( &option );
409     mHandleLength = style()->pixelMetric( QStyle::PM_SliderLength, &option );
410     return mHandleLength;
411 }
412
413 void SeekSlider::hideHandle()
414 {
415     /* If pause is called while not running Qt will complain */
416     if( animHandle->state() == QAbstractAnimation::Running )
417         animHandle->pause();
418     /* Play the animation backward */
419     animHandle->setDirection( QAbstractAnimation::Backward );
420     animHandle->start();
421 }
422
423 bool SeekSlider::isAnimationRunning() const
424 {
425     return animHandle->state() == QAbstractAnimation::Running
426             || hideHandleTimer->isActive();
427 }
428
429
430 /* This work is derived from Amarok's work under GPLv2+
431     - Mark Kretschmann
432     - Gábor Lehel
433    */
434 #define WLENGTH   80 // px
435 #define WHEIGHT   22  // px
436 #define SOUNDMIN  0   // %
437
438 SoundSlider::SoundSlider( QWidget *_parent, int _i_step,
439                           char *psz_colors, int max )
440                         : QAbstractSlider( _parent )
441 {
442     f_step = (float)(_i_step * 10000)
443            / (float)((max - SOUNDMIN) * AOUT_VOLUME_DEFAULT);
444     setRange( SOUNDMIN, max);
445     setMouseTracking( true );
446     isSliding = false;
447     b_mouseOutside = true;
448     b_isMuted = false;
449
450     pixOutside = QPixmap( ":/toolbar/volslide-outside" );
451
452     const QPixmap temp( ":/toolbar/volslide-inside" );
453     const QBitmap mask( temp.createHeuristicMask() );
454
455     setFixedSize( pixOutside.size() );
456
457     pixGradient = QPixmap( mask.size() );
458     pixGradient2 = QPixmap( mask.size() );
459
460     /* Gradient building from the preferences */
461     QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
462     QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
463
464     QStringList colorList = qfu( psz_colors ).split( ";" );
465     free( psz_colors );
466
467     /* Fill with 255 if the list is too short */
468     if( colorList.count() < 12 )
469         for( int i = colorList.count(); i < 12; i++)
470             colorList.append( "255" );
471
472     background = palette().color( QPalette::Active, QPalette::Background );
473     foreground = palette().color( QPalette::Active, QPalette::WindowText );
474     foreground.setHsv( foreground.hue(),
475                     ( background.saturation() + foreground.saturation() ) / 2,
476                     ( background.value() + foreground.value() ) / 2 );
477
478     textfont.setPixelSize( 9 );
479     textrect.setRect( 0, 0, 34, 15 );
480
481     /* Regular colors */
482 #define c(i) colorList.at(i).toInt()
483 #define add_color(gradient, range, c1, c2, c3) \
484     gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
485
486     /* Desaturated colors */
487 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
488 #define add_desaturated_color(gradient, range, c1, c2, c3) \
489     foo = new QColor( c(c1), c(c2), c(c3) );\
490     desaturate( foo ); gradient.setColorAt( range, *foo );\
491     delete foo;
492
493     /* combine the two helpers */
494 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
495     add_color( gradient1, range, c1, c2, c3 ); \
496     add_desaturated_color( gradient2, range, c1, c2, c3 );
497
498     float f_mid_point = ( 100.0 / maximum() );
499     QColor * foo;
500     add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
501     add_colors( gradient, gradient2, f_mid_point - 0.05, 3, 4, 5 );
502     add_colors( gradient, gradient2, f_mid_point + 0.05, 6, 7, 8 );
503     add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
504
505     painter.begin( &pixGradient );
506     painter.setPen( Qt::NoPen );
507     painter.setBrush( gradient );
508     painter.drawRect( pixGradient.rect() );
509     painter.end();
510
511     painter.begin( &pixGradient2 );
512     painter.setPen( Qt::NoPen );
513     painter.setBrush( gradient2 );
514     painter.drawRect( pixGradient2.rect() );
515     painter.end();
516
517     pixGradient.setMask( mask );
518     pixGradient2.setMask( mask );
519 }
520
521 void SoundSlider::wheelEvent( QWheelEvent *event )
522 {
523     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
524     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
525
526     emit sliderReleased();
527     emit sliderMoved( value() );
528 }
529
530 void SoundSlider::mousePressEvent( QMouseEvent *event )
531 {
532     if( event->button() != Qt::RightButton )
533     {
534         /* We enter the sliding mode */
535         isSliding = true;
536         i_oldvalue = value();
537         emit sliderPressed();
538         changeValue( event->x() - paddingL );
539         emit sliderMoved( value() );
540     }
541 }
542
543 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
544 {
545     if( event->button() != Qt::RightButton )
546     {
547         if( !b_mouseOutside && value() != i_oldvalue )
548         {
549             emit sliderReleased();
550             setValue( value() );
551             emit sliderMoved( value() );
552         }
553         isSliding = false;
554         b_mouseOutside = false;
555     }
556 }
557
558 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
559 {
560     if( isSliding )
561     {
562         QRect rect( paddingL - 15,    -1,
563                     WLENGTH + 15 * 2 , WHEIGHT + 5 );
564         if( !rect.contains( event->pos() ) )
565         { /* We are outside */
566             if ( !b_mouseOutside )
567                 setValue( i_oldvalue );
568             b_mouseOutside = true;
569         }
570         else
571         { /* We are inside */
572             b_mouseOutside = false;
573             changeValue( event->x() - paddingL );
574             emit sliderMoved( value() );
575         }
576     }
577     else
578     {
579         int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
580         i = __MIN( __MAX( 0, i ), maximum() );
581         setToolTip( QString("%1  %" ).arg( i ) );
582     }
583 }
584
585 void SoundSlider::changeValue( int x )
586 {
587     setValue( (x * maximum() + 40 ) / WLENGTH );
588 }
589
590 void SoundSlider::setMuted( bool m )
591 {
592     b_isMuted = m;
593     update();
594 }
595
596 void SoundSlider::paintEvent( QPaintEvent *e )
597 {
598     QPixmap *paintGradient;
599     if (b_isMuted)
600         paintGradient = &this->pixGradient2;
601     else
602         paintGradient = &this->pixGradient;
603
604     painter.begin( this );
605
606     const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
607
608     const QRectF boundsG( 0, 0, offset , paintGradient->height() );
609     painter.drawPixmap( boundsG, *paintGradient, boundsG );
610
611     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
612     painter.drawPixmap( boundsO, pixOutside, boundsO );
613
614     painter.setPen( foreground );
615     painter.setFont( textfont );
616     painter.drawText( textrect, Qt::AlignRight | Qt::AlignVCenter,
617                       QString::number( value() ) + '%' );
618
619     painter.end();
620     e->accept();
621 }