Debian Codegraph @

tests/auto/render/picking/tst_picking.cpp

raw symbols SCIP
1 // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/QTest>
5 #include <Qt3DCore/qentity.h>
6 #include <Qt3DCore/qtransform.h>
7 #include <Qt3DCore/private/qscene_p.h>
8 #include <Qt3DRender/private/qboundingvolumeprovider_p.h>
9 #include <Qt3DRender/private/pickboundingvolumejob_p.h>
10 #include <Qt3DRender/private/objectpicker_p.h>
11 #include <Qt3DRender/qobjectpicker.h>
12 #include <Qt3DRender/qgeometryrenderer.h>
13 #include <Qt3DRender/private/qobjectpicker_p.h>
14 #include <Qt3DExtras/qspheremesh.h>
15 #include <Qt3DCore/qattribute.h>
16 #include <Qt3DCore/qbuffer.h>
17 #include <Qt3DExtras/qspheregeometry.h>
18 #include <Qt3DRender/qpickevent.h>
19
20 using namespace Qt3DCore;
21 using namespace Qt3DRender;
22 using namespace Qt3DExtras;
23 using namespace Qt3DRender::Render;
24
25 class MyObjectPicker : public Qt3DRender::QObjectPicker
26 {
27 Q_OBJECT
28 public:
29 MyObjectPicker(Qt3DCore::QNode *parent = nullptr)
30 : Qt3DRender::QObjectPicker(parent)
31 {}
32 };
33
34 class PickableEntity : public QEntity
35 {
36 Q_OBJECT
37 public:
38 explicit PickableEntity(const QVector3D &position, float radius, QEntity *parent = nullptr)
39 : QEntity(parent)
40 , picker(new MyObjectPicker(this))
41 , mesh(new QSphereMesh(this))
42 , transform(new Qt3DCore::QTransform(this))
43 , acceptsEvents(true)
44 , pressedCalled(0)
45 , releasedCalled(0)
46 , clickedCalled(0)
47 {
48 mesh->setRadius(radius);
49 QSphereGeometry *g = static_cast<QSphereGeometry *>(mesh->view()->geometry());
50 QAttribute *positionAttr = static_cast<QAttribute *>(g->attributes().first());
51 Qt3DCore::QBuffer *vertexBuffer = static_cast<Qt3DCore::QBuffer *>(positionAttr->buffer());
52 Q_UNUSED(vertexBuffer);
53
54 transform->setTranslation(position);
55
56 addComponent(picker);
57 addComponent(mesh);
58 addComponent(transform);
59
60 QObject::connect(picker, &QObjectPicker::pressed, this, &PickableEntity::onPressed);
61 QObject::connect(picker, &QObjectPicker::released, this, &PickableEntity::onReleased);
62 QObject::connect(picker, &QObjectPicker::clicked, this, &PickableEntity::onClicked);
63 }
64
65 MyObjectPicker *picker;
66 QSphereMesh *mesh;
67 Qt3DCore::QTransform *transform;
68
69 bool acceptsEvents;
70 int pressedCalled;
71 int releasedCalled;
72 int clickedCalled;
73
74 private Q_SLOTS:
75 void onPressed(QPickEvent *e)
76 {
77 e->setAccepted(acceptsEvents);
78 ++pressedCalled;
79 }
80
81 void onReleased(QPickEvent *e)
82 {
83 e->setAccepted(acceptsEvents);
84 if (acceptsEvents)
85 ++releasedCalled;
86 }
87
88 void onClicked(QPickEvent *e)
89 {
90 e->setAccepted(acceptsEvents);
91 ++clickedCalled;
92 }
93 };
94
95 class tst_Picking : public QObject
96 {
97 Q_OBJECT
98 public:
99 tst_Picking()
100 {
101 qRegisterMetaType<Qt3DCore::QNode*>();
102 }
103
104 ~tst_Picking() {}
105
106 private Q_SLOTS:
107 void testEventPressedAcceptPropagation()
108 {
109 // GIVEN
110 Qt3DCore::QScene scene;
111 PickableEntity root(QVector3D(), 5.0f);
112 PickableEntity child1(QVector3D(), 5.0f, &root);
113 PickableEntity child2(QVector3D(), 5.0f, &root);
114 PickableEntity child11(QVector3D(), 5.0f, &child1);
115 Qt3DCore::QNodePrivate::get(root.picker)->setScene(&scene);
116 Qt3DCore::QNodePrivate::get(child1.picker)->setScene(&scene);
117 Qt3DCore::QNodePrivate::get(child2.picker)->setScene(&scene);
118 Qt3DCore::QNodePrivate::get(child11.picker)->setScene(&scene);
119
120 QCoreApplication::processEvents();
121
122 auto dpicker = [](QObjectPicker *node) {
123 return static_cast<QObjectPickerPrivate *>(QObjectPickerPrivate::get(node));
124 };
125
126 // WHEN
127 Qt3DRender::QPickEventPtr event(new Qt3DRender::QPickEvent());
128 dpicker(child11.picker)->pressedEvent(event.data());
129
130 // THEN
131 QCOMPARE(root.pressedCalled, 0);
132 QCOMPARE(child1.pressedCalled, 0);
133 QCOMPARE(child2.pressedCalled, 0);
134 QCOMPARE(child11.pressedCalled, 1);
135
136 // WHEN
137 child11.pressedCalled = 0;
138 child11.acceptsEvents = false;
139 dpicker(child11.picker)->pressedEvent(event.data());
140
141 // THEN
142 QCOMPARE(root.pressedCalled, 0);
143 QCOMPARE(child1.pressedCalled, 1);
144 QCOMPARE(child2.pressedCalled, 0);
145 QCOMPARE(child11.pressedCalled, 1);
146
147 // WHEN
148 child1.acceptsEvents = false;
149 child1.pressedCalled = 0;
150 child11.acceptsEvents = false;
151 child11.pressedCalled = 0;
152 dpicker(child11.picker)->pressedEvent(event.data());
153
154 // THEN
155 QCOMPARE(root.pressedCalled, 1);
156 QCOMPARE(child1.pressedCalled, 1);
157 QCOMPARE(child2.pressedCalled, 0);
158 QCOMPARE(child11.pressedCalled, 1);
159 }
160
161 void testEventReleasedAcceptPropagation()
162 {
163 // GIVEN
164 Qt3DCore::QScene scene;
165 PickableEntity root(QVector3D(), 5.0f);
166 PickableEntity child1(QVector3D(), 5.0f, &root);
167 PickableEntity child2(QVector3D(), 5.0f, &root);
168 PickableEntity child11(QVector3D(), 5.0f, &child1);
169 Qt3DCore::QNodePrivate::get(root.picker)->setScene(&scene);
170 Qt3DCore::QNodePrivate::get(child1.picker)->setScene(&scene);
171 Qt3DCore::QNodePrivate::get(child2.picker)->setScene(&scene);
172 Qt3DCore::QNodePrivate::get(child11.picker)->setScene(&scene);
173
174 QCoreApplication::processEvents();
175
176 auto dpicker = [](QObjectPicker *node) {
177 return static_cast<QObjectPickerPrivate *>(QObjectPickerPrivate::get(node));
178 };
179
180 // WHEN
181 Qt3DRender::QPickEventPtr event(new Qt3DRender::QPickEvent());
182 dpicker(child11.picker)->pressedEvent(event.data());
183 dpicker(child11.picker)->releasedEvent(event.data());
184
185 // THEN
186 QCOMPARE(root.releasedCalled, 0);
187 QCOMPARE(child1.releasedCalled, 0);
188 QCOMPARE(child2.releasedCalled, 0);
189 QCOMPARE(child11.releasedCalled, 1);
190
191 // WHEN
192 child11.releasedCalled = 0;
193 child11.pressedCalled = 0;
194 child11.acceptsEvents = false;
195 dpicker(child11.picker)->pressedEvent(event.data());
196 dpicker(child11.picker)->releasedEvent(event.data());
197
198 // THEN
199 QCOMPARE(child1.pressedCalled, 1);
200 QCOMPARE(child11.pressedCalled, 1);
201
202 QCOMPARE(root.releasedCalled, 0);
203 QCOMPARE(child1.releasedCalled, 1);
204 QCOMPARE(child2.releasedCalled, 0);
205 QCOMPARE(child11.releasedCalled, 0);
206 }
207
208 void testEventClickedAcceptPropagation()
209 {
210 // GIVEN
211 Qt3DCore::QScene scene;
212 PickableEntity root(QVector3D(), 5.0f);
213 PickableEntity child1(QVector3D(), 5.0f, &root);
214 PickableEntity child2(QVector3D(), 5.0f, &root);
215 PickableEntity child11(QVector3D(), 5.0f, &child1);
216 Qt3DCore::QNodePrivate::get(root.picker)->setScene(&scene);
217 Qt3DCore::QNodePrivate::get(child1.picker)->setScene(&scene);
218 Qt3DCore::QNodePrivate::get(child2.picker)->setScene(&scene);
219 Qt3DCore::QNodePrivate::get(child11.picker)->setScene(&scene);
220
221 QCoreApplication::processEvents();
222
223 auto dpicker = [](QObjectPicker *node) {
224 return static_cast<QObjectPickerPrivate *>(QObjectPickerPrivate::get(node));
225 };
226
227 // WHEN
228 Qt3DRender::QPickEventPtr event(new Qt3DRender::QPickEvent());
229 dpicker(child11.picker)->clickedEvent(event.data());
230
231 // THEN
232 QCOMPARE(root.clickedCalled, 0);
233 QCOMPARE(child1.clickedCalled, 0);
234 QCOMPARE(child2.clickedCalled, 0);
235 QCOMPARE(child11.clickedCalled, 1);
236
237 // WHEN
238 child11.clickedCalled = 0;
239 child11.acceptsEvents = false;
240 dpicker(child11.picker)->clickedEvent(event.data());
241
242 // THEN
243 QCOMPARE(root.clickedCalled, 0);
244 QCOMPARE(child1.clickedCalled, 1);
245 QCOMPARE(child2.clickedCalled, 0);
246 QCOMPARE(child11.clickedCalled, 1);
247
248 // WHEN
249 child1.acceptsEvents = false;
250 child1.clickedCalled = 0;
251 child11.acceptsEvents = false;
252 child11.clickedCalled = 0;
253 dpicker(child11.picker)->clickedEvent(event.data());
254
255 // THEN
256 QCOMPARE(root.clickedCalled, 1);
257 QCOMPARE(child1.clickedCalled, 1);
258 QCOMPARE(child2.clickedCalled, 0);
259 QCOMPARE(child11.clickedCalled, 1);
260 }
261 };
262
263 QTEST_MAIN(tst_Picking)
264
265 #include "tst_picking.moc"