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