input_slider.cpp 23.4 KB
Newer Older
zorglub's avatar
zorglub committed
1
/*****************************************************************************
2
 * input_slider.cpp : VolumeSlider and SeekSlider
zorglub's avatar
zorglub committed
3
 ****************************************************************************
4
 * Copyright (C) 2006-2011 the VideoLAN team
5
 * $Id$
zorglub's avatar
zorglub committed
6
7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Jean-Baptiste Kempf <jb@videolan.org>
9
 *          Ludovic Fauvet <etix@videolan.org>
zorglub's avatar
zorglub committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
25

26
27
28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
zorglub's avatar
zorglub committed
29

30
31
#include "qt4.hpp"

zorglub's avatar
zorglub committed
32
#include "util/input_slider.hpp"
33
#include "adapters/seekpoints.hpp"
34
#include <vlc_aout_intf.h>
zorglub's avatar
zorglub committed
35

36
37
#include <QPaintEvent>
#include <QPainter>
38
#include <QBitmap>
39
40
41
#include <QPainter>
#include <QStyleOptionSlider>
#include <QLinearGradient>
42
#include <QTimer>
43
#include <QRadialGradient>
44
45
46
47
48
#include <QLinearGradient>
#include <QSize>
#include <QPalette>
#include <QColor>
#include <QPoint>
49
#include <QPropertyAnimation>
50
#include <QApplication>
51
52
53

#define MINIMUM 0
#define MAXIMUM 1000
54
#define CHAPTERSSPOTSIZE 3
55
56
#define FADEDURATION 300
#define FADEOUTDELAY 2000
57

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
58
SeekSlider::SeekSlider( QWidget *_parent ) : QSlider( _parent )
zorglub's avatar
Cleanup    
zorglub committed
59
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
60
    SeekSlider( Qt::Horizontal, _parent );
zorglub's avatar
Cleanup    
zorglub committed
61
62
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
63
SeekSlider::SeekSlider( Qt::Orientation q, QWidget *_parent )
64
          : QSlider( q, _parent )
zorglub's avatar
zorglub committed
65
{
66
    b_isSliding = false;
67
    f_buffering = 1.0;
68
    mHandleOpacity = 1.0;
69
    chapters = NULL;
70

71
    /* Timer used to fire intermediate updatePos() when sliding */
72
73
74
75
76
    seekLimitTimer = new QTimer( this );
    seekLimitTimer->setSingleShot( true );

    /* Tooltip bubble */
    mTimeTooltip = new TimeTooltip( this );
77
    mTimeTooltip->setMouseTracking( true );
78
79

    /* Properties */
80
    setRange( MINIMUM, MAXIMUM );
zorglub's avatar
zorglub committed
81
    setSingleStep( 2 );
zorglub's avatar
zorglub committed
82
    setPageStep( 10 );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
83
    setMouseTracking( true );
zorglub's avatar
zorglub committed
84
    setTracking( true );
85
86
87
    setFocusPolicy( Qt::NoFocus );

    /* Init to 0 */
88
    setPosition( -1.0, 0, 0 );
89
    secstotimestr( psz_length, 0 );
90

91
92
93
94
95
96
97
98
99
    animHandle = new QPropertyAnimation( this, "handleOpacity", this );
    animHandle->setDuration( FADEDURATION );
    animHandle->setStartValue( 0.0 );
    animHandle->setEndValue( 1.0 );

    hideHandleTimer = new QTimer( this );
    hideHandleTimer->setSingleShot( true );
    hideHandleTimer->setInterval( FADEOUTDELAY );

100
    CONNECT( this, sliderMoved( int ), this, startSeekTimer() );
101
    CONNECT( seekLimitTimer, timeout(), this, updatePos() );
102
    CONNECT( hideHandleTimer, timeout(), this, hideHandle() );
103
    mTimeTooltip->installEventFilter( this );
zorglub's avatar
zorglub committed
104
105
}

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
SeekSlider::~SeekSlider()
{
    delete chapters;
}

/***
 * \brief Sets the chapters seekpoints adapter
 *
 * \params SeekPoints initilized with current intf thread
***/
void SeekSlider::setChapters( SeekPoints *chapters_ )
{
    delete chapters;
    chapters = chapters_;
    chapters->setParent( this );
}

