|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
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 |
|
43 #include <QtTest/QtTest> |
|
44 #include "../shared/util.h" |
|
45 |
|
46 #include <qevent.h> |
|
47 #include <qwidget.h> |
|
48 #include <qlayout.h> |
|
49 #include <qgesture.h> |
|
50 #include <qgesturerecognizer.h> |
|
51 #include <qgraphicsitem.h> |
|
52 #include <qgraphicsview.h> |
|
53 |
|
54 #include <qdebug.h> |
|
55 |
|
56 //TESTED_CLASS= |
|
57 //TESTED_FILES= |
|
58 |
|
59 static QPointF mapToGlobal(const QPointF &pt, QGraphicsItem *item, QGraphicsView *view) |
|
60 { |
|
61 return view->mapToGlobal(view->mapFromScene(item->mapToScene(pt))); |
|
62 } |
|
63 |
|
64 class CustomGesture : public QGesture |
|
65 { |
|
66 Q_OBJECT |
|
67 public: |
|
68 static Qt::GestureType GestureType; |
|
69 |
|
70 CustomGesture(QObject *parent = 0) |
|
71 : QGesture(parent), serial(0) |
|
72 { |
|
73 } |
|
74 |
|
75 int serial; |
|
76 |
|
77 static const int SerialMaybeThreshold; |
|
78 static const int SerialStartedThreshold; |
|
79 static const int SerialFinishedThreshold; |
|
80 }; |
|
81 Qt::GestureType CustomGesture::GestureType = Qt::CustomGesture; |
|
82 const int CustomGesture::SerialMaybeThreshold = 1; |
|
83 const int CustomGesture::SerialStartedThreshold = 3; |
|
84 const int CustomGesture::SerialFinishedThreshold = 6; |
|
85 |
|
86 class CustomEvent : public QEvent |
|
87 { |
|
88 public: |
|
89 static int EventType; |
|
90 |
|
91 CustomEvent(int serial_ = 0) |
|
92 : QEvent(QEvent::Type(CustomEvent::EventType)), |
|
93 serial(serial_), hasHotSpot(false) |
|
94 { |
|
95 } |
|
96 |
|
97 int serial; |
|
98 QPointF hotSpot; |
|
99 bool hasHotSpot; |
|
100 }; |
|
101 int CustomEvent::EventType = 0; |
|
102 |
|
103 class CustomGestureRecognizer : public QGestureRecognizer |
|
104 { |
|
105 public: |
|
106 static bool ConsumeEvents; |
|
107 |
|
108 CustomGestureRecognizer() |
|
109 { |
|
110 if (!CustomEvent::EventType) |
|
111 CustomEvent::EventType = QEvent::registerEventType(); |
|
112 } |
|
113 |
|
114 QGesture* create(QObject *) |
|
115 { |
|
116 return new CustomGesture; |
|
117 } |
|
118 |
|
119 QGestureRecognizer::Result recognize(QGesture *state, QObject*, QEvent *event) |
|
120 { |
|
121 if (event->type() == CustomEvent::EventType) { |
|
122 QGestureRecognizer::Result result = 0; |
|
123 if (CustomGestureRecognizer::ConsumeEvents) |
|
124 result |= QGestureRecognizer::ConsumeEventHint; |
|
125 CustomGesture *g = static_cast<CustomGesture*>(state); |
|
126 CustomEvent *e = static_cast<CustomEvent*>(event); |
|
127 g->serial = e->serial; |
|
128 if (e->hasHotSpot) |
|
129 g->setHotSpot(e->hotSpot); |
|
130 if (g->serial >= CustomGesture::SerialFinishedThreshold) |
|
131 result |= QGestureRecognizer::FinishGesture; |
|
132 else if (g->serial >= CustomGesture::SerialStartedThreshold) |
|
133 result |= QGestureRecognizer::TriggerGesture; |
|
134 else if (g->serial >= CustomGesture::SerialMaybeThreshold) |
|
135 result |= QGestureRecognizer::MayBeGesture; |
|
136 else |
|
137 result = QGestureRecognizer::CancelGesture; |
|
138 return result; |
|
139 } |
|
140 return QGestureRecognizer::Ignore; |
|
141 } |
|
142 |
|
143 void reset(QGesture *state) |
|
144 { |
|
145 CustomGesture *g = static_cast<CustomGesture *>(state); |
|
146 g->serial = 0; |
|
147 QGestureRecognizer::reset(state); |
|
148 } |
|
149 }; |
|
150 bool CustomGestureRecognizer::ConsumeEvents = false; |
|
151 |
|
152 // same as CustomGestureRecognizer but triggers early without the maybe state |
|
153 class CustomContinuousGestureRecognizer : public QGestureRecognizer |
|
154 { |
|
155 public: |
|
156 CustomContinuousGestureRecognizer() |
|
157 { |
|
158 if (!CustomEvent::EventType) |
|
159 CustomEvent::EventType = QEvent::registerEventType(); |
|
160 } |
|
161 |
|
162 QGesture* create(QObject *) |
|
163 { |
|
164 return new CustomGesture; |
|
165 } |
|
166 |
|
167 QGestureRecognizer::Result recognize(QGesture *state, QObject*, QEvent *event) |
|
168 { |
|
169 if (event->type() == CustomEvent::EventType) { |
|
170 QGestureRecognizer::Result result = QGestureRecognizer::ConsumeEventHint; |
|
171 CustomGesture *g = static_cast<CustomGesture *>(state); |
|
172 CustomEvent *e = static_cast<CustomEvent *>(event); |
|
173 g->serial = e->serial; |
|
174 if (e->hasHotSpot) |
|
175 g->setHotSpot(e->hotSpot); |
|
176 if (g->serial >= CustomGesture::SerialFinishedThreshold) |
|
177 result |= QGestureRecognizer::FinishGesture; |
|
178 else if (g->serial >= CustomGesture::SerialMaybeThreshold) |
|
179 result |= QGestureRecognizer::TriggerGesture; |
|
180 else |
|
181 result = QGestureRecognizer::CancelGesture; |
|
182 return result; |
|
183 } |
|
184 return QGestureRecognizer::Ignore; |
|
185 } |
|
186 |
|
187 void reset(QGesture *state) |
|
188 { |
|
189 CustomGesture *g = static_cast<CustomGesture *>(state); |
|
190 g->serial = 0; |
|
191 QGestureRecognizer::reset(state); |
|
192 } |
|
193 }; |
|
194 |
|
195 class GestureWidget : public QWidget |
|
196 { |
|
197 Q_OBJECT |
|
198 public: |
|
199 GestureWidget(const char *name = 0, QWidget *parent = 0) |
|
200 : QWidget(parent) |
|
201 { |
|
202 if (name) |
|
203 setObjectName(QLatin1String(name)); |
|
204 reset(); |
|
205 acceptGestureOverride = false; |
|
206 } |
|
207 void reset() |
|
208 { |
|
209 customEventsReceived = 0; |
|
210 gestureEventsReceived = 0; |
|
211 gestureOverrideEventsReceived = 0; |
|
212 events.clear(); |
|
213 overrideEvents.clear(); |
|
214 ignoredGestures.clear(); |
|
215 } |
|
216 |
|
217 int customEventsReceived; |
|
218 int gestureEventsReceived; |
|
219 int gestureOverrideEventsReceived; |
|
220 struct Events |
|
221 { |
|
222 QList<Qt::GestureType> all; |
|
223 QList<Qt::GestureType> started; |
|
224 QList<Qt::GestureType> updated; |
|
225 QList<Qt::GestureType> finished; |
|
226 QList<Qt::GestureType> canceled; |
|
227 |
|
228 void clear() |
|
229 { |
|
230 all.clear(); |
|
231 started.clear(); |
|
232 updated.clear(); |
|
233 finished.clear(); |
|
234 canceled.clear(); |
|
235 } |
|
236 } events, overrideEvents; |
|
237 |
|
238 bool acceptGestureOverride; |
|
239 QSet<Qt::GestureType> ignoredGestures; |
|
240 |
|
241 protected: |
|
242 bool event(QEvent *event) |
|
243 { |
|
244 Events *eventsPtr = 0; |
|
245 if (event->type() == QEvent::Gesture) { |
|
246 QGestureEvent *e = static_cast<QGestureEvent*>(event); |
|
247 ++gestureEventsReceived; |
|
248 eventsPtr = &events; |
|
249 foreach(Qt::GestureType type, ignoredGestures) |
|
250 e->ignore(e->gesture(type)); |
|
251 } else if (event->type() == QEvent::GestureOverride) { |
|
252 ++gestureOverrideEventsReceived; |
|
253 eventsPtr = &overrideEvents; |
|
254 if (acceptGestureOverride) |
|
255 event->accept(); |
|
256 } |
|
257 if (eventsPtr) { |
|
258 QGestureEvent *e = static_cast<QGestureEvent*>(event); |
|
259 QList<QGesture*> gestures = e->gestures(); |
|
260 foreach(QGesture *g, gestures) { |
|
261 eventsPtr->all << g->gestureType(); |
|
262 switch(g->state()) { |
|
263 case Qt::GestureStarted: |
|
264 eventsPtr->started << g->gestureType(); |
|
265 break; |
|
266 case Qt::GestureUpdated: |
|
267 eventsPtr->updated << g->gestureType(); |
|
268 break; |
|
269 case Qt::GestureFinished: |
|
270 eventsPtr->finished << g->gestureType(); |
|
271 break; |
|
272 case Qt::GestureCanceled: |
|
273 eventsPtr->canceled << g->gestureType(); |
|
274 break; |
|
275 default: |
|
276 Q_ASSERT(false); |
|
277 } |
|
278 } |
|
279 } else if (event->type() == CustomEvent::EventType) { |
|
280 ++customEventsReceived; |
|
281 } else { |
|
282 return QWidget::event(event); |
|
283 } |
|
284 return true; |
|
285 } |
|
286 }; |
|
287 |
|
288 // TODO rename to sendGestureSequence |
|
289 static void sendCustomGesture(CustomEvent *event, QObject *object, QGraphicsScene *scene = 0) |
|
290 { |
|
291 for (int i = CustomGesture::SerialMaybeThreshold; |
|
292 i <= CustomGesture::SerialFinishedThreshold; ++i) { |
|
293 event->serial = i; |
|
294 if (scene) |
|
295 scene->sendEvent(qobject_cast<QGraphicsObject *>(object), event); |
|
296 else |
|
297 QApplication::sendEvent(object, event); |
|
298 } |
|
299 } |
|
300 |
|
301 class tst_Gestures : public QObject |
|
302 { |
|
303 Q_OBJECT |
|
304 |
|
305 public: |
|
306 tst_Gestures(); |
|
307 virtual ~tst_Gestures(); |
|
308 |
|
309 public slots: |
|
310 void initTestCase(); |
|
311 void cleanupTestCase(); |
|
312 void init(); |
|
313 void cleanup(); |
|
314 |
|
315 private slots: |
|
316 void customGesture(); |
|
317 /* |
|
318 void autoCancelingGestures(); |
|
319 void gestureOverChild(); |
|
320 void multipleWidgetOnlyGestureInTree(); |
|
321 void conflictingGestures(); |
|
322 void finishedWithoutStarted(); |
|
323 void unknownGesture(); |
|
324 void graphicsItemGesture(); |
|
325 void graphicsItemTreeGesture(); |
|
326 void explicitGraphicsObjectTarget(); |
|
327 void gestureOverChildGraphicsItem(); |
|
328 void twoGesturesOnDifferentLevel(); |
|
329 void multipleGesturesInTree(); |
|
330 void multipleGesturesInComplexTree(); |
|
331 void testMapToScene(); |
|
332 void ungrabGesture(); |
|
333 void consumeEventHint(); |
|
334 void unregisterRecognizer(); |
|
335 void autoCancelGestures(); |
|
336 void autoCancelGestures2(); |
|
337 void panelPropagation(); |
|
338 void panelStacksBehindParent(); |
|
339 */ |
|
340 }; |
|
341 |
|
342 tst_Gestures::tst_Gestures() |
|
343 { |
|
344 } |
|
345 |
|
346 tst_Gestures::~tst_Gestures() |
|
347 { |
|
348 } |
|
349 |
|
350 void tst_Gestures::initTestCase() |
|
351 { |
|
352 CustomGesture::GestureType = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
353 QVERIFY(CustomGesture::GestureType != Qt::GestureType(0)); |
|
354 QVERIFY(CustomGesture::GestureType != Qt::CustomGesture); |
|
355 } |
|
356 |
|
357 void tst_Gestures::cleanupTestCase() |
|
358 { |
|
359 QGestureRecognizer::unregisterRecognizer(CustomGesture::GestureType); |
|
360 } |
|
361 |
|
362 void tst_Gestures::init() |
|
363 { |
|
364 } |
|
365 |
|
366 void tst_Gestures::cleanup() |
|
367 { |
|
368 } |
|
369 |
|
370 void tst_Gestures::customGesture() |
|
371 { |
|
372 GestureWidget widget; |
|
373 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
374 CustomEvent event; |
|
375 sendCustomGesture(&event, &widget); |
|
376 |
|
377 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
378 static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
379 QCOMPARE(widget.customEventsReceived, TotalCustomEventsCount); |
|
380 QCOMPARE(widget.gestureEventsReceived, TotalGestureEventsCount); |
|
381 QCOMPARE(widget.gestureOverrideEventsReceived, 0); |
|
382 QCOMPARE(widget.events.all.size(), TotalGestureEventsCount); |
|
383 for(int i = 0; i < widget.events.all.size(); ++i) |
|
384 QCOMPARE(widget.events.all.at(i), CustomGesture::GestureType); |
|
385 QCOMPARE(widget.events.started.size(), 1); |
|
386 QCOMPARE(widget.events.updated.size(), TotalGestureEventsCount - 2); |
|
387 QCOMPARE(widget.events.finished.size(), 1); |
|
388 QCOMPARE(widget.events.canceled.size(), 0); |
|
389 } |
|
390 |
|
391 /* |
|
392 void tst_Gestures::consumeEventHint() |
|
393 { |
|
394 GestureWidget widget; |
|
395 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
396 |
|
397 CustomGestureRecognizer::ConsumeEvents = true; |
|
398 CustomEvent event; |
|
399 sendCustomGesture(&event, &widget); |
|
400 CustomGestureRecognizer::ConsumeEvents = false; |
|
401 |
|
402 QCOMPARE(widget.customEventsReceived, 0); |
|
403 } |
|
404 */ |
|
405 |
|
406 /* |
|
407 |
|
408 void tst_Gestures::autoCancelingGestures() |
|
409 { |
|
410 GestureWidget widget; |
|
411 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
412 // send partial gesture. The gesture will be in the "maybe" state, but will |
|
413 // never get enough events to fire, so Qt will have to kill it. |
|
414 CustomEvent ev; |
|
415 for (int i = CustomGesture::SerialMaybeThreshold; |
|
416 i < CustomGesture::SerialStartedThreshold; ++i) { |
|
417 ev.serial = i; |
|
418 QApplication::sendEvent(&widget, &ev); |
|
419 } |
|
420 // wait long enough so the gesture manager will cancel the gesture |
|
421 QTest::qWait(5000); |
|
422 QCOMPARE(widget.customEventsReceived, CustomGesture::SerialStartedThreshold - CustomGesture::SerialMaybeThreshold); |
|
423 QCOMPARE(widget.gestureEventsReceived, 0); |
|
424 QCOMPARE(widget.gestureOverrideEventsReceived, 0); |
|
425 QCOMPARE(widget.events.all.size(), 0); |
|
426 } |
|
427 |
|
428 void tst_Gestures::gestureOverChild() |
|
429 { |
|
430 GestureWidget widget("widget"); |
|
431 QVBoxLayout *l = new QVBoxLayout(&widget); |
|
432 GestureWidget *child = new GestureWidget("child"); |
|
433 l->addWidget(child); |
|
434 |
|
435 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
436 |
|
437 CustomEvent event; |
|
438 sendCustomGesture(&event, child); |
|
439 |
|
440 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
441 static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
442 |
|
443 QCOMPARE(child->customEventsReceived, TotalCustomEventsCount); |
|
444 QCOMPARE(widget.customEventsReceived, 0); |
|
445 QCOMPARE(child->gestureEventsReceived, 0); |
|
446 QCOMPARE(child->gestureOverrideEventsReceived, 0); |
|
447 QCOMPARE(widget.gestureEventsReceived, 0); |
|
448 QCOMPARE(widget.gestureOverrideEventsReceived, 0); |
|
449 |
|
450 // enable gestures over the children |
|
451 widget.grabGesture(CustomGesture::GestureType); |
|
452 |
|
453 widget.reset(); |
|
454 child->reset(); |
|
455 |
|
456 sendCustomGesture(&event, child); |
|
457 |
|
458 QCOMPARE(child->customEventsReceived, TotalCustomEventsCount); |
|
459 QCOMPARE(widget.customEventsReceived, 0); |
|
460 |
|
461 QCOMPARE(child->gestureEventsReceived, 0); |
|
462 QCOMPARE(child->gestureOverrideEventsReceived, 0); |
|
463 QCOMPARE(widget.gestureEventsReceived, TotalGestureEventsCount); |
|
464 QCOMPARE(widget.gestureOverrideEventsReceived, 0); |
|
465 for(int i = 0; i < widget.events.all.size(); ++i) |
|
466 QCOMPARE(widget.events.all.at(i), CustomGesture::GestureType); |
|
467 QCOMPARE(widget.events.started.size(), 1); |
|
468 QCOMPARE(widget.events.updated.size(), TotalGestureEventsCount - 2); |
|
469 QCOMPARE(widget.events.finished.size(), 1); |
|
470 QCOMPARE(widget.events.canceled.size(), 0); |
|
471 } |
|
472 |
|
473 void tst_Gestures::multipleWidgetOnlyGestureInTree() |
|
474 { |
|
475 GestureWidget parent("parent"); |
|
476 QVBoxLayout *l = new QVBoxLayout(&parent); |
|
477 GestureWidget *child = new GestureWidget("child"); |
|
478 l->addWidget(child); |
|
479 |
|
480 parent.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
481 child->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
482 |
|
483 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
484 static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
485 |
|
486 // sending events to the child and making sure there is no conflict |
|
487 CustomEvent event; |
|
488 sendCustomGesture(&event, child); |
|
489 |
|
490 QCOMPARE(child->customEventsReceived, TotalCustomEventsCount); |
|
491 QCOMPARE(parent.customEventsReceived, 0); |
|
492 QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount); |
|
493 QCOMPARE(child->gestureOverrideEventsReceived, 0); |
|
494 QCOMPARE(parent.gestureEventsReceived, 0); |
|
495 QCOMPARE(parent.gestureOverrideEventsReceived, 0); |
|
496 |
|
497 parent.reset(); |
|
498 child->reset(); |
|
499 |
|
500 // same for the parent widget |
|
501 sendCustomGesture(&event, &parent); |
|
502 |
|
503 QCOMPARE(child->customEventsReceived, 0); |
|
504 QCOMPARE(parent.customEventsReceived, TotalCustomEventsCount); |
|
505 QCOMPARE(child->gestureEventsReceived, 0); |
|
506 QCOMPARE(child->gestureOverrideEventsReceived, 0); |
|
507 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
|
508 QCOMPARE(parent.gestureOverrideEventsReceived, 0); |
|
509 } |
|
510 |
|
511 void tst_Gestures::conflictingGestures() |
|
512 { |
|
513 GestureWidget parent("parent"); |
|
514 QVBoxLayout *l = new QVBoxLayout(&parent); |
|
515 GestureWidget *child = new GestureWidget("child"); |
|
516 l->addWidget(child); |
|
517 |
|
518 parent.grabGesture(CustomGesture::GestureType); |
|
519 child->grabGesture(CustomGesture::GestureType); |
|
520 |
|
521 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
522 |
|
523 // child accepts the override, parent will not receive anything |
|
524 parent.acceptGestureOverride = false; |
|
525 child->acceptGestureOverride = true; |
|
526 |
|
527 // sending events to the child and making sure there is no conflict |
|
528 CustomEvent event; |
|
529 sendCustomGesture(&event, child); |
|
530 |
|
531 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
532 QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount); |
|
533 QCOMPARE(parent.gestureOverrideEventsReceived, 0); |
|
534 QCOMPARE(parent.gestureEventsReceived, 0); |
|
535 |
|
536 parent.reset(); |
|
537 child->reset(); |
|
538 |
|
539 // parent accepts the override |
|
540 parent.acceptGestureOverride = true; |
|
541 child->acceptGestureOverride = false; |
|
542 |
|
543 // sending events to the child and making sure there is no conflict |
|
544 sendCustomGesture(&event, child); |
|
545 |
|
546 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
547 QCOMPARE(child->gestureEventsReceived, 0); |
|
548 QCOMPARE(parent.gestureOverrideEventsReceived, 1); |
|
549 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
|
550 |
|
551 parent.reset(); |
|
552 child->reset(); |
|
553 |
|
554 // nobody accepts the override, we will send normal events to the closest |
|
555 // context (i.e. to the child widget) and it will be propagated and |
|
556 // accepted by the parent widget |
|
557 parent.acceptGestureOverride = false; |
|
558 child->acceptGestureOverride = false; |
|
559 child->ignoredGestures << CustomGesture::GestureType; |
|
560 |
|
561 // sending events to the child and making sure there is no conflict |
|
562 sendCustomGesture(&event, child); |
|
563 |
|
564 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
565 QCOMPARE(child->gestureEventsReceived, 1); |
|
566 QCOMPARE(parent.gestureOverrideEventsReceived, 1); |
|
567 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
|
568 |
|
569 parent.reset(); |
|
570 child->reset(); |
|
571 |
|
572 // nobody accepts the override, and nobody accepts the gesture event |
|
573 parent.acceptGestureOverride = false; |
|
574 child->acceptGestureOverride = false; |
|
575 parent.ignoredGestures << CustomGesture::GestureType; |
|
576 child->ignoredGestures << CustomGesture::GestureType; |
|
577 |
|
578 // sending events to the child and making sure there is no conflict |
|
579 sendCustomGesture(&event, child); |
|
580 |
|
581 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
582 QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount); |
|
583 QCOMPARE(parent.gestureOverrideEventsReceived, 1); |
|
584 QCOMPARE(parent.gestureEventsReceived, 1); |
|
585 |
|
586 parent.reset(); |
|
587 child->reset(); |
|
588 |
|
589 // we set an attribute to make sure all gesture events are propagated |
|
590 parent.grabGesture(CustomGesture::GestureType, Qt::ReceivePartialGestures); |
|
591 parent.acceptGestureOverride = false; |
|
592 child->acceptGestureOverride = false; |
|
593 parent.ignoredGestures << CustomGesture::GestureType; |
|
594 child->ignoredGestures << CustomGesture::GestureType; |
|
595 |
|
596 // sending events to the child and making sure there is no conflict |
|
597 sendCustomGesture(&event, child); |
|
598 |
|
599 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
600 QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount); |
|
601 QCOMPARE(parent.gestureOverrideEventsReceived, 1); |
|
602 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
|
603 |
|
604 parent.reset(); |
|
605 child->reset(); |
|
606 |
|
607 Qt::GestureType ContinuousGesture = QGestureRecognizer::registerRecognizer(new CustomContinuousGestureRecognizer); |
|
608 static const int ContinuousGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
609 child->grabGesture(ContinuousGesture); |
|
610 // child accepts override. And it also receives another custom gesture. |
|
611 parent.acceptGestureOverride = false; |
|
612 child->acceptGestureOverride = true; |
|
613 sendCustomGesture(&event, child); |
|
614 |
|
615 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
616 QVERIFY(child->gestureEventsReceived > TotalGestureEventsCount); |
|
617 QCOMPARE(child->events.all.count(), TotalGestureEventsCount + ContinuousGestureEventsCount); |
|
618 QCOMPARE(parent.gestureOverrideEventsReceived, 0); |
|
619 QCOMPARE(parent.gestureEventsReceived, 0); |
|
620 |
|
621 QGestureRecognizer::unregisterRecognizer(ContinuousGesture); |
|
622 } |
|
623 |
|
624 void tst_Gestures::finishedWithoutStarted() |
|
625 { |
|
626 GestureWidget widget; |
|
627 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
628 |
|
629 // the gesture will claim it finished, but it was never started. |
|
630 CustomEvent ev; |
|
631 ev.serial = CustomGesture::SerialFinishedThreshold; |
|
632 QApplication::sendEvent(&widget, &ev); |
|
633 |
|
634 QCOMPARE(widget.customEventsReceived, 1); |
|
635 QCOMPARE(widget.gestureEventsReceived, 2); |
|
636 QCOMPARE(widget.gestureOverrideEventsReceived, 0); |
|
637 QCOMPARE(widget.events.all.size(), 2); |
|
638 QCOMPARE(widget.events.started.size(), 1); |
|
639 QCOMPARE(widget.events.updated.size(), 0); |
|
640 QCOMPARE(widget.events.finished.size(), 1); |
|
641 QCOMPARE(widget.events.canceled.size(), 0); |
|
642 } |
|
643 |
|
644 */ |
|
645 |
|
646 /* |
|
647 void tst_Gestures::unknownGesture() |
|
648 { |
|
649 GestureWidget widget; |
|
650 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
651 widget.grabGesture(Qt::CustomGesture, Qt::DontStartGestureOnChildren); |
|
652 widget.grabGesture(Qt::GestureType(Qt::PanGesture+512), Qt::DontStartGestureOnChildren); |
|
653 |
|
654 CustomEvent event; |
|
655 sendCustomGesture(&event, &widget); |
|
656 |
|
657 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
658 |
|
659 QCOMPARE(widget.gestureEventsReceived, TotalGestureEventsCount); |
|
660 } |
|
661 */ |
|
662 |
|
663 static const QColor InstanceColors[] = { |
|
664 Qt::blue, Qt::red, Qt::green, Qt::gray, Qt::yellow |
|
665 }; |
|
666 |
|
667 class GestureItem : public QGraphicsObject |
|
668 { |
|
669 static int InstanceCount; |
|
670 |
|
671 public: |
|
672 GestureItem(const char *name = 0) |
|
673 { |
|
674 instanceNumber = InstanceCount++; |
|
675 if (name) |
|
676 setObjectName(QLatin1String(name)); |
|
677 size = QRectF(0, 0, 100, 100); |
|
678 customEventsReceived = 0; |
|
679 gestureEventsReceived = 0; |
|
680 gestureOverrideEventsReceived = 0; |
|
681 events.clear(); |
|
682 overrideEvents.clear(); |
|
683 acceptGestureOverride = false; |
|
684 } |
|
685 ~GestureItem() |
|
686 { |
|
687 --InstanceCount; |
|
688 } |
|
689 |
|
690 int customEventsReceived; |
|
691 int gestureEventsReceived; |
|
692 int gestureOverrideEventsReceived; |
|
693 struct Events |
|
694 { |
|
695 QList<Qt::GestureType> all; |
|
696 QList<Qt::GestureType> started; |
|
697 QList<Qt::GestureType> updated; |
|
698 QList<Qt::GestureType> finished; |
|
699 QList<Qt::GestureType> canceled; |
|
700 |
|
701 void clear() |
|
702 { |
|
703 all.clear(); |
|
704 started.clear(); |
|
705 updated.clear(); |
|
706 finished.clear(); |
|
707 canceled.clear(); |
|
708 } |
|
709 } events, overrideEvents; |
|
710 |
|
711 bool acceptGestureOverride; |
|
712 QSet<Qt::GestureType> ignoredGestures; |
|
713 |
|
714 QRectF size; |
|
715 int instanceNumber; |
|
716 |
|
717 void reset() |
|
718 { |
|
719 customEventsReceived = 0; |
|
720 gestureEventsReceived = 0; |
|
721 gestureOverrideEventsReceived = 0; |
|
722 events.clear(); |
|
723 overrideEvents.clear(); |
|
724 ignoredGestures.clear(); |
|
725 } |
|
726 |
|
727 protected: |
|
728 QRectF boundingRect() const |
|
729 { |
|
730 return size; |
|
731 } |
|
732 void paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) |
|
733 { |
|
734 QColor color = InstanceColors[instanceNumber % (sizeof(InstanceColors)/sizeof(InstanceColors[0]))]; |
|
735 p->fillRect(boundingRect(), color); |
|
736 } |
|
737 |
|
738 bool event(QEvent *event) |
|
739 { |
|
740 Events *eventsPtr = 0; |
|
741 if (event->type() == QEvent::Gesture) { |
|
742 ++gestureEventsReceived; |
|
743 eventsPtr = &events; |
|
744 QGestureEvent *e = static_cast<QGestureEvent *>(event); |
|
745 foreach(Qt::GestureType type, ignoredGestures) |
|
746 e->ignore(e->gesture(type)); |
|
747 } else if (event->type() == QEvent::GestureOverride) { |
|
748 ++gestureOverrideEventsReceived; |
|
749 eventsPtr = &overrideEvents; |
|
750 if (acceptGestureOverride) |
|
751 event->accept(); |
|
752 } |
|
753 if (eventsPtr) { |
|
754 QGestureEvent *e = static_cast<QGestureEvent*>(event); |
|
755 QList<QGesture*> gestures = e->gestures(); |
|
756 foreach(QGesture *g, gestures) { |
|
757 eventsPtr->all << g->gestureType(); |
|
758 switch(g->state()) { |
|
759 case Qt::GestureStarted: |
|
760 eventsPtr->started << g->gestureType(); |
|
761 break; |
|
762 case Qt::GestureUpdated: |
|
763 eventsPtr->updated << g->gestureType(); |
|
764 break; |
|
765 case Qt::GestureFinished: |
|
766 eventsPtr->finished << g->gestureType(); |
|
767 break; |
|
768 case Qt::GestureCanceled: |
|
769 eventsPtr->canceled << g->gestureType(); |
|
770 break; |
|
771 default: |
|
772 Q_ASSERT(false); |
|
773 } |
|
774 } |
|
775 } else if (event->type() == CustomEvent::EventType) { |
|
776 ++customEventsReceived; |
|
777 } else { |
|
778 return QGraphicsObject::event(event); |
|
779 } |
|
780 return true; |
|
781 } |
|
782 }; |
|
783 int GestureItem::InstanceCount = 0; |
|
784 |
|
785 /* |
|
786 void tst_Gestures::graphicsItemGesture() |
|
787 { |
|
788 QGraphicsScene scene; |
|
789 QGraphicsView view(&scene); |
|
790 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
791 |
|
792 GestureItem *item = new GestureItem("item"); |
|
793 scene.addItem(item); |
|
794 item->setPos(100, 100); |
|
795 |
|
796 view.show(); |
|
797 QTest::qWaitForWindowShown(&view); |
|
798 view.ensureVisible(scene.sceneRect()); |
|
799 |
|
800 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
801 item->grabGesture(CustomGesture::GestureType); |
|
802 |
|
803 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
804 static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
805 |
|
806 CustomEvent event; |
|
807 // gesture without hotspot should not be delivered to items in the view |
|
808 QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture"); |
|
809 QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture"); |
|
810 QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture"); |
|
811 QTest::ignoreMessage(QtWarningMsg, "QGestureManager::deliverEvent: could not find the target for gesture"); |
|
812 sendCustomGesture(&event, item, &scene); |
|
813 |
|
814 QCOMPARE(item->customEventsReceived, TotalCustomEventsCount); |
|
815 QCOMPARE(item->gestureEventsReceived, 0); |
|
816 QCOMPARE(item->gestureOverrideEventsReceived, 0); |
|
817 |
|
818 item->reset(); |
|
819 |
|
820 // make sure the event is properly delivered if only the hotspot is set. |
|
821 event.hotSpot = mapToGlobal(QPointF(10, 10), item, &view); |
|
822 event.hasHotSpot = true; |
|
823 sendCustomGesture(&event, item, &scene); |
|
824 |
|
825 QCOMPARE(item->customEventsReceived, TotalCustomEventsCount); |
|
826 QCOMPARE(item->gestureEventsReceived, TotalGestureEventsCount); |
|
827 QCOMPARE(item->gestureOverrideEventsReceived, 0); |
|
828 QCOMPARE(item->events.all.size(), TotalGestureEventsCount); |
|
829 for(int i = 0; i < item->events.all.size(); ++i) |
|
830 QCOMPARE(item->events.all.at(i), CustomGesture::GestureType); |
|
831 QCOMPARE(item->events.started.size(), 1); |
|
832 QCOMPARE(item->events.updated.size(), TotalGestureEventsCount - 2); |
|
833 QCOMPARE(item->events.finished.size(), 1); |
|
834 QCOMPARE(item->events.canceled.size(), 0); |
|
835 |
|
836 item->reset(); |
|
837 |
|
838 // send gesture to the item which ignores it. |
|
839 item->ignoredGestures << CustomGesture::GestureType; |
|
840 |
|
841 event.hotSpot = mapToGlobal(QPointF(10, 10), item, &view); |
|
842 event.hasHotSpot = true; |
|
843 sendCustomGesture(&event, item, &scene); |
|
844 QCOMPARE(item->customEventsReceived, TotalCustomEventsCount); |
|
845 QCOMPARE(item->gestureEventsReceived, TotalGestureEventsCount); |
|
846 QCOMPARE(item->gestureOverrideEventsReceived, 0); |
|
847 } |
|
848 |
|
849 void tst_Gestures::graphicsItemTreeGesture() |
|
850 { |
|
851 QGraphicsScene scene; |
|
852 QGraphicsView view(&scene); |
|
853 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
854 |
|
855 GestureItem *item1 = new GestureItem("item1"); |
|
856 item1->setPos(100, 100); |
|
857 item1->size = QRectF(0, 0, 350, 200); |
|
858 scene.addItem(item1); |
|
859 |
|
860 GestureItem *item1_child1 = new GestureItem("item1_child1"); |
|
861 item1_child1->setPos(50, 50); |
|
862 item1_child1->size = QRectF(0, 0, 100, 100); |
|
863 item1_child1->setParentItem(item1); |
|
864 |
|
865 GestureItem *item1_child2 = new GestureItem("item1_child2"); |
|
866 item1_child2->size = QRectF(0, 0, 100, 100); |
|
867 item1_child2->setPos(200, 50); |
|
868 item1_child2->setParentItem(item1); |
|
869 |
|
870 view.show(); |
|
871 QTest::qWaitForWindowShown(&view); |
|
872 view.ensureVisible(scene.sceneRect()); |
|
873 |
|
874 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
875 item1->grabGesture(CustomGesture::GestureType); |
|
876 |
|
877 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
878 |
|
879 CustomEvent event; |
|
880 event.hotSpot = mapToGlobal(QPointF(10, 10), item1_child1, &view); |
|
881 event.hasHotSpot = true; |
|
882 |
|
883 item1->ignoredGestures << CustomGesture::GestureType; |
|
884 sendCustomGesture(&event, item1_child1, &scene); |
|
885 QCOMPARE(item1_child1->gestureOverrideEventsReceived, 0); |
|
886 QCOMPARE(item1_child1->gestureEventsReceived, 0); |
|
887 QCOMPARE(item1_child2->gestureEventsReceived, 0); |
|
888 QCOMPARE(item1_child2->gestureOverrideEventsReceived, 0); |
|
889 QCOMPARE(item1->gestureOverrideEventsReceived, 0); |
|
890 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
|
891 |
|
892 item1->reset(); item1_child1->reset(); item1_child2->reset(); |
|
893 |
|
894 item1_child1->grabGesture(CustomGesture::GestureType); |
|
895 |
|
896 item1->ignoredGestures << CustomGesture::GestureType; |
|
897 item1_child1->ignoredGestures << CustomGesture::GestureType; |
|
898 sendCustomGesture(&event, item1_child1, &scene); |
|
899 QCOMPARE(item1_child1->gestureOverrideEventsReceived, 1); |
|
900 QCOMPARE(item1_child1->gestureEventsReceived, TotalGestureEventsCount); |
|
901 QCOMPARE(item1_child2->gestureEventsReceived, 0); |
|
902 QCOMPARE(item1_child2->gestureOverrideEventsReceived, 0); |
|
903 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
904 QCOMPARE(item1->gestureEventsReceived, 1); |
|
905 } |
|
906 |
|
907 void tst_Gestures::explicitGraphicsObjectTarget() |
|
908 { |
|
909 QGraphicsScene scene; |
|
910 QGraphicsView view(&scene); |
|
911 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
912 |
|
913 GestureItem *item1 = new GestureItem("item1"); |
|
914 scene.addItem(item1); |
|
915 item1->setPos(100, 100); |
|
916 item1->setZValue(1); |
|
917 |
|
918 GestureItem *item2 = new GestureItem("item2"); |
|
919 scene.addItem(item2); |
|
920 item2->setPos(100, 100); |
|
921 item2->setZValue(5); |
|
922 |
|
923 GestureItem *item2_child1 = new GestureItem("item2_child1"); |
|
924 scene.addItem(item2_child1); |
|
925 item2_child1->setParentItem(item2); |
|
926 item2_child1->setPos(10, 10); |
|
927 |
|
928 view.show(); |
|
929 QTest::qWaitForWindowShown(&view); |
|
930 view.ensureVisible(scene.sceneRect()); |
|
931 |
|
932 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
933 item1->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
934 item2->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
935 item2_child1->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
936 |
|
937 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
938 |
|
939 // sending events to item1, but the hotSpot is set to item2 |
|
940 CustomEvent event; |
|
941 event.hotSpot = mapToGlobal(QPointF(15, 15), item2, &view); |
|
942 event.hasHotSpot = true; |
|
943 |
|
944 sendCustomGesture(&event, item1, &scene); |
|
945 |
|
946 QCOMPARE(item1->gestureEventsReceived, 0); |
|
947 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
948 QCOMPARE(item2_child1->gestureEventsReceived, TotalGestureEventsCount); |
|
949 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 1); |
|
950 QCOMPARE(item2_child1->events.all.size(), TotalGestureEventsCount); |
|
951 for(int i = 0; i < item2_child1->events.all.size(); ++i) |
|
952 QCOMPARE(item2_child1->events.all.at(i), CustomGesture::GestureType); |
|
953 QCOMPARE(item2_child1->events.started.size(), 1); |
|
954 QCOMPARE(item2_child1->events.updated.size(), TotalGestureEventsCount - 2); |
|
955 QCOMPARE(item2_child1->events.finished.size(), 1); |
|
956 QCOMPARE(item2_child1->events.canceled.size(), 0); |
|
957 QCOMPARE(item2->gestureEventsReceived, 0); |
|
958 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
|
959 } |
|
960 |
|
961 void tst_Gestures::gestureOverChildGraphicsItem() |
|
962 { |
|
963 QGraphicsScene scene; |
|
964 QGraphicsView view(&scene); |
|
965 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
966 |
|
967 GestureItem *item0 = new GestureItem("item0"); |
|
968 scene.addItem(item0); |
|
969 item0->setPos(0, 0); |
|
970 item0->grabGesture(CustomGesture::GestureType); |
|
971 item0->setZValue(1); |
|
972 |
|
973 GestureItem *item1 = new GestureItem("item1"); |
|
974 scene.addItem(item1); |
|
975 item1->setPos(100, 100); |
|
976 item1->setZValue(5); |
|
977 |
|
978 GestureItem *item2 = new GestureItem("item2"); |
|
979 scene.addItem(item2); |
|
980 item2->setPos(100, 100); |
|
981 item2->setZValue(10); |
|
982 |
|
983 GestureItem *item2_child1 = new GestureItem("item2_child1"); |
|
984 scene.addItem(item2_child1); |
|
985 item2_child1->setParentItem(item2); |
|
986 item2_child1->setPos(0, 0); |
|
987 |
|
988 view.show(); |
|
989 QTest::qWaitForWindowShown(&view); |
|
990 view.ensureVisible(scene.sceneRect()); |
|
991 |
|
992 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
993 item1->grabGesture(CustomGesture::GestureType); |
|
994 |
|
995 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
996 static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
997 |
|
998 CustomEvent event; |
|
999 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
|
1000 event.hasHotSpot = true; |
|
1001 sendCustomGesture(&event, item0, &scene); |
|
1002 |
|
1003 QCOMPARE(item0->customEventsReceived, TotalCustomEventsCount); |
|
1004 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
|
1005 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
|
1006 QCOMPARE(item2->gestureEventsReceived, 0); |
|
1007 QCOMPARE(item2->gestureOverrideEventsReceived, 0); |
|
1008 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
|
1009 QCOMPARE(item1->gestureOverrideEventsReceived, 0); |
|
1010 |
|
1011 item0->reset(); item1->reset(); item2->reset(); item2_child1->reset(); |
|
1012 item2->grabGesture(CustomGesture::GestureType); |
|
1013 item2->ignoredGestures << CustomGesture::GestureType; |
|
1014 |
|
1015 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
|
1016 event.hasHotSpot = true; |
|
1017 sendCustomGesture(&event, item0, &scene); |
|
1018 |
|
1019 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
|
1020 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
|
1021 QCOMPARE(item2->gestureEventsReceived, 1); |
|
1022 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
|
1023 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
|
1024 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
1025 |
|
1026 item0->reset(); item1->reset(); item2->reset(); item2_child1->reset(); |
|
1027 item2->grabGesture(CustomGesture::GestureType); |
|
1028 item2->ignoredGestures << CustomGesture::GestureType; |
|
1029 item1->ignoredGestures << CustomGesture::GestureType; |
|
1030 |
|
1031 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
|
1032 event.hasHotSpot = true; |
|
1033 sendCustomGesture(&event, item0, &scene); |
|
1034 |
|
1035 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
|
1036 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
|
1037 QCOMPARE(item2->gestureEventsReceived, TotalGestureEventsCount); |
|
1038 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
|
1039 QCOMPARE(item1->gestureEventsReceived, 1); |
|
1040 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
1041 |
|
1042 item0->reset(); item1->reset(); item2->reset(); item2_child1->reset(); |
|
1043 item2->grabGesture(CustomGesture::GestureType); |
|
1044 item2->ignoredGestures << CustomGesture::GestureType; |
|
1045 item1->ignoredGestures << CustomGesture::GestureType; |
|
1046 item1->grabGesture(CustomGesture::GestureType, Qt::ReceivePartialGestures); |
|
1047 |
|
1048 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
|
1049 event.hasHotSpot = true; |
|
1050 sendCustomGesture(&event, item0, &scene); |
|
1051 |
|
1052 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
|
1053 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
|
1054 QCOMPARE(item2->gestureEventsReceived, TotalGestureEventsCount); |
|
1055 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
|
1056 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
|
1057 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
1058 } |
|
1059 |
|
1060 void tst_Gestures::twoGesturesOnDifferentLevel() |
|
1061 { |
|
1062 GestureWidget parent("parent"); |
|
1063 QVBoxLayout *l = new QVBoxLayout(&parent); |
|
1064 GestureWidget *child = new GestureWidget("child"); |
|
1065 l->addWidget(child); |
|
1066 |
|
1067 Qt::GestureType SecondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1068 |
|
1069 parent.grabGesture(CustomGesture::GestureType); |
|
1070 child->grabGesture(SecondGesture); |
|
1071 |
|
1072 CustomEvent event; |
|
1073 // sending events that form a gesture to one widget, but they will be |
|
1074 // filtered by two different gesture recognizers and will generate two |
|
1075 // QGesture objects. Check that those gesture objects are delivered to |
|
1076 // different widgets properly. |
|
1077 sendCustomGesture(&event, child); |
|
1078 |
|
1079 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
1080 static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
1081 |
|
1082 QCOMPARE(child->customEventsReceived, TotalCustomEventsCount); |
|
1083 QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount); |
|
1084 QCOMPARE(child->gestureOverrideEventsReceived, 0); |
|
1085 QCOMPARE(child->events.all.size(), TotalGestureEventsCount); |
|
1086 for(int i = 0; i < child->events.all.size(); ++i) |
|
1087 QCOMPARE(child->events.all.at(i), SecondGesture); |
|
1088 |
|
1089 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
|
1090 QCOMPARE(parent.gestureOverrideEventsReceived, 0); |
|
1091 QCOMPARE(parent.events.all.size(), TotalGestureEventsCount); |
|
1092 for(int i = 0; i < child->events.all.size(); ++i) |
|
1093 QCOMPARE(parent.events.all.at(i), CustomGesture::GestureType); |
|
1094 |
|
1095 QGestureRecognizer::unregisterRecognizer(SecondGesture); |
|
1096 } |
|
1097 |
|
1098 void tst_Gestures::multipleGesturesInTree() |
|
1099 { |
|
1100 GestureWidget a("A"); |
|
1101 GestureWidget *A = &a; |
|
1102 GestureWidget *B = new GestureWidget("B", A); |
|
1103 GestureWidget *C = new GestureWidget("C", B); |
|
1104 GestureWidget *D = new GestureWidget("D", C); |
|
1105 |
|
1106 Qt::GestureType FirstGesture = CustomGesture::GestureType; |
|
1107 Qt::GestureType SecondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1108 Qt::GestureType ThirdGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1109 |
|
1110 Qt::GestureFlags flags = Qt::ReceivePartialGestures; |
|
1111 A->grabGesture(FirstGesture, flags); // A [1 3] |
|
1112 A->grabGesture(ThirdGesture, flags); // | |
|
1113 B->grabGesture(SecondGesture, flags); // B [ 2 3] |
|
1114 B->grabGesture(ThirdGesture, flags); // | |
|
1115 C->grabGesture(FirstGesture, flags); // C [1 2 3] |
|
1116 C->grabGesture(SecondGesture, flags); // | |
|
1117 C->grabGesture(ThirdGesture, flags); // D [1 3] |
|
1118 D->grabGesture(FirstGesture, flags); |
|
1119 D->grabGesture(ThirdGesture, flags); |
|
1120 |
|
1121 // make sure all widgets ignore events, so they get propagated. |
|
1122 A->ignoredGestures << FirstGesture << ThirdGesture; |
|
1123 B->ignoredGestures << SecondGesture << ThirdGesture; |
|
1124 C->ignoredGestures << FirstGesture << SecondGesture << ThirdGesture; |
|
1125 D->ignoredGestures << FirstGesture << ThirdGesture; |
|
1126 |
|
1127 CustomEvent event; |
|
1128 sendCustomGesture(&event, D); |
|
1129 |
|
1130 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
1131 |
|
1132 // gesture override events |
|
1133 QCOMPARE(D->overrideEvents.all.count(FirstGesture), 1); |
|
1134 QCOMPARE(D->overrideEvents.all.count(SecondGesture), 0); |
|
1135 QCOMPARE(D->overrideEvents.all.count(ThirdGesture), 1); |
|
1136 |
|
1137 QCOMPARE(C->overrideEvents.all.count(FirstGesture), 1); |
|
1138 QCOMPARE(C->overrideEvents.all.count(SecondGesture), 1); |
|
1139 QCOMPARE(C->overrideEvents.all.count(ThirdGesture), 1); |
|
1140 |
|
1141 QCOMPARE(B->overrideEvents.all.count(FirstGesture), 0); |
|
1142 QCOMPARE(B->overrideEvents.all.count(SecondGesture), 1); |
|
1143 QCOMPARE(B->overrideEvents.all.count(ThirdGesture), 1); |
|
1144 |
|
1145 QCOMPARE(A->overrideEvents.all.count(FirstGesture), 1); |
|
1146 QCOMPARE(A->overrideEvents.all.count(SecondGesture), 0); |
|
1147 QCOMPARE(A->overrideEvents.all.count(ThirdGesture), 1); |
|
1148 |
|
1149 // normal gesture events |
|
1150 QCOMPARE(D->events.all.count(FirstGesture), TotalGestureEventsCount); |
|
1151 QCOMPARE(D->events.all.count(SecondGesture), 0); |
|
1152 QCOMPARE(D->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1153 |
|
1154 QCOMPARE(C->events.all.count(FirstGesture), TotalGestureEventsCount); |
|
1155 QCOMPARE(C->events.all.count(SecondGesture), TotalGestureEventsCount); |
|
1156 QCOMPARE(C->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1157 |
|
1158 QCOMPARE(B->events.all.count(FirstGesture), 0); |
|
1159 QCOMPARE(B->events.all.count(SecondGesture), TotalGestureEventsCount); |
|
1160 QCOMPARE(B->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1161 |
|
1162 QCOMPARE(A->events.all.count(FirstGesture), TotalGestureEventsCount); |
|
1163 QCOMPARE(A->events.all.count(SecondGesture), 0); |
|
1164 QCOMPARE(A->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1165 |
|
1166 QGestureRecognizer::unregisterRecognizer(SecondGesture); |
|
1167 QGestureRecognizer::unregisterRecognizer(ThirdGesture); |
|
1168 } |
|
1169 |
|
1170 void tst_Gestures::multipleGesturesInComplexTree() |
|
1171 { |
|
1172 GestureWidget a("A"); |
|
1173 GestureWidget *A = &a; |
|
1174 GestureWidget *B = new GestureWidget("B", A); |
|
1175 GestureWidget *C = new GestureWidget("C", B); |
|
1176 GestureWidget *D = new GestureWidget("D", C); |
|
1177 |
|
1178 Qt::GestureType FirstGesture = CustomGesture::GestureType; |
|
1179 Qt::GestureType SecondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1180 Qt::GestureType ThirdGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1181 Qt::GestureType FourthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1182 Qt::GestureType FifthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1183 Qt::GestureType SixthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1184 Qt::GestureType SeventhGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1185 |
|
1186 Qt::GestureFlags flags = Qt::ReceivePartialGestures; |
|
1187 A->grabGesture(FirstGesture, flags); // A [1,3,4] |
|
1188 A->grabGesture(ThirdGesture, flags); // | |
|
1189 A->grabGesture(FourthGesture, flags); // B [2,3,5] |
|
1190 B->grabGesture(SecondGesture, flags); // | |
|
1191 B->grabGesture(ThirdGesture, flags); // C [1,2,3,6] |
|
1192 B->grabGesture(FifthGesture, flags); // | |
|
1193 C->grabGesture(FirstGesture, flags); // D [1,3,7] |
|
1194 C->grabGesture(SecondGesture, flags); |
|
1195 C->grabGesture(ThirdGesture, flags); |
|
1196 C->grabGesture(SixthGesture, flags); |
|
1197 D->grabGesture(FirstGesture, flags); |
|
1198 D->grabGesture(ThirdGesture, flags); |
|
1199 D->grabGesture(SeventhGesture, flags); |
|
1200 |
|
1201 // make sure all widgets ignore events, so they get propagated. |
|
1202 QSet<Qt::GestureType> allGestureTypes; |
|
1203 allGestureTypes << FirstGesture << SecondGesture << ThirdGesture |
|
1204 << FourthGesture << FifthGesture << SixthGesture << SeventhGesture; |
|
1205 A->ignoredGestures = B->ignoredGestures = allGestureTypes; |
|
1206 C->ignoredGestures = D->ignoredGestures = allGestureTypes; |
|
1207 |
|
1208 CustomEvent event; |
|
1209 sendCustomGesture(&event, D); |
|
1210 |
|
1211 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
1212 |
|
1213 // gesture override events |
|
1214 QCOMPARE(D->overrideEvents.all.count(FirstGesture), 1); |
|
1215 QCOMPARE(D->overrideEvents.all.count(SecondGesture), 0); |
|
1216 QCOMPARE(D->overrideEvents.all.count(ThirdGesture), 1); |
|
1217 |
|
1218 QCOMPARE(C->overrideEvents.all.count(FirstGesture), 1); |
|
1219 QCOMPARE(C->overrideEvents.all.count(SecondGesture), 1); |
|
1220 QCOMPARE(C->overrideEvents.all.count(ThirdGesture), 1); |
|
1221 |
|
1222 QCOMPARE(B->overrideEvents.all.count(FirstGesture), 0); |
|
1223 QCOMPARE(B->overrideEvents.all.count(SecondGesture), 1); |
|
1224 QCOMPARE(B->overrideEvents.all.count(ThirdGesture), 1); |
|
1225 |
|
1226 QCOMPARE(A->overrideEvents.all.count(FirstGesture), 1); |
|
1227 QCOMPARE(A->overrideEvents.all.count(SecondGesture), 0); |
|
1228 QCOMPARE(A->overrideEvents.all.count(ThirdGesture), 1); |
|
1229 |
|
1230 // normal gesture events |
|
1231 QCOMPARE(D->events.all.count(FirstGesture), TotalGestureEventsCount); |
|
1232 QCOMPARE(D->events.all.count(SecondGesture), 0); |
|
1233 QCOMPARE(D->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1234 QCOMPARE(D->events.all.count(FourthGesture), 0); |
|
1235 QCOMPARE(D->events.all.count(FifthGesture), 0); |
|
1236 QCOMPARE(D->events.all.count(SixthGesture), 0); |
|
1237 QCOMPARE(D->events.all.count(SeventhGesture), TotalGestureEventsCount); |
|
1238 |
|
1239 QCOMPARE(C->events.all.count(FirstGesture), TotalGestureEventsCount); |
|
1240 QCOMPARE(C->events.all.count(SecondGesture), TotalGestureEventsCount); |
|
1241 QCOMPARE(C->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1242 QCOMPARE(C->events.all.count(FourthGesture), 0); |
|
1243 QCOMPARE(C->events.all.count(FifthGesture), 0); |
|
1244 QCOMPARE(C->events.all.count(SixthGesture), TotalGestureEventsCount); |
|
1245 QCOMPARE(C->events.all.count(SeventhGesture), 0); |
|
1246 |
|
1247 QCOMPARE(B->events.all.count(FirstGesture), 0); |
|
1248 QCOMPARE(B->events.all.count(SecondGesture), TotalGestureEventsCount); |
|
1249 QCOMPARE(B->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1250 QCOMPARE(B->events.all.count(FourthGesture), 0); |
|
1251 QCOMPARE(B->events.all.count(FifthGesture), TotalGestureEventsCount); |
|
1252 QCOMPARE(B->events.all.count(SixthGesture), 0); |
|
1253 QCOMPARE(B->events.all.count(SeventhGesture), 0); |
|
1254 |
|
1255 QCOMPARE(A->events.all.count(FirstGesture), TotalGestureEventsCount); |
|
1256 QCOMPARE(A->events.all.count(SecondGesture), 0); |
|
1257 QCOMPARE(A->events.all.count(ThirdGesture), TotalGestureEventsCount); |
|
1258 QCOMPARE(A->events.all.count(FourthGesture), TotalGestureEventsCount); |
|
1259 QCOMPARE(A->events.all.count(FifthGesture), 0); |
|
1260 QCOMPARE(A->events.all.count(SixthGesture), 0); |
|
1261 QCOMPARE(A->events.all.count(SeventhGesture), 0); |
|
1262 |
|
1263 QGestureRecognizer::unregisterRecognizer(SecondGesture); |
|
1264 QGestureRecognizer::unregisterRecognizer(ThirdGesture); |
|
1265 QGestureRecognizer::unregisterRecognizer(FourthGesture); |
|
1266 QGestureRecognizer::unregisterRecognizer(FifthGesture); |
|
1267 QGestureRecognizer::unregisterRecognizer(SixthGesture); |
|
1268 QGestureRecognizer::unregisterRecognizer(SeventhGesture); |
|
1269 } |
|
1270 |
|
1271 void tst_Gestures::testMapToScene() |
|
1272 { |
|
1273 QGesture gesture; |
|
1274 QList<QGesture*> list; |
|
1275 list << &gesture; |
|
1276 QGestureEvent event(list); |
|
1277 QCOMPARE(event.mapToGraphicsScene(gesture.hotSpot()), QPointF()); // not set, can't do much |
|
1278 |
|
1279 QGraphicsScene scene; |
|
1280 QGraphicsView view(&scene); |
|
1281 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
1282 |
|
1283 GestureItem *item0 = new GestureItem; |
|
1284 scene.addItem(item0); |
|
1285 item0->setPos(14, 16); |
|
1286 |
|
1287 view.show(); // need to show to give it a global coordinate |
|
1288 QTest::qWaitForWindowShown(&view); |
|
1289 view.ensureVisible(scene.sceneRect()); |
|
1290 |
|
1291 QPoint origin = view.mapToGlobal(QPoint()); |
|
1292 event.setWidget(view.viewport()); |
|
1293 |
|
1294 QCOMPARE(event.mapToGraphicsScene(origin + QPoint(100, 200)), view.mapToScene(QPoint(100, 200))); |
|
1295 } |
|
1296 */ |
|
1297 |
|
1298 /* |
|
1299 void tst_Gestures::ungrabGesture() // a method on QWidget |
|
1300 { |
|
1301 class MockGestureWidget : public GestureWidget { |
|
1302 public: |
|
1303 MockGestureWidget(const char *name = 0, QWidget *parent = 0) |
|
1304 : GestureWidget(name, parent) { } |
|
1305 |
|
1306 |
|
1307 QSet<QGesture*> gestures; |
|
1308 protected: |
|
1309 bool event(QEvent *event) |
|
1310 { |
|
1311 if (event->type() == QEvent::Gesture) { |
|
1312 QGestureEvent *gestureEvent = static_cast<QGestureEvent*>(event); |
|
1313 if (gestureEvent) |
|
1314 foreach (QGesture *g, gestureEvent->gestures()) |
|
1315 gestures.insert(g); |
|
1316 } |
|
1317 return GestureWidget::event(event); |
|
1318 } |
|
1319 }; |
|
1320 |
|
1321 MockGestureWidget parent("A"); |
|
1322 MockGestureWidget *a = &parent; |
|
1323 MockGestureWidget *b = new MockGestureWidget("B", a); |
|
1324 |
|
1325 a->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
1326 b->grabGesture(CustomGesture::GestureType); |
|
1327 b->ignoredGestures << CustomGesture::GestureType; |
|
1328 |
|
1329 CustomEvent event; |
|
1330 // sending an event will cause the QGesture objects to be instantiated for the widgets |
|
1331 sendCustomGesture(&event, b); |
|
1332 |
|
1333 QCOMPARE(a->gestures.count(), 1); |
|
1334 QPointer<QGesture> customGestureA; |
|
1335 customGestureA = *(a->gestures.begin()); |
|
1336 QVERIFY(!customGestureA.isNull()); |
|
1337 QCOMPARE(customGestureA->gestureType(), CustomGesture::GestureType); |
|
1338 |
|
1339 QCOMPARE(b->gestures.count(), 1); |
|
1340 QPointer<QGesture> customGestureB; |
|
1341 customGestureB = *(b->gestures.begin()); |
|
1342 QVERIFY(!customGestureB.isNull()); |
|
1343 QVERIFY(customGestureA.data() == customGestureB.data()); |
|
1344 QCOMPARE(customGestureB->gestureType(), CustomGesture::GestureType); |
|
1345 |
|
1346 a->gestures.clear(); |
|
1347 // sending an event will cause the QGesture objects to be instantiated for the widget |
|
1348 sendCustomGesture(&event, a); |
|
1349 |
|
1350 QCOMPARE(a->gestures.count(), 1); |
|
1351 customGestureA = *(a->gestures.begin()); |
|
1352 QVERIFY(!customGestureA.isNull()); |
|
1353 QCOMPARE(customGestureA->gestureType(), CustomGesture::GestureType); |
|
1354 QVERIFY(customGestureA.data() != customGestureB.data()); |
|
1355 |
|
1356 a->ungrabGesture(CustomGesture::GestureType); |
|
1357 QVERIFY(customGestureA.isNull()); |
|
1358 QVERIFY(!customGestureB.isNull()); |
|
1359 |
|
1360 a->gestures.clear(); |
|
1361 a->reset(); |
|
1362 // send again to 'b' and make sure a never gets it. |
|
1363 sendCustomGesture(&event, b); |
|
1364 QCOMPARE(a->gestureEventsReceived, 0); |
|
1365 QCOMPARE(a->gestureOverrideEventsReceived, 0); |
|
1366 } |
|
1367 */ |
|
1368 |
|
1369 /* |
|
1370 void tst_Gestures::unregisterRecognizer() // a method on QApplication |
|
1371 { |
|
1372 The hardest usecase to get right is when we remove a recognizer while several |
|
1373 of the gestures it created are in active state and we immediately add a new recognizer |
|
1374 for the same type (thus replacing the old one). |
|
1375 The expected result is that all old gestures continue till they are finished/cancelled |
|
1376 and the new recognizer starts creating gestures immediately at registration. |
|
1377 |
|
1378 This implies that deleting of the recognizer happens only when there are no more gestures |
|
1379 that it created. (since gestures might have a pointer to the recognizer) |
|
1380 } |
|
1381 */ |
|
1382 |
|
1383 /* |
|
1384 void tst_Gestures::autoCancelGestures() |
|
1385 { |
|
1386 class MockWidget : public GestureWidget { |
|
1387 public: |
|
1388 MockWidget(const char *name) : GestureWidget(name) { } |
|
1389 |
|
1390 bool event(QEvent *event) |
|
1391 { |
|
1392 if (event->type() == QEvent::Gesture) { |
|
1393 QGestureEvent *ge = static_cast<QGestureEvent*>(event); |
|
1394 Q_ASSERT(ge->gestures().count() == 1); // can't use QCOMPARE here... |
|
1395 ge->gestures().first()->setGestureCancelPolicy(QGesture::CancelAllInContext); |
|
1396 } |
|
1397 return GestureWidget::event(event); |
|
1398 } |
|
1399 }; |
|
1400 |
|
1401 const Qt::GestureType secondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1402 |
|
1403 MockWidget parent("parent"); // this one sets the cancel policy to CancelAllInContext |
|
1404 parent.resize(300, 100); |
|
1405 parent.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
1406 GestureWidget *child = new GestureWidget("child", &parent); |
|
1407 child->setGeometry(10, 10, 100, 80); |
|
1408 |
|
1409 parent.grabGesture(CustomGesture::GestureType); |
|
1410 child->grabGesture(secondGesture); |
|
1411 parent.show(); |
|
1412 QTest::qWaitForWindowShown(&parent); |
|
1413 |
|
1414 An event is send to both the child and the parent, when the child gets it a gesture is triggered |
|
1415 and send to the child. |
|
1416 When the parent gets the event a new gesture is triggered and delivered to the parent. When the |
|
1417 parent gets it he accepts it and that causes the cancel policy to activate. |
|
1418 The cause of that is the gesture for the child is cancelled and send to the child as such. |
|
1419 |
|
1420 CustomEvent event; |
|
1421 event.serial = CustomGesture::SerialStartedThreshold; |
|
1422 QApplication::sendEvent(child, &event); |
|
1423 QCOMPARE(child->events.all.count(), 2); |
|
1424 QCOMPARE(child->events.started.count(), 1); |
|
1425 QCOMPARE(child->events.canceled.count(), 1); |
|
1426 QCOMPARE(parent.events.all.count(), 1); |
|
1427 |
|
1428 // clean up, make the parent gesture finish |
|
1429 event.serial = CustomGesture::SerialFinishedThreshold; |
|
1430 QApplication::sendEvent(child, &event); |
|
1431 QCOMPARE(parent.events.all.count(), 2); |
|
1432 } |
|
1433 |
|
1434 void tst_Gestures::autoCancelGestures2() |
|
1435 { |
|
1436 class MockItem : public GestureItem { |
|
1437 public: |
|
1438 MockItem(const char *name) : GestureItem(name) { } |
|
1439 |
|
1440 bool event(QEvent *event) { |
|
1441 if (event->type() == QEvent::Gesture) { |
|
1442 QGestureEvent *ge = static_cast<QGestureEvent*>(event); |
|
1443 Q_ASSERT(ge->gestures().count() == 1); // can't use QCOMPARE here... |
|
1444 ge->gestures().first()->setGestureCancelPolicy(QGesture::CancelAllInContext); |
|
1445 } |
|
1446 return GestureItem::event(event); |
|
1447 } |
|
1448 }; |
|
1449 |
|
1450 const Qt::GestureType secondGesture = QGestureRecognizer ::registerRecognizer(new CustomGestureRecognizer); |
|
1451 |
|
1452 QGraphicsScene scene; |
|
1453 QGraphicsView view(&scene); |
|
1454 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
1455 |
|
1456 MockItem *parent = new MockItem("parent"); |
|
1457 GestureItem *child = new GestureItem("child"); |
|
1458 child->setParentItem(parent); |
|
1459 parent->setPos(0, 0); |
|
1460 child->setPos(10, 10); |
|
1461 scene.addItem(parent); |
|
1462 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
1463 view.viewport()->grabGesture(secondGesture, Qt::DontStartGestureOnChildren); |
|
1464 parent->grabGesture(CustomGesture::GestureType); |
|
1465 child->grabGesture(secondGesture); |
|
1466 |
|
1467 view.show(); |
|
1468 QTest::qWaitForWindowShown(&view); |
|
1469 view.ensureVisible(scene.sceneRect()); |
|
1470 |
|
1471 CustomEvent event; |
|
1472 event.serial = CustomGesture::SerialStartedThreshold; |
|
1473 event.hasHotSpot = true; |
|
1474 event.hotSpot = mapToGlobal(QPointF(5, 5), child, &view); |
|
1475 scene.sendEvent(child, &event); |
|
1476 QCOMPARE(parent->events.all.count(), 1); |
|
1477 QCOMPARE(child->events.started.count(), 1); |
|
1478 QCOMPARE(child->events.canceled.count(), 1); |
|
1479 QCOMPARE(child->events.all.count(), 2); |
|
1480 |
|
1481 // clean up, make the parent gesture finish |
|
1482 event.serial = CustomGesture::SerialFinishedThreshold; |
|
1483 scene.sendEvent(child, &event); |
|
1484 QCOMPARE(parent->events.all.count(), 2); |
|
1485 } |
|
1486 */ |
|
1487 |
|
1488 /* |
|
1489 void tst_Gestures::panelPropagation() |
|
1490 { |
|
1491 QGraphicsScene scene; |
|
1492 QGraphicsView view(&scene); |
|
1493 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
1494 |
|
1495 GestureItem *item0 = new GestureItem("item0"); |
|
1496 scene.addItem(item0); |
|
1497 item0->setPos(0, 0); |
|
1498 item0->size = QRectF(0, 0, 200, 200); |
|
1499 item0->grabGesture(CustomGesture::GestureType); |
|
1500 item0->setZValue(1); |
|
1501 |
|
1502 GestureItem *item1 = new GestureItem("item1"); |
|
1503 item1->grabGesture(CustomGesture::GestureType); |
|
1504 scene.addItem(item1); |
|
1505 item1->setPos(10, 10); |
|
1506 item1->size = QRectF(0, 0, 180, 180); |
|
1507 item1->setZValue(2); |
|
1508 |
|
1509 GestureItem *item1_child1 = new GestureItem("item1_child1[panel]"); |
|
1510 item1_child1->setFlags(QGraphicsItem::ItemIsPanel); |
|
1511 item1_child1->setParentItem(item1); |
|
1512 item1_child1->grabGesture(CustomGesture::GestureType); |
|
1513 item1_child1->setPos(10, 10); |
|
1514 item1_child1->size = QRectF(0, 0, 160, 160); |
|
1515 item1_child1->setZValue(5); |
|
1516 |
|
1517 GestureItem *item1_child1_child1 = new GestureItem("item1_child1_child1"); |
|
1518 item1_child1_child1->setParentItem(item1_child1); |
|
1519 item1_child1_child1->grabGesture(CustomGesture::GestureType); |
|
1520 item1_child1_child1->setPos(10, 10); |
|
1521 item1_child1_child1->size = QRectF(0, 0, 140, 140); |
|
1522 item1_child1_child1->setZValue(10); |
|
1523 |
|
1524 view.show(); |
|
1525 QTest::qWaitForWindowShown(&view); |
|
1526 view.ensureVisible(scene.sceneRect()); |
|
1527 |
|
1528 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
1529 |
|
1530 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
1531 static const int TotalCustomEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1; |
|
1532 |
|
1533 CustomEvent event; |
|
1534 event.hotSpot = mapToGlobal(QPointF(5, 5), item1_child1_child1, &view); |
|
1535 event.hasHotSpot = true; |
|
1536 sendCustomGesture(&event, item0, &scene); |
|
1537 |
|
1538 QCOMPARE(item0->customEventsReceived, TotalCustomEventsCount); |
|
1539 QCOMPARE(item1_child1_child1->gestureEventsReceived, TotalGestureEventsCount); |
|
1540 QCOMPARE(item1_child1_child1->gestureOverrideEventsReceived, 1); |
|
1541 QCOMPARE(item1_child1->gestureOverrideEventsReceived, 1); |
|
1542 QCOMPARE(item1->gestureEventsReceived, 0); |
|
1543 QCOMPARE(item1->gestureOverrideEventsReceived, 0); |
|
1544 QCOMPARE(item0->gestureEventsReceived, 0); |
|
1545 QCOMPARE(item0->gestureOverrideEventsReceived, 0); |
|
1546 |
|
1547 item0->reset(); item1->reset(); item1_child1->reset(); item1_child1_child1->reset(); |
|
1548 |
|
1549 event.hotSpot = mapToGlobal(QPointF(5, 5), item1, &view); |
|
1550 event.hasHotSpot = true; |
|
1551 sendCustomGesture(&event, item1, &scene); |
|
1552 |
|
1553 QCOMPARE(item1_child1_child1->gestureEventsReceived, 0); |
|
1554 QCOMPARE(item1_child1_child1->gestureOverrideEventsReceived, 0); |
|
1555 QCOMPARE(item1_child1->gestureEventsReceived, 0); |
|
1556 QCOMPARE(item1_child1->gestureOverrideEventsReceived, 0); |
|
1557 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
|
1558 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
1559 QCOMPARE(item0->gestureEventsReceived, 0); |
|
1560 QCOMPARE(item0->gestureOverrideEventsReceived, 1); |
|
1561 |
|
1562 item0->reset(); item1->reset(); item1_child1->reset(); item1_child1_child1->reset(); |
|
1563 // try with a modal panel |
|
1564 item1_child1->setPanelModality(QGraphicsItem::PanelModal); |
|
1565 |
|
1566 event.hotSpot = mapToGlobal(QPointF(5, 5), item1, &view); |
|
1567 event.hasHotSpot = true; |
|
1568 sendCustomGesture(&event, item1, &scene); |
|
1569 |
|
1570 QCOMPARE(item1_child1_child1->gestureEventsReceived, 0); |
|
1571 QCOMPARE(item1_child1_child1->gestureOverrideEventsReceived, 0); |
|
1572 QCOMPARE(item1_child1->gestureEventsReceived, TotalGestureEventsCount); |
|
1573 QCOMPARE(item1_child1->gestureOverrideEventsReceived, 0); |
|
1574 QCOMPARE(item1->gestureEventsReceived, 0); |
|
1575 QCOMPARE(item1->gestureOverrideEventsReceived, 0); |
|
1576 QCOMPARE(item0->gestureEventsReceived, 0); |
|
1577 QCOMPARE(item0->gestureOverrideEventsReceived, 0); |
|
1578 |
|
1579 item0->reset(); item1->reset(); item1_child1->reset(); item1_child1_child1->reset(); |
|
1580 // try with a modal panel, however set the hotspot to be outside of the |
|
1581 // panel and its parent |
|
1582 item1_child1->setPanelModality(QGraphicsItem::PanelModal); |
|
1583 |
|
1584 event.hotSpot = mapToGlobal(QPointF(5, 5), item0, &view); |
|
1585 event.hasHotSpot = true; |
|
1586 sendCustomGesture(&event, item1, &scene); |
|
1587 |
|
1588 QCOMPARE(item1_child1_child1->gestureEventsReceived, 0); |
|
1589 QCOMPARE(item1_child1_child1->gestureOverrideEventsReceived, 0); |
|
1590 QCOMPARE(item1_child1->gestureEventsReceived, 0); |
|
1591 QCOMPARE(item1_child1->gestureOverrideEventsReceived, 0); |
|
1592 QCOMPARE(item1->gestureEventsReceived, 0); |
|
1593 QCOMPARE(item1->gestureOverrideEventsReceived, 0); |
|
1594 QCOMPARE(item0->gestureEventsReceived, TotalGestureEventsCount); |
|
1595 QCOMPARE(item0->gestureOverrideEventsReceived, 0); |
|
1596 |
|
1597 item0->reset(); item1->reset(); item1_child1->reset(); item1_child1_child1->reset(); |
|
1598 // try with a scene modal panel |
|
1599 item1_child1->setPanelModality(QGraphicsItem::SceneModal); |
|
1600 |
|
1601 event.hotSpot = mapToGlobal(QPointF(5, 5), item0, &view); |
|
1602 event.hasHotSpot = true; |
|
1603 sendCustomGesture(&event, item0, &scene); |
|
1604 |
|
1605 QCOMPARE(item1_child1_child1->gestureEventsReceived, 0); |
|
1606 QCOMPARE(item1_child1_child1->gestureOverrideEventsReceived, 0); |
|
1607 QCOMPARE(item1_child1->gestureEventsReceived, TotalGestureEventsCount); |
|
1608 QCOMPARE(item1_child1->gestureOverrideEventsReceived, 0); |
|
1609 QCOMPARE(item1->gestureEventsReceived, 0); |
|
1610 QCOMPARE(item1->gestureOverrideEventsReceived, 0); |
|
1611 QCOMPARE(item0->gestureEventsReceived, 0); |
|
1612 QCOMPARE(item0->gestureOverrideEventsReceived, 0); |
|
1613 } |
|
1614 */ |
|
1615 |
|
1616 /* |
|
1617 void tst_Gestures::panelStacksBehindParent() |
|
1618 { |
|
1619 QGraphicsScene scene; |
|
1620 QGraphicsView view(&scene); |
|
1621 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
1622 |
|
1623 GestureItem *item1 = new GestureItem("item1"); |
|
1624 item1->grabGesture(CustomGesture::GestureType); |
|
1625 scene.addItem(item1); |
|
1626 item1->setPos(10, 10); |
|
1627 item1->size = QRectF(0, 0, 180, 180); |
|
1628 item1->setZValue(2); |
|
1629 |
|
1630 GestureItem *panel = new GestureItem("panel"); |
|
1631 panel->setFlags(QGraphicsItem::ItemIsPanel | QGraphicsItem::ItemStacksBehindParent); |
|
1632 panel->setPanelModality(QGraphicsItem::PanelModal); |
|
1633 panel->setParentItem(item1); |
|
1634 panel->grabGesture(CustomGesture::GestureType); |
|
1635 panel->setPos(-10, -10); |
|
1636 panel->size = QRectF(0, 0, 200, 200); |
|
1637 panel->setZValue(5); |
|
1638 |
|
1639 view.show(); |
|
1640 QTest::qWaitForWindowShown(&view); |
|
1641 view.ensureVisible(scene.sceneRect()); |
|
1642 |
|
1643 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
1644 |
|
1645 static const int TotalGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialStartedThreshold + 1; |
|
1646 |
|
1647 CustomEvent event; |
|
1648 event.hotSpot = mapToGlobal(QPointF(5, 5), item1, &view); |
|
1649 event.hasHotSpot = true; |
|
1650 sendCustomGesture(&event, item1, &scene); |
|
1651 |
|
1652 QCOMPARE(item1->gestureEventsReceived, 0); |
|
1653 QCOMPARE(item1->gestureOverrideEventsReceived, 0); |
|
1654 QCOMPARE(panel->gestureEventsReceived, TotalGestureEventsCount); |
|
1655 QCOMPARE(panel->gestureOverrideEventsReceived, 0); |
|
1656 } |
|
1657 */ |
|
1658 |
|
1659 QTEST_MAIN(tst_Gestures) |
|
1660 #include "tst_gestures.moc" |