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