1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtGui>
43
44 #include "mousepangesturerecognizer.h"
45
46 class ScrollArea : public QScrollArea
47 {
48 Q_OBJECT
49 public:
ScrollArea(QWidget * parent=0)50 ScrollArea(QWidget *parent = 0)
51 : QScrollArea(parent), outside(false)
52 {
53 viewport()->grabGesture(Qt::PanGesture, Qt::ReceivePartialGestures);
54 }
55
56 protected:
viewportEvent(QEvent * event)57 bool viewportEvent(QEvent *event)
58 {
59 if (event->type() == QEvent::Gesture) {
60 gestureEvent(static_cast<QGestureEvent *>(event));
61 return true;
62 } else if (event->type() == QEvent::GestureOverride) {
63 QGestureEvent *ge = static_cast<QGestureEvent *>(event);
64 if (QPanGesture *pan = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture)))
65 if (pan->state() == Qt::GestureStarted) {
66 outside = false;
67 }
68 }
69 return QScrollArea::viewportEvent(event);
70 }
gestureEvent(QGestureEvent * event)71 void gestureEvent(QGestureEvent *event)
72 {
73 QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture));
74 if (pan) {
75 switch(pan->state()) {
76 case Qt::GestureStarted: qDebug() << this << "Pan: started"; break;
77 case Qt::GestureFinished: qDebug() << this << "Pan: finished"; break;
78 case Qt::GestureCanceled: qDebug() << this << "Pan: canceled"; break;
79 case Qt::GestureUpdated: break;
80 default: qDebug() << this << "Pan: <unknown state>"; break;
81 }
82
83 if (pan->state() == Qt::GestureStarted)
84 outside = false;
85 event->ignore();
86 event->ignore(pan);
87 if (outside)
88 return;
89
90 const QPointF delta = pan->delta();
91 const QPointF totalOffset = pan->offset();
92 QScrollBar *vbar = verticalScrollBar();
93 QScrollBar *hbar = horizontalScrollBar();
94
95 if ((vbar->value() == vbar->minimum() && totalOffset.y() > 10) ||
96 (vbar->value() == vbar->maximum() && totalOffset.y() < -10)) {
97 outside = true;
98 return;
99 }
100 if ((hbar->value() == hbar->minimum() && totalOffset.x() > 10) ||
101 (hbar->value() == hbar->maximum() && totalOffset.x() < -10)) {
102 outside = true;
103 return;
104 }
105 vbar->setValue(vbar->value() - delta.y());
106 hbar->setValue(hbar->value() - delta.x());
107 event->accept(pan);
108 }
109 }
110
111 private:
112 bool outside;
113 };
114
115 class Slider : public QSlider
116 {
117 public:
Slider(Qt::Orientation orientation,QWidget * parent=0)118 Slider(Qt::Orientation orientation, QWidget *parent = 0)
119 : QSlider(orientation, parent)
120 {
121 grabGesture(Qt::PanGesture);
122 }
123 protected:
event(QEvent * event)124 bool event(QEvent *event)
125 {
126 if (event->type() == QEvent::Gesture) {
127 gestureEvent(static_cast<QGestureEvent *>(event));
128 return true;
129 }
130 return QSlider::event(event);
131 }
gestureEvent(QGestureEvent * event)132 void gestureEvent(QGestureEvent *event)
133 {
134 QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture));
135 if (pan) {
136 switch (pan->state()) {
137 case Qt::GestureStarted: qDebug() << this << "Pan: started"; break;
138 case Qt::GestureFinished: qDebug() << this << "Pan: finished"; break;
139 case Qt::GestureCanceled: qDebug() << this << "Pan: canceled"; break;
140 case Qt::GestureUpdated: break;
141 default: qDebug() << this << "Pan: <unknown state>"; break;
142 }
143
144 if (pan->state() == Qt::GestureStarted)
145 outside = false;
146 event->ignore();
147 event->ignore(pan);
148 if (outside)
149 return;
150 const QPointF delta = pan->delta();
151 const QPointF totalOffset = pan->offset();
152 if (orientation() == Qt::Horizontal) {
153 if ((value() == minimum() && totalOffset.x() < -10) ||
154 (value() == maximum() && totalOffset.x() > 10)) {
155 outside = true;
156 return;
157 }
158 if (totalOffset.y() < 40 && totalOffset.y() > -40) {
159 setValue(value() + delta.x());
160 event->accept(pan);
161 } else {
162 outside = true;
163 }
164 } else if (orientation() == Qt::Vertical) {
165 if ((value() == maximum() && totalOffset.y() < -10) ||
166 (value() == minimum() && totalOffset.y() > 10)) {
167 outside = true;
168 return;
169 }
170 if (totalOffset.x() < 40 && totalOffset.x() > -40) {
171 setValue(value() - delta.y());
172 event->accept(pan);
173 } else {
174 outside = true;
175 }
176 }
177 }
178 }
179 private:
180 bool outside;
181 };
182
183 class MainWindow : public QMainWindow
184 {
185 public:
MainWindow()186 MainWindow()
187 {
188 rootScrollArea = new ScrollArea;
189 rootScrollArea->setObjectName(QLatin1String("rootScrollArea"));
190 setCentralWidget(rootScrollArea);
191
192 QWidget *root = new QWidget;
193 root->setFixedSize(3000, 3000);
194 rootScrollArea->setWidget(root);
195
196 Slider *verticalSlider = new Slider(Qt::Vertical, root);
197 verticalSlider->setObjectName(QLatin1String("verticalSlider"));
198 verticalSlider ->move(650, 1100);
199 Slider *horizontalSlider = new Slider(Qt::Horizontal, root);
200 horizontalSlider->setObjectName(QLatin1String("horizontalSlider"));
201 horizontalSlider ->move(600, 1000);
202
203 childScrollArea = new ScrollArea(root);
204 childScrollArea->setObjectName(QLatin1String("childScrollArea"));
205 childScrollArea->move(500, 500);
206 QWidget *w = new QWidget;
207 w->setMinimumWidth(700);
208 QVBoxLayout *l = new QVBoxLayout(w);
209 l->setMargin(20);
210 for (int i = 0; i < 100; ++i) {
211 QWidget *w = new QWidget;
212 QHBoxLayout *ll = new QHBoxLayout(w);
213 ll->addWidget(new QLabel(QString("Label %1").arg(i)));
214 ll->addWidget(new QPushButton(QString("Button %1").arg(i)));
215 l->addWidget(w);
216 }
217 childScrollArea->setWidget(w);
218 #if defined(Q_OS_WIN)
219 // Windows can force Qt to create a native window handle for an
220 // intermediate widget and that will block gesture to get touch events.
221 // So this hack to make sure gestures get all touch events they need.
222 foreach (QObject *w, children())
223 if (w->isWidgetType())
224 static_cast<QWidget *>(w)->setAttribute(Qt::WA_AcceptTouchEvents);
225 #endif
226 }
227 private:
228 ScrollArea *rootScrollArea;
229 ScrollArea *childScrollArea;
230 };
231
main(int argc,char ** argv)232 int main(int argc, char **argv)
233 {
234 QApplication app(argc, argv);
235 QGestureRecognizer::registerRecognizer(new MousePanGestureRecognizer);
236 MainWindow w;
237 w.show();
238 return app.exec();
239 }
240
241 #include "main.moc"
242