source: smplayer/trunk/src/myslider.cpp@ 176

Last change on this file since 176 was 176, checked in by Silvan Scherrer, 9 years ago

smplayer: update trunk to version 16.4

  • Property svn:eol-style set to LF
File size: 5.9 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2016 Ricardo Villalba <rvm@users.sourceforge.net>
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.), Trolltech ASA
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#include "myslider.h"
23
24#include <QApplication>
25#include <QMouseEvent>
26
27#if CODE_FOR_CLICK == 0
28#include <QStyle>
29#endif
30
31#if CODE_FOR_CLICK == 1
32#include <QStyle>
33#include <QStyleOption>
34#endif
35
36
37MySlider::MySlider( QWidget * parent ) : QSlider(parent)
38{
39 setOrientation( Qt::Horizontal );
40}
41
42MySlider::~MySlider() {
43}
44
45#if CODE_FOR_CLICK == 1
46// Function copied from qslider.cpp
47inline int MySlider::pick(const QPoint &pt) const
48{
49 return orientation() == Qt::Horizontal ? pt.x() : pt.y();
50}
51
52#if QT_VERSION < 0x040300
53// Function copied from qslider.cpp and modified to make it compile
54void MySlider::initStyleOption(QStyleOptionSlider *option) const
55{
56 if (!option)
57 return;
58
59 option->initFrom(this);
60 option->subControls = QStyle::SC_None;
61 option->activeSubControls = QStyle::SC_None;
62 option->orientation = orientation();
63 option->maximum = maximum();
64 option->minimum = minimum();
65 option->tickPosition = (QSlider::TickPosition) tickPosition();
66 option->tickInterval = tickInterval();
67 option->upsideDown = (orientation() == Qt::Horizontal) ?
68 (invertedAppearance() != (option->direction == Qt::RightToLeft))
69 : (!invertedAppearance());
70 option->direction = Qt::LeftToRight; // we use the upsideDown option instead
71 option->sliderPosition = sliderPosition();
72 option->sliderValue = value();
73 option->singleStep = singleStep();
74 option->pageStep = pageStep();
75 if (orientation() == Qt::Horizontal)
76 option->state |= QStyle::State_Horizontal;
77}
78#endif
79
80// Function copied from qslider.cpp and modified to make it compile
81int MySlider::pixelPosToRangeValue(int pos) const
82{
83 QStyleOptionSlider opt;
84 initStyleOption(&opt);
85 QRect gr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
86 QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
87 int sliderMin, sliderMax, sliderLength;
88
89 if (orientation() == Qt::Horizontal) {
90 sliderLength = sr.width();
91 sliderMin = gr.x();
92 sliderMax = gr.right() - sliderLength + 1;
93 } else {
94 sliderLength = sr.height();
95 sliderMin = gr.y();
96 sliderMax = gr.bottom() - sliderLength + 1;
97 }
98 return QStyle::sliderValueFromPosition(minimum(), maximum(), pos - sliderMin,
99 sliderMax - sliderMin, opt.upsideDown);
100}
101
102// Based on code from qslider.cpp
103void MySlider::mousePressEvent( QMouseEvent * e ) {
104 if (e->button() == Qt::LeftButton) {
105 QStyleOptionSlider opt;
106 initStyleOption(&opt);
107 const QRect sliderRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
108 const QPoint center = sliderRect.center() - sliderRect.topLeft();
109 // to take half of the slider off for the setSliderPosition call we use the center - topLeft
110
111 if (!sliderRect.contains(e->pos())) {
112 e->accept();
113
114 setSliderPosition(pixelPosToRangeValue(pick(e->pos() - center)));
115 triggerAction(SliderMove);
116 setRepeatAction(SliderNoAction);
117 } else {
118 QSlider::mousePressEvent(e);
119 }
120 } else {
121 QSlider::mousePressEvent(e);
122 }
123}
124
125#endif // CODE_FOR_CLICK == 1
126
127
128#if CODE_FOR_CLICK == 2
129void MySlider::mousePressEvent( QMouseEvent * e ) {
130 // Swaps middle button click with left click
131 if (e->button() == Qt::LeftButton) {
132 QMouseEvent ev2(QEvent::MouseButtonRelease, e->pos(), e->globalPos(), Qt::MidButton, Qt::MidButton, e->modifiers());
133 QSlider::mousePressEvent(&ev2);
134 }
135 else
136 if (e->button() == Qt::MidButton) {
137 QMouseEvent ev2(QEvent::MouseButtonRelease, e->pos(), e->globalPos(), Qt::LeftButton, Qt::LeftButton, e->modifiers());
138 QSlider::mousePressEvent(&ev2);
139 }
140 else {
141 QSlider::mousePressEvent(e);
142 }
143}
144#endif // CODE_FOR_CLICK == 2
145
146
147#if CODE_FOR_CLICK == 0
148void MySlider::mousePressEvent( QMouseEvent * e ) {
149 // FIXME:
150 // The code doesn't work well with right to left layout,
151 // so it's disabled.
152 if (qApp->isRightToLeft()) {
153 QSlider::mousePressEvent(e);
154 return;
155 }
156
157 int range = maximum()-minimum();
158 int pos = (e->x() * range) / width();
159 //qDebug( "width: %d x: %d", width(), e->x());
160 //qDebug( "range: %d pos: %d value: %d", range, pos, value());
161
162 // Calculate how many positions takes the slider handle
163 int metric = qApp->style()->pixelMetric( QStyle::PM_SliderLength );
164 double one_tick_pixels = (double) width() / range;
165 int slider_handle_positions = (int) (metric / one_tick_pixels);
166
167 /*
168 qDebug("metric: %d", metric );
169 qDebug("one_tick_pixels :%f", one_tick_pixels);
170 qDebug("width() :%d", width());
171 qDebug("slider_handle_positions: %d", slider_handle_positions);
172 */
173
174 if (abs(pos - value()) > slider_handle_positions) {
175 setValue(pos);
176 emit sliderMoved( pos );
177 } else {
178 QSlider::mousePressEvent(e);
179 }
180}
181#endif
182
183#include "moc_myslider.cpp"
184
Note: See TracBrowser for help on using the repository browser.