|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 <QtGui/qgraphicsscene.h> |
|
45 #include <private/qgraphicsscenebsptreeindex_p.h> |
|
46 #include <private/qgraphicssceneindex_p.h> |
|
47 #include <private/qgraphicsscenelinearindex_p.h> |
|
48 |
|
49 //TESTED_CLASS= |
|
50 //TESTED_FILES= |
|
51 |
|
52 class tst_QGraphicsSceneIndex : public QObject |
|
53 { |
|
54 Q_OBJECT |
|
55 public slots: |
|
56 void initTestCase(); |
|
57 |
|
58 private slots: |
|
59 void customIndex_data(); |
|
60 void customIndex(); |
|
61 void scatteredItems_data(); |
|
62 void scatteredItems(); |
|
63 void overlappedItems_data(); |
|
64 void overlappedItems(); |
|
65 void movingItems_data(); |
|
66 void movingItems(); |
|
67 void connectedToSceneRectChanged(); |
|
68 void items(); |
|
69 void clear(); |
|
70 |
|
71 private: |
|
72 void common_data(); |
|
73 QGraphicsSceneIndex *createIndex(const QString &name); |
|
74 }; |
|
75 |
|
76 void tst_QGraphicsSceneIndex::initTestCase() |
|
77 { |
|
78 } |
|
79 |
|
80 void tst_QGraphicsSceneIndex::common_data() |
|
81 { |
|
82 QTest::addColumn<QString>("indexMethod"); |
|
83 |
|
84 QTest::newRow("BSP") << QString("bsp"); |
|
85 QTest::newRow("Linear") << QString("linear"); |
|
86 } |
|
87 |
|
88 QGraphicsSceneIndex *tst_QGraphicsSceneIndex::createIndex(const QString &indexMethod) |
|
89 { |
|
90 QGraphicsSceneIndex *index = 0; |
|
91 QGraphicsScene *scene = new QGraphicsScene(); |
|
92 if (indexMethod == "bsp") |
|
93 index = new QGraphicsSceneBspTreeIndex(scene); |
|
94 |
|
95 if (indexMethod == "linear") |
|
96 index = new QGraphicsSceneLinearIndex(scene); |
|
97 |
|
98 return index; |
|
99 } |
|
100 |
|
101 void tst_QGraphicsSceneIndex::customIndex_data() |
|
102 { |
|
103 common_data(); |
|
104 } |
|
105 |
|
106 void tst_QGraphicsSceneIndex::customIndex() |
|
107 { |
|
108 #if 0 |
|
109 QFETCH(QString, indexMethod); |
|
110 QGraphicsSceneIndex *index = createIndex(indexMethod); |
|
111 |
|
112 QGraphicsScene scene; |
|
113 scene.setSceneIndex(index); |
|
114 |
|
115 scene.addRect(0, 0, 30, 40); |
|
116 QCOMPARE(scene.items(QRectF(0, 0, 10, 10)).count(), 1); |
|
117 #endif |
|
118 } |
|
119 |
|
120 void tst_QGraphicsSceneIndex::scatteredItems_data() |
|
121 { |
|
122 common_data(); |
|
123 } |
|
124 |
|
125 void tst_QGraphicsSceneIndex::scatteredItems() |
|
126 { |
|
127 QFETCH(QString, indexMethod); |
|
128 |
|
129 QGraphicsScene scene; |
|
130 #if 1 |
|
131 scene.setItemIndexMethod(indexMethod == "linear" ? QGraphicsScene::NoIndex : QGraphicsScene::BspTreeIndex); |
|
132 #else |
|
133 QGraphicsSceneIndex *index = createIndex(indexMethod); |
|
134 scene.setSceneIndex(index); |
|
135 #endif |
|
136 |
|
137 for (int i = 0; i < 10; ++i) |
|
138 scene.addRect(i*50, i*50, 40, 35); |
|
139 |
|
140 QCOMPARE(scene.items(QPointF(5, 5)).count(), 1); |
|
141 QCOMPARE(scene.items(QPointF(55, 55)).count(), 1); |
|
142 QCOMPARE(scene.items(QPointF(-100, -100)).count(), 0); |
|
143 |
|
144 QCOMPARE(scene.items(QRectF(0, 0, 10, 10)).count(), 1); |
|
145 QCOMPARE(scene.items(QRectF(0, 0, 1000, 1000)).count(), 10); |
|
146 QCOMPARE(scene.items(QRectF(-100, -1000, 0, 0)).count(), 0); |
|
147 } |
|
148 |
|
149 void tst_QGraphicsSceneIndex::overlappedItems_data() |
|
150 { |
|
151 common_data(); |
|
152 } |
|
153 |
|
154 void tst_QGraphicsSceneIndex::overlappedItems() |
|
155 { |
|
156 QFETCH(QString, indexMethod); |
|
157 |
|
158 QGraphicsScene scene; |
|
159 #if 1 |
|
160 scene.setItemIndexMethod(indexMethod == "linear" ? QGraphicsScene::NoIndex : QGraphicsScene::BspTreeIndex); |
|
161 #else |
|
162 QGraphicsSceneIndex *index = createIndex(indexMethod); |
|
163 scene.setSceneIndex(index); |
|
164 #endif |
|
165 |
|
166 for (int i = 0; i < 10; ++i) |
|
167 for (int j = 0; j < 10; ++j) |
|
168 scene.addRect(i*50, j*50, 200, 200); |
|
169 |
|
170 QCOMPARE(scene.items(QPointF(5, 5)).count(), 1); |
|
171 QCOMPARE(scene.items(QPointF(55, 55)).count(), 4); |
|
172 QCOMPARE(scene.items(QPointF(105, 105)).count(), 9); |
|
173 QCOMPARE(scene.items(QPointF(-100, -100)).count(), 0); |
|
174 |
|
175 QCOMPARE(scene.items(QRectF(0, 0, 1000, 1000)).count(), 100); |
|
176 QCOMPARE(scene.items(QRectF(-100, -1000, 0, 0)).count(), 0); |
|
177 QCOMPARE(scene.items(QRectF(0, 0, 200, 200)).count(), 16); |
|
178 QCOMPARE(scene.items(QRectF(0, 0, 100, 100)).count(), 4); |
|
179 QCOMPARE(scene.items(QRectF(0, 0, 1, 100)).count(), 2); |
|
180 QCOMPARE(scene.items(QRectF(0, 0, 1, 1000)).count(), 10); |
|
181 } |
|
182 |
|
183 void tst_QGraphicsSceneIndex::movingItems_data() |
|
184 { |
|
185 common_data(); |
|
186 } |
|
187 |
|
188 void tst_QGraphicsSceneIndex::movingItems() |
|
189 { |
|
190 QFETCH(QString, indexMethod); |
|
191 |
|
192 QGraphicsScene scene; |
|
193 #if 1 |
|
194 scene.setItemIndexMethod(indexMethod == "linear" ? QGraphicsScene::NoIndex : QGraphicsScene::BspTreeIndex); |
|
195 #else |
|
196 QGraphicsSceneIndex *index = createIndex(indexMethod); |
|
197 scene.setSceneIndex(index); |
|
198 #endif |
|
199 |
|
200 for (int i = 0; i < 10; ++i) |
|
201 scene.addRect(i*50, i*50, 40, 35); |
|
202 |
|
203 QGraphicsRectItem *box = scene.addRect(0, 0, 10, 10); |
|
204 QCOMPARE(scene.items(QPointF(5, 5)).count(), 2); |
|
205 QCOMPARE(scene.items(QPointF(-1, -1)).count(), 0); |
|
206 QCOMPARE(scene.items(QRectF(0, 0, 5, 5)).count(), 2); |
|
207 |
|
208 box->setPos(10, 10); |
|
209 QCOMPARE(scene.items(QPointF(9, 9)).count(), 1); |
|
210 QCOMPARE(scene.items(QPointF(15, 15)).count(), 2); |
|
211 QCOMPARE(scene.items(QRectF(0, 0, 1, 1)).count(), 1); |
|
212 |
|
213 box->setPos(-5, -5); |
|
214 QCOMPARE(scene.items(QPointF(-1, -1)).count(), 1); |
|
215 QCOMPARE(scene.items(QRectF(0, 0, 1, 1)).count(), 2); |
|
216 |
|
217 QCOMPARE(scene.items(QRectF(0, 0, 1000, 1000)).count(), 11); |
|
218 } |
|
219 |
|
220 void tst_QGraphicsSceneIndex::connectedToSceneRectChanged() |
|
221 { |
|
222 |
|
223 class MyScene : public QGraphicsScene |
|
224 { public: using QGraphicsScene::receivers; }; |
|
225 |
|
226 MyScene scene; // Uses QGraphicsSceneBspTreeIndex by default. |
|
227 QCOMPARE(scene.receivers(SIGNAL(sceneRectChanged(const QRectF&))), 1); |
|
228 |
|
229 scene.setItemIndexMethod(QGraphicsScene::NoIndex); // QGraphicsSceneLinearIndex |
|
230 QCOMPARE(scene.receivers(SIGNAL(sceneRectChanged(const QRectF&))), 1); |
|
231 } |
|
232 |
|
233 void tst_QGraphicsSceneIndex::items() |
|
234 { |
|
235 QGraphicsScene scene; |
|
236 QGraphicsItem *item1 = scene.addRect(0, 0, 10, 10); |
|
237 QGraphicsItem *item2 = scene.addRect(10, 10, 10, 10); |
|
238 QCOMPARE(scene.items().size(), 2); |
|
239 |
|
240 // Move from unindexed items into bsp tree. |
|
241 QTest::qWait(50); |
|
242 QCOMPARE(scene.items().size(), 2); |
|
243 |
|
244 // Add untransformable item. |
|
245 QGraphicsItem *item3 = new QGraphicsRectItem(QRectF(20, 20, 10, 10)); |
|
246 item3->setFlag(QGraphicsItem::ItemIgnoresTransformations); |
|
247 scene.addItem(item3); |
|
248 QCOMPARE(scene.items().size(), 3); |
|
249 |
|
250 // Move from unindexed items into untransformable items. |
|
251 QTest::qWait(50); |
|
252 QCOMPARE(scene.items().size(), 3); |
|
253 |
|
254 // Move from untransformable items into unindexed items. |
|
255 item3->setFlag(QGraphicsItem::ItemIgnoresTransformations, false); |
|
256 QCOMPARE(scene.items().size(), 3); |
|
257 QTest::qWait(50); |
|
258 QCOMPARE(scene.items().size(), 3); |
|
259 |
|
260 // Make all items untransformable. |
|
261 item1->setFlag(QGraphicsItem::ItemIgnoresTransformations); |
|
262 item2->setParentItem(item1); |
|
263 item3->setParentItem(item2); |
|
264 QCOMPARE(scene.items().size(), 3); |
|
265 |
|
266 // Move from unindexed items into untransformable items. |
|
267 QTest::qWait(50); |
|
268 QCOMPARE(scene.items().size(), 3); |
|
269 } |
|
270 |
|
271 void tst_QGraphicsSceneIndex::clear() |
|
272 { |
|
273 class MyItem : public QGraphicsItem |
|
274 { |
|
275 public: |
|
276 MyItem(QGraphicsItem *parent = 0) : QGraphicsItem(parent), numPaints(0) {} |
|
277 int numPaints; |
|
278 protected: |
|
279 QRectF boundingRect() const { return QRectF(0, 0, 10, 10); } |
|
280 void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
281 { ++numPaints; } |
|
282 }; |
|
283 |
|
284 QGraphicsScene scene; |
|
285 scene.setSceneRect(0, 0, 100, 100); |
|
286 scene.addItem(new MyItem); |
|
287 |
|
288 QGraphicsView view(&scene); |
|
289 view.show(); |
|
290 #ifdef Q_WS_X11 |
|
291 qt_x11_wait_for_window_manager(&view); |
|
292 #endif |
|
293 QTest::qWait(250); |
|
294 scene.clear(); |
|
295 |
|
296 // Make sure the index is re-generated after QGraphicsScene::clear(); |
|
297 // otherwise no items will be painted. |
|
298 MyItem *item = new MyItem; |
|
299 scene.addItem(item); |
|
300 qApp->processEvents(); |
|
301 QCOMPARE(item->numPaints, 1); |
|
302 } |
|
303 |
|
304 QTEST_MAIN(tst_QGraphicsSceneIndex) |
|
305 #include "tst_qgraphicssceneindex.moc" |