72 if (seenTouchUpdate) qWarning("TouchBegin: TouchUpdate cannot happen before TouchBegin"); |
74 if (seenTouchUpdate) qWarning("TouchBegin: TouchUpdate cannot happen before TouchBegin"); |
73 if (seenTouchEnd) qWarning("TouchBegin: TouchEnd cannot happen before TouchBegin"); |
75 if (seenTouchEnd) qWarning("TouchBegin: TouchEnd cannot happen before TouchBegin"); |
74 seenTouchBegin = !seenTouchBegin && !seenTouchUpdate && !seenTouchEnd; |
76 seenTouchBegin = !seenTouchBegin && !seenTouchUpdate && !seenTouchEnd; |
75 touchBeginPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
77 touchBeginPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
76 event->setAccepted(acceptTouchBegin); |
78 event->setAccepted(acceptTouchBegin); |
|
79 if (deleteInTouchBegin) |
|
80 delete this; |
77 break; |
81 break; |
78 case QEvent::TouchUpdate: |
82 case QEvent::TouchUpdate: |
79 if (!seenTouchBegin) qWarning("TouchUpdate: have not seen TouchBegin"); |
83 if (!seenTouchBegin) qWarning("TouchUpdate: have not seen TouchBegin"); |
80 if (seenTouchEnd) qWarning("TouchUpdate: TouchEnd cannot happen before TouchUpdate"); |
84 if (seenTouchEnd) qWarning("TouchUpdate: TouchEnd cannot happen before TouchUpdate"); |
81 seenTouchUpdate = seenTouchBegin && !seenTouchEnd; |
85 seenTouchUpdate = seenTouchBegin && !seenTouchEnd; |
82 touchUpdatePoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
86 touchUpdatePoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
83 event->setAccepted(acceptTouchUpdate); |
87 event->setAccepted(acceptTouchUpdate); |
|
88 if (deleteInTouchUpdate) |
|
89 delete this; |
84 break; |
90 break; |
85 case QEvent::TouchEnd: |
91 case QEvent::TouchEnd: |
86 if (!seenTouchBegin) qWarning("TouchEnd: have not seen TouchBegin"); |
92 if (!seenTouchBegin) qWarning("TouchEnd: have not seen TouchBegin"); |
87 if (seenTouchEnd) qWarning("TouchEnd: already seen a TouchEnd"); |
93 if (seenTouchEnd) qWarning("TouchEnd: already seen a TouchEnd"); |
88 seenTouchEnd = seenTouchBegin && !seenTouchEnd; |
94 seenTouchEnd = seenTouchBegin && !seenTouchEnd; |
89 touchEndPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
95 touchEndPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
90 event->setAccepted(acceptTouchEnd); |
96 event->setAccepted(acceptTouchEnd); |
|
97 if (deleteInTouchEnd) |
|
98 delete this; |
91 break; |
99 break; |
92 default: |
100 default: |
93 return QWidget::event(event); |
101 return QWidget::event(event); |
94 } |
102 } |
95 return true; |
103 return true; |
129 if (seenTouchUpdate) qWarning("TouchBegin: TouchUpdate cannot happen before TouchBegin"); |
146 if (seenTouchUpdate) qWarning("TouchBegin: TouchUpdate cannot happen before TouchBegin"); |
130 if (seenTouchEnd) qWarning("TouchBegin: TouchEnd cannot happen before TouchBegin"); |
147 if (seenTouchEnd) qWarning("TouchBegin: TouchEnd cannot happen before TouchBegin"); |
131 seenTouchBegin = !seenTouchBegin && !seenTouchUpdate && !seenTouchEnd; |
148 seenTouchBegin = !seenTouchBegin && !seenTouchUpdate && !seenTouchEnd; |
132 touchBeginPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
149 touchBeginPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
133 event->setAccepted(acceptTouchBegin); |
150 event->setAccepted(acceptTouchBegin); |
|
151 if (deleteInTouchBegin) |
|
152 delete this; |
134 break; |
153 break; |
135 case QEvent::TouchUpdate: |
154 case QEvent::TouchUpdate: |
136 if (!seenTouchBegin) qWarning("TouchUpdate: have not seen TouchBegin"); |
155 if (!seenTouchBegin) qWarning("TouchUpdate: have not seen TouchBegin"); |
137 if (seenTouchEnd) qWarning("TouchUpdate: TouchEnd cannot happen before TouchUpdate"); |
156 if (seenTouchEnd) qWarning("TouchUpdate: TouchEnd cannot happen before TouchUpdate"); |
138 seenTouchUpdate = seenTouchBegin && !seenTouchEnd; |
157 seenTouchUpdate = seenTouchBegin && !seenTouchEnd; |
139 touchUpdatePoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
158 touchUpdatePoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
140 event->setAccepted(acceptTouchUpdate); |
159 event->setAccepted(acceptTouchUpdate); |
|
160 if (deleteInTouchUpdate) |
|
161 delete this; |
141 break; |
162 break; |
142 case QEvent::TouchEnd: |
163 case QEvent::TouchEnd: |
143 if (!seenTouchBegin) qWarning("TouchEnd: have not seen TouchBegin"); |
164 if (!seenTouchBegin) qWarning("TouchEnd: have not seen TouchBegin"); |
144 if (seenTouchEnd) qWarning("TouchEnd: already seen a TouchEnd"); |
165 if (seenTouchEnd) qWarning("TouchEnd: already seen a TouchEnd"); |
145 seenTouchEnd = seenTouchBegin && !seenTouchEnd; |
166 seenTouchEnd = seenTouchBegin && !seenTouchEnd; |
146 touchEndPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
167 touchEndPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
147 event->setAccepted(acceptTouchEnd); |
168 event->setAccepted(acceptTouchEnd); |
|
169 if (deleteInTouchEnd) |
|
170 delete this; |
148 break; |
171 break; |
149 default: |
172 default: |
150 return QGraphicsItem::sceneEvent(event); |
173 return QGraphicsItem::sceneEvent(event); |
151 } |
174 } |
152 return true; |
175 return true; |
1057 QCOMPARE(rightTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); |
1082 QCOMPARE(rightTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); |
1058 QCOMPARE(rightTouchPoint.pressure(), qreal(0.)); |
1083 QCOMPARE(rightTouchPoint.pressure(), qreal(0.)); |
1059 } |
1084 } |
1060 } |
1085 } |
1061 |
1086 |
|
1087 void tst_QTouchEvent::deleteInEventHandler() |
|
1088 { |
|
1089 // QWidget |
|
1090 { |
|
1091 QWidget window; |
|
1092 tst_QTouchEventWidget *child1, *child2, *child3; |
|
1093 child1 = new tst_QTouchEventWidget; |
|
1094 child2 = new tst_QTouchEventWidget; |
|
1095 child3 = new tst_QTouchEventWidget; |
|
1096 child1->setParent(&window); |
|
1097 child2->setParent(&window); |
|
1098 child3->setParent(&window); |
|
1099 child1->setAttribute(Qt::WA_AcceptTouchEvents); |
|
1100 child2->setAttribute(Qt::WA_AcceptTouchEvents); |
|
1101 child3->setAttribute(Qt::WA_AcceptTouchEvents); |
|
1102 child1->deleteInTouchBegin = true; |
|
1103 child2->deleteInTouchUpdate = true; |
|
1104 child3->deleteInTouchEnd = true; |
|
1105 |
|
1106 QList<QTouchEvent::TouchPoint> touchPoints; |
|
1107 touchPoints.append(QTouchEvent::TouchPoint(0)); |
|
1108 QTouchEvent touchBeginEvent(QEvent::TouchBegin, |
|
1109 QTouchEvent::TouchScreen, |
|
1110 Qt::NoModifier, |
|
1111 Qt::TouchPointPressed, |
|
1112 touchPoints); |
|
1113 QTouchEvent touchUpdateEvent(QEvent::TouchUpdate, |
|
1114 QTouchEvent::TouchScreen, |
|
1115 Qt::NoModifier, |
|
1116 Qt::TouchPointStationary, |
|
1117 touchPoints); |
|
1118 QTouchEvent touchEndEvent(QEvent::TouchEnd, |
|
1119 QTouchEvent::TouchScreen, |
|
1120 Qt::NoModifier, |
|
1121 Qt::TouchPointReleased, |
|
1122 touchPoints); |
|
1123 QWeakPointer<QWidget> p; |
|
1124 bool res; |
|
1125 |
|
1126 touchBeginEvent.ignore(); |
|
1127 p = child1; |
|
1128 res = QApplication::sendEvent(child1, &touchBeginEvent); |
|
1129 // event is handled, but widget should be deleted |
|
1130 QVERIFY(res && touchBeginEvent.isAccepted() && p.isNull()); |
|
1131 |
|
1132 touchBeginEvent.ignore(); |
|
1133 p = child2; |
|
1134 res = QApplication::sendEvent(child2, &touchBeginEvent); |
|
1135 QVERIFY(res && touchBeginEvent.isAccepted() && !p.isNull()); |
|
1136 touchUpdateEvent.ignore(); |
|
1137 res = QApplication::sendEvent(child2, &touchUpdateEvent); |
|
1138 QVERIFY(res && touchUpdateEvent.isAccepted() && p.isNull()); |
|
1139 |
|
1140 touchBeginEvent.ignore(); |
|
1141 p = child3; |
|
1142 res = QApplication::sendEvent(child3, &touchBeginEvent); |
|
1143 QVERIFY(res && touchBeginEvent.isAccepted() && !p.isNull()); |
|
1144 touchUpdateEvent.ignore(); |
|
1145 res = QApplication::sendEvent(child3, &touchUpdateEvent); |
|
1146 QVERIFY(res && touchUpdateEvent.isAccepted() && !p.isNull()); |
|
1147 touchEndEvent.ignore(); |
|
1148 res = QApplication::sendEvent(child3, &touchEndEvent); |
|
1149 QVERIFY(res && touchEndEvent.isAccepted() && p.isNull()); |
|
1150 } |
|
1151 |
|
1152 // QGraphicsView |
|
1153 { |
|
1154 QGraphicsScene scene; |
|
1155 QGraphicsView view(&scene); |
|
1156 tst_QTouchEventGraphicsItem *root, *child1, *child2, *child3; |
|
1157 root = new tst_QTouchEventGraphicsItem; |
|
1158 child1 = new tst_QTouchEventGraphicsItem; |
|
1159 child2 = new tst_QTouchEventGraphicsItem; |
|
1160 child3 = new tst_QTouchEventGraphicsItem; |
|
1161 child1->setParentItem(root); |
|
1162 child2->setParentItem(root); |
|
1163 child3->setParentItem(root); |
|
1164 child1->setZValue(1.); |
|
1165 child2->setZValue(0.); |
|
1166 child3->setZValue(-1.); |
|
1167 child1->setAcceptTouchEvents(true); |
|
1168 child2->setAcceptTouchEvents(true); |
|
1169 child3->setAcceptTouchEvents(true); |
|
1170 child1->deleteInTouchBegin = true; |
|
1171 child2->deleteInTouchUpdate = true; |
|
1172 child3->deleteInTouchEnd = true; |
|
1173 |
|
1174 scene.addItem(root); |
|
1175 view.resize(200, 200); |
|
1176 view.fitInView(scene.sceneRect()); |
|
1177 |
|
1178 QTouchEvent::TouchPoint touchPoint(0); |
|
1179 touchPoint.setState(Qt::TouchPointPressed); |
|
1180 touchPoint.setPos(view.mapFromScene(child1->mapToScene(child1->boundingRect().center()))); |
|
1181 touchPoint.setScreenPos(view.mapToGlobal(touchPoint.pos().toPoint())); |
|
1182 touchPoint.setScenePos(view.mapToScene(touchPoint.pos().toPoint())); |
|
1183 QList<QTouchEvent::TouchPoint> touchPoints; |
|
1184 touchPoints.append(touchPoint); |
|
1185 QTouchEvent touchBeginEvent(QEvent::TouchBegin, |
|
1186 QTouchEvent::TouchScreen, |
|
1187 Qt::NoModifier, |
|
1188 Qt::TouchPointPressed, |
|
1189 touchPoints); |
|
1190 touchPoints[0].setState(Qt::TouchPointMoved); |
|
1191 QTouchEvent touchUpdateEvent(QEvent::TouchUpdate, |
|
1192 QTouchEvent::TouchScreen, |
|
1193 Qt::NoModifier, |
|
1194 Qt::TouchPointMoved, |
|
1195 touchPoints); |
|
1196 touchPoints[0].setState(Qt::TouchPointReleased); |
|
1197 QTouchEvent touchEndEvent(QEvent::TouchEnd, |
|
1198 QTouchEvent::TouchScreen, |
|
1199 Qt::NoModifier, |
|
1200 Qt::TouchPointReleased, |
|
1201 touchPoints); |
|
1202 bool res; |
|
1203 |
|
1204 child1->weakpointer = &child1; |
|
1205 touchBeginEvent.ignore(); |
|
1206 res = QApplication::sendEvent(view.viewport(), &touchBeginEvent); |
|
1207 QVERIFY(res && touchBeginEvent.isAccepted() && !child1); |
|
1208 touchUpdateEvent.ignore(); |
|
1209 res = QApplication::sendEvent(view.viewport(), &touchUpdateEvent); |
|
1210 QVERIFY(res && touchUpdateEvent.isAccepted() && !child1); |
|
1211 touchEndEvent.ignore(); |
|
1212 res = QApplication::sendEvent(view.viewport(), &touchEndEvent); |
|
1213 QVERIFY(res && touchUpdateEvent.isAccepted() && !child1); |
|
1214 |
|
1215 child2->weakpointer = &child2; |
|
1216 touchBeginEvent.ignore(); |
|
1217 res = QApplication::sendEvent(view.viewport(), &touchBeginEvent); |
|
1218 QVERIFY(res && touchBeginEvent.isAccepted() && child2); |
|
1219 touchUpdateEvent.ignore(); |
|
1220 res = QApplication::sendEvent(view.viewport(), &touchUpdateEvent); |
|
1221 QVERIFY(res && !touchUpdateEvent.isAccepted() && !child2); |
|
1222 touchEndEvent.ignore(); |
|
1223 res = QApplication::sendEvent(view.viewport(), &touchEndEvent); |
|
1224 QVERIFY(res && !touchUpdateEvent.isAccepted() && !child2); |
|
1225 |
|
1226 child3->weakpointer = &child3; |
|
1227 res = QApplication::sendEvent(view.viewport(), &touchBeginEvent); |
|
1228 QVERIFY(res && touchBeginEvent.isAccepted() && child3); |
|
1229 res = QApplication::sendEvent(view.viewport(), &touchUpdateEvent); |
|
1230 QVERIFY(res && !touchUpdateEvent.isAccepted() && child3); |
|
1231 res = QApplication::sendEvent(view.viewport(), &touchEndEvent); |
|
1232 QVERIFY(res && !touchEndEvent.isAccepted() && !child3); |
|
1233 |
|
1234 delete root; |
|
1235 } |
|
1236 } |
|
1237 |
|
1238 void tst_QTouchEvent::deleteInRawEventTranslation() |
|
1239 { |
|
1240 tst_QTouchEventWidget touchWidget; |
|
1241 touchWidget.setAttribute(Qt::WA_AcceptTouchEvents); |
|
1242 touchWidget.setGeometry(100, 100, 300, 300); |
|
1243 |
|
1244 tst_QTouchEventWidget *leftWidget = new tst_QTouchEventWidget; |
|
1245 leftWidget->setParent(&touchWidget); |
|
1246 leftWidget->setAttribute(Qt::WA_AcceptTouchEvents); |
|
1247 leftWidget->setGeometry(0, 100, 100, 100); |
|
1248 leftWidget->deleteInTouchBegin = true; |
|
1249 leftWidget->show(); |
|
1250 |
|
1251 tst_QTouchEventWidget *centerWidget = new tst_QTouchEventWidget; |
|
1252 centerWidget->setParent(&touchWidget); |
|
1253 centerWidget->setAttribute(Qt::WA_AcceptTouchEvents); |
|
1254 centerWidget->setGeometry(100, 100, 100, 100); |
|
1255 centerWidget->deleteInTouchUpdate = true; |
|
1256 centerWidget->show(); |
|
1257 |
|
1258 tst_QTouchEventWidget *rightWidget = new tst_QTouchEventWidget; |
|
1259 rightWidget->setParent(&touchWidget); |
|
1260 rightWidget->setAttribute(Qt::WA_AcceptTouchEvents); |
|
1261 rightWidget->setGeometry(200, 100, 100, 100); |
|
1262 rightWidget->deleteInTouchEnd = true; |
|
1263 rightWidget->show(); |
|
1264 |
|
1265 QPointF leftPos = leftWidget->rect().center(); |
|
1266 QPointF centerPos = centerWidget->rect().center(); |
|
1267 QPointF rightPos = rightWidget->rect().center(); |
|
1268 QPointF leftScreenPos = leftWidget->mapToGlobal(leftPos.toPoint()); |
|
1269 QPointF centerScreenPos = centerWidget->mapToGlobal(centerPos.toPoint()); |
|
1270 QPointF rightScreenPos = rightWidget->mapToGlobal(rightPos.toPoint()); |
|
1271 QRectF screenGeometry = qApp->desktop()->screenGeometry(&touchWidget); |
|
1272 |
|
1273 QWeakPointer<QWidget> pl = leftWidget, pc = centerWidget, pr = rightWidget; |
|
1274 |
|
1275 QList<QTouchEvent::TouchPoint> rawTouchPoints; |
|
1276 rawTouchPoints.append(QTouchEvent::TouchPoint(0)); |
|
1277 rawTouchPoints.append(QTouchEvent::TouchPoint(1)); |
|
1278 rawTouchPoints.append(QTouchEvent::TouchPoint(2)); |
|
1279 rawTouchPoints[0].setState(Qt::TouchPointPressed); |
|
1280 rawTouchPoints[0].setScreenPos(leftScreenPos); |
|
1281 rawTouchPoints[0].setNormalizedPos(normalized(rawTouchPoints[0].pos(), screenGeometry)); |
|
1282 rawTouchPoints[1].setState(Qt::TouchPointPressed); |
|
1283 rawTouchPoints[1].setScreenPos(centerScreenPos); |
|
1284 rawTouchPoints[1].setNormalizedPos(normalized(rawTouchPoints[1].pos(), screenGeometry)); |
|
1285 rawTouchPoints[2].setState(Qt::TouchPointPressed); |
|
1286 rawTouchPoints[2].setScreenPos(rightScreenPos); |
|
1287 rawTouchPoints[2].setNormalizedPos(normalized(rawTouchPoints[2].pos(), screenGeometry)); |
|
1288 |
|
1289 // generate begin events on all widgets, the left widget should die |
|
1290 qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchScreen, rawTouchPoints); |
|
1291 QVERIFY(pl.isNull() && !pc.isNull() && !pr.isNull()); |
|
1292 |
|
1293 // generate update events on all widget, the center widget should die |
|
1294 rawTouchPoints[0].setState(Qt::TouchPointMoved); |
|
1295 rawTouchPoints[1].setState(Qt::TouchPointMoved); |
|
1296 rawTouchPoints[2].setState(Qt::TouchPointMoved); |
|
1297 qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchScreen, rawTouchPoints); |
|
1298 |
|
1299 // generate end events on all widget, the right widget should die |
|
1300 rawTouchPoints[0].setState(Qt::TouchPointReleased); |
|
1301 rawTouchPoints[1].setState(Qt::TouchPointReleased); |
|
1302 rawTouchPoints[2].setState(Qt::TouchPointReleased); |
|
1303 qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchScreen, rawTouchPoints); |
|
1304 } |
|
1305 |
1062 QTEST_MAIN(tst_QTouchEvent) |
1306 QTEST_MAIN(tst_QTouchEvent) |
1063 |
1307 |
1064 #include "tst_qtouchevent.moc" |
1308 #include "tst_qtouchevent.moc" |