123
/***
124
125
126
127
128
 * \brief Main public method, superseeding setValue. Disabling the slider when neeeded
 *
 * \param pos Position, between 0 and 1. -1 disables the slider
 * \param time Elapsed time. Unused
 * \param legnth Duration time.
129
 ***/
130
void SeekSlider::setPosition( float pos, int64_t time, int length )
zorglub's avatar
zorglub committed
131
{
132
    VLC_UNUSED(time);
133
    if( pos == -1.0 )
134
    {
zorglub's avatar
zorglub committed
135
        setEnabled( false );
136
137
        b_isSliding = false;
    }
138
    else
zorglub's avatar
zorglub committed
139
        setEnabled( true );
140

141
    if( !b_isSliding )
Ludovic Fauvet's avatar
Ludovic Fauvet committed
142
        setValue( (int)( pos * 1000.0 ) );
143

144
    inputLength = length;
zorglub's avatar
zorglub committed
145
146
}

147
void SeekSlider::startSeekTimer()
zorglub's avatar
zorglub committed
148
{
François Cartegnie's avatar
François Cartegnie committed
149
    /* Only fire one update, when sliding, every 150ms */
150
151
    if( b_isSliding && !seekLimitTimer->isActive() )
        seekLimitTimer->start( 150 );
152
153
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
154
void SeekSlider::updatePos()
155
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
156
    float f_pos = (float)( value() ) / 1000.0;
157
    emit sliderDragged( f_pos ); /* Send new position to VLC's core */
zorglub's avatar
zorglub committed
158
}
159

160
161
162
163
164
165
void SeekSlider::updateBuffering( float f_buffering_ )
{
    f_buffering = f_buffering_;
    repaint();
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
166
void SeekSlider::mouseReleaseEvent( QMouseEvent *event )
167
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
168
    event->accept();
169
170
    b_isSliding = false;
    seekLimitTimer->stop(); /* We're not sliding anymore: only last seek on release */
171
172
173
174
175
    if ( b_is_jumping )
    {
        b_is_jumping = false;
        return;
    }
176
    QSlider::mouseReleaseEvent( event );
177
    updatePos();
178
179
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
180
void SeekSlider::mousePressEvent( QMouseEvent* event )
181
{
182
    /* Right-click */
183
184
    if( event->button() != Qt::LeftButton &&
        event->button() != Qt::MidButton )
185
186
187
188
189
    {
        QSlider::mousePressEvent( event );
        return;
    }

190
191
192
193
194
195
196
197
198
199
200
201
202
    b_is_jumping = false;
    /* handle chapter clicks */
    int i_width = size().width();
    if ( chapters && inputLength && i_width)
    {
        if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
        {
             /* only on chapters zone */
            if ( event->y() < CHAPTERSSPOTSIZE ||
                 event->y() > ( size().height() - CHAPTERSSPOTSIZE ) )
            {
                QList<SeekPoint> points = chapters->getPoints();
                int i_selected = -1;
203
204
205
                bool b_startsnonzero = false; /* as we always starts at 1 */
                if ( points.count() > 0 ) /* do we need an extra offset ? */
                    b_startsnonzero = ( points.at(0).time > 0 );
206
207
208
209
210
211
212
213
                int i_min_diff = i_width + 1;
                for( int i = 0 ; i < points.count() ; i++ )
                {
                    int x = points.at(i).time / 1000000.0 / inputLength * i_width;
                    int diff_x = abs( x - event->x() );
                    if ( diff_x < i_min_diff )
                    {
                        i_min_diff = diff_x;
214
                        i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
215
216
217
218
219
220
221
222
223
224
225
226
227
                    } else break;
                }
                if ( i_selected && i_min_diff < 4 ) // max 4px around mark
                {
                    chapters->jumpTo( i_selected );
                    event->accept();
                    b_is_jumping = true;
                    return;
                }
            }
        }
    }

228
    b_isSliding = true ;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
229
    setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false ) );
230
    event->accept();
