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