]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/input_slider.cpp
Qt: SeekPoints / Input Slider: fix behaviour for 0 timed seekpoint
[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 #include <vlc_aout_intf.h>
35
36 #include <QPaintEvent>
37 #include <QPainter>
38 #include <QBitmap>
39 #include <QPainter>
40 #include <QStyleOptionSlider>
41 #include <QLinearGradient>
42 #include <QTimer>
43 #include <QRadialGradient>
44 #include <QLinearGradient>
45 #include <QSize>
46 #include <QPalette>
47 #include <QColor>
48 #include <QPoint>
49 #include <QPropertyAnimation>
50 #include <QApplication>
51
52 #define MINIMUM 0
53 #define MAXIMUM 1000
54 #define CHAPTERSSPOTSIZE 3
55 #define FADEDURATION 300
56 #define FADEOUTDELAY 2000
57
58 SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent )
59 {
60     SeekSlider( Qt::Horizontal, _parent );
61 }
62
63 SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
64           : QSlider( q, _parent )
65 {
66     b_isSliding = false;
67     f_buffering = 1.0;
68     mHandleOpacity = 1.0;
69     chapters = NULL;
70
71     /* Timer used to fire intermediate updatePos() when sliding */
72     seekLimitTimer = new QTimer( this );
73     seekLimitTimer->setSingleShot( true );
74
75     /* Tooltip bubble */
76     mTimeTooltip = new TimeTooltip( this );
77     mTimeTooltip->setMouseTracking( true );
78
79     /* Properties */
80     setRange( MINIMUM, MAXIMUM );
81     setSingleStep( 2 );
82     setPageStep( 10 );
83     setMouseTracking( true );
84     setTracking( true );
85     setFocusPolicy( Qt::NoFocus );
86
87     /* Init to 0 */
88     setPosition( -1.0, 0, 0 );
89     secstotimestr( psz_length, 0 );
90
91     animHandle = new QPropertyAnimation( this, "handleOpacity", this );
92     animHandle->setDuration( FADEDURATION );
93     animHandle->setStartValue( 0.0 );
94     animHandle->setEndValue( 1.0 );
95
96     hideHandleTimer = new QTimer( this );
97     hideHandleTimer->setSingleShot( true );
98     hideHandleTimer->setInterval( FADEOUTDELAY );
99
100     CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
101     CONNECT( seekLimitTimer, timeout(), this, updatePos() );
102     CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
103     mTimeTooltip->installEventFilter( this );
104 }
105
106 SeekSlider::~SeekSlider()
107 {
108     delete chapters;
109 }
110
111 /***
112  * \brief Sets the chapters seekpoints adapter
113  *
114  * \params SeekPoints initilized with current intf thread
115 ***/
116 void SeekSlider::setChapters( SeekPoints *chapters_ )
117 {
118     delete chapters;
119     chapters = chapters_;
120     chapters->setParent( this );
121 }
122
123 /***
124  * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
125  *
126  * \param pos Position, between 0 and 1. -1 disables the slider
127  * \param time Elapsed time. Unused
128  * \param legnth Duration time.
129  ***/
130 void SeekSlider::setPosition( float pos, int64_t time, int length )
131 {
132     VLC_UNUSED(time);
133     if( pos == -1.0 )
134     {
135         setEnabled( false );
136         b_isSliding = false;
137     }
138     else
139         setEnabled( true );
140
141     if( !b_isSliding )
142         setValue( (int)( pos * 1000.0 ) );
143
144     inputLength = length;
145 }
146
147 void SeekSlider::startSeekTimer()
148 {
149     /* Only fire one update, when sliding, every 150ms */
150     if( b_isSliding && !seekLimitTimer->isActive() )
151         seekLimitTimer->start( 150 );
152 }
153
154 void SeekSlider::updatePos()
155 {
156     float f_pos = (float)( value() ) / 1000.0;
157     emit sliderDragged( f_pos ); /* Send new position to VLC's core */
158 }
159
160 void SeekSlider::updateBuffering( float f_buffering_ )
161 {
162     f_buffering = f_buffering_;
163     repaint();
164 }
165
166 void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
167 {
168     event->accept();
169     b_isSliding = false;
170     seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
171     if ( b_is_jumping )
172     {
173         b_is_jumping = false;
174         return;
175     }
176     QSlider::mouseReleaseEvent( event );
177     updatePos();
178 }
179
180 void SeekSlider::mousePressEvent( QMouseEvent* event )
181 {
182     /* Right-click */
183     if( event->button() != Qt::LeftButton &&
184         event->button() != Qt::MidButton )
185     {
186         QSlider::mousePressEvent( event );
187         return;
188     }
189
190     b_is_jumping = false;
191     /* handle chapter clicks */
192     int i_width = size().width();
193     if ( chapters && inputLength && i_width)
194     {
195         if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
196         {
197              /* only on chapters zone */
198             if ( event->y() < CHAPTERSSPOTSIZE ||
199                  event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
200             {
201                 QList<SeekPoint> points = chapters->getPoints();
202                 int i_selected = -1;
203                 bool b_startsnonzero = false; /* as we always starts at 1 */
204                 if ( points.count() > 0 ) /* do we need an extra offset ? */
205                     b_startsnonzero = ( points.at(0).time > 0 );
206                 int i_min_diff = i_width + 1;
207                 for( int i = 0 ; i < points.count() ; i++ )
208                 {
209                     int x = points.at(i).time / 1000000.0 / inputLength * i_width;
210                     int diff_x = abs( x - event->x() );
211                     if ( diff_x < i_min_diff )
212                     {
213                         i_min_diff = diff_x;
214                         i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
215                     } else break;
216                 }
217                 if ( i_selected && i_min_diff < 4 ) // max 4px around mark
218                 {
219                     chapters->jumpTo( i_selected );
220                     event->accept();
221                     b_is_jumping = true;
222                     return;
223                 }
224             }
225         }
226     }
227
228     b_isSliding = true ;
229     setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
230     event->accept();
231 }
232
233 void SeekSlider::mouseMoveEvent( QMouseEvent *event )
234 {
235     if( b_isSliding )
236     {
237         setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
238         emit sliderMoved( value() );
239     }
240
241     /* Tooltip */
242     if ( inputLength > 0 )
243     {
244         int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
245
246         QString chapterLabel;
247         QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
248                   QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
249
250         if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
251         {
252                 QList<SeekPoint> points = chapters->getPoints();
253                 int i_selected = -1;
254                 bool b_startsnonzero = false;
255                 if ( points.count() > 0 )
256                     b_startsnonzero = ( points.at(0).time > 0 );
257                 for( int i = 0 ; i < points.count() ; i++ )
258                 {
259                     int x = points.at(i).time / 1000000.0 / inputLength * size().width();
260                     if ( event->x() >= x )
261                         i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
262                 }
263                 if ( i_selected >= 0 )
264                     chapterLabel = points.at( i_selected ).name;
265         }
266
267         secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
268         mTimeTooltip->setText( psz_length, chapterLabel );
269         mTimeTooltip->move( p );
270     }
271     event->accept();
272 }
273
274 void SeekSlider::wheelEvent( QWheelEvent *event )
275 {
276     /* Don't do anything if we are for somehow reason sliding */
277     if( !b_isSliding )
278     {
279         setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
280          Since delta is in 1/8 of ° and mouse have steps of 15 °
281          and that our slider is in 0.1% and we want one step to be a 1%
282          increment of position */
283         emit sliderDragged( value() / 1000.0 );
284     }
285     event->accept();
286 }
287
288 void SeekSlider::enterEvent( QEvent * )
289 {
290     /* Cancel the fade-out timer */
291     hideHandleTimer->stop();
292     /* Only start the fade-in if needed */
293     if( animHandle->direction() != QAbstractAnimation::Forward )
294     {
295         /* If pause is called while not running Qt will complain */
296         if( animHandle->state() == QAbstractAnimation::Running )
297             animHandle->pause();
298         animHandle->setDirection( QAbstractAnimation::Forward );
299         animHandle->start();
300     }
301     /* Don't show the tooltip if the slider is disabled or a menu is open */
302     if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
303         mTimeTooltip->show();
304 }
305
306 void SeekSlider::leaveEvent( QEvent * )
307 {
308     hideHandleTimer->start();
309     /* Hide the tooltip
310        - if the mouse leave the slider rect (Note: it can still be
311          over the tooltip!)
312        - if another window is on the way of the cursor */
313     if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
314       ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
315     {
316         mTimeTooltip->hide();
317     }
318 }
319
320 void SeekSlider::hideEvent( QHideEvent * )
321 {
322     mTimeTooltip->hide();
323 }
324
325 bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
326 {
327     if( obj == mTimeTooltip )
328     {
329         if( event->type() == QEvent::Leave ||
330             event->type() == QEvent::MouseMove )
331         {
332             QMouseEvent *e = static_cast<QMouseEvent*>( event );
333             if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
334                 mTimeTooltip->hide();
335         }
336         return false;
337     }
338     else
339         return QSlider::eventFilter( obj, event );
340 }
341
342 QSize SeekSlider::sizeHint() const
343 {
344     return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
345                                                : QSize( 18, 100 );
346 }
347
348 QSize SeekSlider::handleSize() const
349 {
350     const int size = ( orientation() == Qt::Horizontal ? height() : width() );
351     return QSize( size, size );
352 }
353
354 void SeekSlider::paintEvent( QPaintEvent *event )
355 {
356     Q_UNUSED( event );
357
358     QStyleOptionSlider option;
359     initStyleOption( &option );
360
361     /* */
362     QPainter painter( this );
363     painter.setRenderHints( QPainter::Antialiasing );
364
365     // draw bar
366     const int barCorner = 3;
367     qreal sliderPos     = -1;
368     int range           = MAXIMUM;
369     QRect barRect       = rect();
370
371     // adjust positions based on the current orientation
372     if ( option.sliderPosition != 0 )
373     {
374         switch ( orientation() )
375         {
376             case Qt::Horizontal:
377                 sliderPos = ( ( (qreal)width() ) / (qreal)range )
378                         * (qreal)option.sliderPosition;
379                 break;
380             case Qt::Vertical:
381                 sliderPos = ( ( (qreal)height() ) / (qreal)range )
382                         * (qreal)option.sliderPosition;
383                 break;
384         }
385     }
386
387     switch ( orientation() )
388     {
389         case Qt::Horizontal:
390             barRect.setHeight( handleSize().height() /2 );
391             break;
392         case Qt::Vertical:
393             barRect.setWidth( handleSize().width() /2 );
394             break;
395     }
396
397     barRect.moveCenter( rect().center() );
398
399     // set the background color and gradient
400     QColor backgroundBase( 135, 135, 135 );
401     QLinearGradient backgroundGradient( 0, 0, 0, height() );
402     backgroundGradient.setColorAt( 0.0, backgroundBase );
403     backgroundGradient.setColorAt( 1.0, backgroundBase.lighter( 150 ) );
404
405     // set the foreground color and gradient
406     QColor foregroundBase( 50, 156, 255 );
407     QLinearGradient foregroundGradient( 0, 0, 0, height() );
408     foregroundGradient.setColorAt( 0.0,  foregroundBase );
409     foregroundGradient.setColorAt( 1.0,  foregroundBase.darker( 140 ) );
410
411     // draw a slight 3d effect on the bottom
412     painter.setPen( QColor( 230, 230, 230 ) );
413     painter.setBrush( Qt::NoBrush );
414     painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
415
416     // draw background
417     painter.setPen( Qt::NoPen );
418     painter.setBrush( backgroundGradient );
419     painter.drawRoundedRect( barRect, barCorner, barCorner );
420
421     // adjusted foreground rectangle
422     QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );
423
424     switch ( orientation() )
425     {
426         case Qt::Horizontal:
427             valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
428             break;
429         case Qt::Vertical:
430             valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
431             valueRect.moveBottom( rect().bottom() );
432             break;
433     }
434
435     if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
436     {
437         // draw foreground
438         painter.setPen( Qt::NoPen );
439         painter.setBrush( foregroundGradient );
440         painter.drawRoundedRect( valueRect, barCorner, barCorner );
441     }
442
443     // draw buffering overlay
444     if ( f_buffering < 1.0 )
445     {
446         QRect innerRect = barRect.adjusted( 1, 1,
447                             barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
448         QColor overlayColor = QColor( "Orange" );
449         overlayColor.setAlpha( 128 );
450         painter.setBrush( overlayColor );
451         painter.drawRoundedRect( innerRect, barCorner, barCorner );
452     }
453
454     if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
455     {
456         /* draw chapters tickpoints */
457         if ( chapters && inputLength && size().width() )
458         {
459             QColor background = palette().color( QPalette::Active, QPalette::Background );
460             QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
461             foreground.setHsv( foreground.hue(),
462                             ( background.saturation() + foreground.saturation() ) / 2,
463                             ( background.value() + foreground.value() ) / 2 );
464             if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
465             {
466                 QList<SeekPoint> points = chapters->getPoints();
467                 foreach( SeekPoint point, points )
468                 {
469                     int x = point.time / 1000000.0 / inputLength * size().width();
470                     painter.setPen( foreground );
471                     painter.setBrush( Qt::NoBrush );
472                     painter.drawLine( x, 0, x, CHAPTERSSPOTSIZE );
473                     painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
474                 }
475             }
476         }
477
478         // draw handle
479         if ( sliderPos != -1 )
480         {
481             const int margin = 0;
482             QSize hSize = handleSize() - QSize( 6, 6 );
483             QPoint pos;
484
485             switch ( orientation() )
486             {
487                 case Qt::Horizontal:
488                     pos = QPoint( sliderPos - ( hSize.width() / 2 ), 2 );
489                     pos.rx() = qMax( margin, pos.x() );
490                     pos.rx() = qMin( width() - hSize.width() - margin, pos.x() );
491                     break;
492                 case Qt::Vertical:
493                     pos = QPoint( 2, height() - ( sliderPos + ( hSize.height() / 2 ) ) );
494                     pos.ry() = qMax( margin, pos.y() );
495                     pos.ry() = qMin( height() - hSize.height() - margin, pos.y() );
496                     break;
497             }
498
499             QPalette p;
500             QPoint shadowPos( pos - QPoint( 2, 2 ) );
501             QSize sSize( handleSize() - QSize( 2, 2 ) );
502
503             // prepare the handle's gradient
504             QLinearGradient handleGradient( 0, 0, 0, hSize.height() );
505             handleGradient.setColorAt( 0.0, p.midlight().color() );
506             handleGradient.setColorAt( 0.9, p.mid().color() );
507
508             // prepare the handle's shadow gradient
509             QColor shadowDark( p.shadow().color().darker( 150 ) );
510             QColor shadowLight( p.shadow().color().lighter( 180 ) );
511             shadowLight.setAlpha( 50 );
512
513             QRadialGradient shadowGradient( shadowPos.x() + ( sSize.width() / 2 ),
514                                             shadowPos.y() + ( sSize.height() / 2 ),
515                                             qMax( sSize.width(), sSize.height() ) / 2 );
516             shadowGradient.setColorAt( 0.4, shadowDark );
517             shadowGradient.setColorAt( 1.0, shadowLight );
518
519             painter.setPen( Qt::NoPen );
520             painter.setOpacity( mHandleOpacity );
521
522             // draw the handle's shadow
523             painter.setBrush( shadowGradient );
524             painter.drawEllipse( shadowPos.x(), shadowPos.y() + 1, sSize.width(), sSize.height() );
525
526             // finally draw the handle
527             painter.setBrush( handleGradient );
528             painter.drawEllipse( pos.x(), pos.y(), hSize.width(), hSize.height() );
529         }
530     }
531 }
532
533 qreal SeekSlider::handleOpacity() const
534 {
535     return mHandleOpacity;
536 }
537
538 void SeekSlider::setHandleOpacity(qreal opacity)
539 {
540     mHandleOpacity = opacity;
541     /* Request a new paintevent */
542     update();
543 }
544
545 void SeekSlider::hideHandle()
546 {
547     /* If pause is called while not running Qt will complain */
548     if( animHandle->state() == QAbstractAnimation::Running )
549         animHandle->pause();
550     /* Play the animation backward */
551     animHandle->setDirection( QAbstractAnimation::Backward );
552     animHandle->start();
553 }
554
555 bool SeekSlider::isAnimationRunning() const
556 {
557     return animHandle->state() == QAbstractAnimation::Running
558             || hideHandleTimer->isActive();
559 }
560
561 /* This work is derived from Amarok's work under GPLv2+
562     - Mark Kretschmann
563     - Gábor Lehel
564    */
565 #define WLENGTH   80 // px
566 #define WHEIGHT   22  // px
567 #define SOUNDMIN  0   // %
568 #define SOUNDMAX  200 // % OR 400 ?
569
570 SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
571                           char *psz_colors )
572                         : QAbstractSlider( _parent )
573 {
574     f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
575     setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
576     setMouseTracking( true );
577     b_isSliding = false;
578     b_mouseOutside = true;
579     b_isMuted = false;
580
581     pixOutside = QPixmap( ":/toolbar/volslide-outside" );
582
583     const QPixmap temp( ":/toolbar/volslide-inside" );
584     const QBitmap mask( temp.createHeuristicMask() );
585
586     setFixedSize( pixOutside.size() );
587
588     pixGradient = QPixmap( mask.size() );
589     pixGradient2 = QPixmap( mask.size() );
590
591     /* Gradient building from the preferences */
592     QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
593     QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
594
595     QStringList colorList = qfu( psz_colors ).split( ";" );
596     free( psz_colors );
597
598     /* Fill with 255 if the list is too short */
599     if( colorList.count() < 12 )
600         for( int i = colorList.count(); i < 12; i++)
601             colorList.append( "255" );
602
603     /* Regular colors */
604 #define c(i) colorList.at(i).toInt()
605 #define add_color(gradient, range, c1, c2, c3) \
606     gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
607
608     /* Desaturated colors */
609 #define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
610 #define add_desaturated_color(gradient, range, c1, c2, c3) \
611     foo = new QColor( c(c1), c(c2), c(c3) );\
612     desaturate( foo ); gradient.setColorAt( range, *foo );\
613     delete foo;
614
615     /* combine the two helpers */
616 #define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
617     add_color( gradient1, range, c1, c2, c3 ); \
618     add_desaturated_color( gradient2, range, c1, c2, c3 );
619
620     QColor * foo;
621     add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
622     add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
623     add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
624     add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
625
626     QPainter painter( &pixGradient );
627     painter.setPen( Qt::NoPen );
628     painter.setBrush( gradient );
629     painter.drawRect( pixGradient.rect() );
630     painter.end();
631
632     painter.begin( &pixGradient2 );
633     painter.setPen( Qt::NoPen );
634     painter.setBrush( gradient2 );
635     painter.drawRect( pixGradient2.rect() );
636     painter.end();
637
638     pixGradient.setMask( mask );
639     pixGradient2.setMask( mask );
640 }
641
642 void SoundSlider::wheelEvent( QWheelEvent *event )
643 {
644     int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
645     setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );
646
647     emit sliderReleased();
648     emit sliderMoved( value() );
649 }
650
651 void SoundSlider::mousePressEvent( QMouseEvent *event )
652 {
653     if( event->button() != Qt::RightButton )
654     {
655         /* We enter the sliding mode */
656         b_isSliding = true;
657         i_oldvalue = value();
658         emit sliderPressed();
659         changeValue( event->x() - paddingL );
660         emit sliderMoved( value() );
661     }
662 }
663
664 void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
665 {
666     if( event->button() != Qt::RightButton )
667     {
668         if( !b_mouseOutside && value() != i_oldvalue )
669         {
670             emit sliderReleased();
671             setValue( value() );
672             emit sliderMoved( value() );
673         }
674         b_isSliding = false;
675         b_mouseOutside = false;
676     }
677 }
678
679 void SoundSlider::mouseMoveEvent( QMouseEvent *event )
680 {
681     if( b_isSliding )
682     {
683         QRect rect( paddingL - 15,    -1,
684                     WLENGTH + 15 * 2 , WHEIGHT + 5 );
685         if( !rect.contains( event->pos() ) )
686         { /* We are outside */
687             if ( !b_mouseOutside )
688                 setValue( i_oldvalue );
689             b_mouseOutside = true;
690         }
691         else
692         { /* We are inside */
693             b_mouseOutside = false;
694             changeValue( event->x() - paddingL );
695             emit sliderMoved( value() );
696         }
697     }
698     else
699     {
700         int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
701         i = __MIN( __MAX( 0, i ), maximum() );
702         setToolTip( QString("%1  \%" ).arg( i ) );
703     }
704 }
705
706 void SoundSlider::changeValue( int x )
707 {
708     setValue( (x * maximum() + 40 ) / WLENGTH );
709 }
710
711 void SoundSlider::setMuted( bool m )
712 {
713     b_isMuted = m;
714     update();
715 }
716
717 void SoundSlider::paintEvent( QPaintEvent *e )
718 {
719     QPainter painter( this );
720     QPixmap *pixGradient;
721     if (b_isMuted)
722         pixGradient = &this->pixGradient2;
723     else
724         pixGradient = &this->pixGradient;
725
726     const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
727
728     const QRectF boundsG( 0, 0, offset , pixGradient->height() );
729     painter.drawPixmap( boundsG, *pixGradient, boundsG );
730
731     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
732     painter.drawPixmap( boundsO, pixOutside, boundsO );
733
734     QColor background = palette().color( QPalette::Active, QPalette::Background );
735     QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
736     foreground.setHsv( foreground.hue(),
737                     ( background.saturation() + foreground.saturation() ) / 2,
738                     ( background.value() + foreground.value() ) / 2 );
739     painter.setPen( foreground );
740     QFont font; font.setPixelSize( 9 );
741     painter.setFont( font );
742     const QRect rect( 0, 0, 34, 15 );
743     painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
744                       QString::number( value() ) + '%' );
745
746     painter.end();
747     e->accept();
748 }