231
232
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
233
void SeekSlider::mouseMoveEvent( QMouseEvent *event )
234
{
235
    if( b_isSliding )
236
    {
237
        setValue( QStyle::sliderValueFromPosition( MINIMUM, MAXIMUM, event->x(), width(), false) );
238
        emit sliderMoved( value() );
239
240
    }

241
    /* Tooltip */
242
243
244
    if ( inputLength > 0 )
    {
        int posX = qMax( rect().left(), qMin( rect().right(), event->x() ) );
245

246
        QString chapterLabel;
247
248
        QPoint p( event->globalX() - ( event->x() - posX ) - ( mTimeTooltip->width() / 2 ),
                  QWidget::mapToGlobal( pos() ).y() - ( mTimeTooltip->height() + 2 ) );
249

250
251
252
253
        if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
        {
                QList<SeekPoint> points = chapters->getPoints();
                int i_selected = -1;
254
255
256
                bool b_startsnonzero = false;
                if ( points.count() > 0 )
                    b_startsnonzero = ( points.at(0).time > 0 );
257
258
259
260
                for( int i = 0 ; i < points.count() ; i++ )
                {
                    int x = points.at(i).time / 1000000.0 / inputLength * size().width();
                    if ( event->x() >= x )
261
                        i_selected = i + ( ( b_startsnonzero )? 1 : 0 );
262
263
264
265
                }
                if ( i_selected >= 0 )
                    chapterLabel = points.at( i_selected ).name;
        }
266

267
        secstotimestr( psz_length, ( posX * inputLength ) / size().width() );
268
        mTimeTooltip->setText( psz_length, chapterLabel );
269
270
        mTimeTooltip->move( p );
    }
271
    event->accept();
272
273
}

Ludovic Fauvet's avatar
Ludovic Fauvet committed
274
void SeekSlider::wheelEvent( QWheelEvent *event )
275
276
{
    /* Don't do anything if we are for somehow reason sliding */
277
    if( !b_isSliding )
278
    {
Ludovic Fauvet's avatar
Ludovic Fauvet committed
279
        setValue( value() + event->delta() / 12 ); /* 12 = 8 * 15 / 10
280
281
282
         Since delta is in 1/8 of ° and mouse have steps of 15 °
         and that our slider is in 0.1% and we want one step to be a 1%
         increment of position */
Ludovic Fauvet's avatar
Ludovic Fauvet committed
283
        emit sliderDragged( value() / 1000.0 );
284
285
286
287
    }
    event->accept();
}

288
void SeekSlider::enterEvent( QEvent * )
289
{
290
291
292
293
294
295
296
297
298
299
300
    /* Cancel the fade-out timer */
    hideHandleTimer->stop();
    /* Only start the fade-in if needed */
    if( animHandle->direction() != QAbstractAnimation::Forward )
    {
        /* If pause is called while not running Qt will complain */
        if( animHandle->state() == QAbstractAnimation::Running )
            animHandle->pause();
        animHandle->setDirection( QAbstractAnimation::Forward );
        animHandle->start();
    }
301
302
    /* Don't show the tooltip if the slider is disabled or a menu is open */
    if( isEnabled() && inputLength > 0 && !qApp->activePopupWidget() )
303
304
305
        mTimeTooltip->show();
}

306
void SeekSlider::leaveEvent( QEvent * )
307
{
308
    hideHandleTimer->start();
309
310
311
312
313
314
315
    /* Hide the tooltip
       - if the mouse leave the slider rect (Note: it can still be
         over the tooltip!)
       - if another window is on the way of the cursor */
    if( !rect().contains( mapFromGlobal( QCursor::pos() ) ) ||
      ( !isActiveWindow() && !mTimeTooltip->isActiveWindow() ) )
    {
316
        mTimeTooltip->hide();
317
    }
318
319
320
321
322
}

void SeekSlider::hideEvent( QHideEvent * )
{
    mTimeTooltip->hide();
323
324
325
326
}

