1 /*  This file is part of the KDE project
2     Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) version 3, or any
8     later version accepted by the membership of KDE e.V. (or its
9     successor approved by the membership of KDE e.V.), Nokia Corporation
10     (or its successors, if any) and the KDE Free Qt Foundation, which shall
11     act as a proxy defined in Section 6 of version 3 of the license.
12 
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16     Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #include "swiftslider_p.h"
24 
25 #include <QMouseEvent>
26 #include <QStyle>
27 #include <QStyleOption>
28 
29 #if !defined(QT_NO_PHONON_SEEKSLIDER) && !defined(QT_NO_PHONON_VOLUMESLIDER)
30 
31 namespace Phonon
32 {
33 
SwiftSlider(Qt::Orientation orientation,QWidget * parent)34 SwiftSlider::SwiftSlider(Qt::Orientation orientation, QWidget * parent)
35 	: QSlider(orientation, parent)
36     , m_wheelTimer(this)
37 {
38     m_wheelTimer.setInterval(100);
39     m_wheelTimer.setSingleShot(true);
40     connect(&m_wheelTimer, SIGNAL(timeout()), this, SIGNAL(scrollEnd()));
41 }
42 
~SwiftSlider()43 SwiftSlider::~SwiftSlider()
44 {
45 }
46 
47 // Function copied from qslider.cpp
pick(const QPoint & pt) const48 inline int SwiftSlider::pick(const QPoint &pt) const
49 {
50     return orientation() == Qt::Horizontal ? pt.x() : pt.y();
51 }
52 
53 // Function copied from qslider.cpp and modified to make it compile
pixelPosToRangeValue(int pos) const54 int SwiftSlider::pixelPosToRangeValue(int pos) const
55 {
56     QStyleOptionSlider opt;
57     initStyleOption(&opt);
58     QRect gr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
59     QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
60     int sliderMin, sliderMax, sliderLength;
61 
62     if (orientation() == Qt::Horizontal) {
63         sliderLength = sr.width();
64         sliderMin = gr.x();
65         sliderMax = gr.right() - sliderLength + 1;
66     } else {
67         sliderLength = sr.height();
68         sliderMin = gr.y();
69         sliderMax = gr.bottom() - sliderLength + 1;
70     }
71     return QStyle::sliderValueFromPosition(minimum(), maximum(), pos - sliderMin,
72                                            sliderMax - sliderMin, opt.upsideDown);
73 }
74 
75 // Based on code from qslider.cpp
mousePressEvent(QMouseEvent * event)76 void SwiftSlider::mousePressEvent(QMouseEvent *event)
77 {
78     if (event->button() == Qt::LeftButton) {
79         QStyleOptionSlider opt;
80         initStyleOption(&opt);
81         const QRect sliderRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
82         const QPoint center = sliderRect.center() - sliderRect.topLeft();
83         // to take half of the slider off for the setSliderPosition call we use the center - topLeft
84 
85         if (!sliderRect.contains(event->pos())) {
86             event->accept();
87 
88             setSliderPosition(pixelPosToRangeValue(pick(event->pos() - center)));
89             triggerAction(SliderMove);
90             setRepeatAction(SliderNoAction);
91         } else {
92             QSlider::mousePressEvent(event);
93         }
94     } else {
95         QSlider::mousePressEvent(event);
96     }
97 }
98 
wheelEvent(QWheelEvent * event)99 void SwiftSlider::wheelEvent(QWheelEvent *event)
100 {
101     m_wheelTimer.start();
102     QSlider::wheelEvent(event);
103 }
104 
105 } // namespace Phonon
106 
107 #endif //QT_NO_PHONON_VOLUMESLIDER && QT_NO_PHONON_VOLUMESLIDER
108 
109 #include "moc_swiftslider_p.cpp"
110