author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
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: |
|
50 |
ScrollArea(QWidget *parent = 0) |
|
51 |
: QScrollArea(parent), outside(false) |
|
52 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
viewport()->grabGesture(Qt::PanGesture, Qt::ReceivePartialGestures); |
0 | 54 |
} |
55 |
||
56 |
protected: |
|
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 |
} |
|
71 |
void gestureEvent(QGestureEvent *event) |
|
72 |
{ |
|
73 |
QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture)); |
|
74 |
if (pan) { |
|
75 |
switch(pan->state()) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
76 |
case Qt::GestureStarted: qDebug() << this << "Pan: started"; break; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
case Qt::GestureFinished: qDebug() << this << "Pan: finished"; break; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
case Qt::GestureCanceled: qDebug() << this << "Pan: canceled"; break; |
0 | 79 |
case Qt::GestureUpdated: break; |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
80 |
default: qDebug() << this << "Pan: <unknown state>"; break; |
0 | 81 |
} |
82 |
||
83 |
if (pan->state() == Qt::GestureStarted) |
|
84 |
outside = false; |
|
85 |
event->ignore(); |
|
86 |
event->ignore(pan); |
|
87 |
if (outside) |
|
88 |
return; |
|
89 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
const QPointF delta = pan->delta(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
const QPointF totalOffset = pan->offset(); |
0 | 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 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
105 |
vbar->setValue(vbar->value() - delta.y()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
106 |
hbar->setValue(hbar->value() - delta.x()); |
0 | 107 |
event->accept(pan); |
108 |
} |
|
109 |
} |
|
110 |
||
111 |
private: |
|
112 |
bool outside; |
|
113 |
}; |
|
114 |
||
115 |
class Slider : public QSlider |
|
116 |
{ |
|
117 |
public: |
|
118 |
Slider(Qt::Orientation orientation, QWidget *parent = 0) |
|
119 |
: QSlider(orientation, parent) |
|
120 |
{ |
|
121 |
grabGesture(Qt::PanGesture); |
|
122 |
} |
|
123 |
protected: |
|
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 |
} |
|
132 |
void gestureEvent(QGestureEvent *event) |
|
133 |
{ |
|
134 |
QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture)); |
|
135 |
if (pan) { |
|
136 |
switch (pan->state()) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
case Qt::GestureStarted: qDebug() << this << "Pan: started"; break; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
case Qt::GestureFinished: qDebug() << this << "Pan: finished"; break; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
case Qt::GestureCanceled: qDebug() << this << "Pan: canceled"; break; |
0 | 140 |
case Qt::GestureUpdated: break; |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
default: qDebug() << this << "Pan: <unknown state>"; break; |
0 | 142 |
} |
143 |
||
144 |
if (pan->state() == Qt::GestureStarted) |
|
145 |
outside = false; |
|
146 |
event->ignore(); |
|
147 |
event->ignore(pan); |
|
148 |
if (outside) |
|
149 |
return; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
const QPointF delta = pan->delta(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
const QPointF totalOffset = pan->offset(); |
0 | 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) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
setValue(value() + delta.x()); |
0 | 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) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
setValue(value() - delta.y()); |
0 | 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: |
|
186 |
MainWindow() |
|
187 |
{ |
|
188 |
rootScrollArea = new ScrollArea; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
189 |
rootScrollArea->setObjectName(QLatin1String("rootScrollArea")); |
0 | 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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
197 |
verticalSlider->setObjectName(QLatin1String("verticalSlider")); |
0 | 198 |
verticalSlider ->move(650, 1100); |
199 |
Slider *horizontalSlider = new Slider(Qt::Horizontal, root); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
horizontalSlider->setObjectName(QLatin1String("horizontalSlider")); |
0 | 201 |
horizontalSlider ->move(600, 1000); |
202 |
||
203 |
childScrollArea = new ScrollArea(root); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
childScrollArea->setObjectName(QLatin1String("childScrollArea")); |
0 | 205 |
childScrollArea->move(500, 500); |
206 |
QWidget *w = new QWidget; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
207 |
w->setMinimumWidth(700); |
0 | 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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
#if defined(Q_OS_WIN) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
// Windows can force Qt to create a native window handle for an |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
// intermediate widget and that will block gesture to get touch events. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
// So this hack to make sure gestures get all touch events they need. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
foreach (QObject *w, children()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
if (w->isWidgetType()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
static_cast<QWidget *>(w)->setAttribute(Qt::WA_AcceptTouchEvents); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
225 |
#endif |
0 | 226 |
} |
227 |
private: |
|
228 |
ScrollArea *rootScrollArea; |
|
229 |
ScrollArea *childScrollArea; |
|
230 |
}; |
|
231 |
||
232 |
int main(int argc, char **argv) |
|
233 |
{ |
|
234 |
QApplication app(argc, argv); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
QGestureRecognizer::registerRecognizer(new MousePanGestureRecognizer); |
0 | 236 |
MainWindow w; |
237 |
w.show(); |
|
238 |
return app.exec(); |
|
239 |
} |
|
240 |
||
241 |
#include "main.moc" |