bool SeekSlider::eventFilter( QObject *obj, QEvent *event )
{
327
    if( obj == mTimeTooltip )
328
    {
329
330
331
332
333
334
335
        if( event->type() == QEvent::Leave ||
            event->type() == QEvent::MouseMove )
        {
            QMouseEvent *e = static_cast<QMouseEvent*>( event );
            if( !rect().contains( mapFromGlobal( e->globalPos() ) ) )
                mTimeTooltip->hide();
        }
336
337
338
339
340
341
        return false;
    }
    else
        return QSlider::eventFilter( obj, event );
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
342
QSize SeekSlider::sizeHint() const
343
344
345
346
347
{
    return ( orientation() == Qt::Horizontal ) ? QSize( 100, 18 )
                                               : QSize( 18, 100 );
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
348
QSize SeekSlider::handleSize() const
349
350
351
352
353
{
    const int size = ( orientation() == Qt::Horizontal ? height() : width() );
    return QSize( size, size );
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
354
void SeekSlider::paintEvent( QPaintEvent *event )
355
356
357
358
359
360
361
362
363
364
365
366
367
368
{
    Q_UNUSED( event );

    QStyleOptionSlider option;
    initStyleOption( &option );

    /* */
    QPainter painter( this );
    painter.setRenderHints( QPainter::Antialiasing );

    // draw bar
    const int barCorner = 3;
    qreal sliderPos     = -1;
    int range           = MAXIMUM;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
369
    QRect barRect       = rect();
370

Ludovic Fauvet's avatar
Ludovic Fauvet committed
371
    // adjust positions based on the current orientation
Ludovic Fauvet's avatar
Ludovic Fauvet committed
372
373
374
375
    if ( option.sliderPosition != 0 )
    {
        switch ( orientation() )
        {
376
            case Qt::Horizontal:
Ludovic Fauvet's avatar
Ludovic Fauvet committed
377
378
                sliderPos = ( ( (qreal)width() ) / (qreal)range )
                        * (qreal)option.sliderPosition;
379
380
                break;
            case Qt::Vertical:
Ludovic Fauvet's avatar
Ludovic Fauvet committed
381
382
                sliderPos = ( ( (qreal)height() ) / (qreal)range )
                        * (qreal)option.sliderPosition;
383
384
385
386
                break;
        }
    }

Ludovic Fauvet's avatar
Ludovic Fauvet committed
387
388
    switch ( orientation() )
    {
389
390
391
392
393
394
395
396
397
398
        case Qt::Horizontal:
            barRect.setHeight( handleSize().height() /2 );
            break;
        case Qt::Vertical:
            barRect.setWidth( handleSize().width() /2 );
            break;
    }

    barRect.moveCenter( rect().center() );

Ludovic Fauvet's avatar
Ludovic Fauvet committed
399
    // set the background color and gradient
400
    QColor backgroundBase( palette().window().color() );
401
    QLinearGradient backgroundGradient( 0, 0, 0, height() );
402
403
    backgroundGradient.setColorAt( 0.0, backgroundBase.darker( 140 ) );
    backgroundGradient.setColorAt( 1.0, backgroundBase );
404

Ludovic Fauvet's avatar
Ludovic Fauvet committed
405
    // set the foreground color and gradient
406
    QColor foregroundBase( 50, 156, 255 );
407
    QLinearGradient foregroundGradient( 0, 0, 0, height() );
408
409
    foregroundGradient.setColorAt( 0.0,  foregroundBase );
    foregroundGradient.setColorAt( 1.0,  foregroundBase.darker( 140 ) );
410

Ludovic Fauvet's avatar
Ludovic Fauvet committed
411
    // draw a slight 3d effect on the bottom
412
413
414
    painter.setPen( QColor( 230, 230, 230 ) );
    painter.setBrush( Qt::NoBrush );
    painter.drawRoundedRect( barRect.adjusted( 0, 2, 0, 0 ), barCorner, barCorner );
415

Ludovic Fauvet's avatar
Ludovic Fauvet committed
416
    // draw background
417
418
    painter.setPen( Qt::NoPen );
    painter.setBrush( backgroundGradient );
419
    painter.drawRoundedRect( barRect, barCorner, barCorner );
420

Ludovic Fauvet's avatar
Ludovic Fauvet committed
421
    // adjusted foreground rectangle
422
423
    QRect valueRect = barRect.adjusted( 1, 1, -1, 0 );

Ludovic Fauvet's avatar
Ludovic Fauvet committed
424
425
    switch ( orientation() )
    {
426
        case Qt::Horizontal:
427
            valueRect.setWidth( qMin( width(), int( sliderPos ) ) );
428
429
            break;
        case Qt::Vertical:
430
431
            valueRect.setHeight( qMin( height(), int( sliderPos ) ) );
            valueRect.moveBottom( rect().bottom() );
432
433
434
            break;
    }

Ludovic Fauvet's avatar
Ludovic Fauvet committed
435
436
    if ( option.sliderPosition > minimum() && option.sliderPosition <= maximum() )
    {
Ludovic Fauvet's avatar
Ludovic Fauvet committed
437
        // draw foreground
438
439
        painter.setPen( Qt::NoPen );
        painter.setBrush( foregroundGradient );
440
        painter.drawRoundedRect( valueRect, barCorner, barCorner );
441
442
    }

443
444
445
446
447
448
449
450
451
452
453
    // draw buffering overlay
    if ( f_buffering < 1.0 )
    {
        QRect innerRect = barRect.adjusted( 1, 1,
                            barRect.width() * ( -1.0 + f_buffering ) - 1, 0 );
        QColor overlayColor = QColor( "Orange" );
        overlayColor.setAlpha( 128 );
        painter.setBrush( overlayColor );
        painter.drawRoundedRect( innerRect, barCorner, barCorner );
    }

454
    if ( option.state & QStyle::State_MouseOver || isAnimationRunning() )
Ludovic Fauvet's avatar
Ludovic Fauvet committed
455
    {
456
457
458
        /* draw chapters tickpoints */
        if ( chapters && inputLength && size().width() )
        {
459
460
461
462
463
            QColor background = palette().color( QPalette::Active, QPalette::Background );
            QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
            foreground.setHsv( foreground.hue(),
                            ( background.saturation() + foreground.saturation() ) / 2,
                            ( background.value() + foreground.value() ) / 2 );
464
465
466
467
468
469
            if ( orientation() == Qt::Horizontal ) /* TODO: vertical */
            {
                QList<SeekPoint> points = chapters->getPoints();
                foreach( SeekPoint point, points )
                {
                    int x = point.time / 1000000.0 / inputLength * size().width();
470
                    painter.setPen( foreground );
471
                    painter.setBrush( Qt::NoBrush );
472
                    painter.drawLine( x, height(), x, height() - CHAPTERSSPOTSIZE );
473
474
475
476
477
                }
            }
        }

        // draw handle
Ludovic Fauvet's avatar
Ludovic Fauvet committed
478
479
        if ( sliderPos != -1 )
        {
480
            const int margin = 0;
481
            QSize hSize = handleSize() - QSize( 6, 6 );
482
483
            QPoint pos;

Ludovic Fauvet's avatar
Ludovic Fauvet committed
484
485
            switch ( orientation() )
            {
486
                case Qt::Horizontal:
487
                    pos = QPoint( sliderPos - ( hSize.width() / 2 ), 2 );
488
                    pos.rx() = qMax( margin, pos.x() );
489
                    pos.rx() = qMin( width() - hSize.width() - margin, pos.x() );
490
491
                    break;
                case Qt::Vertical:
492
                    pos = QPoint( 2, height() - ( sliderPos + ( hSize.height() / 2 ) ) );
493
                    pos.ry() = qMax( margin, pos.y() );
494
                    pos.ry() = qMin( height() - hSize.height() - margin, pos.y() );
495
496
497
                    break;
            }

498
499
500
501
502
503
            QPalette p;
            QPoint shadowPos( pos - QPoint( 2, 2 ) );
            QSize sSize( handleSize() - QSize( 2, 2 ) );

            // prepare the handle's gradient
            QLinearGradient handleGradient( 0, 0, 0, hSize.height() );
504
505
            handleGradient.setColorAt( 0.0, p.window().color().lighter( 120 ) );
            handleGradient.setColorAt( 0.9, p.window().color().darker( 120 ) );
506
507

            // prepare the handle's shadow gradient
508
509
510
511
512
            QColor shadowBase = p.shadow().color();
            if( shadowBase.lightness() > 100 )
                shadowBase = QColor( 60, 60, 60 ); // Palette's shadow is too bright
            QColor shadowDark( shadowBase.darker( 150 ) );
            QColor shadowLight( shadowBase.lighter( 180 ) );
513
514
515
516
517
518
519
            shadowLight.setAlpha( 50 );

            QRadialGradient shadowGradient( shadowPos.x() + ( sSize.width() / 2 ),
                                            shadowPos.y() + ( sSize.height() / 2 ),
                                            qMax( sSize.width(), sSize.height() ) / 2 );
            shadowGradient.setColorAt( 0.4, shadowDark );
            shadowGradient.setColorAt( 1.0, shadowLight );
520
521

            painter.setPen( Qt::NoPen );
522
            painter.setOpacity( mHandleOpacity );
523
524
525
526
527
528
529
530

            // draw the handle's shadow
            painter.setBrush( shadowGradient );
            painter.drawEllipse( shadowPos.x(), shadowPos.y() + 1, sSize.width(), sSize.height() );

            // finally draw the handle
            painter.setBrush( handleGradient );
            painter.drawEllipse( pos.x(), pos.y(), hSize.width(), hSize.height() );
531
532
533
534
        }
    }
}

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
qreal SeekSlider::handleOpacity() const
{
    return mHandleOpacity;
}

void SeekSlider::setHandleOpacity(qreal opacity)
{
    mHandleOpacity = opacity;
    /* Request a new paintevent */
    update();
}

void SeekSlider::hideHandle()
{
    /* If pause is called while not running Qt will complain */
    if( animHandle->state() == QAbstractAnimation::Running )
        animHandle->pause();
    /* Play the animation backward */
    animHandle->setDirection( QAbstractAnimation::Backward );
    animHandle->start();
}

bool SeekSlider::isAnimationRunning() const
{
    return animHandle->state() == QAbstractAnimation::Running
            || hideHandleTimer->isActive();
}
562

563
564
565
566
/* This work is derived from Amarok's work under GPLv2+
    - Mark Kretschmann
    - Gábor Lehel
   */
567
568
#define WLENGTH   80 // px
#define WHEIGHT   22  // px
569
570
571
#define SOUNDMIN  0   // %
#define SOUNDMAX  200 // % OR 400 ?

572
573
SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
                          char *psz_colors )
574
                        : QAbstractSlider( _parent )
575
{
576
    f_step = ( _i_step * 100 ) / AOUT_VOLUME_MAX ;
577
    setRange( SOUNDMIN, b_hard ? (2 * SOUNDMAX) : SOUNDMAX  );
578
    setMouseTracking( true );
579
580
    b_isSliding = false;
    b_mouseOutside = true;
581
    b_isMuted = false;
582

583
    pixOutside = QPixmap( ":/toolbar/volslide-outside" );
584

585
    const QPixmap temp( ":/toolbar/volslide-inside" );
586
    const QBitmap mask( temp.createHeuristicMask() );
587

jpd's avatar
jpd committed
588
    setFixedSize( pixOutside.size() );
589
590

    pixGradient = QPixmap( mask.size() );
591
    pixGradient2 = QPixmap( mask.size() );
592

593
    /* Gradient building from the preferences */
594
    QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
595
    QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
596
597

    QStringList colorList = qfu( psz_colors ).split( ";" );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
598
599
    free( psz_colors );

600
    /* Fill with 255 if the list is too short */
601
602
    if( colorList.count() < 12 )
        for( int i = colorList.count(); i < 12; i++)
603
604
            colorList.append( "255" );

605
    /* Regular colors */
606
#define c(i) colorList.at(i).toInt()
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#define add_color(gradient, range, c1, c2, c3) \
    gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );

    /* Desaturated colors */
#define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
#define add_desaturated_color(gradient, range, c1, c2, c3) \
    foo = new QColor( c(c1), c(c2), c(c3) );\
    desaturate( foo ); gradient.setColorAt( range, *foo );\
    delete foo;

    /* combine the two helpers */
#define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
    add_color( gradient1, range, c1, c2, c3 ); \
    add_desaturated_color( gradient2, range, c1, c2, c3 );

    QColor * foo;
    add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
624
625
    add_colors( gradient, gradient2, 0.45, 3, 4, 5 );
    add_colors( gradient, gradient2, 0.55, 6, 7, 8 );
626
    add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
627

628
629
630
631
632
    QPainter painter( &pixGradient );
    painter.setPen( Qt::NoPen );
    painter.setBrush( gradient );
    painter.drawRect( pixGradient.rect() );
    painter.end();
633

634
635
636
637
638
639
    painter.begin( &pixGradient2 );
    painter.setPen( Qt::NoPen );
    painter.setBrush( gradient2 );
    painter.drawRect( pixGradient2.rect() );
    painter.end();

640
    pixGradient.setMask( mask );
641
    pixGradient2.setMask( mask );
642
643
644
645
}

void SoundSlider::wheelEvent( QWheelEvent *event )
{
646
    int newvalue = value() + event->delta() / ( 8 * 15 ) * f_step;
647
648
649
    setValue( __MIN( __MAX( minimum(), newvalue ), maximum() ) );

    emit sliderReleased();
650
    emit sliderMoved( value() );
651
652
653
654
655
656
657
}

void SoundSlider::mousePressEvent( QMouseEvent *event )
{
    if( event->button() != Qt::RightButton )
    {
        /* We enter the sliding mode */
658
        b_isSliding = true;
659
660
        i_oldvalue = value();
        emit sliderPressed();
661
        changeValue( event->x() - paddingL );
662
        emit sliderMoved( value() );
663
664
665
666
667
668
669
    }
}

void SoundSlider::mouseReleaseEvent( QMouseEvent *event )
{
    if( event->button() != Qt::RightButton )
    {
670
        if( !b_mouseOutside && value() != i_oldvalue )
671
672
673
        {
            emit sliderReleased();
            setValue( value() );
674
            emit sliderMoved( value() );
675
        }
676
677
        b_isSliding = false;
        b_mouseOutside = false;
678
679
680
681
682
    }
}

void SoundSlider::mouseMoveEvent( QMouseEvent *event )
{
683
    if( b_isSliding )
684
    {
685
        QRect rect( paddingL - 15,    -1,
686
                    WLENGTH + 15 * 2 , WHEIGHT + 5 );
687
688
        if( !rect.contains( event->pos() ) )
        { /* We are outside */
689
            if ( !b_mouseOutside )
690
                setValue( i_oldvalue );
691
            b_mouseOutside = true;
692
693
694
        }
        else
        { /* We are inside */
695
            b_mouseOutside = false;
696
            changeValue( event->x() - paddingL );
697
698
699
700
            emit sliderMoved( value() );
        }
    }
    else
701
    {
702
        int i = ( ( event->x() - paddingL ) * maximum() + 40 ) / WLENGTH;
703
704
        i = __MIN( __MAX( 0, i ), maximum() );
        setToolTip( QString("%1  \%" ).arg( i ) );
705
    }
706
707
708
709
}

void SoundSlider::changeValue( int x )
{
710
    setValue( (x * maximum() + 40 ) / WLENGTH );
711
712
}

713
714
715
716
717
718
void SoundSlider::setMuted( bool m )
{
    b_isMuted = m;
    update();
}

719
void SoundSlider::paintEvent( QPaintEvent *e )
720
721
{
    QPainter painter( this );
722
723
724
725
726
727
    QPixmap *pixGradient;
    if (b_isMuted)
        pixGradient = &this->pixGradient2;
    else
        pixGradient = &this->pixGradient;

728
    const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
729

730
731
    const QRectF boundsG( 0, 0, offset , pixGradient->height() );
    painter.drawPixmap( boundsG, *pixGradient, boundsG );
732

733
734
    const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
    painter.drawPixmap( boundsO, pixOutside, boundsO );
735

736
737
738
739
740
741
    QColor background = palette().color( QPalette::Active, QPalette::Background );
    QColor foreground = palette().color( QPalette::Active, QPalette::WindowText );
    foreground.setHsv( foreground.hue(),
                    ( background.saturation() + foreground.saturation() ) / 2,
                    ( background.value() + foreground.value() ) / 2 );
    painter.setPen( foreground );
742
    QFont font; font.setPixelSize( 9 );
743
    painter.setFont( font );
744
    const QRect rect( 0, 0, 34, 15 );
745
746
747
    painter.drawText( rect, Qt::AlignRight | Qt::AlignVCenter,
                      QString::number( value() ) + '%' );

748
    painter.end();
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
749
    e->accept();
750
}