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