1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
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.
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.
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
21
#include <QApplication>
22
#include <QMouseEvent>
24
#if CODE_FOR_CLICK == 0
28
#if CODE_FOR_CLICK == 1
30
#include <QStyleOption>
34
MySlider::MySlider( QWidget * parent ) : QSlider(parent)
36
setOrientation( Qt::Horizontal );
39
MySlider::~MySlider() {
42
#if CODE_FOR_CLICK == 1
43
// Function copied from qslider.cpp
44
inline int MySlider::pick(const QPoint &pt) const
46
return orientation() == Qt::Horizontal ? pt.x() : pt.y();
49
#if QT_VERSION < 0x040300
50
// Function copied from qslider.cpp and modified to make it compile
51
void MySlider::initStyleOption(QStyleOptionSlider *option) const
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;
77
// Function copied from qslider.cpp and modified to make it compile
78
int MySlider::pixelPosToRangeValue(int pos) const
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;
86
if (orientation() == Qt::Horizontal) {
87
sliderLength = sr.width();
89
sliderMax = gr.right() - sliderLength + 1;
91
sliderLength = sr.height();
93
sliderMax = gr.bottom() - sliderLength + 1;
95
return QStyle::sliderValueFromPosition(minimum(), maximum(), pos - sliderMin,
96
sliderMax - sliderMin, opt.upsideDown);
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
108
if (!sliderRect.contains(e->pos())) {
111
setSliderPosition(pixelPosToRangeValue(pick(e->pos() - center)));
112
triggerAction(SliderMove);
113
setRepeatAction(SliderNoAction);
115
QSlider::mousePressEvent(e);
118
QSlider::mousePressEvent(e);
122
#endif // CODE_FOR_CLICK == 1
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);
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);
138
QSlider::mousePressEvent(e);
141
#endif // CODE_FOR_CLICK == 2
144
#if CODE_FOR_CLICK == 0
145
void MySlider::mousePressEvent( QMouseEvent * e ) {
147
// The code doesn't work well with right to left layout,
149
if (qApp->isRightToLeft()) {
150
QSlider::mousePressEvent(e);
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());
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);
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);
171
if (abs(pos - value()) > slider_handle_positions) {
173
emit sliderMoved( pos );
175
QSlider::mousePressEvent(e);
180
#include "moc_myslider.cpp"