File indexing completed on 2024-04-14 04:38:29

0001 /*  This file is part of the KDE project
0002     Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), Nokia Corporation
0010     (or its successors, if any) and the KDE Free Qt Foundation, which shall
0011     act as a proxy defined in Section 6 of version 3 of the license.
0012 
0013     This library is distributed in the hope that it will be useful,
0014     but WITHOUT ANY WARRANTY; without even the implied warranty of
0015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016     Lesser General Public License for more details.
0017 
0018     You should have received a copy of the GNU Lesser General Public
0019     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0020 
0021 */
0022 
0023 #include "swiftslider_p.h"
0024 
0025 #include <QMouseEvent>
0026 #include <QStyle>
0027 #include <QStyleOption>
0028 
0029 #if !defined(QT_NO_PHONON_SEEKSLIDER) && !defined(QT_NO_PHONON_VOLUMESLIDER)
0030 
0031 namespace Phonon
0032 {
0033 
0034 SwiftSlider::SwiftSlider(Qt::Orientation orientation, QWidget * parent)
0035     : QSlider(orientation, parent)
0036     , m_wheelTimer(this)
0037 {
0038     m_wheelTimer.setInterval(100);
0039     m_wheelTimer.setSingleShot(true);
0040     connect(&m_wheelTimer, SIGNAL(timeout()), this, SIGNAL(scrollEnd()));
0041 }
0042 
0043 SwiftSlider::~SwiftSlider()
0044 {
0045 }
0046 
0047 // Function copied from qslider.cpp
0048 inline int SwiftSlider::pick(const QPoint &pt) const
0049 {
0050     return orientation() == Qt::Horizontal ? pt.x() : pt.y();
0051 }
0052 
0053 // Function copied from qslider.cpp and modified to make it compile
0054 int SwiftSlider::pixelPosToRangeValue(int pos) const
0055 {
0056     QStyleOptionSlider opt;
0057     initStyleOption(&opt);
0058     QRect gr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
0059     QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
0060     int sliderMin, sliderMax, sliderLength;
0061 
0062     if (orientation() == Qt::Horizontal) {
0063         sliderLength = sr.width();
0064         sliderMin = gr.x();
0065         sliderMax = gr.right() - sliderLength + 1;
0066     } else {
0067         sliderLength = sr.height();
0068         sliderMin = gr.y();
0069         sliderMax = gr.bottom() - sliderLength + 1;
0070     }
0071     return QStyle::sliderValueFromPosition(minimum(), maximum(), pos - sliderMin,
0072                                            sliderMax - sliderMin, opt.upsideDown);
0073 }
0074 
0075 // Based on code from qslider.cpp
0076 void SwiftSlider::mousePressEvent(QMouseEvent *event)
0077 {
0078     if (event->button() == Qt::LeftButton) {
0079         QStyleOptionSlider opt;
0080         initStyleOption(&opt);
0081         const QRect sliderRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
0082         const QPoint center = sliderRect.center() - sliderRect.topLeft();
0083         // to take half of the slider off for the setSliderPosition call we use the center - topLeft
0084 
0085         if (!sliderRect.contains(event->pos())) {
0086             event->accept();
0087 
0088             setSliderPosition(pixelPosToRangeValue(pick(event->pos() - center)));
0089             triggerAction(SliderMove);
0090             setRepeatAction(SliderNoAction);
0091         } else {
0092             QSlider::mousePressEvent(event);
0093         }
0094     } else {
0095         QSlider::mousePressEvent(event);
0096     }
0097 }
0098 
0099 void SwiftSlider::wheelEvent(QWheelEvent *event)
0100 {
0101     m_wheelTimer.start();
0102     QSlider::wheelEvent(event);
0103 }
0104 
0105 } // namespace Phonon
0106 
0107 #endif //QT_NO_PHONON_VOLUMESLIDER && QT_NO_PHONON_VOLUMESLIDER
0108 
0109 #include "moc_swiftslider_p.cpp"