101 int CustomEvent::EventType = 0; |
101 int CustomEvent::EventType = 0; |
102 |
102 |
103 class CustomGestureRecognizer : public QGestureRecognizer |
103 class CustomGestureRecognizer : public QGestureRecognizer |
104 { |
104 { |
105 public: |
105 public: |
|
106 static bool ConsumeEvents; |
|
107 |
106 CustomGestureRecognizer() |
108 CustomGestureRecognizer() |
107 { |
109 { |
108 if (!CustomEvent::EventType) |
110 if (!CustomEvent::EventType) |
109 CustomEvent::EventType = QEvent::registerEventType(); |
111 CustomEvent::EventType = QEvent::registerEventType(); |
110 } |
112 } |
111 |
113 |
112 QGesture* createGesture(QObject *) |
114 QGesture* create(QObject *) |
113 { |
115 { |
114 return new CustomGesture; |
116 return new CustomGesture; |
115 } |
117 } |
116 |
118 |
117 QGestureRecognizer::Result filterEvent(QGesture *state, QObject*, QEvent *event) |
119 QGestureRecognizer::Result recognize(QGesture *state, QObject*, QEvent *event) |
118 { |
120 { |
119 if (event->type() == CustomEvent::EventType) { |
121 if (event->type() == CustomEvent::EventType) { |
120 QGestureRecognizer::Result result = QGestureRecognizer::ConsumeEventHint; |
122 QGestureRecognizer::Result result = 0; |
|
123 if (CustomGestureRecognizer::ConsumeEvents) |
|
124 result |= QGestureRecognizer::ConsumeEventHint; |
121 CustomGesture *g = static_cast<CustomGesture*>(state); |
125 CustomGesture *g = static_cast<CustomGesture*>(state); |
122 CustomEvent *e = static_cast<CustomEvent*>(event); |
126 CustomEvent *e = static_cast<CustomEvent*>(event); |
123 g->serial = e->serial; |
127 g->serial = e->serial; |
124 if (e->hasHotSpot) |
128 if (e->hasHotSpot) |
125 g->setHotSpot(e->hotSpot); |
129 g->setHotSpot(e->hotSpot); |
126 if (g->serial >= CustomGesture::SerialFinishedThreshold) |
130 if (g->serial >= CustomGesture::SerialFinishedThreshold) |
127 result |= QGestureRecognizer::GestureFinished; |
131 result |= QGestureRecognizer::FinishGesture; |
128 else if (g->serial >= CustomGesture::SerialStartedThreshold) |
132 else if (g->serial >= CustomGesture::SerialStartedThreshold) |
129 result |= QGestureRecognizer::GestureTriggered; |
133 result |= QGestureRecognizer::TriggerGesture; |
130 else if (g->serial >= CustomGesture::SerialMaybeThreshold) |
134 else if (g->serial >= CustomGesture::SerialMaybeThreshold) |
131 result |= QGestureRecognizer::MaybeGesture; |
135 result |= QGestureRecognizer::MayBeGesture; |
132 else |
136 else |
133 result = QGestureRecognizer::NotGesture; |
137 result = QGestureRecognizer::CancelGesture; |
134 return result; |
138 return result; |
135 } |
139 } |
136 return QGestureRecognizer::Ignore; |
140 return QGestureRecognizer::Ignore; |
137 } |
141 } |
138 |
142 |
139 void reset(QGesture *state) |
143 void reset(QGesture *state) |
140 { |
144 { |
141 CustomGesture *g = static_cast<CustomGesture*>(state); |
145 CustomGesture *g = static_cast<CustomGesture *>(state); |
142 g->serial = 0; |
146 g->serial = 0; |
143 QGestureRecognizer::reset(state); |
147 QGestureRecognizer::reset(state); |
144 } |
148 } |
145 }; |
149 }; |
|
150 bool CustomGestureRecognizer::ConsumeEvents = false; |
146 |
151 |
147 // same as CustomGestureRecognizer but triggers early without the maybe state |
152 // same as CustomGestureRecognizer but triggers early without the maybe state |
148 class CustomContinuousGestureRecognizer : public QGestureRecognizer |
153 class CustomContinuousGestureRecognizer : public QGestureRecognizer |
149 { |
154 { |
150 public: |
155 public: |
152 { |
157 { |
153 if (!CustomEvent::EventType) |
158 if (!CustomEvent::EventType) |
154 CustomEvent::EventType = QEvent::registerEventType(); |
159 CustomEvent::EventType = QEvent::registerEventType(); |
155 } |
160 } |
156 |
161 |
157 QGesture* createGesture(QObject *) |
162 QGesture* create(QObject *) |
158 { |
163 { |
159 return new CustomGesture; |
164 return new CustomGesture; |
160 } |
165 } |
161 |
166 |
162 QGestureRecognizer::Result filterEvent(QGesture *state, QObject*, QEvent *event) |
167 QGestureRecognizer::Result recognize(QGesture *state, QObject*, QEvent *event) |
163 { |
168 { |
164 if (event->type() == CustomEvent::EventType) { |
169 if (event->type() == CustomEvent::EventType) { |
165 QGestureRecognizer::Result result = QGestureRecognizer::ConsumeEventHint; |
170 QGestureRecognizer::Result result = QGestureRecognizer::ConsumeEventHint; |
166 CustomGesture *g = static_cast<CustomGesture*>(state); |
171 CustomGesture *g = static_cast<CustomGesture *>(state); |
167 CustomEvent *e = static_cast<CustomEvent*>(event); |
172 CustomEvent *e = static_cast<CustomEvent *>(event); |
168 g->serial = e->serial; |
173 g->serial = e->serial; |
169 if (e->hasHotSpot) |
174 if (e->hasHotSpot) |
170 g->setHotSpot(e->hotSpot); |
175 g->setHotSpot(e->hotSpot); |
171 if (g->serial >= CustomGesture::SerialFinishedThreshold) |
176 if (g->serial >= CustomGesture::SerialFinishedThreshold) |
172 result |= QGestureRecognizer::GestureFinished; |
177 result |= QGestureRecognizer::FinishGesture; |
173 else if (g->serial >= CustomGesture::SerialMaybeThreshold) |
178 else if (g->serial >= CustomGesture::SerialMaybeThreshold) |
174 result |= QGestureRecognizer::GestureTriggered; |
179 result |= QGestureRecognizer::TriggerGesture; |
175 else |
180 else |
176 result = QGestureRecognizer::NotGesture; |
181 result = QGestureRecognizer::CancelGesture; |
177 return result; |
182 return result; |
178 } |
183 } |
179 return QGestureRecognizer::Ignore; |
184 return QGestureRecognizer::Ignore; |
180 } |
185 } |
181 |
186 |
182 void reset(QGesture *state) |
187 void reset(QGesture *state) |
183 { |
188 { |
184 CustomGesture *g = static_cast<CustomGesture*>(state); |
189 CustomGesture *g = static_cast<CustomGesture *>(state); |
185 g->serial = 0; |
190 g->serial = 0; |
186 QGestureRecognizer::reset(state); |
191 QGestureRecognizer::reset(state); |
187 } |
192 } |
188 }; |
193 }; |
189 |
194 |
370 QCOMPARE(widget.events.updated.size(), TotalGestureEventsCount - 2); |
382 QCOMPARE(widget.events.updated.size(), TotalGestureEventsCount - 2); |
371 QCOMPARE(widget.events.finished.size(), 1); |
383 QCOMPARE(widget.events.finished.size(), 1); |
372 QCOMPARE(widget.events.canceled.size(), 0); |
384 QCOMPARE(widget.events.canceled.size(), 0); |
373 } |
385 } |
374 |
386 |
|
387 void tst_Gestures::consumeEventHint() |
|
388 { |
|
389 GestureWidget widget; |
|
390 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
391 |
|
392 CustomGestureRecognizer::ConsumeEvents = true; |
|
393 CustomEvent event; |
|
394 sendCustomGesture(&event, &widget); |
|
395 CustomGestureRecognizer::ConsumeEvents = false; |
|
396 |
|
397 QCOMPARE(widget.customEventsReceived, 0); |
|
398 } |
|
399 |
375 void tst_Gestures::autoCancelingGestures() |
400 void tst_Gestures::autoCancelingGestures() |
376 { |
401 { |
377 GestureWidget widget; |
402 GestureWidget widget; |
378 widget.grabGesture(CustomGesture::GestureType, Qt::WidgetGesture); |
403 widget.grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
379 // send partial gesture. The gesture will be in the "maybe" state, but will |
404 // send partial gesture. The gesture will be in the "maybe" state, but will |
380 // never get enough events to fire, so Qt will have to kill it. |
405 // never get enough events to fire, so Qt will have to kill it. |
381 CustomEvent ev; |
406 CustomEvent ev; |
382 for (int i = CustomGesture::SerialMaybeThreshold; |
407 for (int i = CustomGesture::SerialMaybeThreshold; |
383 i < CustomGesture::SerialStartedThreshold; ++i) { |
408 i < CustomGesture::SerialStartedThreshold; ++i) { |
516 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
541 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
517 |
542 |
518 parent.reset(); |
543 parent.reset(); |
519 child->reset(); |
544 child->reset(); |
520 |
545 |
521 // nobody accepts the override, we will send normal events to the closest context (to the child) |
546 // nobody accepts the override, we will send normal events to the closest |
|
547 // context (i.e. to the child widget) and it will be propagated and |
|
548 // accepted by the parent widget |
522 parent.acceptGestureOverride = false; |
549 parent.acceptGestureOverride = false; |
523 child->acceptGestureOverride = false; |
550 child->acceptGestureOverride = false; |
|
551 child->ignoredGestures << CustomGesture::GestureType; |
|
552 |
|
553 // sending events to the child and making sure there is no conflict |
|
554 sendCustomGesture(&event, child); |
|
555 |
|
556 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
557 QCOMPARE(child->gestureEventsReceived, 1); |
|
558 QCOMPARE(parent.gestureOverrideEventsReceived, 1); |
|
559 QCOMPARE(parent.gestureEventsReceived, TotalGestureEventsCount); |
|
560 |
|
561 parent.reset(); |
|
562 child->reset(); |
|
563 |
|
564 // nobody accepts the override, and nobody accepts the gesture event |
|
565 parent.acceptGestureOverride = false; |
|
566 child->acceptGestureOverride = false; |
|
567 parent.ignoredGestures << CustomGesture::GestureType; |
|
568 child->ignoredGestures << CustomGesture::GestureType; |
|
569 |
|
570 // sending events to the child and making sure there is no conflict |
|
571 sendCustomGesture(&event, child); |
|
572 |
|
573 QCOMPARE(child->gestureOverrideEventsReceived, 1); |
|
574 QCOMPARE(child->gestureEventsReceived, TotalGestureEventsCount); |
|
575 QCOMPARE(parent.gestureOverrideEventsReceived, 1); |
|
576 QCOMPARE(parent.gestureEventsReceived, 1); |
|
577 |
|
578 parent.reset(); |
|
579 child->reset(); |
|
580 |
|
581 // we set an attribute to make sure all gesture events are propagated |
|
582 parent.grabGesture(CustomGesture::GestureType, Qt::ReceivePartialGestures); |
|
583 parent.acceptGestureOverride = false; |
|
584 child->acceptGestureOverride = false; |
|
585 parent.ignoredGestures << CustomGesture::GestureType; |
524 child->ignoredGestures << CustomGesture::GestureType; |
586 child->ignoredGestures << CustomGesture::GestureType; |
525 |
587 |
526 // sending events to the child and making sure there is no conflict |
588 // sending events to the child and making sure there is no conflict |
527 sendCustomGesture(&event, child); |
589 sendCustomGesture(&event, child); |
528 |
590 |
933 |
1001 |
934 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
1002 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
935 event.hasHotSpot = true; |
1003 event.hasHotSpot = true; |
936 sendCustomGesture(&event, item0, &scene); |
1004 sendCustomGesture(&event, item0, &scene); |
937 |
1005 |
938 QCOMPARE(item0->customEventsReceived, TotalCustomEventsCount); |
1006 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
|
1007 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
|
1008 QCOMPARE(item2->gestureEventsReceived, 1); |
|
1009 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
|
1010 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
|
1011 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
1012 |
|
1013 item0->reset(); item1->reset(); item2->reset(); item2_child1->reset(); |
|
1014 item2->grabGesture(CustomGesture::GestureType); |
|
1015 item2->ignoredGestures << CustomGesture::GestureType; |
|
1016 item1->ignoredGestures << CustomGesture::GestureType; |
|
1017 |
|
1018 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
|
1019 event.hasHotSpot = true; |
|
1020 sendCustomGesture(&event, item0, &scene); |
|
1021 |
|
1022 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
|
1023 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
|
1024 QCOMPARE(item2->gestureEventsReceived, TotalGestureEventsCount); |
|
1025 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
|
1026 QCOMPARE(item1->gestureEventsReceived, 1); |
|
1027 QCOMPARE(item1->gestureOverrideEventsReceived, 1); |
|
1028 |
|
1029 item0->reset(); item1->reset(); item2->reset(); item2_child1->reset(); |
|
1030 item2->grabGesture(CustomGesture::GestureType); |
|
1031 item2->ignoredGestures << CustomGesture::GestureType; |
|
1032 item1->ignoredGestures << CustomGesture::GestureType; |
|
1033 item1->grabGesture(CustomGesture::GestureType, Qt::ReceivePartialGestures); |
|
1034 |
|
1035 event.hotSpot = mapToGlobal(QPointF(10, 10), item2_child1, &view); |
|
1036 event.hasHotSpot = true; |
|
1037 sendCustomGesture(&event, item0, &scene); |
|
1038 |
939 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
1039 QCOMPARE(item2_child1->gestureEventsReceived, 0); |
940 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
1040 QCOMPARE(item2_child1->gestureOverrideEventsReceived, 0); |
941 QCOMPARE(item2->gestureEventsReceived, TotalGestureEventsCount); |
1041 QCOMPARE(item2->gestureEventsReceived, TotalGestureEventsCount); |
942 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
1042 QCOMPARE(item2->gestureOverrideEventsReceived, 1); |
943 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
1043 QCOMPARE(item1->gestureEventsReceived, TotalGestureEventsCount); |
987 GestureWidget *B = new GestureWidget("B", A); |
1089 GestureWidget *B = new GestureWidget("B", A); |
988 GestureWidget *C = new GestureWidget("C", B); |
1090 GestureWidget *C = new GestureWidget("C", B); |
989 GestureWidget *D = new GestureWidget("D", C); |
1091 GestureWidget *D = new GestureWidget("D", C); |
990 |
1092 |
991 Qt::GestureType FirstGesture = CustomGesture::GestureType; |
1093 Qt::GestureType FirstGesture = CustomGesture::GestureType; |
992 Qt::GestureType SecondGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1094 Qt::GestureType SecondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
993 Qt::GestureType ThirdGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1095 Qt::GestureType ThirdGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
994 |
1096 |
995 A->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture); // A [1 3] |
1097 Qt::GestureFlags flags = Qt::ReceivePartialGestures; |
996 A->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); // | |
1098 A->grabGesture(FirstGesture, flags); // A [1 3] |
997 B->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture); // B [ 2 3] |
1099 A->grabGesture(ThirdGesture, flags); // | |
998 B->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); // | |
1100 B->grabGesture(SecondGesture, flags); // B [ 2 3] |
999 C->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture); // C [1 2 3] |
1101 B->grabGesture(ThirdGesture, flags); // | |
1000 C->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture); // | |
1102 C->grabGesture(FirstGesture, flags); // C [1 2 3] |
1001 C->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); // D [1 3] |
1103 C->grabGesture(SecondGesture, flags); // | |
1002 D->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture); |
1104 C->grabGesture(ThirdGesture, flags); // D [1 3] |
1003 D->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); |
1105 D->grabGesture(FirstGesture, flags); |
|
1106 D->grabGesture(ThirdGesture, flags); |
1004 |
1107 |
1005 // make sure all widgets ignore events, so they get propagated. |
1108 // make sure all widgets ignore events, so they get propagated. |
1006 A->ignoredGestures << FirstGesture << ThirdGesture; |
1109 A->ignoredGestures << FirstGesture << ThirdGesture; |
1007 B->ignoredGestures << SecondGesture << ThirdGesture; |
1110 B->ignoredGestures << SecondGesture << ThirdGesture; |
1008 C->ignoredGestures << FirstGesture << SecondGesture << ThirdGesture; |
1111 C->ignoredGestures << FirstGesture << SecondGesture << ThirdGesture; |
1055 GestureWidget *B = new GestureWidget("B", A); |
1161 GestureWidget *B = new GestureWidget("B", A); |
1056 GestureWidget *C = new GestureWidget("C", B); |
1162 GestureWidget *C = new GestureWidget("C", B); |
1057 GestureWidget *D = new GestureWidget("D", C); |
1163 GestureWidget *D = new GestureWidget("D", C); |
1058 |
1164 |
1059 Qt::GestureType FirstGesture = CustomGesture::GestureType; |
1165 Qt::GestureType FirstGesture = CustomGesture::GestureType; |
1060 Qt::GestureType SecondGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1166 Qt::GestureType SecondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
1061 Qt::GestureType ThirdGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1167 Qt::GestureType ThirdGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
1062 Qt::GestureType FourthGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1168 Qt::GestureType FourthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
1063 Qt::GestureType FifthGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1169 Qt::GestureType FifthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
1064 Qt::GestureType SixthGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1170 Qt::GestureType SixthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
1065 Qt::GestureType SeventhGesture = qApp->registerGestureRecognizer(new CustomGestureRecognizer); |
1171 Qt::GestureType SeventhGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
1066 |
1172 |
1067 A->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture); // A [1,3,4] |
1173 Qt::GestureFlags flags = Qt::ReceivePartialGestures; |
1068 A->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); // | |
1174 A->grabGesture(FirstGesture, flags); // A [1,3,4] |
1069 A->grabGesture(FourthGesture, Qt::WidgetWithChildrenGesture); // B [2,3,5] |
1175 A->grabGesture(ThirdGesture, flags); // | |
1070 B->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture); // | |
1176 A->grabGesture(FourthGesture, flags); // B [2,3,5] |
1071 B->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); // C [1,2,3,6] |
1177 B->grabGesture(SecondGesture, flags); // | |
1072 B->grabGesture(FifthGesture, Qt::WidgetWithChildrenGesture); // | |
1178 B->grabGesture(ThirdGesture, flags); // C [1,2,3,6] |
1073 C->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture); // D [1,3,7] |
1179 B->grabGesture(FifthGesture, flags); // | |
1074 C->grabGesture(SecondGesture, Qt::WidgetWithChildrenGesture); |
1180 C->grabGesture(FirstGesture, flags); // D [1,3,7] |
1075 C->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); |
1181 C->grabGesture(SecondGesture, flags); |
1076 C->grabGesture(SixthGesture, Qt::WidgetWithChildrenGesture); |
1182 C->grabGesture(ThirdGesture, flags); |
1077 D->grabGesture(FirstGesture, Qt::WidgetWithChildrenGesture); |
1183 C->grabGesture(SixthGesture, flags); |
1078 D->grabGesture(ThirdGesture, Qt::WidgetWithChildrenGesture); |
1184 D->grabGesture(FirstGesture, flags); |
1079 D->grabGesture(SeventhGesture, Qt::WidgetWithChildrenGesture); |
1185 D->grabGesture(ThirdGesture, flags); |
|
1186 D->grabGesture(SeventhGesture, flags); |
1080 |
1187 |
1081 // make sure all widgets ignore events, so they get propagated. |
1188 // make sure all widgets ignore events, so they get propagated. |
1082 QSet<Qt::GestureType> allGestureTypes; |
1189 QSet<Qt::GestureType> allGestureTypes; |
1083 allGestureTypes << FirstGesture << SecondGesture << ThirdGesture |
1190 allGestureTypes << FirstGesture << SecondGesture << ThirdGesture |
1084 << FourthGesture << FifthGesture << SixthGesture << SeventhGesture; |
1191 << FourthGesture << FifthGesture << SixthGesture << SeventhGesture; |
1137 QCOMPARE(A->events.all.count(ThirdGesture), TotalGestureEventsCount); |
1244 QCOMPARE(A->events.all.count(ThirdGesture), TotalGestureEventsCount); |
1138 QCOMPARE(A->events.all.count(FourthGesture), TotalGestureEventsCount); |
1245 QCOMPARE(A->events.all.count(FourthGesture), TotalGestureEventsCount); |
1139 QCOMPARE(A->events.all.count(FifthGesture), 0); |
1246 QCOMPARE(A->events.all.count(FifthGesture), 0); |
1140 QCOMPARE(A->events.all.count(SixthGesture), 0); |
1247 QCOMPARE(A->events.all.count(SixthGesture), 0); |
1141 QCOMPARE(A->events.all.count(SeventhGesture), 0); |
1248 QCOMPARE(A->events.all.count(SeventhGesture), 0); |
|
1249 |
|
1250 QGestureRecognizer::unregisterRecognizer(SecondGesture); |
|
1251 QGestureRecognizer::unregisterRecognizer(ThirdGesture); |
|
1252 QGestureRecognizer::unregisterRecognizer(FourthGesture); |
|
1253 QGestureRecognizer::unregisterRecognizer(FifthGesture); |
|
1254 QGestureRecognizer::unregisterRecognizer(SixthGesture); |
|
1255 QGestureRecognizer::unregisterRecognizer(SeventhGesture); |
1142 } |
1256 } |
1143 |
1257 |
1144 void tst_Gestures::testMapToScene() |
1258 void tst_Gestures::testMapToScene() |
1145 { |
1259 { |
1146 QGesture gesture; |
1260 QGesture gesture; |
1147 QList<QGesture*> list; |
1261 QList<QGesture*> list; |
1148 list << &gesture; |
1262 list << &gesture; |
1149 QGestureEvent event(list); |
1263 QGestureEvent event(list); |
1150 QCOMPARE(event.mapToScene(gesture.hotSpot()), QPointF()); // not set, can't do much |
1264 QCOMPARE(event.mapToGraphicsScene(gesture.hotSpot()), QPointF()); // not set, can't do much |
1151 |
1265 |
1152 QGraphicsScene scene; |
1266 QGraphicsScene scene; |
1153 QGraphicsView view(&scene); |
1267 QGraphicsView view(&scene); |
|
1268 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
1154 |
1269 |
1155 GestureItem *item0 = new GestureItem; |
1270 GestureItem *item0 = new GestureItem; |
1156 scene.addItem(item0); |
1271 scene.addItem(item0); |
1157 item0->setPos(14, 16); |
1272 item0->setPos(14, 16); |
1158 |
1273 |
1161 view.ensureVisible(scene.sceneRect()); |
1276 view.ensureVisible(scene.sceneRect()); |
1162 |
1277 |
1163 QPoint origin = view.mapToGlobal(QPoint()); |
1278 QPoint origin = view.mapToGlobal(QPoint()); |
1164 event.setWidget(view.viewport()); |
1279 event.setWidget(view.viewport()); |
1165 |
1280 |
1166 QCOMPARE(event.mapToScene(origin + QPoint(100, 200)), view.mapToScene(QPoint(100, 200))); |
1281 QCOMPARE(event.mapToGraphicsScene(origin + QPoint(100, 200)), view.mapToScene(QPoint(100, 200))); |
|
1282 } |
|
1283 |
|
1284 void tst_Gestures::ungrabGesture() // a method on QWidget |
|
1285 { |
|
1286 class MockGestureWidget : public GestureWidget { |
|
1287 public: |
|
1288 MockGestureWidget(const char *name = 0, QWidget *parent = 0) |
|
1289 : GestureWidget(name, parent) { } |
|
1290 |
|
1291 |
|
1292 QSet<QGesture*> gestures; |
|
1293 protected: |
|
1294 bool event(QEvent *event) |
|
1295 { |
|
1296 if (event->type() == QEvent::Gesture) { |
|
1297 QGestureEvent *gestureEvent = static_cast<QGestureEvent*>(event); |
|
1298 if (gestureEvent) |
|
1299 foreach (QGesture *g, gestureEvent->gestures()) |
|
1300 gestures.insert(g); |
|
1301 } |
|
1302 return GestureWidget::event(event); |
|
1303 } |
|
1304 }; |
|
1305 |
|
1306 MockGestureWidget parent("A"); |
|
1307 MockGestureWidget *a = &parent; |
|
1308 MockGestureWidget *b = new MockGestureWidget("B", a); |
|
1309 |
|
1310 a->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
1311 b->grabGesture(CustomGesture::GestureType); |
|
1312 b->ignoredGestures << CustomGesture::GestureType; |
|
1313 |
|
1314 CustomEvent event; |
|
1315 // sending an event will cause the QGesture objects to be instantiated for the widgets |
|
1316 sendCustomGesture(&event, b); |
|
1317 |
|
1318 QCOMPARE(a->gestures.count(), 1); |
|
1319 QPointer<QGesture> customGestureA; |
|
1320 customGestureA = *(a->gestures.begin()); |
|
1321 QVERIFY(!customGestureA.isNull()); |
|
1322 QCOMPARE(customGestureA->gestureType(), CustomGesture::GestureType); |
|
1323 |
|
1324 QCOMPARE(b->gestures.count(), 1); |
|
1325 QPointer<QGesture> customGestureB; |
|
1326 customGestureB = *(b->gestures.begin()); |
|
1327 QVERIFY(!customGestureB.isNull()); |
|
1328 QVERIFY(customGestureA.data() == customGestureB.data()); |
|
1329 QCOMPARE(customGestureB->gestureType(), CustomGesture::GestureType); |
|
1330 |
|
1331 a->gestures.clear(); |
|
1332 // sending an event will cause the QGesture objects to be instantiated for the widget |
|
1333 sendCustomGesture(&event, a); |
|
1334 |
|
1335 QCOMPARE(a->gestures.count(), 1); |
|
1336 customGestureA = *(a->gestures.begin()); |
|
1337 QVERIFY(!customGestureA.isNull()); |
|
1338 QCOMPARE(customGestureA->gestureType(), CustomGesture::GestureType); |
|
1339 QVERIFY(customGestureA.data() != customGestureB.data()); |
|
1340 |
|
1341 a->ungrabGesture(CustomGesture::GestureType); |
|
1342 QVERIFY(customGestureA.isNull()); |
|
1343 QVERIFY(!customGestureB.isNull()); |
|
1344 |
|
1345 a->gestures.clear(); |
|
1346 a->reset(); |
|
1347 // send again to 'b' and make sure a never gets it. |
|
1348 sendCustomGesture(&event, b); |
|
1349 QCOMPARE(a->gestureEventsReceived, 0); |
|
1350 QCOMPARE(a->gestureOverrideEventsReceived, 0); |
|
1351 } |
|
1352 |
|
1353 void tst_Gestures::unregisterRecognizer() // a method on QApplication |
|
1354 { |
|
1355 /* |
|
1356 The hardest usecase to get right is when we remove a recognizer while several |
|
1357 of the gestures it created are in active state and we immediately add a new recognizer |
|
1358 for the same type (thus replacing the old one). |
|
1359 The expected result is that all old gestures continue till they are finished/cancelled |
|
1360 and the new recognizer starts creating gestures immediately at registration. |
|
1361 |
|
1362 This implies that deleting of the recognizer happens only when there are no more gestures |
|
1363 that it created. (since gestures might have a pointer to the recognizer) |
|
1364 */ |
|
1365 |
|
1366 } |
|
1367 |
|
1368 void tst_Gestures::autoCancelGestures() |
|
1369 { |
|
1370 class MockWidget : public GestureWidget { |
|
1371 public: |
|
1372 MockWidget(const char *name) : GestureWidget(name) { } |
|
1373 |
|
1374 bool event(QEvent *event) |
|
1375 { |
|
1376 if (event->type() == QEvent::Gesture) { |
|
1377 QGestureEvent *ge = static_cast<QGestureEvent*>(event); |
|
1378 Q_ASSERT(ge->gestures().count() == 1); // can't use QCOMPARE here... |
|
1379 ge->gestures().first()->setGestureCancelPolicy(QGesture::CancelAllInContext); |
|
1380 } |
|
1381 return GestureWidget::event(event); |
|
1382 } |
|
1383 }; |
|
1384 |
|
1385 const Qt::GestureType secondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer); |
|
1386 |
|
1387 MockWidget parent("parent"); // this one sets the cancel policy to CancelAllInContext |
|
1388 parent.resize(300, 100); |
|
1389 parent.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
1390 GestureWidget *child = new GestureWidget("child", &parent); |
|
1391 child->setGeometry(10, 10, 100, 80); |
|
1392 |
|
1393 parent.grabGesture(CustomGesture::GestureType); |
|
1394 child->grabGesture(secondGesture); |
|
1395 parent.show(); |
|
1396 QTest::qWaitForWindowShown(&parent); |
|
1397 |
|
1398 /* |
|
1399 An event is send to both the child and the parent, when the child gets it a gesture is triggered |
|
1400 and send to the child. |
|
1401 When the parent gets the event a new gesture is triggered and delivered to the parent. When the |
|
1402 parent gets it he accepts it and that causes the cancel policy to activate. |
|
1403 The cause of that is the gesture for the child is cancelled and send to the child as such. |
|
1404 */ |
|
1405 CustomEvent event; |
|
1406 event.serial = CustomGesture::SerialStartedThreshold; |
|
1407 QApplication::sendEvent(child, &event); |
|
1408 QCOMPARE(child->events.all.count(), 2); |
|
1409 QCOMPARE(child->events.started.count(), 1); |
|
1410 QCOMPARE(child->events.canceled.count(), 1); |
|
1411 QCOMPARE(parent.events.all.count(), 1); |
|
1412 |
|
1413 // clean up, make the parent gesture finish |
|
1414 event.serial = CustomGesture::SerialFinishedThreshold; |
|
1415 QApplication::sendEvent(child, &event); |
|
1416 QCOMPARE(parent.events.all.count(), 2); |
|
1417 } |
|
1418 |
|
1419 void tst_Gestures::autoCancelGestures2() |
|
1420 { |
|
1421 class MockItem : public GestureItem { |
|
1422 public: |
|
1423 MockItem(const char *name) : GestureItem(name) { } |
|
1424 |
|
1425 bool event(QEvent *event) { |
|
1426 if (event->type() == QEvent::Gesture) { |
|
1427 QGestureEvent *ge = static_cast<QGestureEvent*>(event); |
|
1428 Q_ASSERT(ge->gestures().count() == 1); // can't use QCOMPARE here... |
|
1429 ge->gestures().first()->setGestureCancelPolicy(QGesture::CancelAllInContext); |
|
1430 } |
|
1431 return GestureItem::event(event); |
|
1432 } |
|
1433 }; |
|
1434 |
|
1435 const Qt::GestureType secondGesture = QGestureRecognizer ::registerRecognizer(new CustomGestureRecognizer); |
|
1436 |
|
1437 QGraphicsScene scene; |
|
1438 QGraphicsView view(&scene); |
|
1439 view.setWindowFlags(Qt::X11BypassWindowManagerHint); |
|
1440 |
|
1441 MockItem *parent = new MockItem("parent"); |
|
1442 GestureItem *child = new GestureItem("child"); |
|
1443 child->setParentItem(parent); |
|
1444 parent->setPos(0, 0); |
|
1445 child->setPos(10, 10); |
|
1446 scene.addItem(parent); |
|
1447 view.viewport()->grabGesture(CustomGesture::GestureType, Qt::DontStartGestureOnChildren); |
|
1448 view.viewport()->grabGesture(secondGesture, Qt::DontStartGestureOnChildren); |
|
1449 parent->grabGesture(CustomGesture::GestureType); |
|
1450 child->grabGesture(secondGesture); |
|
1451 |
|
1452 view.show(); |
|
1453 QTest::qWaitForWindowShown(&view); |
|
1454 view.ensureVisible(scene.sceneRect()); |
|
1455 |
|
1456 CustomEvent event; |
|
1457 event.serial = CustomGesture::SerialStartedThreshold; |
|
1458 event.hasHotSpot = true; |
|
1459 event.hotSpot = mapToGlobal(QPointF(5, 5), child, &view); |
|
1460 // qDebug() << event.hotSpot; |
|
1461 scene.sendEvent(child, &event); |
|
1462 //QEventLoop().exec(); |
|
1463 QCOMPARE(parent->events.all.count(), 1); |
|
1464 QCOMPARE(child->events.started.count(), 1); |
|
1465 QCOMPARE(child->events.canceled.count(), 1); |
|
1466 QCOMPARE(child->events.all.count(), 2); |
|
1467 |
|
1468 // clean up, make the parent gesture finish |
|
1469 event.serial = CustomGesture::SerialFinishedThreshold; |
|
1470 scene.sendEvent(child, &event); |
|
1471 QCOMPARE(parent->events.all.count(), 2); |
1167 } |
1472 } |
1168 |
1473 |
1169 QTEST_MAIN(tst_Gestures) |
1474 QTEST_MAIN(tst_Gestures) |
1170 #include "tst_gestures.moc" |
1475 #include "tst_gestures.moc" |