~ubuntu-branches/ubuntu/utopic/smplayer/utopic-proposed

« back to all changes in this revision

Viewing changes to src/myslider.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2008-05-13 10:12:30 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080513101230-3rx6z4ulb2zkyd2t
Tags: 0.6.0-1
* New upstream release.
* Bumped debian/compat to 6.
* debian/control:
  - Require debhelper >= 6.
  - Relaxed the dependency of smplayer-translations on smplayer
    to Recommends. (Closes: #478713)
* Dropped debian/smplayer.desktop, changes applied upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  smplayer, GUI front-end for mplayer.
 
2
    Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
 
3
 
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
*/
 
18
 
 
19
#include "myslider.h"
 
20
 
 
21
#include <QApplication>
 
22
#include <QMouseEvent>
 
23
 
 
24
#if CODE_FOR_CLICK == 0
 
25
#include <QStyle>
 
26
#endif
 
27
 
 
28
#if CODE_FOR_CLICK == 1
 
29
#include <QStyle>
 
30
#include <QStyleOption>
 
31
#endif
 
32
 
 
33
 
 
34
MySlider::MySlider( QWidget * parent ) : QSlider(parent)
 
35
{
 
36
        setOrientation( Qt::Horizontal );
 
37
}
 
38
 
 
39
MySlider::~MySlider() {
 
40
}
 
41
 
 
42
#if CODE_FOR_CLICK == 1
 
43
// Function copied from qslider.cpp
 
44
inline int MySlider::pick(const QPoint &pt) const
 
45
{
 
46
    return orientation() == Qt::Horizontal ? pt.x() : pt.y();
 
47
}
 
48
 
 
49
#if QT_VERSION < 0x040300
 
50
// Function copied from qslider.cpp and modified to make it compile
 
51
void MySlider::initStyleOption(QStyleOptionSlider *option) const
 
52
{
 
53
    if (!option)
 
54
        return;
 
55
 
 
56
    option->initFrom(this);
 
57
    option->subControls = QStyle::SC_None;
 
58
    option->activeSubControls = QStyle::SC_None;
 
59
    option->orientation = orientation();
 
60
    option->maximum = maximum();
 
61
    option->minimum = minimum();
 
62
    option->tickPosition = (QSlider::TickPosition) tickPosition();
 
63
    option->tickInterval = tickInterval();
 
64
    option->upsideDown = (orientation() == Qt::Horizontal) ?
 
65
                     (invertedAppearance() != (option->direction == Qt::RightToLeft))
 
66
                     : (!invertedAppearance());
 
67
    option->direction = Qt::LeftToRight; // we use the upsideDown option instead
 
68
    option->sliderPosition = sliderPosition();
 
69
    option->sliderValue = value();
 
70
    option->singleStep = singleStep();
 
71
    option->pageStep = pageStep();
 
72
    if (orientation() == Qt::Horizontal)
 
73
        option->state |= QStyle::State_Horizontal;
 
74
}
 
75
#endif
 
76
 
 
77
// Function copied from qslider.cpp and modified to make it compile
 
78
int MySlider::pixelPosToRangeValue(int pos) const
 
79
{
 
80
    QStyleOptionSlider opt;
 
81
    initStyleOption(&opt);
 
82
    QRect gr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
 
83
    QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
 
84
    int sliderMin, sliderMax, sliderLength;
 
85
 
 
86
    if (orientation() == Qt::Horizontal) {
 
87
        sliderLength = sr.width();
 
88
        sliderMin = gr.x();
 
89
        sliderMax = gr.right() - sliderLength + 1;
 
90
    } else {
 
91
        sliderLength = sr.height();
 
92
        sliderMin = gr.y();
 
93
        sliderMax = gr.bottom() - sliderLength + 1;
 
94
    }
 
95
    return QStyle::sliderValueFromPosition(minimum(), maximum(), pos - sliderMin,
 
96
                                           sliderMax - sliderMin, opt.upsideDown);
 
97
}
 
98
 
 
99
// Based on code from qslider.cpp
 
100
void MySlider::mousePressEvent( QMouseEvent * e ) {
 
101
        if (e->button() == Qt::LeftButton) {
 
102
        QStyleOptionSlider opt;
 
103
        initStyleOption(&opt);
 
104
        const QRect sliderRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
 
105
        const QPoint center = sliderRect.center() - sliderRect.topLeft();
 
106
        // to take half of the slider off for the setSliderPosition call we use the center - topLeft
 
107
 
 
108
        if (!sliderRect.contains(e->pos())) {
 
109
            e->accept();
 
110
 
 
111
            setSliderPosition(pixelPosToRangeValue(pick(e->pos() - center)));
 
112
            triggerAction(SliderMove);
 
113
            setRepeatAction(SliderNoAction);
 
114
        } else {
 
115
            QSlider::mousePressEvent(e);
 
116
                }
 
117
        } else {
 
118
                QSlider::mousePressEvent(e);
 
119
        }
 
120
}
 
121
 
 
122
#endif // CODE_FOR_CLICK == 1
 
123
 
 
124
 
 
125
#if CODE_FOR_CLICK == 2
 
126
void MySlider::mousePressEvent( QMouseEvent * e ) {
 
127
        // Swaps middle button click with left click
 
128
        if (e->button() == Qt::LeftButton) {
 
129
                QMouseEvent ev2(QEvent::MouseButtonRelease, e->pos(), e->globalPos(), Qt::MidButton, Qt::MidButton, e->modifiers());
 
130
                QSlider::mousePressEvent(&ev2);
 
131
        } 
 
132
        else
 
133
        if (e->button() == Qt::MidButton) {
 
134
                QMouseEvent ev2(QEvent::MouseButtonRelease, e->pos(), e->globalPos(), Qt::LeftButton, Qt::LeftButton, e->modifiers());
 
135
                QSlider::mousePressEvent(&ev2);
 
136
        }
 
137
        else {
 
138
                QSlider::mousePressEvent(e);
 
139
        }
 
140
}
 
141
#endif // CODE_FOR_CLICK == 2
 
142
 
 
143
 
 
144
#if CODE_FOR_CLICK == 0
 
145
void MySlider::mousePressEvent( QMouseEvent * e ) {
 
146
        // FIXME:
 
147
        // The code doesn't work well with right to left layout,
 
148
        // so it's disabled.
 
149
        if (qApp->isRightToLeft()) {
 
150
                QSlider::mousePressEvent(e);
 
151
                return;
 
152
        }
 
153
 
 
154
        int range = maximum()-minimum();
 
155
        int pos = (e->x() * range) / width();
 
156
        //qDebug( "width: %d x: %d", width(), e->x());
 
157
        //qDebug( "range: %d pos: %d value: %d", range, pos, value());
 
158
 
 
159
        // Calculate how many positions takes the slider handle
 
160
        int metric = qApp->style()->pixelMetric( QStyle::PM_SliderLength );
 
161
        double one_tick_pixels = (double) width() / range;
 
162
        int slider_handle_positions = (int) (metric / one_tick_pixels);
 
163
 
 
164
        /*
 
165
        qDebug("metric: %d", metric );
 
166
        qDebug("one_tick_pixels :%f", one_tick_pixels);
 
167
        qDebug("width() :%d", width());
 
168
        qDebug("slider_handle_positions: %d", slider_handle_positions);
 
169
        */
 
170
 
 
171
        if (abs(pos - value()) > slider_handle_positions) { 
 
172
                setValue(pos);
 
173
                emit sliderMoved( pos );
 
174
        } else {
 
175
                QSlider::mousePressEvent(e);
 
176
        }
 
177
}
 
178
#endif
 
179
 
 
180
#include "moc_myslider.cpp"
 
181