Make QPen default to 1-width non-cosmetic.
[profile/ivi/qtbase.git] / tests / auto / widgets / graphicsview / qgraphicsscene / tst_qgraphicsscene.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44 #if defined(Q_OS_WINCE)
45 #include <ceconfig.h>
46 #endif
47
48 #include <QtGui>
49 #include <QtWidgets>
50 #include <private/qgraphicsscene_p.h>
51 #include <private/qgraphicssceneindex_p.h>
52 #include <math.h>
53 #include "../../../gui/painting/qpathclipper/pathcompare.h"
54 #include "../../../shared/platforminputcontext.h"
55 #include <private/qinputmethod_p.h>
56
57 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
58 #include <windows.h>
59 #define Q_CHECK_PAINTEVENTS \
60     if (::SwitchDesktop(::GetThreadDesktop(::GetCurrentThreadId())) == 0) \
61         QSKIP("The Graphics View doesn't get the paint events");
62 #else
63 #define Q_CHECK_PAINTEVENTS
64 #endif
65
66 Q_DECLARE_METATYPE(QList<int>)
67 Q_DECLARE_METATYPE(QList<QRectF>)
68 Q_DECLARE_METATYPE(QMatrix)
69 Q_DECLARE_METATYPE(QPainterPath)
70 Q_DECLARE_METATYPE(QPointF)
71 Q_DECLARE_METATYPE(QRectF)
72 Q_DECLARE_METATYPE(Qt::AspectRatioMode)
73 Q_DECLARE_METATYPE(Qt::ItemSelectionMode)
74
75 static const int randomX[] = {276, 40, 250, 864, -56, 426, 855, 825, 184, 955, -798, -804, 773,
76                               282, 489, 686, 780, -220, 50, 749, -856, -205, 81, 492, -819, 518,
77                               895, 57, -559, 788, -965, 68, -442, -247, -339, -648, 292, 891,
78                               -865, 462, 864, 673, 640, 523, 194, 500, -727, 307, -243, 320,
79                               -545, 415, 448, 341, -619, 652, 892, -16, -14, -659, -101, -934,
80                               532, 356, 824, 132, 160, 130, 104, 886, -179, -174, 543, -644, 60,
81                               -470, -354, -728, 689, 682, -587, -694, -221, -741, 37, 372, -289,
82                               741, -300, 858, -320, 729, -602, -956, -544, -403, 203, 398, 284,
83                               -972, -572, -946, 81, 51, -403, -580, 867, 546, 565, -580, -484,
84                               659, 982, -518, -976, 423, -800, 659, -297, 712, 938, -19, -16,
85                               824, -252, 197, 321, -837, 824, 136, 226, -980, -909, -826, -479,
86                               -835, -503, -828, -901, -810, -641, -548, -179, 194, 749, -296, 539,
87                               -37, -599, -235, 121, 35, -230, -915, 789, 764, -622, -382, -90, -701,
88                               676, -407, 998, 267, 913, 817, -748, -370, -162, -797, 19, -556, 933,
89                               -670, -101, -765, -941, -17, 360, 31, 960, 509, 933, -35, 974, -924,
90                               -734, 589, 963, 724, 794, 843, 16, -272, -811, 721, 99, -122, 216,
91                               -404, 158, 787, -443, -437, -337, 383, -342, 538, -641, 791, 637,
92                               -848, 397, 820, 109, 11, 45, 809, 591, 933, 961, 625, -140, -592,
93                               -694, -969, 317, 293, 777, -18, -282, 835, -455, -708, -407, -204,
94                               748, 347, -501, -545, 292, -362, 176, 546, -573, -38, -854, -395,
95                               560, -624, -940, -971, 66, -910, 782, 985};
96
97 static const int randomY[] = {603, 70, -318, 843, 450, -637, 199, -527, 407, 964, -54, 620, -207,
98                               -736, -700, -476, -706, -142, 837, 621, 522, -98, 232, 292, -267, 900,
99                               615, -356, -415, 783, 290, 462, -857, -314, 677, 36, 772, 424, -72,
100                               -121, 547, -533, 537, -656, 289, 508, 914, 601, 434, 588, -779, -714,
101                               -368, 628, -276, 432, -1, -929, 638, -36, 253, -922, -943, 979, -34,
102                               -268, -193, 601, 686, -330, 165, 98, 75, -691, -605, 617, 773, 617,
103                               619, 238, -42, -405, 17, 384, -472, -846, 520, 110, 591, -217, 936,
104                               -373, 731, 734, 810, 961, 881, 939, 379, -905, -137, 437, 298, 688,
105                               -71, -204, 573, -120, -821, 489, -722, -926, 529, -113, -243, 543,
106                               868, -301, -781, -549, -842, -489, -80, -910, -928, 51, -91, 324,
107                               204, -92, 867, 723, 248, 709, -357, 591, -365, -379, 266, -649, -95,
108                               205, 551, 355, -631, 79, -186, 795, -7, -225, 46, -410, 665, -874,
109                               -618, 845, -548, 443, 471, -644, 606, -607, 59, -619, 288, -244, 529,
110                               690, 349, -738, -611, -879, -642, 801, -178, 823, -748, -552, -247,
111                               -223, -408, 651, -62, 949, -795, 171, -107, -210, -207, -842, -86,
112                               436, 528, 366, -178, 245, -695, 665, 613, -948, 667, -620, -979, -949,
113                               905, 181, -412, -467, -437, -774, 750, -10, 54, 205, -674, -290, -924,
114                               -361, -463, 912, -702, 622, -542, 220, 115, 832, 451, -38, -952, -230,
115                               -588, 864, 234, 225, -303, 493, 246, 153, 338, -378, 377, -819, 140, 136,
116                               467, -849, -326, -533, 166, 252, -994, -699, 904, -566, 621, -752};
117
118 class HoverItem : public QGraphicsRectItem
119 {
120 public:
121     HoverItem()
122         : QGraphicsRectItem(QRectF(-10, -10, 20, 20)), isHovered(false)
123     { setAcceptsHoverEvents(true); }
124
125     bool isHovered;
126
127 protected:
128     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
129     {
130         isHovered = (option->state & QStyle::State_MouseOver);
131
132         painter->setOpacity(0.75);
133         painter->setPen(Qt::NoPen);
134         painter->setBrush(Qt::darkGray);
135         painter->drawRoundRect(boundingRect().adjusted(3, 3, -3, -3), Qt::darkGray);
136         painter->setPen(Qt::black);
137         if (isHovered) {
138             painter->setBrush(QColor(Qt::blue).light(120));
139         } else {
140             painter->setBrush(Qt::gray);
141         }
142         painter->drawRoundRect(boundingRect().adjusted(0, 0, -5, -5));
143     }
144 };
145
146 class EventSpy : public QGraphicsWidget
147 {
148     Q_OBJECT
149 public:
150     EventSpy(QObject *watched, QEvent::Type type)
151         : _count(0), spied(type)
152     {
153         watched->installEventFilter(this);
154     }
155
156     EventSpy(QGraphicsScene *scene, QGraphicsItem *watched, QEvent::Type type)
157         : _count(0), spied(type)
158     {
159         scene->addItem(this);
160         watched->installSceneEventFilter(this);
161     }
162
163     int count() const { return _count; }
164
165 protected:
166     bool eventFilter(QObject *watched, QEvent *event)
167     {
168         Q_UNUSED(watched);
169         if (event->type() == spied)
170             ++_count;
171         return false;
172     }
173
174     bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
175     {
176         Q_UNUSED(watched);
177         if (event->type() == spied)
178             ++_count;
179         return false;
180     }
181
182     int _count;
183     QEvent::Type spied;
184 };
185
186 class tst_QGraphicsScene : public QObject
187 {
188     Q_OBJECT
189 public slots:
190     void initTestCase();
191     void cleanup();
192
193 private slots:
194     void construction();
195     void sceneRect();
196     void itemIndexMethod();
197     void bspTreeDepth();
198     void itemsBoundingRect_data();
199     void itemsBoundingRect();
200     void items();
201     void items_QPointF_data();
202     void items_QPointF();
203     void items_QRectF();
204     void items_QRectF_2_data();
205     void items_QRectF_2();
206     void items_QPolygonF();
207     void items_QPolygonF_2();
208     void items_QPainterPath();
209     void items_QPainterPath_2();
210     void selectionChanged();
211     void selectionChanged2();
212     void addItem();
213     void addEllipse();
214     void addLine();
215     void addPath();
216     void addPixmap();
217     void addRect();
218     void addText();
219     void removeItem();
220     void clear();
221     void focusItem();
222     void focusItemLostFocus();
223     void setFocusItem();
224     void setFocusItem_inactive();
225     void mouseGrabberItem();
226     void hoverEvents_siblings();
227     void hoverEvents_parentChild();
228     void createItemGroup();
229     void mouseEventPropagation();
230     void mouseEventPropagation_ignore();
231     void mouseEventPropagation_focus();
232     void mouseEventPropagation_doubleclick();
233     void mouseEventPropagation_mouseMove();
234 #ifndef QT_NO_DRAGANDDROP
235     void dragAndDrop_simple();
236     void dragAndDrop_disabledOrInvisible();
237     void dragAndDrop_propagate();
238 #endif
239     void render_data();
240     void render();
241     void renderItemsWithNegativeWidthOrHeight();
242     void contextMenuEvent();
243     void contextMenuEvent_ItemIgnoresTransformations();
244     void update();
245     void update2();
246     void views();
247     void event();
248     void eventsToDisabledItems();
249     void exposedRect();
250     void tabFocus_emptyScene();
251     void tabFocus_sceneWithFocusableItems();
252     void tabFocus_sceneWithFocusWidgets();
253     void tabFocus_sceneWithNestedFocusWidgets();
254     void style();
255     void sorting_data();
256     void sorting();
257     void changedSignal_data();
258     void changedSignal();
259     void stickyFocus_data();
260     void stickyFocus();
261     void sendEvent();
262     void inputMethod_data();
263     void inputMethod();
264     void dispatchHoverOnPress();
265     void initialFocus_data();
266     void initialFocus();
267     void polishItems();
268     void polishItems2();
269     void isActive();
270     void siblingIndexAlwaysValid();
271     void removeFullyTransparentItem();
272     void zeroScale();
273
274     // task specific tests below me
275     void task139710_bspTreeCrash();
276     void task139782_containsItemBoundingRect();
277     void task176178_itemIndexMethodBreaksSceneRect();
278     void task160653_selectionChanged();
279     void task250680_childClip();
280     void taskQTBUG_5904_crashWithDeviceCoordinateCache();
281     void taskQT657_paintIntoCacheWithTransparentParts();
282     void taskQTBUG_7863_paintIntoCacheWithTransparentParts();
283     void taskQT_3674_doNotCrash();
284     void taskQTBUG_15977_renderWithDeviceCoordinateCache();
285     void taskQTBUG_16401_focusItem();
286 };
287
288 void tst_QGraphicsScene::initTestCase()
289 {
290 #ifdef Q_OS_WINCE //disable magic for WindowsCE
291     qApp->setAutoMaximizeThreshold(-1);
292 #endif
293 }
294
295 void tst_QGraphicsScene::cleanup()
296 {
297     // ensure not even skipped tests with custom input context leave it dangling
298     QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
299     inputMethodPrivate->testContext = 0;
300 }
301
302 void tst_QGraphicsScene::construction()
303 {
304     QGraphicsScene scene;
305     QCOMPARE(scene.itemsBoundingRect(), QRectF());
306     QVERIFY(scene.items().isEmpty());
307     QVERIFY(scene.items(QPointF()).isEmpty());
308     QVERIFY(scene.items(QRectF()).isEmpty());
309     QVERIFY(scene.items(QPolygonF()).isEmpty());
310     QVERIFY(scene.items(QPainterPath()).isEmpty());
311     QTest::ignoreMessage(QtWarningMsg, "QGraphicsScene::collidingItems: cannot find collisions for null item");
312     QVERIFY(scene.collidingItems(0).isEmpty());
313     QVERIFY(!scene.itemAt(QPointF()));
314     QVERIFY(scene.selectedItems().isEmpty());
315     QVERIFY(!scene.focusItem());
316 }
317
318 void tst_QGraphicsScene::sceneRect()
319 {
320     QGraphicsScene scene;
321     QSignalSpy sceneRectChanged(&scene, SIGNAL(sceneRectChanged(QRectF)));
322     QCOMPARE(scene.sceneRect(), QRectF());
323     QCOMPARE(sceneRectChanged.count(), 0);
324
325     QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 10, 10));
326     item->setPen(QPen(Qt::black, 0));
327     item->setPos(-5, -5);
328     QCOMPARE(sceneRectChanged.count(), 0);
329
330     QCOMPARE(scene.itemAt(0, 0), item);
331     QCOMPARE(scene.itemAt(10, 10), (QGraphicsItem *)0);
332     QCOMPARE(sceneRectChanged.count(), 0);
333     QCOMPARE(scene.sceneRect(), QRectF(-5, -5, 10, 10));
334     QCOMPARE(sceneRectChanged.count(), 1);
335     QCOMPARE(sceneRectChanged.last().at(0).toRectF(), scene.sceneRect());
336
337     item->setPos(0, 0);
338     QCOMPARE(scene.sceneRect(), QRectF(-5, -5, 15, 15));
339     QCOMPARE(sceneRectChanged.count(), 2);
340     QCOMPARE(sceneRectChanged.last().at(0).toRectF(), scene.sceneRect());
341
342     scene.setSceneRect(-100, -100, 10, 10);
343     QCOMPARE(sceneRectChanged.count(), 3);
344     QCOMPARE(sceneRectChanged.last().at(0).toRectF(), scene.sceneRect());
345
346     QCOMPARE(scene.itemAt(0, 0), item);
347     QCOMPARE(scene.itemAt(10, 10), (QGraphicsItem *)0);
348     QCOMPARE(scene.sceneRect(), QRectF(-100, -100, 10, 10));
349     item->setPos(10, 10);
350     QCOMPARE(scene.sceneRect(), QRectF(-100, -100, 10, 10));
351     QCOMPARE(sceneRectChanged.count(), 3);
352     QCOMPARE(sceneRectChanged.last().at(0).toRectF(), scene.sceneRect());
353
354     scene.setSceneRect(QRectF());
355
356     QCOMPARE(scene.itemAt(10, 10), item);
357     QCOMPARE(scene.itemAt(20, 20), (QGraphicsItem *)0);
358     QCOMPARE(sceneRectChanged.count(), 4);
359     QCOMPARE(scene.sceneRect(), QRectF(-5, -5, 25, 25));
360     QCOMPARE(sceneRectChanged.count(), 5);
361     QCOMPARE(sceneRectChanged.last().at(0).toRectF(), scene.sceneRect());
362 }
363
364 void tst_QGraphicsScene::itemIndexMethod()
365 {
366     QGraphicsScene scene;
367     QCOMPARE(scene.itemIndexMethod(), QGraphicsScene::BspTreeIndex);
368
369 #ifdef Q_PROCESSOR_ARM
370     const int minY = -500;
371     const int maxY = 500;
372     const int minX = -500;
373     const int maxX = 500;
374 #else
375     const int minY = -1000;
376     const int maxY = 2000;
377     const int minX = -1000;
378     const int maxX = 2000;
379 #endif
380
381     QList<QGraphicsItem *> items;
382     for (int y = minY; y < maxY; y += 100) {
383         for (int x = minX; x < maxX; x += 100) {
384             QGraphicsItem *item = scene.addRect(QRectF(0, 0, 10, 10));
385             item->setPos(x, y);
386             QCOMPARE(scene.itemAt(x, y), item);
387             items << item;
388         }
389     }
390
391     int n = 0;
392     for (int y = minY; y < maxY; y += 100) {
393         for (int x = minX; x < maxX; x += 100)
394             QCOMPARE(scene.itemAt(x, y), items.at(n++));
395     }
396
397     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
398     QCOMPARE(scene.itemIndexMethod(), QGraphicsScene::NoIndex);
399
400     n = 0;
401     for (int y = minY; y < maxY; y += 100) {
402         for (int x = minX; x < maxX; x += 100)
403             QCOMPARE(scene.itemAt(x, y), items.at(n++));
404     }
405
406     scene.setItemIndexMethod(QGraphicsScene::BspTreeIndex);
407     QCOMPARE(scene.itemIndexMethod(), QGraphicsScene::BspTreeIndex);
408
409     n = 0;
410     for (int y = minY; y < maxY; y += 100) {
411         for (int x = minX; x < maxX; x += 100)
412             QCOMPARE(scene.itemAt(x, y), items.at(n++));
413     }
414 }
415
416 void tst_QGraphicsScene::bspTreeDepth()
417 {
418     QGraphicsScene scene;
419     QCOMPARE(scene.itemIndexMethod(), QGraphicsScene::BspTreeIndex);
420     QCOMPARE(scene.bspTreeDepth(), 0);
421     scene.setBspTreeDepth(1);
422     QCOMPARE(scene.bspTreeDepth(), 1);
423     QTest::ignoreMessage(QtWarningMsg, "QGraphicsScene::setBspTreeDepth: invalid depth -1 ignored; must be >= 0");
424     scene.setBspTreeDepth(-1);
425     QCOMPARE(scene.bspTreeDepth(), 1);
426 }
427
428 void tst_QGraphicsScene::items()
429 {
430 #ifdef Q_PROCESSOR_ARM
431     const int minY = -500;
432     const int maxY = 500;
433     const int minX = -500;
434     const int maxX = 500;
435 #else
436     const int minY = -1000;
437     const int maxY = 2000;
438     const int minX = -1000;
439     const int maxX = 2000;
440 #endif
441
442     {
443         QGraphicsScene scene;
444
445         QList<QGraphicsItem *> items;
446         for (int y = minY; y < maxY; y += 100) {
447             for (int x = minX; x < maxX; x += 100)
448                 items << scene.addRect(QRectF(0, 0, 10, 10));
449         }
450         QCOMPARE(scene.items().size(), items.size());
451         scene.itemAt(0, 0); // trigger indexing
452
453         scene.removeItem(items.at(5));
454         delete items.at(5);
455         QVERIFY(!scene.items().contains(0));
456         delete items.at(7);
457         QVERIFY(!scene.items().contains(0));
458     }
459     {
460         QGraphicsScene scene;
461         QGraphicsLineItem *l1 = scene.addLine(-5, 0, 5, 0);
462         l1->setPen(QPen(Qt::black, 0));
463         QGraphicsLineItem *l2 = scene.addLine(0, -5, 0, 5);
464         l2->setPen(QPen(Qt::black, 0));
465         QVERIFY(!l1->sceneBoundingRect().intersects(l2->sceneBoundingRect()));
466         QVERIFY(!l2->sceneBoundingRect().intersects(l1->sceneBoundingRect()));
467         QList<QGraphicsItem *> items;
468         items<<l1<<l2;
469         QCOMPARE(scene.items().size(), items.size());
470         QVERIFY(scene.items(-1, -1, 2, 2).contains(l1));
471         QVERIFY(scene.items(-1, -1, 2, 2).contains(l2));
472     }
473 }
474
475 void tst_QGraphicsScene::itemsBoundingRect_data()
476 {
477     QTest::addColumn<QList<QRectF> >("rects");
478     QTest::addColumn<QMatrix>("matrix");
479     QTest::addColumn<QRectF>("boundingRect");
480
481     QMatrix transformationMatrix;
482     transformationMatrix.translate(50, -50);
483     transformationMatrix.scale(2, 2);
484     transformationMatrix.rotate(90);
485
486     QTest::newRow("none")
487         << QList<QRectF>()
488         << QMatrix()
489         << QRectF();
490     QTest::newRow("{{0, 0, 10, 10}}")
491         << (QList<QRectF>() << QRectF(0, 0, 10, 10))
492         << QMatrix()
493         << QRectF(0, 0, 10, 10);
494     QTest::newRow("{{-10, -10, 10, 10}}")
495         << (QList<QRectF>() << QRectF(-10, -10, 10, 10))
496         << QMatrix()
497         << QRectF(-10, -10, 10, 10);
498     QTest::newRow("{{-1000, -1000, 1, 1}, {-10, -10, 10, 10}}")
499         << (QList<QRectF>() << QRectF(-1000, -1000, 1, 1) << QRectF(-10, -10, 10, 10))
500         << QMatrix()
501         << QRectF(-1000, -1000, 1000, 1000);
502     QTest::newRow("transformed {{0, 0, 10, 10}}")
503         << (QList<QRectF>() << QRectF(0, 0, 10, 10))
504         << transformationMatrix
505         << QRectF(30, -50, 20, 20);
506     QTest::newRow("transformed {{-10, -10, 10, 10}}")
507         << (QList<QRectF>() << QRectF(-10, -10, 10, 10))
508         << transformationMatrix
509         << QRectF(50, -70, 20, 20);
510     QTest::newRow("transformed {{-1000, -1000, 1, 1}, {-10, -10, 10, 10}}")
511         << (QList<QRectF>() << QRectF(-1000, -1000, 1, 1) << QRectF(-10, -10, 10, 10))
512         << transformationMatrix
513         << QRectF(50, -2050, 2000, 2000);
514
515     QList<QRectF> all;
516     for (int i = 0; i < 256; ++i)
517         all << QRectF(randomX[i], randomY[i], 10, 10);
518     QTest::newRow("all")
519         << all
520         << QMatrix()
521         << QRectF(-980, -994, 1988, 1983);
522     QTest::newRow("transformed all")
523         << all
524         << transformationMatrix
525         << QRectF(-1928, -2010, 3966, 3976);
526 }
527
528 void tst_QGraphicsScene::itemsBoundingRect()
529 {
530     QFETCH(QList<QRectF>, rects);
531     QFETCH(QMatrix, matrix);
532     QFETCH(QRectF, boundingRect);
533
534     QGraphicsScene scene;
535
536     foreach (QRectF rect, rects) {
537         QPainterPath path;
538         path.addRect(rect);
539         QGraphicsPathItem *item = scene.addPath(path);
540         item->setPen(QPen(Qt::black, 0));
541         item->setMatrix(matrix);
542     }
543
544     QCOMPARE(scene.itemsBoundingRect(), boundingRect);
545 }
546
547 void tst_QGraphicsScene::items_QPointF_data()
548 {
549     QTest::addColumn<QList<QRectF> >("items");
550     QTest::addColumn<QPointF>("point");
551     QTest::addColumn<QList<int> >("itemsAtPoint");
552
553     QTest::newRow("empty")
554         << QList<QRectF>()
555         << QPointF()
556         << QList<int>();
557     QTest::newRow("1")
558         << (QList<QRectF>() << QRectF(0, 0, 10, 10))
559         << QPointF(0, 0)
560         << (QList<int>() << 0);
561     QTest::newRow("2")
562         << (QList<QRectF>() << QRectF(0, 0, 10, 10))
563         << QPointF(5, 5)
564         << (QList<int>() << 0);
565     QTest::newRow("3")
566         << (QList<QRectF>() << QRectF(0, 0, 10, 10))
567         << QPointF(9.9, 9.9)
568         << (QList<int>() << 0);
569     QTest::newRow("3.5")
570         << (QList<QRectF>() << QRectF(0, 0, 10, 10))
571         << QPointF(10, 10)
572         << QList<int>();
573     QTest::newRow("4")
574         << (QList<QRectF>() << QRectF(0, 0, 10, 10) << QRectF(9.9, 9.9, 10, 10))
575         << QPointF(9.9, 9.9)
576         << (QList<int>() << 1 << 0);
577     QTest::newRow("4.5")
578         << (QList<QRectF>() << QRectF(0, 0, 10, 10) << QRectF(10, 10, 10, 10))
579         << QPointF(10, 10)
580         << (QList<int>() << 1);
581     QTest::newRow("5")
582         << (QList<QRectF>() << QRectF(5, 5, 10, 10) << QRectF(10, 10, 10, 10))
583         << QPointF(10, 10)
584         << (QList<int>() << 1 << 0);
585     QTest::newRow("6")
586         << (QList<QRectF>() << QRectF(5, 5, 10, 10) << QRectF(10, 10, 10, 10) << QRectF(0, 0, 20, 30))
587         << QPointF(10, 10)
588         << (QList<int>() << 2 << 1 << 0);
589 }
590
591 void tst_QGraphicsScene::items_QPointF()
592 {
593     QFETCH(QList<QRectF>, items);
594     QFETCH(QPointF, point);
595     QFETCH(QList<int>, itemsAtPoint);
596
597     QGraphicsScene scene;
598
599     int n = 0;
600     QList<QGraphicsItem *> addedItems;
601     foreach(QRectF rect, items) {
602         QPainterPath path;
603         path.addRect(0, 0, rect.width(), rect.height());
604
605         QGraphicsPathItem *item = scene.addPath(path);
606         item->setPen(QPen(Qt::black, 0));
607         item->setZValue(n++);
608         item->setPos(rect.topLeft());
609         addedItems << item;
610     }
611
612     QList<int> itemIndexes;
613     foreach (QGraphicsItem *item, scene.items(point))
614         itemIndexes << addedItems.indexOf(item);
615
616     QCOMPARE(itemIndexes, itemsAtPoint);
617 }
618
619 void tst_QGraphicsScene::items_QRectF()
620 {
621     QGraphicsScene scene;
622     QGraphicsItem *item1 = scene.addRect(QRectF(-10, -10, 10, 10));
623     QGraphicsItem *item2 = scene.addRect(QRectF(10, -10, 10, 10));
624     QGraphicsItem *item3 = scene.addRect(QRectF(10, 10, 10, 10));
625     QGraphicsItem *item4 = scene.addRect(QRectF(-10, 10, 10, 10));
626
627     item1->setZValue(0);
628     item2->setZValue(1);
629     item3->setZValue(2);
630     item4->setZValue(3);
631
632     QCOMPARE(scene.items(QRectF(-10, -10, 10, 10)), QList<QGraphicsItem *>() << item1);
633     QCOMPARE(scene.items(QRectF(10, -10, 10, 10)), QList<QGraphicsItem *>() << item2);
634     QCOMPARE(scene.items(QRectF(10, 10, 10, 10)), QList<QGraphicsItem *>() << item3);
635     QCOMPARE(scene.items(QRectF(-10, 10, 10, 10)), QList<QGraphicsItem *>() << item4);
636     QCOMPARE(scene.items(QRectF(-10, -10, 1, 1)), QList<QGraphicsItem *>() << item1);
637     QCOMPARE(scene.items(QRectF(10, -10, 1, 1)), QList<QGraphicsItem *>() << item2);
638     QCOMPARE(scene.items(QRectF(10, 10, 1, 1)), QList<QGraphicsItem *>() << item3);
639     QCOMPARE(scene.items(QRectF(-10, 10, 1, 1)), QList<QGraphicsItem *>() << item4);
640
641     QCOMPARE(scene.items(QRectF(-10, -10, 40, 10)), QList<QGraphicsItem *>() << item2 << item1);
642     QCOMPARE(scene.items(QRectF(-10, 10, 40, 10)), QList<QGraphicsItem *>() << item4 << item3);
643
644     item1->setZValue(3);
645     item2->setZValue(2);
646     item3->setZValue(1);
647     item4->setZValue(0);
648
649     QCOMPARE(scene.items(QRectF(-10, -10, 40, 10)), QList<QGraphicsItem *>() << item1 << item2);
650     QCOMPARE(scene.items(QRectF(-10, 10, 40, 10)), QList<QGraphicsItem *>() << item3 << item4);
651 }
652
653 void tst_QGraphicsScene::items_QRectF_2_data()
654 {
655     QTest::addColumn<QRectF>("ellipseRect");
656     QTest::addColumn<QRectF>("sceneRect");
657     QTest::addColumn<Qt::ItemSelectionMode>("selectionMode");
658     QTest::addColumn<bool>("contained");
659     QTest::addColumn<bool>("containedRotated");
660
661     // None of the rects contain the ellipse's shape nor bounding rect
662     QTest::newRow("1") << QRectF(0, 0, 100, 100) << QRectF(1, 1, 10, 10) << Qt::ContainsItemShape << false << false;
663     QTest::newRow("2") << QRectF(0, 0, 100, 100) << QRectF(1, 89, 10, 10) << Qt::ContainsItemShape << false << false;
664     QTest::newRow("3") << QRectF(0, 0, 100, 100) << QRectF(89, 1, 10, 10) << Qt::ContainsItemShape << false << false;
665     QTest::newRow("4") << QRectF(0, 0, 100, 100) << QRectF(89, 89, 10, 10) << Qt::ContainsItemShape << false << false;
666     QTest::newRow("5") << QRectF(0, 0, 100, 100) << QRectF(1, 1, 10, 10) << Qt::ContainsItemBoundingRect << false << false;
667     QTest::newRow("6") << QRectF(0, 0, 100, 100) << QRectF(1, 89, 10, 10) << Qt::ContainsItemBoundingRect << false << false;
668     QTest::newRow("7") << QRectF(0, 0, 100, 100) << QRectF(89, 1, 10, 10) << Qt::ContainsItemBoundingRect << false << false;
669     QTest::newRow("8") << QRectF(0, 0, 100, 100) << QRectF(89, 89, 10, 10) << Qt::ContainsItemBoundingRect << false << false;
670     QTest::newRow("9") << QRectF(0, 0, 100, 100) << QRectF(0, 0, 50, 50) << Qt::ContainsItemShape << false << false;
671     QTest::newRow("10") << QRectF(0, 0, 100, 100) << QRectF(0, 50, 50, 50) << Qt::ContainsItemShape << false << false;
672     QTest::newRow("11") << QRectF(0, 0, 100, 100) << QRectF(50, 0, 50, 50) << Qt::ContainsItemShape << false << false;
673     QTest::newRow("12") << QRectF(0, 0, 100, 100) << QRectF(50, 50, 50, 50) << Qt::ContainsItemShape << false << false;
674     QTest::newRow("13") << QRectF(0, 0, 100, 100) << QRectF(0, 0, 50, 50) << Qt::ContainsItemBoundingRect << false << false;
675     QTest::newRow("14") << QRectF(0, 0, 100, 100) << QRectF(0, 50, 50, 50) << Qt::ContainsItemBoundingRect << false << false;
676     QTest::newRow("15") << QRectF(0, 0, 100, 100) << QRectF(50, 0, 50, 50) << Qt::ContainsItemBoundingRect << false << false;
677     QTest::newRow("16") << QRectF(0, 0, 100, 100) << QRectF(50, 50, 50, 50) << Qt::ContainsItemBoundingRect << false << false;
678     QTest::newRow("17") << QRectF(0, 0, 100, 100) << QRectF(-50, -50, 100, 100) << Qt::ContainsItemShape << false << false;
679     QTest::newRow("18") << QRectF(0, 0, 100, 100) << QRectF(0, -50, 100, 100) << Qt::ContainsItemShape << false << false;
680     QTest::newRow("19") << QRectF(0, 0, 100, 100) << QRectF(-50, 0, 100, 100) << Qt::ContainsItemShape << false << false;
681     QTest::newRow("20") << QRectF(0, 0, 100, 100) << QRectF(0, 0, 100, 100) << Qt::ContainsItemShape << false << false;
682     QTest::newRow("21") << QRectF(0, 0, 100, 100) << QRectF(-50, -50, 100, 100) << Qt::ContainsItemBoundingRect << false << false;
683     QTest::newRow("22") << QRectF(0, 0, 100, 100) << QRectF(0, -50, 100, 100) << Qt::ContainsItemBoundingRect << false << false;
684     QTest::newRow("23") << QRectF(0, 0, 100, 100) << QRectF(-50, 0, 100, 100) << Qt::ContainsItemBoundingRect << false << false;
685
686     // The rect is the same as the ellipse's bounding rect
687     QTest::newRow("24") << QRectF(0, 0, 100, 100) << QRectF(0, 0, 100, 100) << Qt::ContainsItemBoundingRect << false << false;
688
689     // None intersects with the item's shape, but they all intersects with the
690     // item's bounding rect.
691     QTest::newRow("25") << QRectF(0, 0, 100, 100) << QRectF(1, 1, 10, 10) << Qt::IntersectsItemShape << false << false;
692     QTest::newRow("26") << QRectF(0, 0, 100, 100) << QRectF(1, 89, 10, 10) << Qt::IntersectsItemShape << false << true;
693     QTest::newRow("27") << QRectF(0, 0, 100, 100) << QRectF(89, 1, 10, 10) << Qt::IntersectsItemShape << false << false;
694     QTest::newRow("28") << QRectF(0, 0, 100, 100) << QRectF(89, 89, 10, 10) << Qt::IntersectsItemShape << false << false;
695     QTest::newRow("29") << QRectF(0, 0, 100, 100) << QRectF(1, 1, 10, 10) << Qt::IntersectsItemBoundingRect << true << true;
696     QTest::newRow("30") << QRectF(0, 0, 100, 100) << QRectF(1, 89, 10, 10) << Qt::IntersectsItemBoundingRect << true << true;
697     QTest::newRow("31") << QRectF(0, 0, 100, 100) << QRectF(89, 1, 10, 10) << Qt::IntersectsItemBoundingRect << true << false;
698     QTest::newRow("32") << QRectF(0, 0, 100, 100) << QRectF(89, 89, 10, 10) << Qt::IntersectsItemBoundingRect << true << false;
699
700     // This rect does not contain the shape nor the bounding rect
701     QTest::newRow("33") << QRectF(0, 0, 100, 100) << QRectF(5, 5, 90, 90) << Qt::ContainsItemShape << false << false;
702     QTest::newRow("34") << QRectF(0, 0, 100, 100) << QRectF(5, 5, 90, 90) << Qt::ContainsItemBoundingRect << false << false;
703
704     // It will, however, intersect with both
705     QTest::newRow("35") << QRectF(0, 0, 100, 100) << QRectF(5, 5, 90, 90) << Qt::IntersectsItemShape << true << true;
706     QTest::newRow("36") << QRectF(0, 0, 100, 100) << QRectF(5, 5, 90, 90) << Qt::IntersectsItemBoundingRect << true << true;
707
708     // A rect that contains the whole ellipse will both contain and intersect
709     // with both the ellipse's shape and bounding rect.
710     QTest::newRow("37") << QRectF(0, 0, 100, 100) << QRectF(-5, -5, 110, 110) << Qt::IntersectsItemBoundingRect << true << true;
711     QTest::newRow("38") << QRectF(0, 0, 100, 100) << QRectF(-5, -5, 110, 110) << Qt::IntersectsItemShape << true << true;
712     QTest::newRow("39") << QRectF(0, 0, 100, 100) << QRectF(-5, -5, 110, 110) << Qt::ContainsItemBoundingRect << true << false;
713     QTest::newRow("40") << QRectF(0, 0, 100, 100) << QRectF(-5, -5, 110, 110) << Qt::ContainsItemShape << true << false;
714
715     // A rect that is fully contained within the ellipse will intersect only
716     QTest::newRow("41") << QRectF(0, 0, 100, 100) << QRectF(40, 40, 20, 20) << Qt::ContainsItemShape << false << false;
717     QTest::newRow("42") << QRectF(0, 0, 100, 100) << QRectF(40, 40, 20, 20) << Qt::ContainsItemBoundingRect << false << false;
718     QTest::newRow("43") << QRectF(0, 0, 100, 100) << QRectF(40, 40, 20, 20) << Qt::IntersectsItemShape << true << true;
719     QTest::newRow("44") << QRectF(0, 0, 100, 100) << QRectF(40, 40, 20, 20) << Qt::IntersectsItemBoundingRect << true << true;
720 }
721
722 void tst_QGraphicsScene::items_QRectF_2()
723 {
724     QFETCH(QRectF, ellipseRect);
725     QFETCH(QRectF, sceneRect);
726     QFETCH(Qt::ItemSelectionMode, selectionMode);
727     QFETCH(bool, contained);
728     QFETCH(bool, containedRotated);
729
730     QGraphicsScene scene;
731     QGraphicsItem *item = scene.addEllipse(ellipseRect);
732
733     QCOMPARE(!scene.items(sceneRect, selectionMode).isEmpty(), contained);
734     item->rotate(45);
735     QCOMPARE(!scene.items(sceneRect, selectionMode).isEmpty(), containedRotated);
736 }
737
738 void tst_QGraphicsScene::items_QPolygonF()
739 {
740     QGraphicsScene scene;
741     QGraphicsItem *item1 = scene.addRect(QRectF(-10, -10, 10, 10));
742     QGraphicsItem *item2 = scene.addRect(QRectF(10, -10, 10, 10));
743     QGraphicsItem *item3 = scene.addRect(QRectF(10, 10, 10, 10));
744     QGraphicsItem *item4 = scene.addRect(QRectF(-10, 10, 10, 10));
745
746     item1->setZValue(0);
747     item2->setZValue(1);
748     item3->setZValue(2);
749     item4->setZValue(3);
750
751     QPolygonF poly1(item1->boundingRect());
752     QPolygonF poly2(item2->boundingRect());
753     QPolygonF poly3(item3->boundingRect());
754     QPolygonF poly4(item4->boundingRect());
755
756     QCOMPARE(scene.items(poly1), QList<QGraphicsItem *>() << item1);
757     QCOMPARE(scene.items(poly2), QList<QGraphicsItem *>() << item2);
758     QCOMPARE(scene.items(poly3), QList<QGraphicsItem *>() << item3);
759     QCOMPARE(scene.items(poly4), QList<QGraphicsItem *>() << item4);
760
761     poly1 = QPolygonF(QRectF(-10, -10, 1, 1));
762     poly2 = QPolygonF(QRectF(10, -10, 1, 1));
763     poly3 = QPolygonF(QRectF(10, 10, 1, 1));
764     poly4 = QPolygonF(QRectF(-10, 10, 1, 1));
765
766     QCOMPARE(scene.items(poly1), QList<QGraphicsItem *>() << item1);
767     QCOMPARE(scene.items(poly2), QList<QGraphicsItem *>() << item2);
768     QCOMPARE(scene.items(poly3), QList<QGraphicsItem *>() << item3);
769     QCOMPARE(scene.items(poly4), QList<QGraphicsItem *>() << item4);
770
771     poly1 = QPolygonF(QRectF(-10, -10, 40, 10));
772     poly2 = QPolygonF(QRectF(-10, 10, 40, 10));
773
774     QCOMPARE(scene.items(poly1), QList<QGraphicsItem *>() << item2 << item1);
775     QCOMPARE(scene.items(poly2), QList<QGraphicsItem *>() << item4 << item3);
776
777     item1->setZValue(3);
778     item2->setZValue(2);
779     item3->setZValue(1);
780     item4->setZValue(0);
781
782     QCOMPARE(scene.items(poly1), QList<QGraphicsItem *>() << item1 << item2);
783     QCOMPARE(scene.items(poly2), QList<QGraphicsItem *>() << item3 << item4);
784 }
785
786 void tst_QGraphicsScene::items_QPolygonF_2()
787 {
788     QGraphicsScene scene;
789     QGraphicsItem *ellipse = scene.addEllipse(QRectF(0, 0, 100, 100));
790
791     // None of the rects contain the ellipse's shape nor bounding rect
792     QVERIFY(scene.items(QPolygonF(QRectF(1, 1, 10, 10)), Qt::ContainsItemShape).isEmpty());
793     QVERIFY(scene.items(QPolygonF(QRectF(1, 89, 10, 10)), Qt::ContainsItemShape).isEmpty());
794     QVERIFY(scene.items(QPolygonF(QRectF(89, 1, 10, 10)), Qt::ContainsItemShape).isEmpty());
795     QVERIFY(scene.items(QPolygonF(QRectF(89, 89, 10, 10)), Qt::ContainsItemShape).isEmpty());
796     QVERIFY(scene.items(QPolygonF(QRectF(1, 1, 10, 10)), Qt::ContainsItemBoundingRect).isEmpty());
797     QVERIFY(scene.items(QPolygonF(QRectF(1, 89, 10, 10)), Qt::ContainsItemBoundingRect).isEmpty());
798     QVERIFY(scene.items(QPolygonF(QRectF(89, 1, 10, 10)), Qt::ContainsItemBoundingRect).isEmpty());
799     QVERIFY(scene.items(QPolygonF(QRectF(89, 89, 10, 10)), Qt::ContainsItemBoundingRect).isEmpty());
800
801     // None intersects with the item's shape, but they all intersects with the
802     // item's bounding rect.
803     QVERIFY(scene.items(QPolygonF(QRectF(1, 1, 10, 10)), Qt::IntersectsItemShape).isEmpty());
804     QVERIFY(scene.items(QPolygonF(QRectF(1, 89, 10, 10)), Qt::IntersectsItemShape).isEmpty());
805     QVERIFY(scene.items(QPolygonF(QRectF(89, 1, 10, 10)), Qt::IntersectsItemShape).isEmpty());
806     QVERIFY(scene.items(QPolygonF(QRectF(89, 89, 10, 10)), Qt::IntersectsItemShape).isEmpty());
807     QCOMPARE(scene.items(QPolygonF(QRectF(1, 1, 10, 10)), Qt::IntersectsItemBoundingRect).first(), ellipse);
808     QCOMPARE(scene.items(QPolygonF(QRectF(1, 89, 10, 10)), Qt::IntersectsItemBoundingRect).first(), ellipse);
809     QCOMPARE(scene.items(QPolygonF(QRectF(89, 1, 10, 10)), Qt::IntersectsItemBoundingRect).first(), ellipse);
810     QCOMPARE(scene.items(QPolygonF(QRectF(89, 89, 10, 10)), Qt::IntersectsItemBoundingRect).first(), ellipse);
811
812     // This rect does not contain the shape nor the bounding rect
813     QVERIFY(scene.items(QPolygonF(QRectF(5, 5, 90, 90)), Qt::ContainsItemShape).isEmpty());
814     QVERIFY(scene.items(QPolygonF(QRectF(5, 5, 90, 90)), Qt::ContainsItemBoundingRect).isEmpty());
815
816     // It will, however, intersect with both
817     QCOMPARE(scene.items(QPolygonF(QRectF(5, 5, 90, 90)), Qt::IntersectsItemShape).first(), ellipse);
818     QCOMPARE(scene.items(QPolygonF(QRectF(5, 5, 90, 90)), Qt::IntersectsItemBoundingRect).first(), ellipse);
819
820     // A rect that contains the whole ellipse will both contain and intersect
821     // with both the ellipse's shape and bounding rect.
822     QCOMPARE(scene.items(QPolygonF(QRectF(-5, -5, 110, 110)), Qt::IntersectsItemShape).first(), ellipse);
823     QCOMPARE(scene.items(QPolygonF(QRectF(-5, -5, 110, 110)), Qt::ContainsItemShape).first(), ellipse);
824     QCOMPARE(scene.items(QPolygonF(QRectF(-5, -5, 110, 110)), Qt::IntersectsItemBoundingRect).first(), ellipse);
825     QCOMPARE(scene.items(QPolygonF(QRectF(-5, -5, 110, 110)), Qt::ContainsItemBoundingRect).first(), ellipse);
826 }
827
828 void tst_QGraphicsScene::items_QPainterPath()
829 {
830     QGraphicsScene scene;
831     QGraphicsItem *item1 = scene.addRect(QRectF(-10, -10, 10, 10));
832     QGraphicsItem *item2 = scene.addRect(QRectF(10, -10, 10, 10));
833     QGraphicsItem *item3 = scene.addRect(QRectF(10, 10, 10, 10));
834     QGraphicsItem *item4 = scene.addRect(QRectF(-10, 10, 10, 10));
835
836     item1->setZValue(0);
837     item2->setZValue(1);
838     item3->setZValue(2);
839     item4->setZValue(3);
840
841     QPainterPath path1; path1.addEllipse(item1->boundingRect());
842     QPainterPath path2; path2.addEllipse(item2->boundingRect());
843     QPainterPath path3; path3.addEllipse(item3->boundingRect());
844     QPainterPath path4; path4.addEllipse(item4->boundingRect());
845
846     QCOMPARE(scene.items(path1), QList<QGraphicsItem *>() << item1);
847     QCOMPARE(scene.items(path2), QList<QGraphicsItem *>() << item2);
848     QCOMPARE(scene.items(path3), QList<QGraphicsItem *>() << item3);
849     QCOMPARE(scene.items(path4), QList<QGraphicsItem *>() << item4);
850
851     path1 = QPainterPath(); path1.addEllipse(QRectF(-10, -10, 1, 1));
852     path2 = QPainterPath(); path2.addEllipse(QRectF(10, -10, 1, 1));
853     path3 = QPainterPath(); path3.addEllipse(QRectF(10, 10, 1, 1));
854     path4 = QPainterPath(); path4.addEllipse(QRectF(-10, 10, 1, 1));
855
856     QCOMPARE(scene.items(path1), QList<QGraphicsItem *>() << item1);
857     QCOMPARE(scene.items(path2), QList<QGraphicsItem *>() << item2);
858     QCOMPARE(scene.items(path3), QList<QGraphicsItem *>() << item3);
859     QCOMPARE(scene.items(path4), QList<QGraphicsItem *>() << item4);
860
861     path1 = QPainterPath(); path1.addRect(QRectF(-10, -10, 40, 10));
862     path2 = QPainterPath(); path2.addRect(QRectF(-10, 10, 40, 10));
863
864     QCOMPARE(scene.items(path1), QList<QGraphicsItem *>() << item2 << item1);
865     QCOMPARE(scene.items(path2), QList<QGraphicsItem *>() << item4 << item3);
866
867     item1->setZValue(3);
868     item2->setZValue(2);
869     item3->setZValue(1);
870     item4->setZValue(0);
871
872     QCOMPARE(scene.items(path1), QList<QGraphicsItem *>() << item1 << item2);
873     QCOMPARE(scene.items(path2), QList<QGraphicsItem *>() << item3 << item4);
874 }
875
876 void tst_QGraphicsScene::items_QPainterPath_2()
877 {
878     QGraphicsScene scene;
879     QGraphicsItem *ellipse = scene.addEllipse(QRectF(0, 0, 100, 100));
880
881     QPainterPath p1; p1.addRect(QRectF(1, 1, 10, 10));
882     QPainterPath p2; p2.addRect(QRectF(1, 89, 10, 10));
883     QPainterPath p3; p3.addRect(QRectF(89, 1, 10, 10));
884     QPainterPath p4; p4.addRect(QRectF(89, 89, 10, 10));
885
886     // None of the rects contain the ellipse's shape nor bounding rect
887     QVERIFY(scene.items(p1, Qt::ContainsItemShape).isEmpty());
888     QVERIFY(scene.items(p2, Qt::ContainsItemShape).isEmpty());
889     QVERIFY(scene.items(p3, Qt::ContainsItemShape).isEmpty());
890     QVERIFY(scene.items(p4, Qt::ContainsItemShape).isEmpty());
891     QVERIFY(scene.items(p1, Qt::ContainsItemBoundingRect).isEmpty());
892     QVERIFY(scene.items(p2, Qt::ContainsItemBoundingRect).isEmpty());
893     QVERIFY(scene.items(p3, Qt::ContainsItemBoundingRect).isEmpty());
894     QVERIFY(scene.items(p4, Qt::ContainsItemBoundingRect).isEmpty());
895
896     // None intersects with the item's shape, but they all intersects with the
897     // item's bounding rect.
898     QVERIFY(scene.items(p1, Qt::IntersectsItemShape).isEmpty());
899     QVERIFY(scene.items(p2, Qt::IntersectsItemShape).isEmpty());
900     QVERIFY(scene.items(p3, Qt::IntersectsItemShape).isEmpty());
901     QVERIFY(scene.items(p4, Qt::IntersectsItemShape).isEmpty());
902     QCOMPARE(scene.items(p1, Qt::IntersectsItemBoundingRect).first(), ellipse);
903     QCOMPARE(scene.items(p2, Qt::IntersectsItemBoundingRect).first(), ellipse);
904     QCOMPARE(scene.items(p3, Qt::IntersectsItemBoundingRect).first(), ellipse);
905     QCOMPARE(scene.items(p4, Qt::IntersectsItemBoundingRect).first(), ellipse);
906
907     QPainterPath p5;
908     p5.addRect(QRectF(5, 5, 90, 90));
909
910     // This rect does not contain the shape nor the bounding rect
911     QVERIFY(scene.items(p5, Qt::ContainsItemShape).isEmpty());
912     QVERIFY(scene.items(p5, Qt::ContainsItemBoundingRect).isEmpty());
913
914     // It will, however, intersect with both
915     QCOMPARE(scene.items(p5, Qt::IntersectsItemShape).first(), ellipse);
916     QCOMPARE(scene.items(p5, Qt::IntersectsItemBoundingRect).first(), ellipse);
917
918     QPainterPath p6;
919     p6.addRect(QRectF(-5, -5, 110, 110));
920
921     // A rect that contains the whole ellipse will both contain and intersect
922     // with both the ellipse's shape and bounding rect.
923     QCOMPARE(scene.items(p6, Qt::IntersectsItemShape).first(), ellipse);
924     QCOMPARE(scene.items(p6, Qt::ContainsItemShape).first(), ellipse);
925     QCOMPARE(scene.items(p6, Qt::IntersectsItemBoundingRect).first(), ellipse);
926     QCOMPARE(scene.items(p6, Qt::ContainsItemBoundingRect).first(), ellipse);
927 }
928
929 class CustomView : public QGraphicsView
930 {
931 public:
932     CustomView() : repaints(0)
933     { }
934
935     int repaints;
936 protected:
937     void paintEvent(QPaintEvent *event)
938     {
939         ++repaints;
940         QGraphicsView::paintEvent(event);
941     }
942 };
943
944 void tst_QGraphicsScene::selectionChanged()
945 {
946     QGraphicsScene scene(0, 0, 1000, 1000);
947     QSignalSpy spy(&scene, SIGNAL(selectionChanged()));
948     QCOMPARE(spy.count(), 0);
949
950     QPainterPath path;
951     path.addRect(scene.sceneRect());
952     QCOMPARE(scene.selectionArea(), QPainterPath());
953     scene.setSelectionArea(path);
954     QCOMPARE(scene.selectionArea(), path);
955     QCOMPARE(spy.count(), 0); // selection didn't change
956     QVERIFY(scene.selectedItems().isEmpty());
957
958     QGraphicsItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
959     QCOMPARE(spy.count(), 0); // selection didn't change
960
961     rect->setSelected(true);
962     QVERIFY(!rect->isSelected());
963     QCOMPARE(spy.count(), 0); // selection didn't change, item isn't selectable
964
965     rect->setFlag(QGraphicsItem::ItemIsSelectable);
966     rect->setSelected(true);
967     QVERIFY(rect->isSelected());
968     QCOMPARE(spy.count(), 1); // selection changed
969     QCOMPARE(scene.selectedItems(), QList<QGraphicsItem *>() << rect);
970
971     rect->setSelected(false);
972     QVERIFY(!rect->isSelected());
973     QCOMPARE(spy.count(), 2); // selection changed
974     QVERIFY(scene.selectedItems().isEmpty());
975
976     QGraphicsEllipseItem *parentItem = new QGraphicsEllipseItem(QRectF(0, 0, 100, 100));
977     QGraphicsEllipseItem *childItem = new QGraphicsEllipseItem(QRectF(0, 0, 100, 100), parentItem);
978     QGraphicsEllipseItem *grandChildItem = new QGraphicsEllipseItem(QRectF(0, 0, 100, 100), childItem);
979     grandChildItem->setFlag(QGraphicsItem::ItemIsSelectable);
980     grandChildItem->setSelected(true);
981     grandChildItem->setSelected(false);
982     grandChildItem->setSelected(true);
983     scene.addItem(parentItem);
984
985     QCOMPARE(spy.count(), 3); // the grandchild was added, so the selection changed once
986
987     scene.removeItem(parentItem);
988     QCOMPARE(spy.count(), 4); // the grandchild was removed, so the selection changed
989
990     rect->setSelected(true);
991     QCOMPARE(spy.count(), 5); // the rect was reselected, so the selection changed
992
993     scene.clearSelection();
994     QCOMPARE(spy.count(), 6); // the scene selection was cleared
995
996     rect->setSelected(true);
997     QCOMPARE(spy.count(), 7); // the rect was reselected, so the selection changed
998
999     rect->setFlag(QGraphicsItem::ItemIsSelectable, false);
1000     QCOMPARE(spy.count(), 8); // the rect was unselected, so the selection changed
1001
1002     rect->setSelected(true);
1003     QCOMPARE(spy.count(), 8); // the rect is not longer selectable, so the selection does not change
1004
1005
1006     rect->setFlag(QGraphicsItem::ItemIsSelectable, true);
1007     rect->setSelected(true);
1008     QCOMPARE(spy.count(), 9); // the rect is again selectable, so the selection changed
1009
1010     delete rect;
1011     QCOMPARE(spy.count(), 10); // a selected item was deleted; selection changed
1012 }
1013
1014 void tst_QGraphicsScene::selectionChanged2()
1015 {
1016     QGraphicsScene scene;
1017     QSignalSpy spy(&scene, SIGNAL(selectionChanged()));
1018
1019     QGraphicsItem *item1 = scene.addRect(0, 0, 100, 100);
1020     QGraphicsItem *item2 = scene.addRect(100, 100, 100, 100);
1021     item1->setFlag(QGraphicsItem::ItemIsSelectable);
1022     item2->setFlag(QGraphicsItem::ItemIsSelectable);
1023
1024     QCOMPARE(spy.count(), 0);
1025     {
1026         QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
1027         event.setScenePos(QPointF(50, 50));
1028         event.setButton(Qt::LeftButton);
1029         qApp->sendEvent(&scene, &event);
1030     }
1031     {
1032         QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
1033         event.setScenePos(QPointF(50, 50));
1034         event.setButton(Qt::LeftButton);
1035         qApp->sendEvent(&scene, &event);
1036     }
1037     QVERIFY(item1->isSelected());
1038     QVERIFY(!item2->isSelected());
1039     QCOMPARE(spy.count(), 1);
1040     {
1041         QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
1042         event.setScenePos(QPointF(150, 150));
1043         event.setButton(Qt::LeftButton);
1044         qApp->sendEvent(&scene, &event);
1045     }
1046     {
1047         QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
1048         event.setScenePos(QPointF(150, 150));
1049         event.setButton(Qt::LeftButton);
1050         qApp->sendEvent(&scene, &event);
1051     }
1052     QVERIFY(!item1->isSelected());
1053     QVERIFY(item2->isSelected());
1054     QCOMPARE(spy.count(), 2);
1055     {
1056         QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
1057         event.setScenePos(QPointF(50, 50));
1058         event.setButton(Qt::LeftButton);
1059         event.setModifiers(Qt::ControlModifier);
1060         qApp->sendEvent(&scene, &event);
1061     }
1062     QVERIFY(!item1->isSelected());
1063     QVERIFY(item2->isSelected());
1064     QCOMPARE(spy.count(), 2);
1065     {
1066         QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
1067         event.setScenePos(QPointF(50, 50));
1068         event.setButton(Qt::LeftButton);
1069         qApp->sendEvent(&scene, &event);
1070     }
1071     QVERIFY(item1->isSelected());
1072     QVERIFY(!item2->isSelected());
1073     QCOMPARE(spy.count(), 3);
1074 }
1075
1076 void tst_QGraphicsScene::addItem()
1077 {
1078     Q_CHECK_PAINTEVENTS
1079     {
1080         // 1) Create item, then scene, then add item
1081         QGraphicsItem *path = new QGraphicsEllipseItem(QRectF(-10, -10, 20, 20));
1082         QGraphicsScene scene;
1083
1084         CustomView view;
1085         view.setScene(&scene);
1086         view.show();
1087         QVERIFY(QTest::qWaitForWindowExposed(&view));
1088         qApp->processEvents();
1089         view.repaints = 0;
1090
1091         scene.addItem(path);
1092
1093         // Adding an item should always issue a repaint.
1094         QTRY_VERIFY(view.repaints > 0);
1095         view.repaints = 0;
1096
1097         QCOMPARE(scene.itemAt(0, 0), path);
1098
1099         QGraphicsItem *path2 = new QGraphicsEllipseItem(QRectF(-10, -10, 20, 20));
1100         path2->setPos(100, 100);
1101
1102         QCOMPARE(scene.itemAt(0, 0), path);
1103         QCOMPARE(scene.itemAt(100, 100), (QGraphicsItem *)0);
1104         scene.addItem(path2);
1105
1106         // Adding an item should always issue a repaint.
1107         QTRY_VERIFY(view.repaints > 0);
1108
1109         QCOMPARE(scene.itemAt(100, 100), path2);
1110     }
1111     {
1112         // 2) Create scene, then item, then add item
1113         QGraphicsScene scene;
1114         QGraphicsItem *path = new QGraphicsEllipseItem(QRectF(-10, -10, 20, 20));
1115         scene.addItem(path);
1116
1117         QGraphicsItem *path2 = new QGraphicsEllipseItem(QRectF(-10, -10, 20, 20));
1118         path2->setPos(100, 100);
1119         scene.addItem(path2);
1120
1121         QCOMPARE(scene.itemAt(0, 0), path);
1122         QCOMPARE(scene.itemAt(100, 100), path2);
1123     }
1124 }
1125
1126 void tst_QGraphicsScene::addEllipse()
1127 {
1128     QGraphicsScene scene;
1129     QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20),
1130                                                      QPen(Qt::red), QBrush(Qt::blue));
1131     QCOMPARE(ellipse->pos(), QPointF());
1132     QCOMPARE(ellipse->pen(), QPen(Qt::red));
1133     QCOMPARE(ellipse->brush(), QBrush(Qt::blue));
1134     QCOMPARE(ellipse->rect(), QRectF(-10, -10, 20, 20));
1135     QCOMPARE(scene.itemAt(0, 0), (QGraphicsItem *)ellipse);
1136     QCOMPARE(scene.itemAt(-10, -10), (QGraphicsItem *)0);
1137     QCOMPARE(scene.itemAt(-9.9, 0), (QGraphicsItem *)ellipse);
1138     QCOMPARE(scene.itemAt(-10, 10), (QGraphicsItem *)0);
1139     QCOMPARE(scene.itemAt(0, -9.9), (QGraphicsItem *)ellipse);
1140     QCOMPARE(scene.itemAt(0, 9.9), (QGraphicsItem *)ellipse);
1141     QCOMPARE(scene.itemAt(10, -10), (QGraphicsItem *)0);
1142     QCOMPARE(scene.itemAt(9.9, 0), (QGraphicsItem *)ellipse);
1143     QCOMPARE(scene.itemAt(10, 10), (QGraphicsItem *)0);
1144 }
1145
1146 void tst_QGraphicsScene::addLine()
1147 {
1148     QGraphicsScene scene;
1149     QPen pen(Qt::red);
1150     pen.setWidthF(1.0);
1151     QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20),
1152                                             pen);
1153     QCOMPARE(line->pos(), QPointF());
1154     QCOMPARE(line->pen(), pen);
1155     QCOMPARE(line->line(), QLineF(-10, -10, 20, 20));
1156     QCOMPARE(scene.itemAt(0, 0), (QGraphicsItem *)line);
1157     QCOMPARE(scene.itemAt(-10, -10), (QGraphicsItem *)line);
1158     QCOMPARE(scene.itemAt(-9.9, 0), (QGraphicsItem *)0);
1159     QCOMPARE(scene.itemAt(-10, 10), (QGraphicsItem *)0);
1160     QCOMPARE(scene.itemAt(0, -9.9), (QGraphicsItem *)0);
1161     QCOMPARE(scene.itemAt(0, 9.9), (QGraphicsItem *)0);
1162     QCOMPARE(scene.itemAt(10, -10), (QGraphicsItem *)0);
1163     QCOMPARE(scene.itemAt(9.9, 0), (QGraphicsItem *)0);
1164     QCOMPARE(scene.itemAt(10, 10), (QGraphicsItem *)line);
1165 }
1166
1167 void tst_QGraphicsScene::addPath()
1168 {
1169     QGraphicsScene scene;
1170     QPainterPath p;
1171     p.addEllipse(QRectF(-10, -10, 20, 20));
1172     p.addEllipse(QRectF(-10, 20, 20, 20));
1173
1174     QGraphicsPathItem *path = scene.addPath(p, QPen(Qt::red), QBrush(Qt::blue));
1175     QCOMPARE(path->pos(), QPointF());
1176     QCOMPARE(path->pen(), QPen(Qt::red));
1177     QCOMPARE(path->path(), p);
1178     QCOMPARE(path->brush(), QBrush(Qt::blue));
1179
1180     path->setPen(QPen(Qt::red, 0));
1181
1182     QCOMPARE(scene.itemAt(0, 0), (QGraphicsItem *)path);
1183     QCOMPARE(scene.itemAt(-9.9, 0), (QGraphicsItem *)path);
1184     QCOMPARE(scene.itemAt(9.9, 0), (QGraphicsItem *)path);
1185     QCOMPARE(scene.itemAt(0, -9.9), (QGraphicsItem *)path);
1186     QCOMPARE(scene.itemAt(0, 9.9), (QGraphicsItem *)path);
1187     QCOMPARE(scene.itemAt(0, 30), (QGraphicsItem *)path);
1188     QCOMPARE(scene.itemAt(-9.9, 30), (QGraphicsItem *)path);
1189     QCOMPARE(scene.itemAt(9.9, 30), (QGraphicsItem *)path);
1190     QCOMPARE(scene.itemAt(0, 20.1), (QGraphicsItem *)path);
1191     QCOMPARE(scene.itemAt(0, 39.9), (QGraphicsItem *)path);
1192     QCOMPARE(scene.itemAt(-10, -10), (QGraphicsItem *)0);
1193     QCOMPARE(scene.itemAt(10, -10), (QGraphicsItem *)0);
1194     QCOMPARE(scene.itemAt(-10, 10), (QGraphicsItem *)0);
1195     QCOMPARE(scene.itemAt(10, 10), (QGraphicsItem *)0);
1196     QCOMPARE(scene.itemAt(-10, 20), (QGraphicsItem *)0);
1197     QCOMPARE(scene.itemAt(10, 20), (QGraphicsItem *)0);
1198 if (sizeof(qreal) != sizeof(double))
1199     QWARN("Skipping test because of rounding errors when qreal != double");
1200 else
1201     QCOMPARE(scene.itemAt(-10, 30), (QGraphicsItem *)0);
1202     QCOMPARE(scene.itemAt(10.1, 30), (QGraphicsItem *)0);
1203 }
1204
1205 void tst_QGraphicsScene::addPixmap()
1206 {
1207     QGraphicsScene scene;
1208     QPixmap pix(":/Ash_European.jpg");
1209     QGraphicsPixmapItem *pixmap = scene.addPixmap(pix);
1210
1211     QCOMPARE(pixmap->pos(), QPointF());
1212     QCOMPARE(pixmap->pixmap(), pix);
1213     QCOMPARE(scene.itemAt(0, 0), (QGraphicsItem *)pixmap);
1214     QCOMPARE(scene.itemAt(pix.width() - 1, 0), (QGraphicsItem *)pixmap);
1215     QCOMPARE(scene.itemAt(0, pix.height() - 1), (QGraphicsItem *)pixmap);
1216     QCOMPARE(scene.itemAt(pix.width() - 1, pix.height() - 1), (QGraphicsItem *)pixmap);
1217     QCOMPARE(scene.itemAt(-1, -1), (QGraphicsItem *)0);
1218     QCOMPARE(scene.itemAt(pix.width() - 1, -1), (QGraphicsItem *)0);
1219     QCOMPARE(scene.itemAt(-1, pix.height() - 1), (QGraphicsItem *)0);
1220     QCOMPARE(scene.itemAt(pix.width(), pix.height()), (QGraphicsItem *)0);
1221     QCOMPARE(scene.itemAt(0, pix.height()), (QGraphicsItem *)0);
1222     QCOMPARE(scene.itemAt(pix.width(), 0), (QGraphicsItem *)0);
1223 }
1224
1225 void tst_QGraphicsScene::addRect()
1226 {
1227     QGraphicsScene scene;
1228     QGraphicsRectItem *rect = scene.addRect(QRectF(-10, -10, 20, 20),
1229                                             QPen(Qt::red), QBrush(Qt::blue));
1230     QCOMPARE(rect->pos(), QPointF());
1231     QCOMPARE(rect->pen(), QPen(Qt::red));
1232     QCOMPARE(rect->brush(), QBrush(Qt::blue));
1233     QCOMPARE(rect->rect(), QRectF(-10, -10, 20, 20));
1234
1235     rect->setPen(QPen(Qt::red, 0));
1236
1237     QCOMPARE(scene.itemAt(0, 0), (QGraphicsItem *)rect);
1238     QCOMPARE(scene.itemAt(-10, -10), (QGraphicsItem *)rect);
1239     QCOMPARE(scene.itemAt(-9.9, 0), (QGraphicsItem *)rect);
1240     QCOMPARE(scene.itemAt(-10, 10), (QGraphicsItem *)0);
1241     QCOMPARE(scene.itemAt(0, -9.9), (QGraphicsItem *)rect);
1242     QCOMPARE(scene.itemAt(0, 9.9), (QGraphicsItem *)rect);
1243     QCOMPARE(scene.itemAt(10, -10), (QGraphicsItem *)0);
1244     QCOMPARE(scene.itemAt(9.9, 0), (QGraphicsItem *)rect);
1245     QCOMPARE(scene.itemAt(10, 10), (QGraphicsItem *)0);
1246 }
1247
1248 void tst_QGraphicsScene::addText()
1249 {
1250     QGraphicsScene scene;
1251     QGraphicsTextItem *text = scene.addText("Qt", QFont());
1252     QCOMPARE(text->pos(), QPointF());
1253     QCOMPARE(text->toPlainText(), QString("Qt"));
1254     QCOMPARE(text->font(), QFont());
1255 }
1256
1257 void tst_QGraphicsScene::removeItem()
1258 {
1259 #if defined(Q_OS_WINCE) && !defined(GWES_ICONCURS)
1260     QSKIP("No mouse cursor support");
1261 #endif
1262     QGraphicsScene scene;
1263     QGraphicsItem *item = scene.addRect(QRectF(0, 0, 10, 10));
1264     QCOMPARE(scene.itemAt(0, 0), item); // forces indexing
1265     scene.removeItem(item);
1266     QCOMPARE(scene.itemAt(0, 0), (QGraphicsItem *)0);
1267     delete item;
1268
1269     QGraphicsItem *item2 = scene.addRect(QRectF(0, 0, 10, 10));
1270     item2->setFlag(QGraphicsItem::ItemIsSelectable);
1271     QCOMPARE(scene.itemAt(0, 0), item2);
1272
1273     // Removing a selected item
1274     QVERIFY(scene.selectedItems().isEmpty());
1275     item2->setSelected(true);
1276     QVERIFY(scene.selectedItems().contains(item2));
1277     scene.removeItem(item2);
1278     QVERIFY(scene.selectedItems().isEmpty());
1279
1280     // Check that we are in a state that can receive paint events
1281     // (i.e., not logged out on Windows).
1282     Q_CHECK_PAINTEVENTS
1283
1284     // Removing a hovered item
1285     HoverItem *hoverItem = new HoverItem;
1286     scene.addItem(hoverItem);
1287     scene.setSceneRect(-50, -50, 100, 100);
1288
1289     QGraphicsView view(&scene);
1290     view.setFixedSize(150, 150);
1291     view.show();
1292     QApplication::setActiveWindow(&view);
1293     QVERIFY(QTest::qWaitForWindowActive(&view));
1294     QTest::mouseMove(view.viewport(), QPoint(-1, -1));
1295     {
1296         QMouseEvent moveEvent(QEvent::MouseMove, view.mapFromScene(hoverItem->scenePos() + QPointF(20, 20)), Qt::NoButton, 0, 0);
1297         QApplication::sendEvent(view.viewport(), &moveEvent);
1298     }
1299     qApp->processEvents(); // update
1300     qApp->processEvents(); // draw
1301     QVERIFY(!hoverItem->isHovered);
1302
1303     {
1304         QTest::qWait(250);
1305         QTest::mouseMove(view.viewport(), view.mapFromScene(hoverItem->scenePos()), Qt::NoButton);
1306         QTest::qWait(10);
1307         QMouseEvent moveEvent(QEvent::MouseMove, view.mapFromScene(hoverItem->scenePos()), Qt::NoButton, 0, 0);
1308         QApplication::sendEvent(view.viewport(), &moveEvent);
1309     }
1310     qApp->processEvents(); // update
1311     qApp->processEvents(); // draw
1312     QVERIFY(hoverItem->isHovered);
1313
1314     scene.removeItem(hoverItem);
1315     hoverItem->setAcceptsHoverEvents(false);
1316     scene.addItem(hoverItem);
1317     qApp->processEvents(); // <- delayed update is called
1318     qApp->processEvents(); // <- scene schedules pending update
1319     qApp->processEvents(); // <- pending update is sent to view
1320     QVERIFY(!hoverItem->isHovered);
1321 }
1322
1323 void tst_QGraphicsScene::focusItem()
1324 {
1325     QGraphicsScene scene;
1326     QEvent activate(QEvent::WindowActivate);
1327     QApplication::sendEvent(&scene, &activate);
1328
1329     QVERIFY(!scene.focusItem());
1330     QGraphicsItem *item = scene.addText("Qt");
1331     QVERIFY(!scene.focusItem());
1332     item->setFocus();
1333     QVERIFY(!scene.focusItem());
1334     item->setFlag(QGraphicsItem::ItemIsFocusable);
1335     QVERIFY(!scene.focusItem());
1336     item->setFocus();
1337     QCOMPARE(scene.focusItem(), item);
1338
1339     QFocusEvent focusOut(QEvent::FocusOut);
1340     QApplication::sendEvent(&scene, &focusOut);
1341
1342     QVERIFY(!scene.focusItem());
1343
1344     QFocusEvent focusIn(QEvent::FocusIn);
1345     QApplication::sendEvent(&scene, &focusIn);
1346     QCOMPARE(scene.focusItem(), item);
1347
1348     QGraphicsItem *item2 = scene.addText("Qt");
1349     item2->setFlag(QGraphicsItem::ItemIsFocusable);
1350     QCOMPARE(scene.focusItem(), item);
1351
1352     item2->setFocus();
1353     QCOMPARE(scene.focusItem(), item2);
1354     item->setFocus();
1355     QCOMPARE(scene.focusItem(), item);
1356
1357     item2->setFocus();
1358     QCOMPARE(scene.focusItem(), item2);
1359     QApplication::sendEvent(&scene, &focusOut);
1360     QVERIFY(!scene.hasFocus());
1361     QVERIFY(!scene.focusItem());
1362     QApplication::sendEvent(&scene, &focusIn);
1363     QCOMPARE(scene.focusItem(), item2);
1364
1365     QApplication::sendEvent(&scene, &focusOut);
1366
1367     QVERIFY(!scene.focusItem());
1368     scene.removeItem(item2);
1369     delete item2;
1370
1371     QApplication::sendEvent(&scene, &focusIn);
1372     QVERIFY(!scene.focusItem());
1373 }
1374
1375 class FocusItem : public QGraphicsTextItem
1376 {
1377 protected:
1378     void focusOutEvent(QFocusEvent *)
1379     {
1380         QVERIFY(!scene()->focusItem());
1381     }
1382 };
1383
1384 void tst_QGraphicsScene::focusItemLostFocus()
1385 {
1386     QGraphicsScene scene;
1387     QEvent activate(QEvent::WindowActivate);
1388     QApplication::sendEvent(&scene, &activate);
1389
1390     FocusItem *item = new FocusItem;
1391     item->setTextInteractionFlags(Qt::TextEditorInteraction);
1392     scene.addItem(item);
1393
1394     item->setFocus();
1395     QCOMPARE(scene.focusItem(), (QGraphicsItem *)item);
1396     item->clearFocus();
1397 }
1398
1399 class ClearTestItem : public QGraphicsRectItem
1400 {
1401 public:
1402     ClearTestItem(QGraphicsItem *parent = 0) : QGraphicsRectItem(parent) {}
1403     ~ClearTestItem() { qDeleteAll(items); }
1404     QList<QGraphicsItem *> items;
1405 };
1406
1407 void tst_QGraphicsScene::clear()
1408 {
1409     QGraphicsScene scene;
1410     scene.clear();
1411     QVERIFY(scene.items().isEmpty());
1412     scene.addRect(0, 0, 100, 100)->setPen(QPen(Qt::black, 0));
1413     QCOMPARE(scene.sceneRect(), QRectF(0, 0, 100, 100));
1414     scene.clear();
1415     QVERIFY(scene.items().isEmpty());
1416     QCOMPARE(scene.sceneRect(), QRectF(0, 0, 100, 100));
1417
1418     ClearTestItem *firstItem = new ClearTestItem;
1419     QGraphicsItem *secondItem = new QGraphicsRectItem;
1420     firstItem->items += secondItem;
1421
1422     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
1423     scene.addItem(firstItem);
1424     scene.addItem(secondItem);
1425     QCOMPARE(scene.items().at(0), (QGraphicsItem*)firstItem);
1426     QCOMPARE(scene.items().at(1), secondItem);
1427
1428     ClearTestItem *thirdItem = new ClearTestItem(firstItem);
1429     QGraphicsItem *forthItem = new QGraphicsRectItem(firstItem);
1430     thirdItem->items += forthItem;
1431
1432     // must not crash even if firstItem deletes secondItem
1433     scene.clear();
1434     QVERIFY(scene.items().isEmpty());
1435 }
1436
1437 void tst_QGraphicsScene::setFocusItem()
1438 {
1439     QGraphicsScene scene;
1440     QEvent activate(QEvent::WindowActivate);
1441     QApplication::sendEvent(&scene, &activate);
1442
1443     QGraphicsItem *item = scene.addText("Qt");
1444     QVERIFY(!scene.focusItem());
1445     QVERIFY(!scene.hasFocus());
1446     scene.setFocusItem(item);
1447     QVERIFY(!scene.hasFocus());
1448     QVERIFY(!scene.focusItem());
1449     item->setFlag(QGraphicsItem::ItemIsFocusable);
1450
1451     for (int i = 0; i < 3; ++i) {
1452         scene.setFocusItem(item);
1453         QVERIFY(scene.hasFocus());
1454         QCOMPARE(scene.focusItem(), item);
1455         QVERIFY(item->hasFocus());
1456     }
1457
1458     QGraphicsItem *item2 = scene.addText("Qt");
1459     item2->setFlag(QGraphicsItem::ItemIsFocusable);
1460
1461     scene.setFocusItem(item2);
1462     QVERIFY(!item->hasFocus());
1463     QVERIFY(item2->hasFocus());
1464
1465     scene.setFocusItem(item);
1466     QVERIFY(item->hasFocus());
1467     QVERIFY(!item2->hasFocus());
1468
1469     scene.clearFocus();
1470     QVERIFY(!item->hasFocus());
1471     QVERIFY(!item2->hasFocus());
1472
1473     scene.setFocus();
1474     QVERIFY(item->hasFocus());
1475     QVERIFY(!item2->hasFocus());
1476
1477     scene.setFocusItem(0);
1478     QVERIFY(!item->hasFocus());
1479     QVERIFY(!item2->hasFocus());
1480
1481     scene.setFocus();
1482     QVERIFY(!item->hasFocus());
1483     QVERIFY(!item2->hasFocus());
1484 }
1485
1486 void tst_QGraphicsScene::setFocusItem_inactive()
1487 {
1488     QGraphicsScene scene;
1489     QGraphicsItem *item = scene.addText("Qt");
1490     QVERIFY(!scene.focusItem());
1491     QVERIFY(!scene.hasFocus());
1492     scene.setFocusItem(item);
1493     QVERIFY(!scene.hasFocus());
1494     QVERIFY(!scene.focusItem());
1495     item->setFlag(QGraphicsItem::ItemIsFocusable);
1496
1497     for (int i = 0; i < 3; ++i) {
1498         scene.setFocusItem(item);
1499         QCOMPARE(scene.focusItem(), item);
1500         QVERIFY(!item->hasFocus());
1501     }
1502
1503 }
1504
1505
1506 void tst_QGraphicsScene::mouseGrabberItem()
1507 {
1508     QGraphicsScene scene;
1509     QVERIFY(!scene.mouseGrabberItem());
1510
1511     QGraphicsItem *item = scene.addRect(QRectF(-10, -10, 20, 20));
1512     item->setFlag(QGraphicsItem::ItemIsMovable);
1513     item->setZValue(1);
1514
1515     QGraphicsItem *item2 = scene.addRect(QRectF(-10, -10, 20, 20));
1516     item2->setFlag(QGraphicsItem::ItemIsMovable);
1517     item2->setZValue(0);
1518
1519     for (int i = 0; i < 3; ++i) {
1520         item->setPos(0, 0);
1521         item2->setPos(0, 0);
1522         item->setZValue((i & 1) ? 0 : 1);
1523         item2->setZValue((i & 1) ? 1 : 0);
1524         QGraphicsItem *topMostItem = (i & 1) ? item2 : item;
1525
1526         QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
1527         pressEvent.setButton(Qt::LeftButton);
1528         pressEvent.setScenePos(QPointF(0, 0));
1529         pressEvent.setScreenPos(QPoint(100, 100));
1530
1531         QApplication::sendEvent(&scene, &pressEvent);
1532         QCOMPARE(scene.mouseGrabberItem(), topMostItem);
1533
1534         for (int i = 0; i < 1000; ++i) {
1535             QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove);
1536             moveEvent.setButtons(Qt::LeftButton);
1537             moveEvent.setScenePos(QPointF(i * 10, i * 10));
1538             moveEvent.setScreenPos(QPoint(100 + i * 10, 100 + i * 10));
1539             QApplication::sendEvent(&scene, &moveEvent);
1540             QCOMPARE(scene.mouseGrabberItem(), topMostItem);
1541
1542             // Geometrical changes should not affect the mouse grabber.
1543             item->setZValue(rand() % 500);
1544             item2->setZValue(rand() % 500);
1545             item->setPos(rand() % 50000, rand() % 50000);
1546             item2->setPos(rand() % 50000, rand() % 50000);
1547         }
1548
1549         QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease);
1550         releaseEvent.setScenePos(QPointF(10000, 10000));
1551         releaseEvent.setScreenPos(QPoint(1000000, 1000000));
1552         QApplication::sendEvent(&scene, &releaseEvent);
1553         QVERIFY(!scene.mouseGrabberItem());
1554     }
1555
1556     // Structural change: deleting the mouse grabber
1557     item->setPos(0, 0);
1558     item->setZValue(1);
1559     item2->setPos(0, 0);
1560     item2->setZValue(0);
1561     QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
1562     pressEvent.setButton(Qt::LeftButton);
1563     pressEvent.setScenePos(QPointF(0, 0));
1564     pressEvent.setScreenPos(QPoint(100, 100));
1565
1566     QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove);
1567     moveEvent.setButtons(Qt::LeftButton);
1568     moveEvent.setScenePos(QPointF(0, 0));
1569     moveEvent.setScreenPos(QPoint(100, 100));
1570
1571     QApplication::sendEvent(&scene, &pressEvent);
1572     QApplication::sendEvent(&scene, &moveEvent);
1573     QCOMPARE(scene.mouseGrabberItem(), item);
1574     item->setVisible(false);
1575     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1576     QApplication::sendEvent(&scene, &pressEvent);
1577     QCOMPARE(scene.mouseGrabberItem(), item2);
1578     item2->setVisible(false);
1579     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1580     QApplication::sendEvent(&scene, &moveEvent);
1581     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1582     item2->setVisible(true);
1583     QApplication::sendEvent(&scene, &moveEvent);
1584     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1585     QApplication::sendEvent(&scene, &pressEvent);
1586     QApplication::sendEvent(&scene, &moveEvent);
1587     QCOMPARE(scene.mouseGrabberItem(), item2);
1588     scene.removeItem(item2);
1589     delete item2;
1590     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1591 }
1592
1593 void tst_QGraphicsScene::hoverEvents_siblings()
1594 {
1595     Q_CHECK_PAINTEVENTS
1596
1597     QGraphicsScene scene;
1598     QGraphicsItem *lastItem = 0;
1599     QList<HoverItem *> items;
1600     for (int i = 0; i < 15; ++i) {
1601         QGraphicsItem *item = new HoverItem;
1602         scene.addItem(item);
1603         items << (HoverItem *)item;
1604         if (lastItem) {
1605             item->setPos(lastItem->pos() + QPointF(sin(i / 3.0) * 17, cos(i / 3.0) * 17));
1606         }
1607         item->setZValue(i);
1608         lastItem = item;
1609     }
1610
1611     QGraphicsView view(&scene);
1612     view.setRenderHint(QPainter::Antialiasing, true);
1613 #if defined(Q_OS_WINCE)
1614     view.setMinimumSize(230, 200);
1615 #else
1616     view.setMinimumSize(400, 300);
1617 #endif
1618     view.rotate(10);
1619     view.scale(1.7, 1.7);
1620     view.show();
1621     qApp->setActiveWindow(&view);
1622     view.activateWindow();
1623     QVERIFY(QTest::qWaitForWindowActive(&view));
1624
1625     QCursor::setPos(view.mapToGlobal(QPoint(-5, -5)));
1626
1627     QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
1628     mouseEvent.setScenePos(QPointF(-1000, -1000));
1629     QApplication::sendEvent(&scene, &mouseEvent);
1630
1631     QTest::qWait(50);
1632
1633     for (int j = 1; j >= 0; --j) {
1634         int i = j ? 0 : 14;
1635         forever {
1636             if (j)
1637                 QVERIFY(!items.at(i)->isHovered);
1638             else
1639                 QVERIFY(!items.at(i)->isHovered);
1640             QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
1641             mouseEvent.setScenePos(items.at(i)->mapToScene(0, 0));
1642             QApplication::sendEvent(&scene, &mouseEvent);
1643
1644             qApp->processEvents(); // this posts updates from the scene to the view
1645             qApp->processEvents(); // which trigger a repaint here
1646
1647             QTRY_VERIFY(items.at(i)->isHovered);
1648             if (j && i > 0)
1649                 QVERIFY(!items.at(i - 1)->isHovered);
1650             if (!j && i < 14)
1651                 QVERIFY(!items.at(i + 1)->isHovered);
1652             i += j ? 1 : -1;
1653             if ((j && i == 15) || (!j && i == -1))
1654                 break;
1655         }
1656
1657         QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
1658         mouseEvent.setScenePos(QPointF(-1000, -1000));
1659         QApplication::sendEvent(&scene, &mouseEvent);
1660
1661         qApp->processEvents(); // this posts updates from the scene to the view
1662         qApp->processEvents(); // which trigger a repaint here
1663     }
1664 }
1665
1666 void tst_QGraphicsScene::hoverEvents_parentChild()
1667 {
1668     Q_CHECK_PAINTEVENTS
1669
1670     QGraphicsScene scene;
1671     QGraphicsItem *lastItem = 0;
1672     QList<HoverItem *> items;
1673     for (int i = 0; i < 15; ++i) {
1674         QGraphicsItem *item = new HoverItem;
1675         scene.addItem(item);
1676         items << (HoverItem *)item;
1677         if (lastItem) {
1678             item->setParentItem(lastItem);
1679             item->setPos(sin(i / 3.0) * 17, cos(i / 3.0) * 17);
1680         }
1681         lastItem = item;
1682     }
1683
1684     QGraphicsView view(&scene);
1685     view.setRenderHint(QPainter::Antialiasing, true);
1686 #if defined(Q_OS_WINCE)
1687     view.setMinimumSize(230, 200);
1688 #else
1689     view.setMinimumSize(400, 300);
1690 #endif
1691     view.rotate(10);
1692     view.scale(1.7, 1.7);
1693     view.show();
1694     qApp->setActiveWindow(&view);
1695     QVERIFY(QTest::qWaitForWindowActive(&view));
1696
1697     QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
1698     mouseEvent.setScenePos(QPointF(-1000, -1000));
1699     QApplication::sendEvent(&scene, &mouseEvent);
1700
1701     for (int j = 1; j >= 0; --j) {
1702         int i = j ? 0 : 14;
1703         forever {
1704             if (j) {
1705                 QVERIFY(!items.at(i)->isHovered);
1706             } else {
1707                 if (i == 14)
1708                     QVERIFY(!items.at(13)->isHovered);
1709             }
1710             mouseEvent.setScenePos(items.at(i)->mapToScene(0, 0));
1711             QApplication::sendEvent(&scene, &mouseEvent);
1712
1713             qApp->processEvents(); // this posts updates from the scene to the view
1714             qApp->processEvents(); // which trigger a repaint here
1715
1716             QTRY_VERIFY(items.at(i)->isHovered);
1717             if (i < 14)
1718                 QVERIFY(!items.at(i + 1)->isHovered);
1719             i += j ? 1 : -1;
1720             if ((j && i == 15) || (!j && i == -1))
1721                 break;
1722         }
1723
1724         mouseEvent.setScenePos(QPointF(-1000, -1000));
1725         QApplication::sendEvent(&scene, &mouseEvent);
1726
1727         qApp->processEvents(); // this posts updates from the scene to the view
1728         qApp->processEvents(); // which trigger a repaint here
1729     }
1730 }
1731
1732 void tst_QGraphicsScene::createItemGroup()
1733 {
1734     QGraphicsScene scene;
1735
1736     QList<QGraphicsItem *> children1;
1737     children1 << scene.addRect(QRectF(-10, -10, 20, 20));
1738     children1 << scene.addRect(QRectF(-10, -10, 20, 20));
1739     children1 << scene.addRect(QRectF(-10, -10, 20, 20));
1740     children1 << scene.addRect(QRectF(-10, -10, 20, 20));
1741
1742     QList<QGraphicsItem *> children2;
1743     children2 << scene.addRect(QRectF(-10, -10, 20, 20));
1744     children2 << scene.addRect(QRectF(-10, -10, 20, 20));
1745     children2 << scene.addRect(QRectF(-10, -10, 20, 20));
1746     children2 << scene.addRect(QRectF(-10, -10, 20, 20));
1747
1748     QList<QGraphicsItem *> children3;
1749     children3 << scene.addRect(QRectF(-10, -10, 20, 20));
1750     children3 << scene.addRect(QRectF(-10, -10, 20, 20));
1751     children3 << scene.addRect(QRectF(-10, -10, 20, 20));
1752     children3 << scene.addRect(QRectF(-10, -10, 20, 20));
1753
1754     // All items in children1 are children of parent1
1755     QGraphicsItem *parent1 = scene.addRect(QRectF(-10, -10, 20, 20));
1756     foreach (QGraphicsItem *item, children1)
1757         item->setParentItem(parent1);
1758
1759     QGraphicsItemGroup *group = scene.createItemGroup(children1);
1760     QCOMPARE(group->parentItem(), parent1);
1761     QCOMPARE(children1.first()->parentItem(), (QGraphicsItem *)group);
1762     scene.destroyItemGroup(group);
1763     QCOMPARE(children1.first()->parentItem(), parent1);
1764     group = scene.createItemGroup(children1);
1765     QCOMPARE(group->parentItem(), parent1);
1766     QCOMPARE(children1.first()->parentItem(), (QGraphicsItem *)group);
1767     scene.destroyItemGroup(group);
1768     QCOMPARE(children1.first()->parentItem(), parent1);
1769
1770     // All items in children2 are children of parent2
1771     QGraphicsItem *parent2 = scene.addRect(QRectF(-10, -10, 20, 20));
1772     foreach (QGraphicsItem *item, children2)
1773         item->setParentItem(parent2);
1774
1775     // Now make parent2 a child of parent1, so all children2 are also children
1776     // of parent1.
1777     parent2->setParentItem(parent1);
1778
1779     // The children2 group should still have parent2 as their common ancestor.
1780     group = scene.createItemGroup(children2);
1781     QCOMPARE(group->parentItem(), parent2);
1782     QCOMPARE(children2.first()->parentItem(), (QGraphicsItem *)group);
1783     scene.destroyItemGroup(group);
1784     QCOMPARE(children2.first()->parentItem(), parent2);
1785
1786     // But the set of both children2 and children1 share only parent1.
1787     group = scene.createItemGroup(children2 + children1);
1788     QCOMPARE(group->parentItem(), parent1);
1789     QCOMPARE(children1.first()->parentItem(), (QGraphicsItem *)group);
1790     QCOMPARE(children2.first()->parentItem(), (QGraphicsItem *)group);
1791     scene.destroyItemGroup(group);
1792     QCOMPARE(children1.first()->parentItem(), parent1);
1793     QCOMPARE(children2.first()->parentItem(), parent1);
1794
1795     // Fixup the parent-child chain
1796     foreach (QGraphicsItem *item, children2)
1797         item->setParentItem(parent2);
1798
1799     // These share no common parent
1800     group = scene.createItemGroup(children3);
1801     QCOMPARE(group->parentItem(), (QGraphicsItem *)0);
1802     scene.destroyItemGroup(group);
1803
1804     // Make children3 children of parent3
1805     QGraphicsItem *parent3 = scene.addRect(QRectF(-10, -10, 20, 20));
1806     foreach (QGraphicsItem *item, children3)
1807         item->setParentItem(parent3);
1808
1809     // These should have parent3 as a parent
1810     group = scene.createItemGroup(children3);
1811     QCOMPARE(group->parentItem(), parent3);
1812     scene.destroyItemGroup(group);
1813
1814     // Now make them all children of parent1
1815     parent3->setParentItem(parent1);
1816
1817     group = scene.createItemGroup(children3);
1818     QCOMPARE(group->parentItem(), parent3);
1819     scene.destroyItemGroup(group);
1820
1821     group = scene.createItemGroup(children2);
1822     QCOMPARE(group->parentItem(), parent2);
1823     scene.destroyItemGroup(group);
1824
1825     group = scene.createItemGroup(children1);
1826     QCOMPARE(group->parentItem(), parent1);
1827     scene.destroyItemGroup(group);
1828
1829     QGraphicsItemGroup *emptyGroup = scene.createItemGroup(QList<QGraphicsItem *>());
1830     QCOMPARE(emptyGroup->children(), QList<QGraphicsItem *>());
1831     QVERIFY(!emptyGroup->parentItem());
1832     QCOMPARE(emptyGroup->scene(), &scene);
1833 }
1834
1835 class EventTester : public QGraphicsEllipseItem
1836 {
1837 public:
1838     EventTester()
1839         : QGraphicsEllipseItem(QRectF(-10, -10, 20, 20)), ignoreMouse(false)
1840     { }
1841
1842     bool ignoreMouse;
1843     QList<QEvent::Type> eventTypes;
1844
1845 protected:
1846     bool sceneEvent(QEvent *event)
1847     {
1848         eventTypes << QEvent::Type(event->type());
1849         switch (event->type()) {
1850         case QEvent::GraphicsSceneMousePress:
1851         case QEvent::GraphicsSceneMouseMove:
1852         case QEvent::GraphicsSceneMouseRelease:
1853             if (ignoreMouse) {
1854                 event->ignore();
1855                 return true;
1856             }
1857         default:
1858             break;
1859         }
1860
1861         return QGraphicsEllipseItem::sceneEvent(event);
1862     }
1863 };
1864
1865 void tst_QGraphicsScene::mouseEventPropagation()
1866 {
1867     EventTester *a = new EventTester;
1868     EventTester *b = new EventTester;
1869     EventTester *c = new EventTester;
1870     EventTester *d = new EventTester;
1871     b->setParentItem(a);
1872     c->setParentItem(b);
1873     d->setParentItem(c);
1874
1875     a->setFlag(QGraphicsItem::ItemIsMovable);
1876     b->setFlag(QGraphicsItem::ItemIsMovable);
1877     c->setFlag(QGraphicsItem::ItemIsMovable);
1878     d->setFlag(QGraphicsItem::ItemIsMovable);
1879
1880     a->setData(0, "A");
1881     b->setData(0, "B");
1882     c->setData(0, "C");
1883     d->setData(0, "D");
1884
1885     // scene -> a -> b -> c -> d
1886     QGraphicsScene scene;
1887     QEvent activate(QEvent::WindowActivate);
1888     QApplication::sendEvent(&scene, &activate);
1889
1890     scene.addItem(a);
1891
1892     // Prepare some events
1893     QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
1894     pressEvent.setButton(Qt::LeftButton);
1895     pressEvent.setScenePos(QPointF(0, 0));
1896     QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove);
1897     moveEvent.setButton(Qt::LeftButton);
1898     moveEvent.setScenePos(QPointF(0, 0));
1899     QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease);
1900     releaseEvent.setButton(Qt::LeftButton);
1901     releaseEvent.setScenePos(QPointF(0, 0));
1902
1903     // Send a press
1904     QApplication::sendEvent(&scene, &pressEvent);
1905     QCOMPARE(d->eventTypes.size(), 2);
1906     QCOMPARE(d->eventTypes.at(0), QEvent::GrabMouse);
1907     QCOMPARE(d->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
1908     QCOMPARE(c->eventTypes.size(), 0);
1909     QCOMPARE(b->eventTypes.size(), 0);
1910     QCOMPARE(a->eventTypes.size(), 0);
1911     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)d);
1912
1913     // Send a move
1914     QApplication::sendEvent(&scene, &moveEvent);
1915     QCOMPARE(d->eventTypes.size(), 3);
1916     QCOMPARE(d->eventTypes.at(2), QEvent::GraphicsSceneMouseMove);
1917     QCOMPARE(c->eventTypes.size(), 0);
1918     QCOMPARE(b->eventTypes.size(), 0);
1919     QCOMPARE(a->eventTypes.size(), 0);
1920     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)d);
1921
1922     // Send a release
1923     QApplication::sendEvent(&scene, &releaseEvent);
1924     QCOMPARE(d->eventTypes.size(), 5);
1925     QCOMPARE(d->eventTypes.at(3), QEvent::GraphicsSceneMouseRelease);
1926     QCOMPARE(d->eventTypes.at(4), QEvent::UngrabMouse);
1927     QCOMPARE(c->eventTypes.size(), 0);
1928     QCOMPARE(b->eventTypes.size(), 0);
1929     QCOMPARE(a->eventTypes.size(), 0);
1930     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1931
1932     d->setAcceptedMouseButtons(Qt::RightButton);
1933
1934     // Send a press
1935     QApplication::sendEvent(&scene, &pressEvent);
1936     QCOMPARE(d->eventTypes.size(), 5);
1937     QCOMPARE(c->eventTypes.size(), 2);
1938     QCOMPARE(c->eventTypes.at(0), QEvent::GrabMouse);
1939     QCOMPARE(c->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
1940     QCOMPARE(b->eventTypes.size(), 0);
1941     QCOMPARE(a->eventTypes.size(), 0);
1942     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)c);
1943
1944     // Send another press, with a button that isn't actually accepted
1945     QApplication::sendEvent(&scene, &pressEvent);
1946     pressEvent.setButton(Qt::RightButton);
1947     QCOMPARE(d->eventTypes.size(), 5);
1948     QCOMPARE(c->eventTypes.size(), 3);
1949     QCOMPARE(c->eventTypes.at(2), QEvent::GraphicsSceneMousePress);
1950     QCOMPARE(b->eventTypes.size(), 0);
1951     QCOMPARE(a->eventTypes.size(), 0);
1952     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)c);
1953
1954     // Send a move
1955     QApplication::sendEvent(&scene, &moveEvent);
1956     QCOMPARE(d->eventTypes.size(), 5);
1957     QCOMPARE(c->eventTypes.size(), 4);
1958     QCOMPARE(c->eventTypes.at(3), QEvent::GraphicsSceneMouseMove);
1959     QCOMPARE(b->eventTypes.size(), 0);
1960     QCOMPARE(a->eventTypes.size(), 0);
1961     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)c);
1962
1963     // Send a release
1964     QApplication::sendEvent(&scene, &releaseEvent);
1965     QCOMPARE(d->eventTypes.size(), 5);
1966     QCOMPARE(c->eventTypes.size(), 6);
1967     QCOMPARE(c->eventTypes.at(4), QEvent::GraphicsSceneMouseRelease);
1968     QCOMPARE(c->eventTypes.at(5), QEvent::UngrabMouse);
1969     QCOMPARE(b->eventTypes.size(), 0);
1970     QCOMPARE(a->eventTypes.size(), 0);
1971     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1972
1973     // Disabled items eat events. c should not get this.
1974     d->setEnabled(false);
1975     d->setAcceptedMouseButtons(Qt::RightButton);
1976
1977     // Send a right press. This disappears in d.
1978     QApplication::sendEvent(&scene, &pressEvent);
1979     QCOMPARE(d->eventTypes.size(), 5);
1980     QCOMPARE(c->eventTypes.size(), 6);
1981     QCOMPARE(b->eventTypes.size(), 0);
1982     QCOMPARE(a->eventTypes.size(), 0);
1983     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1984
1985     // Send a left press. This goes to c.
1986     pressEvent.setButton(Qt::LeftButton);
1987     QApplication::sendEvent(&scene, &pressEvent);
1988     QCOMPARE(d->eventTypes.size(), 5);
1989     QCOMPARE(c->eventTypes.size(), 8);
1990     QCOMPARE(c->eventTypes.at(6), QEvent::GrabMouse);
1991     QCOMPARE(c->eventTypes.at(7), QEvent::GraphicsSceneMousePress);
1992     QCOMPARE(b->eventTypes.size(), 0);
1993     QCOMPARE(a->eventTypes.size(), 0);
1994     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)c);
1995
1996     // Clicking outside the items removes the mouse grabber
1997 }
1998
1999 void tst_QGraphicsScene::mouseEventPropagation_ignore()
2000 {
2001     EventTester *a = new EventTester;
2002     EventTester *b = new EventTester;
2003     EventTester *c = new EventTester;
2004     EventTester *d = new EventTester;
2005     b->setParentItem(a);
2006     c->setParentItem(b);
2007     d->setParentItem(c);
2008
2009     a->setFlags(QGraphicsItem::ItemIsMovable);
2010     b->setFlags(QGraphicsItem::ItemIsMovable);
2011     c->setFlags(QGraphicsItem::ItemIsMovable);
2012     d->setFlags(QGraphicsItem::ItemIsMovable);
2013
2014     // scene -> a -> b -> c -> d
2015     QGraphicsScene scene;
2016     scene.addItem(a);
2017
2018     // Prepare some events
2019     QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
2020     pressEvent.setButton(Qt::LeftButton);
2021     pressEvent.setScenePos(QPointF(0, 0));
2022
2023     b->ignoreMouse = true;
2024     c->ignoreMouse = true;
2025     d->ignoreMouse = true;
2026
2027     QApplication::sendEvent(&scene, &pressEvent);
2028     QCOMPARE(a->eventTypes.size(), 2);
2029     QCOMPARE(a->eventTypes.at(0), QEvent::GrabMouse);
2030     QCOMPARE(a->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
2031     QCOMPARE(b->eventTypes.size(), 3);
2032     QCOMPARE(b->eventTypes.at(0), QEvent::GrabMouse);
2033     QCOMPARE(b->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
2034     QCOMPARE(b->eventTypes.at(2), QEvent::UngrabMouse);
2035     QCOMPARE(c->eventTypes.size(), 3);
2036     QCOMPARE(c->eventTypes.at(0), QEvent::GrabMouse);
2037     QCOMPARE(c->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
2038     QCOMPARE(c->eventTypes.at(2), QEvent::UngrabMouse);
2039     QCOMPARE(d->eventTypes.size(), 3);
2040     QCOMPARE(d->eventTypes.at(0), QEvent::GrabMouse);
2041     QCOMPARE(d->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
2042     QCOMPARE(d->eventTypes.at(2), QEvent::UngrabMouse);
2043     QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)a);
2044
2045     a->ignoreMouse = true;
2046
2047     QApplication::sendEvent(&scene, &pressEvent);
2048     QCOMPARE(a->eventTypes.size(), 3);
2049     QCOMPARE(a->eventTypes.at(2), QEvent::GraphicsSceneMousePress);
2050     QCOMPARE(b->eventTypes.size(), 3);
2051     QCOMPARE(c->eventTypes.size(), 3);
2052     QCOMPARE(d->eventTypes.size(), 3);
2053
2054     QVERIFY(!pressEvent.isAccepted());
2055 }
2056
2057 void tst_QGraphicsScene::mouseEventPropagation_focus()
2058 {
2059     EventTester *a = new EventTester;
2060     EventTester *b = new EventTester;
2061     EventTester *c = new EventTester;
2062     EventTester *d = new EventTester;
2063     b->setParentItem(a);
2064     c->setParentItem(b);
2065     d->setParentItem(c);
2066
2067     a->setFlag(QGraphicsItem::ItemIsMovable);
2068     b->setFlag(QGraphicsItem::ItemIsMovable);
2069     c->setFlag(QGraphicsItem::ItemIsMovable);
2070     d->setFlag(QGraphicsItem::ItemIsMovable);
2071
2072     // scene -> a -> b -> c -> d
2073     QGraphicsScene scene;
2074     QEvent activate(QEvent::WindowActivate);
2075     QApplication::sendEvent(&scene, &activate);
2076
2077     scene.addItem(a);
2078
2079     // Prepare some events
2080     QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
2081     pressEvent.setButton(Qt::LeftButton);
2082     pressEvent.setScenePos(QPointF(0, 0));
2083
2084     a->setFlag(QGraphicsItem::ItemIsFocusable);
2085     QVERIFY(!a->hasFocus());
2086
2087     QApplication::sendEvent(&scene, &pressEvent);
2088
2089     QVERIFY(a->hasFocus());
2090     QCOMPARE(a->eventTypes.size(), 1);
2091     QCOMPARE(a->eventTypes.first(), QEvent::FocusIn);
2092     QCOMPARE(d->eventTypes.size(), 2);
2093     QCOMPARE(d->eventTypes.at(0), QEvent::GrabMouse);
2094     QCOMPARE(d->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
2095 }
2096
2097 void tst_QGraphicsScene::mouseEventPropagation_doubleclick()
2098 {
2099     EventTester *a = new EventTester;
2100     EventTester *b = new EventTester;
2101     a->setFlags(QGraphicsItem::ItemIsMovable);
2102     b->setFlags(QGraphicsItem::ItemIsMovable);
2103
2104     a->setPos(-50, 0);
2105     b->setPos(50, 0);
2106
2107     QGraphicsScene scene;
2108     scene.addItem(a);
2109     scene.addItem(b);
2110
2111     // Prepare some events
2112     QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
2113     pressEvent.setButton(Qt::LeftButton);
2114     pressEvent.setScenePos(QPointF(0, 0));
2115     QGraphicsSceneMouseEvent doubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
2116     doubleClickEvent.setButton(Qt::LeftButton);
2117     doubleClickEvent.setScenePos(QPointF(0, 0));
2118     QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease);
2119     releaseEvent.setButton(Qt::LeftButton);
2120     releaseEvent.setScenePos(QPointF(0, 0));
2121
2122     // Send press to A
2123     pressEvent.setScenePos(a->mapToScene(0, 0));
2124     QApplication::sendEvent(&scene, &pressEvent);
2125     QCOMPARE(a->eventTypes.size(), 2);
2126     QCOMPARE(a->eventTypes.at(0), QEvent::GrabMouse);
2127     QCOMPARE(a->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
2128
2129     // Send release to A
2130     releaseEvent.setScenePos(a->mapToScene(0, 0));
2131     QApplication::sendEvent(&scene, &releaseEvent);
2132     QCOMPARE(a->eventTypes.size(), 4);
2133     QCOMPARE(a->eventTypes.at(2), QEvent::GraphicsSceneMouseRelease);
2134     QCOMPARE(a->eventTypes.at(3), QEvent::UngrabMouse);
2135
2136     // Send doubleclick to B
2137     doubleClickEvent.setScenePos(b->mapToScene(0, 0));
2138     QApplication::sendEvent(&scene, &doubleClickEvent);
2139     QCOMPARE(a->eventTypes.size(), 4);
2140     QCOMPARE(b->eventTypes.size(), 2);
2141     QCOMPARE(b->eventTypes.at(0), QEvent::GrabMouse);
2142     QCOMPARE(b->eventTypes.at(1), QEvent::GraphicsSceneMousePress);
2143
2144     // Send release to B
2145     releaseEvent.setScenePos(b->mapToScene(0, 0));
2146     QApplication::sendEvent(&scene, &releaseEvent);
2147     QCOMPARE(a->eventTypes.size(), 4);
2148     QCOMPARE(b->eventTypes.size(), 4);
2149     QCOMPARE(b->eventTypes.at(2), QEvent::GraphicsSceneMouseRelease);
2150     QCOMPARE(b->eventTypes.at(3), QEvent::UngrabMouse);
2151 }
2152
2153 class Scene : public QGraphicsScene
2154 {
2155 public:
2156     QVector<QPointF> mouseMovePoints;
2157
2158 protected:
2159     void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
2160     {
2161         mouseMovePoints << event->scenePos();
2162     }
2163 };
2164
2165 void tst_QGraphicsScene::mouseEventPropagation_mouseMove()
2166 {
2167     Scene scene;
2168     scene.addRect(QRectF(5, 0, 12, 12));
2169     scene.addRect(QRectF(15, 0, 12, 12))->setAcceptsHoverEvents(true);
2170     for (int i = 0; i < 30; ++i) {
2171         QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseMove);
2172         event.setScenePos(QPointF(i, 5));
2173         QApplication::sendEvent(&scene, &event);
2174     }
2175
2176     QCOMPARE(scene.mouseMovePoints.size(), 30);
2177     for (int i = 0; i < 30; ++i)
2178         QCOMPARE(scene.mouseMovePoints.at(i), QPointF(i, 5));
2179 }
2180
2181 class DndTester : public QGraphicsEllipseItem
2182 {
2183 public:
2184     DndTester(const QRectF &rect)
2185         : QGraphicsEllipseItem(rect), lastEvent(0),
2186           ignoresDragEnter(false), ignoresDragMove(false)
2187
2188     {
2189     }
2190
2191     ~DndTester()
2192     {
2193         delete lastEvent;
2194     }
2195
2196     QGraphicsSceneDragDropEvent *lastEvent;
2197     QList<QEvent::Type> eventList;
2198     bool ignoresDragEnter;
2199     bool ignoresDragMove;
2200
2201 protected:
2202     void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
2203     {
2204         storeLastEvent(event);
2205         event->setAccepted(!ignoresDragEnter);
2206         if (!ignoresDragEnter)
2207             event->setDropAction(Qt::IgnoreAction);
2208         eventList << event->type();
2209     }
2210
2211     void dragMoveEvent(QGraphicsSceneDragDropEvent *event)
2212     {
2213         storeLastEvent(event);
2214         event->setAccepted(!ignoresDragMove);
2215         eventList << event->type();
2216     }
2217
2218     void dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
2219     {
2220         storeLastEvent(event);
2221         eventList << event->type();
2222     }
2223
2224     void dropEvent(QGraphicsSceneDragDropEvent *event)
2225     {
2226         storeLastEvent(event);
2227         eventList << event->type();
2228     }
2229
2230 private:
2231     void storeLastEvent(QGraphicsSceneDragDropEvent *event)
2232     {
2233         delete lastEvent;
2234         lastEvent = new QGraphicsSceneDragDropEvent(event->type());
2235         lastEvent->setScenePos(event->scenePos());
2236         lastEvent->setScreenPos(event->screenPos());
2237         lastEvent->setButtons(event->buttons());
2238         lastEvent->setModifiers(event->modifiers());
2239         lastEvent->setPossibleActions(event->possibleActions());
2240         lastEvent->setProposedAction(event->proposedAction());
2241         lastEvent->setDropAction(event->dropAction());
2242         lastEvent->setMimeData(event->mimeData());
2243         lastEvent->setWidget(event->widget());
2244         lastEvent->setSource(event->source());
2245     }
2246 };
2247
2248 #ifndef QT_NO_DRAGANDDROP
2249 void tst_QGraphicsScene::dragAndDrop_simple()
2250 {
2251     DndTester *item = new DndTester(QRectF(-10, -10, 20, 20));
2252
2253     QGraphicsScene scene;
2254     scene.addItem(item);
2255
2256     QGraphicsView view(&scene);
2257     view.setFixedSize(100, 100);
2258
2259     QMimeData mimeData;
2260
2261     // Initial drag enter for the scene
2262     QDragEnterEvent dragEnter(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2263     QApplication::sendEvent(view.viewport(), &dragEnter);
2264     QVERIFY(dragEnter.isAccepted());
2265     QCOMPARE(dragEnter.dropAction(), Qt::CopyAction);
2266
2267     {
2268         // Move outside the item
2269         QDragMoveEvent dragMove(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2270         QApplication::sendEvent(view.viewport(), &dragMove);
2271         QVERIFY(!dragMove.isAccepted());
2272         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2273     }
2274     {
2275         // Move inside the item without setAcceptDrops
2276         QDragMoveEvent dragMove(view.mapFromScene(item->scenePos()), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2277         QApplication::sendEvent(view.viewport(), &dragMove);
2278         QVERIFY(!dragMove.isAccepted());
2279         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2280         QCOMPARE(item->eventList.size(), 0);
2281     }
2282     item->setAcceptDrops(true);
2283     {
2284         // Move inside the item with setAcceptDrops
2285         QDragMoveEvent dragMove(view.mapFromScene(item->scenePos()), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2286         QApplication::sendEvent(view.viewport(), &dragMove);
2287         QVERIFY(dragMove.isAccepted());
2288         QCOMPARE(dragMove.dropAction(), Qt::IgnoreAction);
2289         QCOMPARE(item->eventList.size(), 2);
2290         QCOMPARE(item->eventList.at(0), QEvent::GraphicsSceneDragEnter);
2291         QCOMPARE(item->eventList.at(1), QEvent::GraphicsSceneDragMove);
2292         QCOMPARE(item->lastEvent->screenPos(), view.mapToGlobal(dragMove.pos()));
2293         QCOMPARE(item->lastEvent->scenePos(), view.mapToScene(dragMove.pos()));
2294         QVERIFY(item->lastEvent->isAccepted());
2295         QCOMPARE(item->lastEvent->dropAction(), Qt::IgnoreAction);
2296     }
2297     {
2298         // Another move inside the item
2299         QDragMoveEvent dragMove(view.mapFromScene(item->mapToScene(5, 5)), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2300         QApplication::sendEvent(view.viewport(), &dragMove);
2301         QVERIFY(dragMove.isAccepted());
2302         QCOMPARE(dragMove.dropAction(), Qt::IgnoreAction);
2303         QCOMPARE(item->eventList.size(), 3);
2304         QCOMPARE(item->eventList.at(2), QEvent::GraphicsSceneDragMove);
2305         QCOMPARE(item->lastEvent->screenPos(), view.mapToGlobal(dragMove.pos()));
2306         QCOMPARE(item->lastEvent->scenePos(), view.mapToScene(dragMove.pos()));
2307         QVERIFY(item->lastEvent->isAccepted());
2308         QCOMPARE(item->lastEvent->dropAction(), Qt::IgnoreAction);
2309     }
2310     {
2311         // Move outside the item
2312         QDragMoveEvent dragMove(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2313         QApplication::sendEvent(view.viewport(), &dragMove);
2314         QVERIFY(!dragMove.isAccepted());
2315         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2316         QCOMPARE(item->eventList.size(), 4);
2317         QCOMPARE(item->eventList.at(3), QEvent::GraphicsSceneDragLeave);
2318         QCOMPARE(item->lastEvent->screenPos(), view.mapToGlobal(dragMove.pos()));
2319         QCOMPARE(item->lastEvent->scenePos(), view.mapToScene(dragMove.pos()));
2320         QVERIFY(item->lastEvent->isAccepted());
2321         QCOMPARE(item->lastEvent->dropAction(), Qt::CopyAction);
2322     }
2323     {
2324         // Move inside the item again
2325         QDragMoveEvent dragMove(view.mapFromScene(item->scenePos()), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2326         QApplication::sendEvent(view.viewport(), &dragMove);
2327         QVERIFY(dragMove.isAccepted());
2328         QCOMPARE(dragMove.dropAction(), Qt::IgnoreAction);
2329         QCOMPARE(item->eventList.size(), 6);
2330         QCOMPARE(item->eventList.at(4), QEvent::GraphicsSceneDragEnter);
2331         QCOMPARE(item->eventList.at(5), QEvent::GraphicsSceneDragMove);
2332         QCOMPARE(item->lastEvent->screenPos(), view.mapToGlobal(dragMove.pos()));
2333         QCOMPARE(item->lastEvent->scenePos(), view.mapToScene(dragMove.pos()));
2334         QVERIFY(item->lastEvent->isAccepted());
2335         QCOMPARE(item->lastEvent->dropAction(), Qt::IgnoreAction);
2336     }
2337     {
2338         // Drop inside the item
2339         QDropEvent drop(view.mapFromScene(item->scenePos()), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2340         QApplication::sendEvent(view.viewport(), &drop);
2341         QVERIFY(drop.isAccepted());
2342         QCOMPARE(drop.dropAction(), Qt::CopyAction);
2343         QCOMPARE(item->eventList.size(), 7);
2344         QCOMPARE(item->eventList.at(6), QEvent::GraphicsSceneDrop);
2345         QCOMPARE(item->lastEvent->screenPos(), view.mapToGlobal(drop.pos()));
2346         QCOMPARE(item->lastEvent->scenePos(), view.mapToScene(drop.pos()));
2347         QVERIFY(item->lastEvent->isAccepted());
2348         QCOMPARE(item->lastEvent->dropAction(), Qt::CopyAction);
2349     }
2350 }
2351
2352 void tst_QGraphicsScene::dragAndDrop_disabledOrInvisible()
2353 {
2354     DndTester *item = new DndTester(QRectF(-10, -10, 20, 20));
2355     item->setAcceptDrops(true);
2356
2357     QGraphicsScene scene;
2358     scene.addItem(item);
2359
2360     QGraphicsView view(&scene);
2361     view.setFixedSize(100, 100);
2362
2363     QMimeData mimeData;
2364
2365     // Initial drag enter for the scene
2366     QDragEnterEvent dragEnter(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2367     QApplication::sendEvent(view.viewport(), &dragEnter);
2368     QVERIFY(dragEnter.isAccepted());
2369     QCOMPARE(dragEnter.dropAction(), Qt::CopyAction);
2370     {
2371         // Move inside the item
2372         QDragMoveEvent dragMove(view.mapFromScene(item->scenePos()), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2373         QApplication::sendEvent(view.viewport(), &dragMove);
2374         QVERIFY(dragMove.isAccepted());
2375         QCOMPARE(dragMove.dropAction(), Qt::IgnoreAction);
2376         QCOMPARE(item->eventList.size(), 2);
2377         QCOMPARE(item->eventList.at(0), QEvent::GraphicsSceneDragEnter);
2378         QCOMPARE(item->eventList.at(1), QEvent::GraphicsSceneDragMove);
2379     }
2380     {
2381         // Move outside the item
2382         QDragMoveEvent dragMove(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2383         QApplication::sendEvent(view.viewport(), &dragMove);
2384         QVERIFY(!dragMove.isAccepted());
2385         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2386         QCOMPARE(item->eventList.size(), 3);
2387         QCOMPARE(item->eventList.at(2), QEvent::GraphicsSceneDragLeave);
2388     }
2389
2390     // Now disable the item
2391     item->setEnabled(false);
2392     QVERIFY(!item->isEnabled());
2393     QVERIFY(item->isVisible());
2394
2395     {
2396         // Move inside the item
2397         QDragMoveEvent dragMove(view.mapFromScene(item->scenePos()), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2398         QApplication::sendEvent(view.viewport(), &dragMove);
2399         QVERIFY(!dragMove.isAccepted());
2400         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2401         QCOMPARE(item->eventList.size(), 3);
2402         QCOMPARE(item->eventList.at(2), QEvent::GraphicsSceneDragLeave);
2403     }
2404
2405     // Reenable it, and make it invisible
2406     item->setEnabled(true);
2407     item->setVisible(false);
2408     QVERIFY(item->isEnabled());
2409     QVERIFY(!item->isVisible());
2410
2411     {
2412         // Move inside the item
2413         QDragMoveEvent dragMove(view.mapFromScene(item->scenePos()), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2414         QApplication::sendEvent(view.viewport(), &dragMove);
2415         QVERIFY(!dragMove.isAccepted());
2416         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2417         QCOMPARE(item->eventList.size(), 3);
2418         QCOMPARE(item->eventList.at(2), QEvent::GraphicsSceneDragLeave);
2419     }
2420
2421     // Dummy drop event to keep the Mac from crashing.
2422     QDropEvent dropEvent(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2423     QApplication::sendEvent(view.viewport(), &dropEvent);
2424 }
2425
2426 void tst_QGraphicsScene::dragAndDrop_propagate()
2427 {
2428     DndTester *item1 = new DndTester(QRectF(-10, -10, 20, 20));
2429     DndTester *item2 = new DndTester(QRectF(0, 0, 20, 20));
2430     item1->setAcceptDrops(true);
2431     item2->setAcceptDrops(true);
2432     item2->ignoresDragMove = true;
2433     item2->ignoresDragEnter = false;
2434     item2->setZValue(1);
2435
2436     item1->setData(0, "item1");
2437     item2->setData(0, "item2");
2438
2439     QGraphicsScene scene;
2440     scene.addItem(item1);
2441     scene.addItem(item2);
2442
2443     QGraphicsView view(&scene);
2444     view.setFixedSize(100, 100);
2445
2446     QMimeData mimeData;
2447
2448     // Initial drag enter for the scene
2449     QDragEnterEvent dragEnter(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2450     QApplication::sendEvent(view.viewport(), &dragEnter);
2451     QVERIFY(dragEnter.isAccepted());
2452     QCOMPARE(dragEnter.dropAction(), Qt::CopyAction);
2453
2454     {
2455         // Move outside the items
2456         QDragMoveEvent dragMove(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2457         QApplication::sendEvent(view.viewport(), &dragMove);
2458         QVERIFY(!dragMove.isAccepted());
2459         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2460         QVERIFY(item1->eventList.isEmpty());
2461         QVERIFY(item2->eventList.isEmpty());
2462     }
2463     {
2464         // Move inside item1
2465         QDragMoveEvent dragMove(view.mapFromScene(-5, -5), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2466         QApplication::sendEvent(view.viewport(), &dragMove);
2467         QVERIFY(dragMove.isAccepted());
2468         QCOMPARE(dragMove.dropAction(), Qt::IgnoreAction);
2469         QCOMPARE(item1->eventList.size(), 2);
2470         QCOMPARE(item1->eventList.at(0), QEvent::GraphicsSceneDragEnter);
2471         QCOMPARE(item1->eventList.at(1), QEvent::GraphicsSceneDragMove);
2472     }
2473
2474     {
2475         // Move into the intersection item1-item2
2476         QDragMoveEvent dragMove(view.mapFromScene(5, 5), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2477         QApplication::sendEvent(view.viewport(), &dragMove);
2478         QVERIFY(!dragMove.isAccepted());    // move does not propagate, (ignoresDragMove = true)
2479         QCOMPARE(item1->eventList.size(), 3);
2480         QCOMPARE(item1->eventList.at(2), QEvent::GraphicsSceneDragLeave);
2481         QCOMPARE(item2->eventList.size(), 2);
2482         QCOMPARE(item2->eventList.at(0), QEvent::GraphicsSceneDragEnter);
2483         QCOMPARE(item2->eventList.at(1), QEvent::GraphicsSceneDragMove);
2484     }
2485     {
2486         // Move into the item2
2487         QDragMoveEvent dragMove(view.mapFromScene(15, 15), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2488         QApplication::sendEvent(view.viewport(), &dragMove);
2489         QVERIFY(!dragMove.isAccepted());
2490         QCOMPARE(dragMove.dropAction(), Qt::CopyAction);
2491         QCOMPARE(item1->eventList.size(), 3);
2492         QCOMPARE(item2->eventList.size(), 3);
2493         QCOMPARE(item2->eventList.at(2), QEvent::GraphicsSceneDragMove);
2494     }
2495     {
2496         // Move inside item1
2497         QDragMoveEvent dragMove(view.mapFromScene(-5, -5), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2498         QApplication::sendEvent(view.viewport(), &dragMove);
2499         QVERIFY(dragMove.isAccepted());
2500         QCOMPARE(dragMove.dropAction(), Qt::IgnoreAction);
2501         QCOMPARE(item1->eventList.size(), 5);
2502         QCOMPARE(item1->eventList.at(3), QEvent::GraphicsSceneDragEnter);
2503         QCOMPARE(item1->eventList.at(4), QEvent::GraphicsSceneDragMove);
2504         QCOMPARE(item2->eventList.size(), 4);
2505         QCOMPARE(item2->eventList.at(3), QEvent::GraphicsSceneDragLeave);
2506     }
2507
2508     {
2509         item2->ignoresDragEnter = true;
2510         // Move into the intersection item1-item2
2511         QDragMoveEvent dragMove(view.mapFromScene(5, 5), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2512         QApplication::sendEvent(view.viewport(), &dragMove);
2513         QVERIFY(dragMove.isAccepted());    // dragEnter propagates down to item1, which then accepts the move event.
2514         QCOMPARE(dragMove.dropAction(), Qt::IgnoreAction);
2515         QCOMPARE(item1->eventList.size(), 6);
2516         QCOMPARE(item1->eventList.at(5), QEvent::GraphicsSceneDragMove);
2517         QCOMPARE(item2->eventList.size(), 5);
2518         QCOMPARE(item2->eventList.at(4), QEvent::GraphicsSceneDragEnter);
2519     }
2520
2521     {
2522         item2->ignoresDragEnter = false;
2523         // Drop on the intersection item1-item2
2524         QDropEvent drop(view.mapFromScene(5, 5), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2525         QApplication::sendEvent(view.viewport(), &drop);
2526         QVERIFY(drop.isAccepted());
2527         QCOMPARE(drop.dropAction(), Qt::CopyAction);
2528
2529         QCOMPARE(item1->eventList.size(), 7);
2530         QCOMPARE(item1->eventList.at(6), QEvent::GraphicsSceneDrop);
2531         QCOMPARE(item2->eventList.size(), 5);
2532     }
2533
2534     // Dummy drop event to keep the Mac from crashing.
2535     QDropEvent dropEvent(QPoint(0, 0), Qt::CopyAction, &mimeData, Qt::LeftButton, 0);
2536     QApplication::sendEvent(view.viewport(), &dropEvent);
2537 }
2538 #endif
2539
2540 void tst_QGraphicsScene::render_data()
2541 {
2542     QTest::addColumn<QRectF>("targetRect");
2543     QTest::addColumn<QRectF>("sourceRect");
2544     QTest::addColumn<Qt::AspectRatioMode>("aspectRatioMode");
2545     QTest::addColumn<QMatrix>("matrix");
2546     QTest::addColumn<QPainterPath>("clip");
2547
2548     QPainterPath clip_rect;
2549     clip_rect.addRect(50, 100, 200, 150);
2550
2551     QPainterPath clip_ellipse;
2552     clip_ellipse.addEllipse(100,50,150,200);
2553
2554     QTest::newRow("all-all-untransformed") << QRectF() << QRectF()
2555                                            << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2556     QTest::newRow("all-topleft-untransformed") << QRectF(0, 0, 150, 150)
2557                                                << QRectF() << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2558     QTest::newRow("all-topright-untransformed") << QRectF(150, 0, 150, 150)
2559                                                 << QRectF() << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2560     QTest::newRow("all-bottomleft-untransformed") << QRectF(0, 150, 150, 150)
2561                                                   << QRectF() << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2562     QTest::newRow("all-bottomright-untransformed") << QRectF(150, 150, 150, 150)
2563                                                    << QRectF() << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2564     QTest::newRow("topleft-all-untransformed") << QRectF() << QRectF(-10, -10, 10, 10)
2565                                                << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2566     QTest::newRow("topright-all-untransformed") << QRectF() << QRectF(0, -10, 10, 10)
2567                                                 << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2568     QTest::newRow("bottomleft-all-untransformed") << QRectF() << QRectF(-10, 0, 10, 10)
2569                                                   << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2570     QTest::newRow("bottomright-all-untransformed") << QRectF() << QRectF(0, 0, 10, 10)
2571                                                    << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2572     QTest::newRow("topleft-topleft-untransformed") << QRectF(0, 0, 150, 150) << QRectF(-10, -10, 10, 10)
2573                                                    << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2574     QTest::newRow("topright-topleft-untransformed") << QRectF(150, 0, 150, 150) << QRectF(-10, -10, 10, 10)
2575                                                     << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2576     QTest::newRow("bottomleft-topleft-untransformed") << QRectF(0, 150, 150, 150) << QRectF(-10, -10, 10, 10)
2577                                                       << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2578     QTest::newRow("bottomright-topleft-untransformed") << QRectF(150, 150, 150, 150) << QRectF(-10, -10, 10, 10)
2579                                                        << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2580     QTest::newRow("top-topleft-untransformed") << QRectF(0, 0, 300, 150) << QRectF(-10, -10, 10, 10)
2581                                                << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2582     QTest::newRow("bottom-topleft-untransformed") << QRectF(0, 150, 300, 150) << QRectF(-10, -10, 10, 10)
2583                                                   << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2584     QTest::newRow("left-topleft-untransformed") << QRectF(0, 0, 150, 300) << QRectF(-10, -10, 10, 10)
2585                                                 << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2586     QTest::newRow("right-topleft-untransformed") << QRectF(150, 0, 150, 300) << QRectF(-10, -10, 10, 10)
2587                                                  << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2588     QTest::newRow("top-bottomright-untransformed") << QRectF(0, 0, 300, 150) << QRectF(0, 0, 10, 10)
2589                                                    << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2590     QTest::newRow("bottom-bottomright-untransformed") << QRectF(0, 150, 300, 150) << QRectF(0, 0, 10, 10)
2591                                                       << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2592     QTest::newRow("left-bottomright-untransformed") << QRectF(0, 0, 150, 300) << QRectF(0, 0, 10, 10)
2593                                                     << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2594     QTest::newRow("right-bottomright-untransformed") << QRectF(150, 0, 150, 300) << QRectF(0, 0, 10, 10)
2595                                                      << Qt::IgnoreAspectRatio << QMatrix() << QPainterPath();
2596     QTest::newRow("all-all-45-deg-right") << QRectF() << QRectF()
2597                                           << Qt::IgnoreAspectRatio << QMatrix().rotate(-45) << QPainterPath();
2598     QTest::newRow("all-all-45-deg-left") << QRectF() << QRectF()
2599                                          << Qt::IgnoreAspectRatio << QMatrix().rotate(45) << QPainterPath();
2600     QTest::newRow("all-all-scale-2x") << QRectF() << QRectF()
2601                                       << Qt::IgnoreAspectRatio << QMatrix().scale(2, 2) << QPainterPath();
2602     QTest::newRow("all-all-translate-50-0") << QRectF() << QRectF()
2603                                             << Qt::IgnoreAspectRatio << QMatrix().translate(50, 0) << QPainterPath();
2604     QTest::newRow("all-all-translate-0-50") << QRectF() << QRectF()
2605                                             << Qt::IgnoreAspectRatio << QMatrix().translate(0, 50) << QPainterPath();
2606     QTest::newRow("all-all-untransformed-clip-rect") << QRectF() << QRectF()
2607                                            << Qt::IgnoreAspectRatio << QMatrix() << clip_rect;
2608     QTest::newRow("all-all-untransformed-clip-ellipse") << QRectF() << QRectF()
2609                                            << Qt::IgnoreAspectRatio << QMatrix() << clip_ellipse;
2610 }
2611
2612 void tst_QGraphicsScene::render()
2613 {
2614     QFETCH(QRectF, targetRect);
2615     QFETCH(QRectF, sourceRect);
2616     QFETCH(Qt::AspectRatioMode, aspectRatioMode);
2617     QFETCH(QMatrix, matrix);
2618     QFETCH(QPainterPath, clip);
2619
2620     QPixmap pix(30, 30);
2621     pix.fill(Qt::blue);
2622
2623     QGraphicsView view;
2624     QGraphicsScene scene(&view);
2625     scene.addEllipse(QRectF(-10, -10, 20, 20), QPen(Qt::black, 0), QBrush(Qt::white));
2626     scene.addEllipse(QRectF(-2, -7, 4, 4), QPen(Qt::black, 0), QBrush(Qt::yellow))->setZValue(1);
2627     QGraphicsPixmapItem *item = scene.addPixmap(pix);
2628     item->setZValue(2);
2629     item->setOffset(QPointF(3, 3));
2630     view.show();
2631
2632     scene.setSceneRect(scene.itemsBoundingRect());
2633
2634     QImage bigImage(300, 300, QImage::Format_RGB32);
2635     bigImage.fill(0);
2636     QPainter painter(&bigImage);
2637     painter.setPen(Qt::lightGray);
2638     for (int i = 0; i <= 300; i += 25) {
2639         painter.drawLine(0, i, 300, i);
2640         painter.drawLine(i, 0, i, 300);
2641     }
2642     painter.setPen(QPen(Qt::darkGray, 2));
2643     painter.drawLine(0, 150, 300, 150);
2644     painter.drawLine(150, 0, 150, 300);
2645     painter.setMatrix(matrix);
2646     if (!clip.isEmpty()) painter.setClipPath(clip);
2647     scene.render(&painter, targetRect, sourceRect, aspectRatioMode);
2648     painter.end();
2649
2650     const QString renderPath = QLatin1String(SRCDIR) + "/testData/render";
2651     QString fileName = renderPath + QString("/%1.png").arg(QTest::currentDataTag());
2652     QImage original(fileName);
2653     QVERIFY(!original.isNull());
2654
2655     // Compare
2656     int wrongPixels = 0;
2657     for (int y = 0; y < original.height(); ++y) {
2658         for (int x = 0; x < original.width(); ++x) {
2659             if (bigImage.pixel(x, y) != original.pixel(x, y))
2660                 ++wrongPixels;
2661         }
2662     }
2663
2664     // This is a pixmap compare test - because of rounding errors on diverse
2665     // platforms, and especially because tests are compiled in release mode,
2666     // we set a 95% acceptance threshold for comparing images. This number may
2667     // have to be adjusted if this test fails.
2668     qreal threshold = 0.95;
2669     qreal similarity = (1 - (wrongPixels / qreal(original.width() * original.height())));
2670     if (similarity < threshold) {
2671 #if 1
2672         // fail
2673         QLabel *expectedLabel = new QLabel;
2674         expectedLabel->setPixmap(QPixmap::fromImage(original));
2675
2676         QLabel *newLabel = new QLabel;
2677         newLabel->setPixmap(QPixmap::fromImage(bigImage));
2678
2679         QGridLayout *gridLayout = new QGridLayout;
2680         gridLayout->addWidget(new QLabel(tr("MISMATCH: %1").arg(QTest::currentDataTag())), 0, 0, 1, 2);
2681         gridLayout->addWidget(new QLabel(tr("Current")), 1, 0);
2682         gridLayout->addWidget(new QLabel(tr("Expected")), 1, 1);
2683         gridLayout->addWidget(expectedLabel, 2, 1);
2684         gridLayout->addWidget(newLabel, 2, 0);
2685
2686         QWidget widget;
2687         widget.setLayout(gridLayout);
2688         widget.show();
2689
2690         QTestEventLoop::instance().enterLoop(1);
2691
2692         QFAIL("Images are not identical.");
2693 #else
2694         // generate
2695         qDebug() << "Updating" << QTest::currentDataTag() << ":" << bigImage.save(fileName, "png");
2696 #endif
2697     }
2698 }
2699
2700 void tst_QGraphicsScene::renderItemsWithNegativeWidthOrHeight()
2701 {
2702     QGraphicsScene scene(0, 0, 150, 150);
2703
2704     // Add item with negative width.
2705     QGraphicsRectItem *item1 = new QGraphicsRectItem(0, 0, -150, 50);
2706     item1->setBrush(Qt::red);
2707     item1->setPos(150, 50);
2708     scene.addItem(item1);
2709
2710     // Add item with negative height.
2711     QGraphicsRectItem *item2 = new QGraphicsRectItem(0, 0, 50, -150);
2712     item2->setBrush(Qt::blue);
2713     item2->setPos(50, 150);
2714     scene.addItem(item2);
2715
2716     QGraphicsView view(&scene);
2717     view.setFrameStyle(QFrame::NoFrame);
2718     view.resize(150, 150);
2719     view.show();
2720     QCOMPARE(view.viewport()->size(), QSize(150, 150));
2721
2722     QImage expected(view.viewport()->size(), QImage::Format_RGB32);
2723     view.viewport()->render(&expected);
2724
2725     // Make sure the scene background is the same as the viewport background.
2726     scene.setBackgroundBrush(view.viewport()->palette().brush(view.viewport()->backgroundRole()));
2727     QImage actual(150, 150, QImage::Format_RGB32);
2728     QPainter painter(&actual);
2729     scene.render(&painter);
2730     painter.end();
2731
2732     QCOMPARE(actual, expected);
2733 }
2734
2735 void tst_QGraphicsScene::contextMenuEvent()
2736 {
2737     QGraphicsScene scene;
2738     QEvent activate(QEvent::WindowActivate);
2739     QApplication::sendEvent(&scene, &activate);
2740
2741     EventTester *item = new EventTester;
2742     scene.addItem(item);
2743     item->setFlag(QGraphicsItem::ItemIsFocusable);
2744     item->setFocus();
2745
2746     QVERIFY(item->hasFocus());
2747     QVERIFY(scene.hasFocus());
2748
2749     QGraphicsView view(&scene);
2750     view.show();
2751     view.activateWindow();
2752     QVERIFY(QTest::qWaitForWindowActive(&view));
2753     view.centerOn(item);
2754
2755     {
2756         QContextMenuEvent event(QContextMenuEvent::Keyboard, view.viewport()->rect().center(),
2757                                 view.mapToGlobal(view.viewport()->rect().center()));
2758         QApplication::sendEvent(view.viewport(), &event);
2759         QCOMPARE(item->eventTypes.last(), QEvent::GraphicsSceneContextMenu);
2760     }
2761 }
2762
2763 class ContextMenuItem : public QGraphicsRectItem
2764 {
2765 public:
2766     ContextMenuItem() : QGraphicsRectItem(0, 0, 100, 100)
2767     { setBrush(Qt::red); }
2768
2769 protected:
2770     void contextMenuEvent(QGraphicsSceneContextMenuEvent *)
2771     { /* just accept */ }
2772 };
2773
2774 void tst_QGraphicsScene::contextMenuEvent_ItemIgnoresTransformations()
2775 {
2776     QGraphicsScene scene(0, 0, 200, 200);
2777     ContextMenuItem *item = new ContextMenuItem;
2778     item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
2779     scene.addItem(item);
2780
2781     QWidget topLevel;
2782     QGraphicsView view(&scene, &topLevel);
2783     view.resize(200, 200);
2784     topLevel.show();
2785     QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
2786
2787     {
2788         QPoint pos(50, 50);
2789         QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2790         event.ignore();
2791         QApplication::sendEvent(view.viewport(), &event);
2792         QVERIFY(event.isAccepted());
2793     }
2794     {
2795         QPoint pos(150, 150);
2796         QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2797         event.ignore();
2798         QApplication::sendEvent(view.viewport(), &event);
2799         QVERIFY(!event.isAccepted());
2800     }
2801     view.scale(1.5, 1.5);
2802     {
2803         QPoint pos(25, 25);
2804         QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2805         event.ignore();
2806         QApplication::sendEvent(view.viewport(), &event);
2807         QVERIFY(event.isAccepted());
2808     }
2809     {
2810         QPoint pos(55, 55);
2811         QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2812         event.ignore();
2813         QApplication::sendEvent(view.viewport(), &event);
2814         QVERIFY(!event.isAccepted());
2815     }
2816 }
2817
2818 void tst_QGraphicsScene::update()
2819 {
2820     QGraphicsScene scene;
2821
2822     QGraphicsRectItem *rect = new QGraphicsRectItem(0, 0, 100, 100);
2823     rect->setPen(QPen(Qt::black, 0));
2824     scene.addItem(rect);
2825     qApp->processEvents();
2826     rect->setPos(-100, -100);
2827
2828     // This function forces indexing
2829     scene.itemAt(0, 0);
2830
2831     qRegisterMetaType<QList<QRectF> >("QList<QRectF>");
2832     QSignalSpy spy(&scene, SIGNAL(changed(QList<QRectF>)));
2833
2834     // We update the scene.
2835     scene.update();
2836
2837     // This function forces a purge, which will post an update signal
2838     scene.itemAt(0, 0);
2839
2840     // This will process the pending update
2841     QApplication::instance()->processEvents();
2842
2843     // Check that the update region is correct
2844     QCOMPARE(spy.count(), 1);
2845     QRectF region;
2846     foreach (QRectF rectF, qvariant_cast<QList<QRectF> >(spy.at(0).at(0)))
2847         region |= rectF;
2848     QCOMPARE(region, QRectF(-100, -100, 200, 200));
2849 }
2850
2851 void tst_QGraphicsScene::update2()
2852 {
2853     QGraphicsScene scene;
2854     scene.setSceneRect(-200, -200, 200, 200);
2855     CustomView view;
2856     view.setScene(&scene);
2857     view.show();
2858     qApp->setActiveWindow(&view);
2859     QVERIFY(QTest::qWaitForWindowActive(&view));
2860     QTRY_VERIFY(view.repaints >= 1);
2861     view.repaints = 0;
2862
2863     // Make sure QGraphicsScene::update only requires one event-loop iteration
2864     // before the view is updated.
2865     scene.update();
2866     qApp->processEvents();
2867     QTRY_COMPARE(view.repaints, 1);
2868     view.repaints = 0;
2869
2870     // The same for partial scene updates.
2871     scene.update(QRectF(-100, -100, 100, 100));
2872     qApp->processEvents();
2873     QCOMPARE(view.repaints, 1);
2874 }
2875
2876 void tst_QGraphicsScene::views()
2877 {
2878     QGraphicsScene scene;
2879     QGraphicsView view(&scene);
2880
2881     QCOMPARE(scene.views().size(), 1);
2882     QCOMPARE(scene.views().at(0), &view);
2883
2884     QGraphicsView view1(&scene);
2885     QCOMPARE(scene.views().size(), 2);
2886     QVERIFY(scene.views().contains(&view1));
2887
2888     view.setScene(0);
2889     QCOMPARE(scene.views().size(), 1);
2890     QCOMPARE(scene.views().at(0), &view1);
2891
2892     QGraphicsView *view2 = new QGraphicsView(&scene);
2893     QCOMPARE(scene.views().size(), 2);
2894     QCOMPARE(scene.views().at(0), &view1);
2895     QCOMPARE(scene.views().at(1), view2);
2896
2897     delete view2;
2898
2899     QCOMPARE(scene.views().size(), 1);
2900     QCOMPARE(scene.views().at(0), &view1);
2901 }
2902
2903 class CustomScene : public QGraphicsScene
2904 {
2905 public:
2906     CustomScene() : gotTimerEvent(false)
2907     { startTimer(10); }
2908
2909     bool gotTimerEvent;
2910 protected:
2911     void timerEvent(QTimerEvent *)
2912     {
2913         gotTimerEvent = true;
2914     }
2915 };
2916
2917 void tst_QGraphicsScene::event()
2918 {
2919     // Test that QGraphicsScene properly propagates events to QObject.
2920     CustomScene scene;
2921     QTestEventLoop::instance().enterLoop(1);
2922     QVERIFY(scene.gotTimerEvent);
2923 }
2924
2925 class DisabledItemTester : public QGraphicsRectItem
2926 {
2927 public:
2928     DisabledItemTester(const QRectF &rect, QGraphicsItem *parent = 0)
2929         : QGraphicsRectItem(rect, parent)
2930     { }
2931
2932     QList<QEvent::Type> receivedSceneEvents;
2933     QList<QEvent::Type> receivedSceneEventFilters;
2934
2935 protected:
2936     bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
2937     {
2938         receivedSceneEventFilters << event->type();
2939         return QGraphicsRectItem::sceneEventFilter(watched, event);
2940     }
2941
2942     bool sceneEvent(QEvent *event)
2943     {
2944         receivedSceneEvents << event->type();
2945         return QGraphicsRectItem::sceneEvent(event);
2946     }
2947 };
2948
2949 void tst_QGraphicsScene::eventsToDisabledItems()
2950 {
2951     QGraphicsScene scene;
2952
2953     DisabledItemTester *item1 = new DisabledItemTester(QRectF(-50, -50, 100, 100));
2954     DisabledItemTester *item2 = new DisabledItemTester(QRectF(-50, -50, 100, 100));
2955     item1->setZValue(1); // on top
2956
2957     scene.addItem(item1);
2958     scene.addItem(item2);
2959
2960     item1->installSceneEventFilter(item2);
2961
2962     QVERIFY(item1->receivedSceneEvents.isEmpty());
2963     QVERIFY(item2->receivedSceneEvents.isEmpty());
2964     QVERIFY(item1->receivedSceneEventFilters.isEmpty());
2965     QVERIFY(item2->receivedSceneEventFilters.isEmpty());
2966
2967     QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
2968     event.setButton(Qt::LeftButton);
2969     QApplication::sendEvent(&scene, &event);
2970
2971     // First item2 receives a scene event filter. Then item1 receives the
2972     // actual event. Finally the event propagates to item2. So both items
2973     // should have received the event, and item1 also got the filter.
2974     QCOMPARE(item1->receivedSceneEvents.size(), 3);
2975     QCOMPARE(item2->receivedSceneEvents.size(), 3);
2976     QCOMPARE(item1->receivedSceneEventFilters.size(), 0);
2977     QCOMPARE(item2->receivedSceneEventFilters.size(), 3);
2978
2979     item1->receivedSceneEvents.clear();
2980     item1->receivedSceneEventFilters.clear();
2981     item2->receivedSceneEvents.clear();
2982     item2->receivedSceneEventFilters.clear();
2983
2984     item1->setEnabled(false); // disable the topmost item, eat mouse events
2985
2986     event.setButton(Qt::LeftButton);
2987     event.setAccepted(false);
2988     QApplication::sendEvent(&scene, &event);
2989
2990     // Check that only item1 received anything - it only got the filter.
2991     QCOMPARE(item1->receivedSceneEvents.size(), 0);
2992     QCOMPARE(item2->receivedSceneEvents.size(), 0);
2993     QCOMPARE(item1->receivedSceneEventFilters.size(), 0);
2994     QCOMPARE(item2->receivedSceneEventFilters.size(), 3);
2995 }
2996
2997 class ExposedPixmapItem : public QGraphicsPixmapItem
2998 {
2999 public:
3000     ExposedPixmapItem(QGraphicsItem *item = 0)
3001         : QGraphicsPixmapItem(item)
3002     { }
3003
3004     void paint(QPainter *, const QStyleOptionGraphicsItem *option, QWidget *)
3005     {
3006         exposed = option->exposedRect;
3007     }
3008
3009     QRectF exposed;
3010 };
3011
3012 void tst_QGraphicsScene::exposedRect()
3013 {
3014     ExposedPixmapItem *item = new ExposedPixmapItem;
3015     item->setPixmap(QPixmap(":/Ash_European.jpg"));
3016     QGraphicsScene scene;
3017     scene.addItem(item);
3018
3019     QCOMPARE(item->exposed, QRectF());
3020
3021     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
3022     QPainter painter(&image);
3023
3024     scene.render(&painter);
3025     QCOMPARE(item->exposed, item->boundingRect());
3026
3027     painter.rotate(180);
3028     painter.translate(100, 100);
3029
3030     scene.render(&painter);
3031     QCOMPARE(item->exposed, item->boundingRect());
3032 }
3033
3034 void tst_QGraphicsScene::tabFocus_emptyScene()
3035 {
3036     QGraphicsScene scene;
3037     QDial *dial1 = new QDial;
3038     QGraphicsView *view = new QGraphicsView(&scene);
3039     QDial *dial2 = new QDial;
3040
3041     QHBoxLayout *layout = new QHBoxLayout;
3042     layout->addWidget(dial1);
3043     layout->addWidget(view);
3044     layout->addWidget(dial2);
3045
3046     QWidget widget;
3047     widget.setLayout(layout);
3048     widget.show();
3049     qApp->setActiveWindow(&widget);
3050     widget.activateWindow();
3051     QVERIFY(QTest::qWaitForWindowActive(&widget));
3052
3053     dial1->setFocus();
3054     QVERIFY(dial1->hasFocus());
3055     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3056     QVERIFY(!dial1->hasFocus());
3057     QVERIFY(view->hasFocus());
3058     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3059     QVERIFY(!view->hasFocus());
3060     QVERIFY(dial2->hasFocus());
3061     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3062     QVERIFY(!dial2->hasFocus());
3063     QVERIFY(view->hasFocus());
3064     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3065     QVERIFY(dial1->hasFocus());
3066     QVERIFY(!dial2->hasFocus());
3067 }
3068
3069 void tst_QGraphicsScene::tabFocus_sceneWithFocusableItems()
3070 {
3071     QGraphicsScene scene;
3072     QGraphicsTextItem *item = scene.addText("Qt rocks!");
3073     item->setTabChangesFocus(true);
3074     item->setTextInteractionFlags(Qt::TextEditorInteraction);
3075     QVERIFY(item->flags() & QGraphicsItem::ItemIsFocusable);
3076     item->setFocus();
3077     item->clearFocus();
3078
3079     QGraphicsTextItem *item2 = scene.addText("Qt rocks!");
3080     item2->setTabChangesFocus(true);
3081     item2->setTextInteractionFlags(Qt::TextEditorInteraction);
3082     item2->setPos(0, item->boundingRect().bottom());
3083     QVERIFY(item2->flags() & QGraphicsItem::ItemIsFocusable);
3084
3085     QDial *dial1 = new QDial;
3086     QGraphicsView *view = new QGraphicsView(&scene);
3087     QDial *dial2 = new QDial;
3088
3089     QHBoxLayout *layout = new QHBoxLayout;
3090     layout->addWidget(dial1);
3091     layout->addWidget(view);
3092     layout->addWidget(dial2);
3093
3094     QWidget widget;
3095     widget.setLayout(layout);
3096     widget.show();
3097     qApp->setActiveWindow(&widget);
3098     widget.activateWindow();
3099     QVERIFY(QTest::qWaitForWindowActive(&widget));
3100
3101     dial1->setFocus();
3102     QTRY_VERIFY(dial1->hasFocus());
3103     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3104     QApplication::processEvents();
3105     QTRY_VERIFY(view->hasFocus());
3106     QVERIFY(view->viewport()->hasFocus());
3107     QVERIFY(scene.hasFocus());
3108     QVERIFY(item->hasFocus());
3109
3110     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3111     QApplication::processEvents();
3112     QTRY_VERIFY(dial2->hasFocus());
3113
3114     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3115     QApplication::processEvents();
3116     QTRY_VERIFY(view->hasFocus());
3117     QTRY_VERIFY(view->viewport()->hasFocus());
3118     QTRY_VERIFY(scene.hasFocus());
3119     QTRY_VERIFY(item->hasFocus());
3120
3121     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3122     QApplication::processEvents();
3123     QTRY_VERIFY(dial1->hasFocus());
3124
3125     // If the item requests input focus, it can only ensure that the scene
3126     // sets focus on itself, but the scene cannot request focus from any view.
3127     item->setFocus();
3128     QApplication::processEvents();
3129     QTRY_VERIFY(!view->hasFocus());
3130     QVERIFY(!view->viewport()->hasFocus());
3131     QTRY_VERIFY(scene.hasFocus());
3132     QVERIFY(item->hasFocus());
3133
3134     view->setFocus();
3135     QApplication::processEvents();
3136     QTRY_VERIFY(view->hasFocus());
3137     QTRY_VERIFY(view->viewport()->hasFocus());
3138     QTRY_VERIFY(scene.hasFocus());
3139     QTRY_VERIFY(item->hasFocus());
3140
3141     // Check that everyone loses focus when the widget is hidden.
3142     widget.hide();
3143     QTest::qWait(15);
3144     QTRY_VERIFY(!view->hasFocus());
3145     QVERIFY(!view->viewport()->hasFocus());
3146     QVERIFY(!scene.hasFocus());
3147     QVERIFY(!item->hasFocus());
3148     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
3149
3150     // Check that the correct item regains focus.
3151     widget.show();
3152     qApp->setActiveWindow(&widget);
3153     widget.activateWindow();
3154     QVERIFY(QTest::qWaitForWindowActive(&widget));
3155     QVERIFY(view->hasFocus());
3156     QTRY_VERIFY(scene.isActive());
3157     QVERIFY(view->viewport()->hasFocus());
3158     QVERIFY(scene.hasFocus());
3159     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
3160     QVERIFY(item->hasFocus());
3161 }
3162
3163 class FocusWidget : public QGraphicsWidget
3164 {
3165     Q_OBJECT
3166 public:
3167     FocusWidget(QGraphicsItem *parent = 0)
3168         : QGraphicsWidget(parent), tabs(0), backTabs(0)
3169     {
3170         setFocusPolicy(Qt::StrongFocus);
3171         resize(100, 100);
3172     }
3173
3174     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
3175     {
3176         if (option->state & QStyle::State_HasFocus) {
3177             painter->fillRect(rect(), Qt::blue);
3178         }
3179         painter->setBrush(Qt::green);
3180         painter->drawEllipse(rect());
3181         if (option->state & QStyle::State_HasFocus) {
3182             painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
3183             painter->setBrush(Qt::NoBrush);
3184             painter->drawEllipse(rect().adjusted(5, 5, -5, -5));
3185         }
3186     }
3187
3188     int tabs;
3189     int backTabs;
3190
3191 protected:
3192     bool sceneEvent(QEvent *event)
3193     {
3194         if (event->type() == QEvent::KeyPress) {
3195             QKeyEvent *k = static_cast<QKeyEvent *>(event);
3196             if (k->key() == Qt::Key_Tab)
3197                 ++tabs;
3198             if (k->key() == Qt::Key_Backtab)
3199                 ++backTabs;
3200         }
3201         return QGraphicsWidget::sceneEvent(event);
3202     }
3203
3204     void focusInEvent(QFocusEvent *)
3205     { update(); }
3206     void focusOutEvent(QFocusEvent *)
3207     { update(); }
3208 };
3209
3210 void tst_QGraphicsScene::tabFocus_sceneWithFocusWidgets()
3211 {
3212     QGraphicsScene scene;
3213
3214     FocusWidget *widget1 = new FocusWidget;
3215     FocusWidget *widget2 = new FocusWidget;
3216     widget2->setPos(widget1->boundingRect().right(), 0);
3217     scene.addItem(widget1);
3218     scene.addItem(widget2);
3219
3220     QDial *dial1 = new QDial;
3221     QGraphicsView *view = new QGraphicsView(&scene);
3222     view->setRenderHint(QPainter::Antialiasing);
3223     QDial *dial2 = new QDial;
3224
3225     QHBoxLayout *layout = new QHBoxLayout;
3226     layout->addWidget(dial1);
3227     layout->addWidget(view);
3228     layout->addWidget(dial2);
3229
3230     QWidget widget;
3231     widget.setLayout(layout);
3232     widget.show();
3233     qApp->setActiveWindow(&widget);
3234     widget.activateWindow();
3235     QVERIFY(QTest::qWaitForWindowActive(&widget));
3236
3237     dial1->setFocus();
3238     QTRY_VERIFY(dial1->hasFocus());
3239
3240     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3241     QApplication::processEvents();
3242     QTRY_VERIFY(view->hasFocus());
3243     QTRY_VERIFY(view->viewport()->hasFocus());
3244     QTRY_VERIFY(scene.hasFocus());
3245     QCOMPARE(widget1->tabs, 0);
3246     QVERIFY(widget1->hasFocus());
3247     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3248     QApplication::processEvents();
3249     QTRY_COMPARE(widget1->tabs, 1);
3250     QTRY_VERIFY(widget2->hasFocus());
3251     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3252     QApplication::processEvents();
3253     QTRY_COMPARE(widget2->tabs, 1);
3254     QTRY_VERIFY(dial2->hasFocus());
3255     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3256     QApplication::processEvents();
3257     QTRY_VERIFY(widget2->hasFocus());
3258     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3259     QApplication::processEvents();
3260     QTRY_COMPARE(widget2->backTabs, 1);
3261     QTRY_VERIFY(widget1->hasFocus());
3262     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3263     QApplication::processEvents();
3264     QTRY_COMPARE(widget1->backTabs, 1);
3265     QTRY_VERIFY(dial1->hasFocus());
3266
3267     widget1->setFocus();
3268     view->viewport()->setFocus();
3269     widget.hide();
3270     QTest::qWait(15);
3271     widget.show();
3272     qApp->setActiveWindow(&widget);
3273     widget.activateWindow();
3274     QVERIFY(QTest::qWaitForWindowActive(&widget));
3275     QTRY_VERIFY(widget1->hasFocus());
3276 }
3277
3278 void tst_QGraphicsScene::tabFocus_sceneWithNestedFocusWidgets()
3279 {
3280     QGraphicsScene scene;
3281
3282     FocusWidget *widget1 = new FocusWidget;
3283     FocusWidget *widget1_1 = new FocusWidget;
3284     FocusWidget *widget1_2 = new FocusWidget;
3285     widget1_1->setParentItem(widget1);
3286     widget1_1->scale(0.5, 0.5);
3287     widget1_1->setPos(0, widget1->boundingRect().height() / 2);
3288     widget1_2->setParentItem(widget1);
3289     widget1_2->scale(0.5, 0.5);
3290     widget1_2->setPos(widget1->boundingRect().width() / 2, widget1->boundingRect().height() / 2);
3291
3292     FocusWidget *widget2 = new FocusWidget;
3293     widget2->setPos(widget1->boundingRect().right(), 0);
3294
3295     widget1->setData(0, "widget1");
3296     widget1_1->setData(0, "widget1_1");
3297     widget1_2->setData(0, "widget1_2");
3298     widget2->setData(0, "widget2");
3299
3300     scene.addItem(widget1);
3301     scene.addItem(widget2);
3302
3303     QDial *dial1 = new QDial;
3304     QGraphicsView *view = new QGraphicsView(&scene);
3305     view->setRenderHint(QPainter::Antialiasing);
3306     QDial *dial2 = new QDial;
3307
3308     QHBoxLayout *layout = new QHBoxLayout;
3309     layout->addWidget(dial1);
3310     layout->addWidget(view);
3311     layout->addWidget(dial2);
3312
3313     QWidget widget;
3314     widget.setLayout(layout);
3315     widget.show();
3316     qApp->setActiveWindow(&widget);
3317     widget.activateWindow();
3318     QVERIFY(QTest::qWaitForWindowActive(&widget));
3319
3320     dial1->setFocus();
3321     QTRY_VERIFY(dial1->hasFocus());
3322
3323     EventSpy focusInSpy_1(widget1, QEvent::FocusIn);
3324     EventSpy focusOutSpy_1(widget1, QEvent::FocusOut);
3325     EventSpy focusInSpy_1_1(widget1_1, QEvent::FocusIn);
3326     EventSpy focusOutSpy_1_1(widget1_1, QEvent::FocusOut);
3327     EventSpy focusInSpy_1_2(widget1_2, QEvent::FocusIn);
3328     EventSpy focusOutSpy_1_2(widget1_2, QEvent::FocusOut);
3329     EventSpy focusInSpy_2(widget2, QEvent::FocusIn);
3330     EventSpy focusOutSpy_2(widget2, QEvent::FocusOut);
3331
3332     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3333     QApplication::processEvents();
3334     QTRY_VERIFY(widget1->hasFocus());
3335     QCOMPARE(focusInSpy_1.count(), 1);
3336
3337     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3338     QApplication::processEvents();
3339     QTRY_VERIFY(!widget1->hasFocus());
3340     QVERIFY(widget1_1->hasFocus());
3341     QCOMPARE(focusOutSpy_1.count(), 1);
3342     QCOMPARE(focusInSpy_1_1.count(), 1);
3343
3344     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3345     QApplication::processEvents();
3346     QTRY_VERIFY(!widget1_1->hasFocus());
3347     QVERIFY(widget1_2->hasFocus());
3348     QCOMPARE(focusOutSpy_1_1.count(), 1);
3349     QCOMPARE(focusInSpy_1_2.count(), 1);
3350
3351     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3352     QApplication::processEvents();
3353     QTRY_VERIFY(!widget1_2->hasFocus());
3354     QVERIFY(widget2->hasFocus());
3355     QCOMPARE(focusOutSpy_1_2.count(), 1);
3356     QCOMPARE(focusInSpy_2.count(), 1);
3357
3358     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3359     QApplication::processEvents();
3360     QTRY_VERIFY(!widget2->hasFocus());
3361     QVERIFY(dial2->hasFocus());
3362     QCOMPARE(focusOutSpy_2.count(), 1);
3363
3364     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3365     QApplication::processEvents();
3366     QTRY_VERIFY(widget2->hasFocus());
3367     QCOMPARE(focusInSpy_2.count(), 2);
3368
3369     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3370     QApplication::processEvents();
3371     QTRY_VERIFY(!widget2->hasFocus());
3372     QTRY_VERIFY(widget1_2->hasFocus());
3373     QCOMPARE(focusOutSpy_2.count(), 2);
3374     QCOMPARE(focusInSpy_1_2.count(), 2);
3375
3376     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3377     QApplication::processEvents();
3378     QTRY_VERIFY(!widget1_2->hasFocus());
3379     QTRY_VERIFY(widget1_1->hasFocus());
3380     QCOMPARE(focusOutSpy_1_2.count(), 2);
3381     QCOMPARE(focusInSpy_1_1.count(), 2);
3382
3383     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3384     QApplication::processEvents();
3385     QTRY_VERIFY(!widget1_1->hasFocus());
3386     QTRY_VERIFY(widget1->hasFocus());
3387     QCOMPARE(focusOutSpy_1_1.count(), 2);
3388     QCOMPARE(focusInSpy_1.count(), 2);
3389
3390     QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3391     QApplication::processEvents();
3392     QTRY_VERIFY(!widget1->hasFocus());
3393     QTRY_VERIFY(dial1->hasFocus());
3394     QCOMPARE(focusOutSpy_1.count(), 2);
3395
3396     widget1->setFocus();
3397     view->viewport()->setFocus();
3398     widget.hide();
3399     QTest::qWait(12);
3400     widget.show();
3401     qApp->setActiveWindow(&widget);
3402     widget.activateWindow();
3403     QVERIFY(QTest::qWaitForWindowActive(&widget));
3404     QTRY_VERIFY(widget1->hasFocus());
3405 }
3406
3407 void tst_QGraphicsScene::style()
3408 {
3409     QPointer<QWindowsStyle> windowsStyle = new QWindowsStyle;
3410
3411     QGraphicsScene scene;
3412     QLineEdit *edit = new QLineEdit;
3413     QGraphicsProxyWidget *proxy = scene.addWidget(edit);
3414
3415     EventSpy sceneSpy(&scene, QEvent::StyleChange);
3416     EventSpy proxySpy(proxy, QEvent::StyleChange);
3417     EventSpy editSpy(edit, QEvent::StyleChange);
3418
3419     QCOMPARE(scene.style(), QApplication::style());
3420
3421     scene.setStyle(windowsStyle);
3422     QCOMPARE(sceneSpy.count(), 1);
3423     QCOMPARE(proxySpy.count(), 1);
3424     QCOMPARE(editSpy.count(), 1);
3425     QCOMPARE(scene.style(), (QStyle *)windowsStyle);
3426     QCOMPARE(proxy->style(), (QStyle *)windowsStyle);
3427     QCOMPARE(edit->style(), (QStyle *)windowsStyle);
3428
3429     scene.setStyle(0);
3430     QCOMPARE(sceneSpy.count(), 2);
3431     QCOMPARE(proxySpy.count(), 2);
3432     QCOMPARE(editSpy.count(), 2);
3433     QCOMPARE(scene.style(), QApplication::style());
3434     QCOMPARE(proxy->style(), QApplication::style());
3435     QCOMPARE(edit->style(), QApplication::style());
3436     QVERIFY(!windowsStyle); // deleted
3437 }
3438
3439 void tst_QGraphicsScene::task139710_bspTreeCrash()
3440 {
3441     // create a scene with 2000 items
3442     QGraphicsScene scene(0, 0, 1000, 1000);
3443
3444     for (int i = 0; i < 2; ++i) {
3445         // trigger delayed item indexing
3446         qApp->processEvents();
3447         scene.setSceneRect(0, 0, 10000, 10000);
3448
3449         // delete all items in the scene - pointers are now likely to be recycled
3450         foreach (QGraphicsItem *item, scene.items()) {
3451             scene.removeItem(item);
3452             delete item;
3453         }
3454
3455         // add 1000 more items - the BSP tree is now resized
3456         for (int i = 0; i < 1000; ++i) {
3457             QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 200, 200));
3458             item->setPos(qrand() % 10000, qrand() % 10000);
3459         }
3460
3461         // trigger delayed item indexing for the first 1000 items
3462         qApp->processEvents();
3463
3464         // add 1000 more items - the BSP tree is now resized
3465         for (int i = 0; i < 1000; ++i) {
3466             QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 200, 200));
3467             item->setPos(qrand() % 10000, qrand() % 10000);
3468         }
3469
3470         // get items from the BSP tree and use them. there was junk in the tree
3471         // the second time this happened.
3472         foreach (QGraphicsItem *item, scene.items(QRectF(0, 0, 1000, 1000)))
3473             item->moveBy(0, 0);
3474     }
3475 }
3476
3477 void tst_QGraphicsScene::task139782_containsItemBoundingRect()
3478 {
3479     // The item in question has a scene bounding rect of (10, 10, 50, 50)
3480     QGraphicsScene scene(0.0, 0.0, 200.0, 200.0);
3481     QGraphicsRectItem *item = new QGraphicsRectItem(0.0, 0.0, 50.0, 50.0, 0);
3482     scene.addItem(item);
3483     item->setPos(10.0, 10.0);
3484
3485     // The (0, 0, 50, 50) scene rect should not include the item's bounding rect
3486     QVERIFY(!scene.items(QRectF(0.0, 0.0, 50.0, 50.0), Qt::ContainsItemBoundingRect).contains(item));
3487
3488     // The (9, 9, 500, 500) scene rect _should_ include the item's bounding rect
3489     QVERIFY(scene.items(QRectF(9.0, 9.0, 500.0, 500.0), Qt::ContainsItemBoundingRect).contains(item));
3490
3491     // The (25, 25, 5, 5) scene rect should not include the item's bounding rect
3492     QVERIFY(!scene.items(QRectF(25.0, 25.0, 5.0, 5.0), Qt::ContainsItemBoundingRect).contains(item));
3493 }
3494
3495 void tst_QGraphicsScene::task176178_itemIndexMethodBreaksSceneRect()
3496 {
3497     QGraphicsScene scene;
3498     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
3499     QGraphicsRectItem *rect = new QGraphicsRectItem;
3500     rect->setPen(QPen(Qt::black, 0));
3501     rect->setRect(0,0,100,100);
3502     scene.addItem(rect);
3503     QCOMPARE(scene.sceneRect(), rect->rect());
3504 }
3505
3506 void tst_QGraphicsScene::task160653_selectionChanged()
3507 {
3508     QGraphicsScene scene(0, 0, 100, 100);
3509     scene.addItem(new QGraphicsRectItem(0, 0, 20, 20));
3510     scene.addItem(new QGraphicsRectItem(30, 30, 20, 20));
3511     foreach (QGraphicsItem *item, scene.items()) {
3512         item->setFlags(
3513             item->flags() | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
3514         item->setSelected(true);
3515     }
3516     QVERIFY(scene.items().size() > 1);
3517     QCOMPARE(scene.items().size(), scene.selectedItems().size());
3518
3519     QSignalSpy spy(&scene, SIGNAL(selectionChanged()));
3520     QGraphicsView view(&scene);
3521     QTest::mouseClick(
3522         view.viewport(), Qt::LeftButton, 0, view.mapFromScene(scene.items().first()->scenePos()));
3523     QCOMPARE(spy.count(), 1);
3524 }
3525
3526 void tst_QGraphicsScene::task250680_childClip()
3527 {
3528     QGraphicsRectItem *clipper = new QGraphicsRectItem;
3529     clipper->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
3530     clipper->setPen(QPen(Qt::green, 0));
3531     clipper->setRect(200, 200, 640, 480);
3532
3533     QGraphicsRectItem *rect = new QGraphicsRectItem(clipper);
3534     rect->setPen(QPen(Qt::red, 0));
3535     rect->setBrush(QBrush(QColor(255, 0, 0, 75)));
3536     rect->setPos(320, 240);
3537     rect->setRect(-25, -25, 50, 50);
3538
3539     QGraphicsScene scene;
3540     scene.addItem(clipper);
3541
3542     QPainterPath path;
3543     path.addRect(-25, -25, 50, 50);
3544     QVERIFY(QPathCompare::comparePaths(rect->clipPath().simplified(), path));
3545
3546     QCOMPARE(scene.items(QRectF(320, 240, 5, 5)).size(), 2);
3547     rect->rotate(45);
3548     QCOMPARE(scene.items(QRectF(320, 240, 5, 5)).size(), 2);
3549 }
3550
3551 void tst_QGraphicsScene::sorting_data()
3552 {
3553     QTest::addColumn<bool>("cache");
3554
3555     QTest::newRow("Normal sorting") << false;
3556     QTest::newRow("Cached sorting") << true;
3557 }
3558
3559 void tst_QGraphicsScene::sorting()
3560 {
3561     QFETCH(bool, cache);
3562
3563     QGraphicsScene scene;
3564     scene.setSortCacheEnabled(cache);
3565
3566     QGraphicsRectItem *t_1 = new QGraphicsRectItem(0, 0, 50, 50);
3567     QGraphicsRectItem *c_1 = new QGraphicsRectItem(0, 0, 40, 40, t_1);
3568     QGraphicsRectItem *c_1_1 = new QGraphicsRectItem(0, 0, 30, 30, c_1);
3569     QGraphicsRectItem *c_1_1_1 = new QGraphicsRectItem(0, 0, 20, 20, c_1_1);
3570     QGraphicsRectItem *c_1_2 = new QGraphicsRectItem(0, 0, 30, 30, c_1);
3571     QGraphicsRectItem *c_2 = new QGraphicsRectItem(0, 0, 40, 40, t_1);
3572     QGraphicsRectItem *c_2_1 = new QGraphicsRectItem(0, 0, 30, 30, c_2);
3573     QGraphicsRectItem *c_2_1_1 = new QGraphicsRectItem(0, 0, 20, 20, c_2_1);
3574     QGraphicsRectItem *c_2_2 = new QGraphicsRectItem(0, 0, 30, 30, c_2);
3575     t_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3576     c_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3577     c_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3578     c_1_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3579     c_1_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3580     c_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3581     c_2_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3582     c_2_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3583     c_2_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
3584
3585     c_1->setPos(23, 18);
3586     c_1_1->setPos(24, 28);
3587     c_1_1_1->setPos(-16, 16);
3588     c_1_2->setPos(-16, 28);
3589     c_1_2->setZValue(1);
3590     c_2->setPos(-23, 18);
3591     c_2->setZValue(1);
3592     c_2_1->setPos(24, 28);
3593     c_2_1_1->setPos(-16, 16);
3594     c_2_2->setPos(-16, 28);
3595     c_2_2->setZValue(1);
3596
3597     c_1->setFlag(QGraphicsItem::ItemIsMovable);
3598     c_1_1->setFlag(QGraphicsItem::ItemIsMovable);
3599     c_1_1_1->setFlag(QGraphicsItem::ItemIsMovable);
3600     c_1_2->setFlag(QGraphicsItem::ItemIsMovable);
3601     c_2->setFlag(QGraphicsItem::ItemIsMovable);
3602     c_2_1->setFlag(QGraphicsItem::ItemIsMovable);
3603     c_2_1_1->setFlag(QGraphicsItem::ItemIsMovable);
3604     c_2_2->setFlag(QGraphicsItem::ItemIsMovable);
3605
3606     t_1->setData(0, "t_1");
3607     c_1->setData(0, "c_1");
3608     c_1_1->setData(0, "c_1_1");
3609     c_1_1_1->setData(0, "c_1_1_1");
3610     c_1_2->setData(0, "c_1_2");
3611     c_2->setData(0, "c_2");
3612     c_2_1->setData(0, "c_2_1");
3613     c_2_1_1->setData(0, "c_2_1_1");
3614     c_2_2->setData(0, "c_2_2");
3615
3616     scene.addItem(t_1);
3617
3618     foreach (QGraphicsItem *item, scene.items())
3619         item->setFlag(QGraphicsItem::ItemIsSelectable);
3620
3621     // QGraphicsView view(&scene);
3622     // view.setDragMode(QGraphicsView::RubberBandDrag);
3623     // view.show();
3624
3625     qDebug() << "items: {";
3626     foreach (QGraphicsItem *item, scene.items(32, 31, 4, 55))
3627         qDebug() << "\t" << item->data(0).toString();
3628     qDebug() << "}";
3629
3630     QCOMPARE(scene.items(32, 31, 4, 55),
3631              QList<QGraphicsItem *>()
3632              << c_1_2 << c_1_1_1 << c_1 << t_1);
3633     QCOMPARE(scene.items(-53, 47, 136, 3),
3634              QList<QGraphicsItem *>()
3635              << c_2_2 << c_2_1 << c_2 << c_1_2 << c_1_1 << c_1 << t_1);
3636     QCOMPARE(scene.items(-23, 79, 104, 3),
3637              QList<QGraphicsItem *>()
3638              << c_2_1_1 << c_1_1_1);
3639     QCOMPARE(scene.items(-26, -3, 92, 79),
3640              QList<QGraphicsItem *>()
3641              << c_2_2 << c_2_1_1 << c_2_1 << c_2
3642              << c_1_2 << c_1_1_1 << c_1_1 << c_1
3643              << t_1);
3644 }
3645
3646 class ChangedListener : public QObject
3647 {
3648     Q_OBJECT
3649 public:
3650     QList<QList<QRectF> > changes;
3651
3652 public slots:
3653     void changed(const QList<QRectF> &dirty)
3654     {
3655         changes << dirty;
3656     }
3657 };
3658
3659 void tst_QGraphicsScene::changedSignal_data()
3660 {
3661     QTest::addColumn<bool>("withView");
3662
3663     QTest::newRow("without view") << false;
3664     QTest::newRow("with view") << true;
3665 }
3666
3667 void tst_QGraphicsScene::changedSignal()
3668 {
3669     QFETCH(bool, withView);
3670     QGraphicsScene scene;
3671     ChangedListener cl;
3672     connect(&scene, SIGNAL(changed(QList<QRectF>)), &cl, SLOT(changed(QList<QRectF>)));
3673
3674     QGraphicsView *view = 0;
3675     if (withView)
3676         view = new QGraphicsView(&scene);
3677
3678     QGraphicsRectItem *rect = new QGraphicsRectItem(0, 0, 10, 10);
3679     rect->setPen(QPen(Qt::black, 0));
3680     scene.addItem(rect);
3681
3682     QCOMPARE(cl.changes.size(), 0);
3683     QTRY_COMPARE(cl.changes.size(), 1);
3684     QCOMPARE(cl.changes.at(0).size(), 1);
3685     QCOMPARE(cl.changes.at(0).first(), QRectF(0, 0, 10, 10));
3686
3687     rect->setPos(20, 0);
3688
3689     QCOMPARE(cl.changes.size(), 1);
3690     qApp->processEvents();
3691     QCOMPARE(cl.changes.size(), 2);
3692     QCOMPARE(cl.changes.at(1).size(), 2);
3693     QCOMPARE(cl.changes.at(1).first(), QRectF(0, 0, 10, 10));
3694     QCOMPARE(cl.changes.at(1).last(), QRectF(20, 0, 10, 10));
3695
3696     QCOMPARE(scene.sceneRect(), QRectF(0, 0, 30, 10));
3697
3698     if (withView)
3699         delete view;
3700 }
3701
3702 void tst_QGraphicsScene::stickyFocus_data()
3703 {
3704     QTest::addColumn<bool>("sticky");
3705     QTest::newRow("sticky") << true;
3706     QTest::newRow("not sticky") << false;
3707 }
3708
3709 void tst_QGraphicsScene::stickyFocus()
3710 {
3711     QFETCH(bool, sticky);
3712
3713     QGraphicsScene scene;
3714     QEvent activate(QEvent::WindowActivate);
3715     QApplication::sendEvent(&scene, &activate);
3716
3717     QGraphicsTextItem *text = scene.addText("Hei");
3718     text->setTextInteractionFlags(Qt::TextEditorInteraction);
3719     text->setFocus();
3720
3721     scene.setStickyFocus(sticky);
3722
3723     QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
3724     event.setScenePos(QPointF(-10, -10)); // outside item
3725     event.setButton(Qt::LeftButton);
3726     qApp->sendEvent(&scene, &event);
3727
3728     QCOMPARE(text->hasFocus(), sticky);
3729 }
3730
3731 void tst_QGraphicsScene::sendEvent()
3732 {
3733     QGraphicsScene scene;
3734     QGraphicsTextItem *item = scene.addText(QString());
3735     EventSpy *spy = new EventSpy(&scene, item, QEvent::User);
3736     QCOMPARE(spy->count(), 0);
3737     QEvent event(QEvent::User);
3738     scene.sendEvent(item, &event);
3739     QCOMPARE(spy->count(), 1);
3740 }
3741
3742 void tst_QGraphicsScene::inputMethod_data()
3743 {
3744     QTest::addColumn<int>("flags");
3745     QTest::addColumn<bool>("callFocusItem");
3746     QTest::newRow("0") << 0 << false;
3747     QTest::newRow("1") << (int)QGraphicsItem::ItemAcceptsInputMethod << false;
3748     QTest::newRow("2") << (int)QGraphicsItem::ItemIsFocusable << false;
3749     QTest::newRow("3") <<
3750         (int)(QGraphicsItem::ItemAcceptsInputMethod|QGraphicsItem::ItemIsFocusable) << true;
3751 }
3752
3753 class InputMethodTester : public QGraphicsRectItem
3754 {
3755     void inputMethodEvent(QInputMethodEvent *) { ++eventCalls; }
3756     QVariant inputMethodQuery(Qt::InputMethodQuery) const { ++queryCalls; return QVariant(); }
3757 public:
3758     int eventCalls;
3759     mutable int queryCalls;
3760 };
3761
3762 void tst_QGraphicsScene::inputMethod()
3763 {
3764     PlatformInputContext inputContext;
3765     QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
3766     inputMethodPrivate->testContext = &inputContext;
3767
3768     QFETCH(int, flags);
3769     QFETCH(bool, callFocusItem);
3770
3771     InputMethodTester *item = new InputMethodTester;
3772     item->setFlags((QGraphicsItem::GraphicsItemFlags)flags);
3773
3774     QGraphicsScene scene;
3775     QGraphicsView view(&scene);
3776     view.show();
3777     QApplication::setActiveWindow(&view);
3778     view.setFocus();
3779     QVERIFY(QTest::qWaitForWindowActive(&view));
3780     QCOMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view));
3781
3782     inputContext.m_resetCallCount = 0;
3783     inputContext.m_commitCallCount = 0;
3784     scene.addItem(item);
3785     QInputMethodEvent event;
3786
3787     scene.setFocusItem(item);
3788     QCOMPARE(!!(item->flags() & QGraphicsItem::ItemIsFocusable), scene.focusItem() == item);
3789     QCOMPARE(inputContext.m_resetCallCount, 0);
3790
3791     item->eventCalls = 0;
3792     qApp->sendEvent(&scene, &event);
3793     QCOMPARE(item->eventCalls, callFocusItem ? 1 : 0);
3794
3795     item->queryCalls = 0;
3796     scene.inputMethodQuery((Qt::InputMethodQuery)0);
3797     QCOMPARE(item->queryCalls, callFocusItem ? 1 : 0);
3798
3799     scene.setFocusItem(0);
3800     // the input context is reset twice, once because an item has lost focus and again because
3801     // the Qt::WA_InputMethodEnabled flag is cleared because no item has focus.
3802     QCOMPARE(inputContext.m_resetCallCount + inputContext.m_commitCallCount, callFocusItem ? 2 : 0);
3803     QCOMPARE(item->queryCalls, callFocusItem ? 1 : 0); // verify that value is unaffected
3804
3805     item->eventCalls = 0;
3806     qApp->sendEvent(&scene, &event);
3807     QCOMPARE(item->eventCalls, 0);
3808
3809     item->queryCalls = 0;
3810     scene.inputMethodQuery((Qt::InputMethodQuery)0);
3811     QCOMPARE(item->queryCalls, 0);
3812 }
3813
3814 void tst_QGraphicsScene::dispatchHoverOnPress()
3815 {
3816     QGraphicsScene scene;
3817     EventTester *tester1 = new EventTester;
3818     tester1->setAcceptHoverEvents(true);
3819     EventTester *tester2 = new EventTester;
3820     tester2->setAcceptHoverEvents(true);
3821     tester2->setPos(30, 30);
3822     scene.addItem(tester1);
3823     scene.addItem(tester2);
3824
3825     tester1->eventTypes.clear();
3826     tester2->eventTypes.clear();
3827
3828     {
3829         QGraphicsSceneMouseEvent me(QEvent::GraphicsSceneMousePress);
3830         me.setButton(Qt::LeftButton);
3831         me.setButtons(Qt::LeftButton);
3832         QGraphicsSceneMouseEvent me2(QEvent::GraphicsSceneMouseRelease);
3833         me2.setButton(Qt::LeftButton);
3834         qApp->sendEvent(&scene, &me);
3835         qApp->sendEvent(&scene, &me2);
3836         QCOMPARE(tester1->eventTypes, QList<QEvent::Type>()
3837                  << QEvent::GraphicsSceneHoverEnter
3838                  << QEvent::GraphicsSceneHoverMove
3839                  << QEvent::GrabMouse
3840                  << QEvent::GraphicsSceneMousePress
3841                  << QEvent::UngrabMouse);
3842         tester1->eventTypes.clear();
3843         qApp->sendEvent(&scene, &me);
3844         qApp->sendEvent(&scene, &me2);
3845         QCOMPARE(tester1->eventTypes, QList<QEvent::Type>()
3846                  << QEvent::GraphicsSceneHoverMove
3847                  << QEvent::GrabMouse
3848                  << QEvent::GraphicsSceneMousePress
3849                  << QEvent::UngrabMouse);
3850     }
3851     {
3852         QGraphicsSceneMouseEvent me(QEvent::GraphicsSceneMousePress);
3853         me.setScenePos(QPointF(30, 30));
3854         me.setButton(Qt::LeftButton);
3855         me.setButtons(Qt::LeftButton);
3856         QGraphicsSceneMouseEvent me2(QEvent::GraphicsSceneMouseRelease);
3857         me2.setScenePos(QPointF(30, 30));
3858         me2.setButton(Qt::LeftButton);
3859         tester1->eventTypes.clear();
3860         qApp->sendEvent(&scene, &me);
3861         qApp->sendEvent(&scene, &me2);
3862         qDebug() << tester1->eventTypes;
3863         QCOMPARE(tester1->eventTypes, QList<QEvent::Type>()
3864                  << QEvent::GraphicsSceneHoverLeave);
3865         QCOMPARE(tester2->eventTypes, QList<QEvent::Type>()
3866                  << QEvent::GraphicsSceneHoverEnter
3867                  << QEvent::GraphicsSceneHoverMove
3868                  << QEvent::GrabMouse
3869                  << QEvent::GraphicsSceneMousePress
3870                  << QEvent::UngrabMouse);
3871         tester2->eventTypes.clear();
3872         qApp->sendEvent(&scene, &me);
3873         qApp->sendEvent(&scene, &me2);
3874         QCOMPARE(tester2->eventTypes, QList<QEvent::Type>()
3875                  << QEvent::GraphicsSceneHoverMove
3876                  << QEvent::GrabMouse
3877                  << QEvent::GraphicsSceneMousePress
3878                  << QEvent::UngrabMouse);
3879     }
3880 }
3881
3882 void tst_QGraphicsScene::initialFocus_data()
3883 {
3884     QTest::addColumn<bool>("activeScene");
3885     QTest::addColumn<bool>("explicitSetFocus");
3886     QTest::addColumn<bool>("isPanel");
3887     QTest::addColumn<bool>("shouldHaveFocus");
3888
3889     QTest::newRow("inactive scene, normal item") << false << false << false << false;
3890     QTest::newRow("inactive scene, panel item") << false << false << true << false;
3891     QTest::newRow("inactive scene, normal item, explicit focus") << false << true << false << true;
3892     QTest::newRow("inactive scene, panel, explicit focus") << false << true << true << true;
3893     QTest::newRow("active scene, normal item") << true << false << false << false;
3894     QTest::newRow("active scene, panel item") << true << false << true << false;
3895     QTest::newRow("active scene, normal item, explicit focus") << true << true << false << true;
3896     QTest::newRow("active scene, panel, explicit focus") << true << true << true << true;
3897 }
3898
3899 void tst_QGraphicsScene::initialFocus()
3900 {
3901     QFETCH(bool, activeScene);
3902     QFETCH(bool, explicitSetFocus);
3903     QFETCH(bool, isPanel);
3904     QFETCH(bool, shouldHaveFocus);
3905
3906     QGraphicsRectItem *rect = new QGraphicsRectItem;
3907     rect->setFlag(QGraphicsItem::ItemIsFocusable);
3908     QVERIFY(!rect->hasFocus());
3909
3910     if (isPanel)
3911         rect->setFlag(QGraphicsItem::ItemIsPanel);
3912
3913     // Setting focus on an item before adding to the scene will ensure
3914     // it gets focus when the scene is activated.
3915     if (explicitSetFocus)
3916         rect->setFocus();
3917
3918     QGraphicsScene scene;
3919     QVERIFY(!scene.isActive());
3920
3921     if (activeScene) {
3922         QEvent windowActivate(QEvent::WindowActivate);
3923         qApp->sendEvent(&scene, &windowActivate);
3924         scene.setFocus();
3925     }
3926
3927     scene.addItem(rect);
3928
3929     if (!activeScene) {
3930         QEvent windowActivate(QEvent::WindowActivate);
3931         qApp->sendEvent(&scene, &windowActivate);
3932         scene.setFocus();
3933     }
3934
3935     QCOMPARE(rect->hasFocus(), shouldHaveFocus);
3936 }
3937
3938 class PolishItem : public QGraphicsTextItem
3939 {
3940 public:
3941     PolishItem(QGraphicsItem *parent = 0)
3942         : QGraphicsTextItem(parent), polished(false), deleteChildrenInPolish(true), addChildrenInPolish(false) { }
3943
3944     bool polished;
3945     bool deleteChildrenInPolish;
3946     bool addChildrenInPolish;
3947 protected:
3948     QVariant itemChange(GraphicsItemChange change, const QVariant& value)
3949     {
3950         if (change == ItemVisibleChange) {
3951             polished = true;
3952             if (deleteChildrenInPolish)
3953                 qDeleteAll(childItems());
3954             if (addChildrenInPolish) {
3955                 for (int i = 0; i < 10; ++i)
3956                     new PolishItem(this);
3957             }
3958         }
3959         return QGraphicsItem::itemChange(change, value);
3960     }
3961 };
3962
3963 void tst_QGraphicsScene::polishItems()
3964 {
3965     QGraphicsScene scene;
3966     PolishItem *parent = new PolishItem;
3967     scene.addItem(parent);
3968     PolishItem *child = new PolishItem(parent);
3969     Q_UNUSED(child)
3970     // test that QGraphicsScenePrivate::_q_polishItems() doesn't crash
3971     QMetaObject::invokeMethod(&scene,"_q_polishItems");
3972 }
3973
3974 void tst_QGraphicsScene::polishItems2()
3975 {
3976     QGraphicsScene scene;
3977     PolishItem *item = new PolishItem;
3978     item->addChildrenInPolish = true;
3979     item->deleteChildrenInPolish = true;
3980     // These children should be deleted in the polish.
3981     for (int i = 0; i < 20; ++i)
3982         new PolishItem(item);
3983     scene.addItem(item);
3984
3985     // Wait for the polish event to be delivered.
3986     QVERIFY(!item->polished);
3987     QApplication::sendPostedEvents(&scene, QEvent::MetaCall);
3988     QVERIFY(item->polished);
3989
3990     // We deleted the children we added above, but we also
3991     // added 10 new children. These should be polished in the next
3992     // event loop iteration.
3993     QList<QGraphicsItem *> children = item->childItems();
3994     QCOMPARE(children.count(), 10);
3995     foreach (QGraphicsItem *child, children)
3996         QVERIFY(!static_cast<PolishItem *>(child)->polished);
3997
3998     QApplication::sendPostedEvents(&scene, QEvent::MetaCall);
3999     foreach (QGraphicsItem *child, children)
4000         QVERIFY(static_cast<PolishItem *>(child)->polished);
4001 }
4002
4003 void tst_QGraphicsScene::isActive()
4004 {
4005     QGraphicsScene scene1;
4006     QVERIFY(!scene1.isActive());
4007     QGraphicsScene scene2;
4008     QVERIFY(!scene2.isActive());
4009
4010     {
4011         QWidget toplevel1;
4012         QHBoxLayout *layout = new QHBoxLayout;
4013         toplevel1.setLayout(layout);
4014         QGraphicsView *view1 = new QGraphicsView(&scene1);
4015         QGraphicsView *view2 = new QGraphicsView(&scene2);
4016         layout->addWidget(view1);
4017         layout->addWidget(view2);
4018
4019         QVERIFY(!scene1.isActive());
4020         QVERIFY(!scene2.isActive());
4021
4022         view1->setVisible(false);
4023
4024         toplevel1.show();
4025         QApplication::setActiveWindow(&toplevel1);
4026         QVERIFY(QTest::qWaitForWindowActive(&toplevel1));
4027         QCOMPARE(QApplication::activeWindow(), &toplevel1);
4028
4029         QVERIFY(!scene1.isActive()); //it is hidden;
4030         QVERIFY(scene2.isActive());
4031         QVERIFY(!scene1.hasFocus());
4032         QVERIFY(scene2.hasFocus());
4033
4034         view1->show();
4035         QVERIFY(scene1.isActive());
4036         QVERIFY(scene2.isActive());
4037         QVERIFY(!scene1.hasFocus());
4038         QVERIFY(scene2.hasFocus());
4039
4040         view2->hide();
4041
4042         QVERIFY(scene1.isActive());
4043         QVERIFY(!scene2.isActive());
4044         QVERIFY(scene1.hasFocus());
4045         QVERIFY(!scene2.hasFocus());
4046
4047         toplevel1.hide();
4048         QTest::qWait(50);
4049         QTRY_VERIFY(!scene1.isActive());
4050         QTRY_VERIFY(!scene2.isActive());
4051         QVERIFY(!scene1.hasFocus());
4052         QVERIFY(!scene2.hasFocus());
4053
4054         toplevel1.show();
4055         QApplication::setActiveWindow(&toplevel1);
4056         QVERIFY(QTest::qWaitForWindowActive(&toplevel1));
4057         QCOMPARE(QApplication::activeWindow(), &toplevel1);
4058
4059         QTRY_VERIFY(scene1.isActive());
4060         QTRY_VERIFY(!scene2.isActive());
4061         QVERIFY(scene1.hasFocus());
4062         QVERIFY(!scene2.hasFocus());
4063
4064         view2->show();
4065         QVERIFY(scene1.isActive());
4066         QVERIFY(scene2.isActive());
4067         QVERIFY(scene1.hasFocus());
4068         QVERIFY(!scene2.hasFocus());
4069     }
4070
4071     QVERIFY(!scene1.isActive());
4072     QVERIFY(!scene2.isActive());
4073     QVERIFY(!scene1.hasFocus());
4074     QVERIFY(!scene2.hasFocus());
4075
4076
4077     {
4078         QWidget toplevel2;
4079         QHBoxLayout *layout = new QHBoxLayout;
4080         toplevel2.setLayout(layout);
4081         QGraphicsView *view1 = new QGraphicsView(&scene1);
4082         QGraphicsView *view2 = new QGraphicsView();
4083         layout->addWidget(view1);
4084         layout->addWidget(view2);
4085
4086         QVERIFY(!scene1.isActive());
4087         QVERIFY(!scene2.isActive());
4088         QVERIFY(!scene1.hasFocus());
4089         QVERIFY(!scene2.hasFocus());
4090
4091         toplevel2.show();
4092         QApplication::setActiveWindow(&toplevel2);
4093         QVERIFY(QTest::qWaitForWindowActive(&toplevel2));
4094         QCOMPARE(QApplication::activeWindow(), &toplevel2);
4095
4096         QVERIFY(scene1.isActive());
4097         QVERIFY(!scene2.isActive());
4098         QVERIFY(scene1.hasFocus());
4099         QVERIFY(!scene2.hasFocus());
4100
4101         view2->setScene(&scene2);
4102
4103         QVERIFY(scene1.isActive());
4104         QVERIFY(scene2.isActive());
4105         QVERIFY(scene1.hasFocus());
4106         QVERIFY(!scene2.hasFocus());
4107
4108         view1->setScene(&scene2);
4109         QVERIFY(!scene1.isActive());
4110         QVERIFY(scene2.isActive());
4111         QVERIFY(!scene1.hasFocus());
4112         QVERIFY(scene2.hasFocus());
4113
4114         view1->hide();
4115         QVERIFY(!scene1.isActive());
4116         QVERIFY(scene2.isActive());
4117         QVERIFY(!scene1.hasFocus());
4118         QVERIFY(scene2.hasFocus());
4119
4120         view1->setScene(&scene1);
4121         QVERIFY(!scene1.isActive());
4122         QVERIFY(scene2.isActive());
4123         QVERIFY(!scene1.hasFocus());
4124         QVERIFY(scene2.hasFocus());
4125
4126         view1->show();
4127         QVERIFY(scene1.isActive());
4128         QVERIFY(scene2.isActive());
4129         QVERIFY(!scene1.hasFocus());
4130         QVERIFY(scene2.hasFocus());
4131
4132         view2->hide();
4133         QVERIFY(scene1.isActive());
4134         QVERIFY(!scene2.isActive());
4135         QVERIFY(scene1.hasFocus());
4136         QVERIFY(!scene2.hasFocus());
4137
4138         QGraphicsView topLevelView;
4139         topLevelView.show();
4140         QApplication::setActiveWindow(&topLevelView);
4141         topLevelView.setFocus();
4142         QVERIFY(QTest::qWaitForWindowActive(&topLevelView));
4143         QCOMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&topLevelView));
4144
4145         QVERIFY(!scene1.isActive());
4146         QVERIFY(!scene2.isActive());
4147         QVERIFY(!scene1.hasFocus());
4148         QVERIFY(!scene2.hasFocus());
4149
4150         topLevelView.setScene(&scene1);
4151         QVERIFY(scene1.isActive());
4152         QVERIFY(!scene2.isActive());
4153         QVERIFY(scene1.hasFocus());
4154         QVERIFY(!scene2.hasFocus());
4155
4156         view2->show();
4157         QVERIFY(scene1.isActive());
4158         QVERIFY(!scene2.isActive());
4159         QVERIFY(scene1.hasFocus());
4160         QVERIFY(!scene2.hasFocus());
4161
4162         view1->hide();
4163         QVERIFY(scene1.isActive());
4164         QVERIFY(!scene2.isActive());
4165         QVERIFY(scene1.hasFocus());
4166         QVERIFY(!scene2.hasFocus());
4167
4168         QApplication::setActiveWindow(&toplevel2);
4169         QVERIFY(QTest::qWaitForWindowActive(&toplevel2));
4170
4171         QVERIFY(!scene1.isActive());
4172         QVERIFY(scene2.isActive());
4173         QVERIFY(!scene1.hasFocus());
4174         QVERIFY(scene2.hasFocus());
4175     }
4176
4177     QVERIFY(!scene1.isActive());
4178     QVERIFY(!scene2.isActive());
4179     QVERIFY(!scene1.hasFocus());
4180     QVERIFY(!scene2.hasFocus());
4181
4182     {
4183         QWidget toplevel3;
4184         QHBoxLayout *layout = new QHBoxLayout;
4185         toplevel3.setLayout(layout);
4186         QGraphicsView *view1 = new QGraphicsView(&scene1);
4187         QGraphicsView *view2 = new QGraphicsView(&scene2);
4188         layout->addWidget(view1);
4189
4190         QVERIFY(!scene1.isActive());
4191         QVERIFY(!scene2.isActive());
4192         QVERIFY(!scene1.hasFocus());
4193         QVERIFY(!scene2.hasFocus());
4194
4195
4196         toplevel3.show();
4197         QApplication::setActiveWindow(&toplevel3);
4198         QVERIFY(QTest::qWaitForWindowActive(&toplevel3));
4199         QCOMPARE(QApplication::activeWindow(), &toplevel3);
4200
4201         QVERIFY(scene1.isActive());
4202         QVERIFY(!scene2.isActive());
4203         QVERIFY(scene1.hasFocus());
4204         QVERIFY(!scene2.hasFocus());
4205
4206         layout->addWidget(view2);
4207         QApplication::processEvents();
4208         QVERIFY(scene1.isActive());
4209         QVERIFY(scene2.isActive());
4210         QVERIFY(scene1.hasFocus());
4211         QVERIFY(!scene2.hasFocus());
4212
4213         view1->setParent(0);
4214         QVERIFY(!scene1.isActive());
4215         QVERIFY(scene2.isActive());
4216         QVERIFY(!scene1.hasFocus());
4217         QVERIFY(scene2.hasFocus());
4218         delete view1;
4219     }
4220
4221     QVERIFY(!scene1.isActive());
4222     QVERIFY(!scene2.isActive());
4223     QVERIFY(!scene1.hasFocus());
4224     QVERIFY(!scene2.hasFocus());
4225
4226 }
4227
4228 void tst_QGraphicsScene::siblingIndexAlwaysValid()
4229 {
4230     QGraphicsScene scene;
4231
4232     QGraphicsWidget *parent = new QGraphicsWidget;
4233     parent->setZValue(350);
4234     parent->setGeometry(0, 0, 100, 100);
4235     QGraphicsWidget *parent2 = new QGraphicsWidget;
4236     parent2->setGeometry(10, 10, 50, 50);
4237     QGraphicsWidget *child = new QGraphicsWidget(parent2);
4238     child->setGeometry(15, 15, 25, 25);
4239     child->setZValue(150);
4240     //Both are top level
4241     scene.addItem(parent);
4242     scene.addItem(parent2);
4243
4244     //Then we make the child a top level
4245     child->setParentItem(0);
4246
4247     //This is trigerred by a repaint...
4248     QGraphicsScenePrivate::get(&scene)->index->estimateTopLevelItems(QRectF(), Qt::AscendingOrder);
4249
4250     delete child;
4251
4252     //If there are in the list that's bad, we crash...
4253     QVERIFY(!QGraphicsScenePrivate::get(&scene)->topLevelItems.contains(static_cast<QGraphicsItem *>(child)));
4254
4255     //Other case
4256     QGraphicsScene scene2;
4257     // works with bsp tree index
4258     scene2.setItemIndexMethod(QGraphicsScene::NoIndex);
4259
4260     QGraphicsView view2(&scene2);
4261
4262     // first add the blue rect
4263     QGraphicsRectItem* const item1 = new QGraphicsRectItem(QRect( 10, 10, 10, 10 ));
4264     item1->setPen(QPen(Qt::blue, 0));
4265     item1->setBrush(Qt::blue);
4266     scene2.addItem(item1);
4267
4268     // then add the red rect
4269     QGraphicsRectItem* const item2 = new QGraphicsRectItem(5, 5, 10, 10);
4270     item2->setPen(QPen(Qt::red, 0));
4271     item2->setBrush(Qt::red);
4272     scene2.addItem(item2);
4273
4274     // now the blue one is visible on top of the red one -> swap them (important for the bug)
4275     item1->setZValue(1.0);
4276     item2->setZValue(0.0);
4277
4278     view2.show();
4279
4280     // handle events as a real life app would do
4281     QApplication::processEvents();
4282
4283     // now delete the red rect
4284     delete item2;
4285
4286     // handle events as a real life app would do
4287      QApplication::processEvents();
4288
4289      //We should not crash
4290
4291 }
4292
4293 void tst_QGraphicsScene::removeFullyTransparentItem()
4294 {
4295     QGraphicsScene scene;
4296
4297     QGraphicsItem *parent = scene.addRect(0, 0, 100, 100);
4298     parent->setFlag(QGraphicsItem::ItemHasNoContents);
4299
4300     QGraphicsItem *child = scene.addRect(0, 0, 100, 100);
4301     child->setParentItem(parent);
4302
4303     CustomView view;
4304     view.setScene(&scene);
4305     view.show();
4306     qApp->setActiveWindow(&view);
4307     QVERIFY(QTest::qWaitForWindowActive(&view));
4308
4309     // NB! The parent has the ItemHasNoContents flag set, which means
4310     // the parent itself doesn't generate any update requests, only the
4311     // child can possibly trigger an update. Also note that the child
4312     // is removed before processing events.
4313     view.repaints = 0;
4314     parent->setOpacity(0);
4315     QVERIFY(qFuzzyIsNull(child->effectiveOpacity()));
4316     scene.removeItem(child);
4317     QVERIFY(!scene.items().contains(child));
4318     QTRY_VERIFY(view.repaints > 0);
4319
4320     // Re-add child. There's nothing new to display (child is still
4321     // effectively hidden), so it shouldn't trigger an update.
4322     view.repaints = 0;
4323     child->setParentItem(parent);
4324     QVERIFY(scene.items().contains(child));
4325     QVERIFY(qFuzzyIsNull(child->effectiveOpacity()));
4326     QApplication::processEvents();
4327     QCOMPARE(view.repaints, 0);
4328
4329     // Nothing is visible on the screen, removing child item shouldn't trigger an update.
4330     scene.removeItem(child);
4331     QApplication::processEvents();
4332     QCOMPARE(view.repaints, 0);
4333     delete child;
4334 }
4335
4336 void tst_QGraphicsScene::taskQTBUG_5904_crashWithDeviceCoordinateCache()
4337 {
4338     QGraphicsScene scene;
4339     QGraphicsRectItem *rectItem = scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
4340
4341     rectItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
4342
4343     QPixmap pixmap(100,200);
4344     QPainter painter(&pixmap);
4345     painter.setRenderHint(QPainter::Antialiasing);
4346     scene.render(&painter);
4347     painter.end();
4348     // No crash, then it passed!
4349 }
4350
4351 void tst_QGraphicsScene::taskQT657_paintIntoCacheWithTransparentParts()
4352 {
4353     // Test using DeviceCoordinateCache and opaque item
4354     QWidget *w = new QWidget();
4355     w->setPalette(QColor(0, 0, 255));
4356     w->setGeometry(0, 0, 50, 50);
4357
4358     QGraphicsScene *scene = new QGraphicsScene();
4359     CustomView *view = new CustomView;
4360     view->setScene(scene);
4361
4362     QGraphicsProxyWidget *proxy = scene->addWidget(w);
4363     proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
4364     proxy->rotate(15);
4365
4366     view->show();
4367     QVERIFY(QTest::qWaitForWindowExposed(view));
4368     view->repaints = 0;
4369     proxy->update(10, 10, 10, 10);
4370     QTest::qWait(50);
4371     QTRY_VERIFY(view->repaints > 0);
4372
4373     QPixmap pix;
4374     QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(proxy);
4375     QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->deviceData.value(view->viewport()).key, &pix));
4376
4377     QTransform t = proxy->sceneTransform();
4378     // Map from scene coordinates to pixmap coordinates.
4379     // X origin in the pixmap is the most-left point
4380     // of the item's boundingRect in the scene.
4381     qreal adjust = t.mapRect(proxy->boundingRect().toRect()).left();
4382     QRect rect = t.mapRect(QRect(10, 10, 10, 10)).adjusted(-adjust, 0, -adjust + 1, 1);
4383     QPixmap subpix = pix.copy(rect);
4384
4385     QImage im = subpix.toImage();
4386     for(int i = 0; i < im.width(); i++) {
4387         for(int j = 0; j < im.height(); j++)
4388             QCOMPARE(qAlpha(im.pixel(i, j)), 255);
4389     }
4390
4391     delete w;
4392 }
4393
4394 void tst_QGraphicsScene::taskQTBUG_7863_paintIntoCacheWithTransparentParts()
4395 {
4396     // Test using DeviceCoordinateCache and semi-transparent item
4397     {
4398         QGraphicsRectItem *backItem = new QGraphicsRectItem(0, 0, 100, 100);
4399         backItem->setBrush(QColor(255, 255, 0));
4400         QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
4401         rectItem->setBrush(QColor(0, 0, 255, 125));
4402         rectItem->setParentItem(backItem);
4403
4404         QGraphicsScene *scene = new QGraphicsScene();
4405         CustomView *view = new CustomView;
4406         view->setScene(scene);
4407
4408         scene->addItem(backItem);
4409         rectItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
4410         backItem->rotate(15);
4411
4412         view->show();
4413         QVERIFY(QTest::qWaitForWindowExposed(view));
4414         view->repaints = 0;
4415         rectItem->update(10, 10, 10, 10);
4416         QTest::qWait(50);
4417         QTRY_VERIFY(view->repaints > 0);
4418
4419         QPixmap pix;
4420         QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
4421         QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->deviceData.value(view->viewport()).key, &pix));
4422
4423         QTransform t = rectItem->sceneTransform();
4424         // Map from scene coordinates to pixmap coordinates.
4425         // X origin in the pixmap is the most-left point
4426         // of the item's boundingRect in the scene.
4427         qreal adjust = t.mapRect(rectItem->boundingRect().toRect()).left();
4428         QRect rect = t.mapRect(QRect(10, 10, 10, 10)).adjusted(-adjust, 0, -adjust + 1, 1);
4429         QPixmap subpix = pix.copy(rect);
4430
4431         QImage im = subpix.toImage();
4432         for(int i = 0; i < im.width(); i++) {
4433             for(int j = 0; j < im.height(); j++) {
4434                 QCOMPARE(qAlpha(im.pixel(i, j)), 125);
4435             }
4436         }
4437
4438         delete view;
4439     }
4440
4441     // Test using ItemCoordinateCache and opaque item
4442     {
4443         QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
4444         rectItem->setBrush(QColor(0, 0, 255));
4445
4446         QGraphicsScene *scene = new QGraphicsScene();
4447         CustomView *view = new CustomView;
4448         view->setScene(scene);
4449
4450         scene->addItem(rectItem);
4451         rectItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
4452         rectItem->rotate(15);
4453
4454         view->show();
4455         QVERIFY(QTest::qWaitForWindowExposed(view));
4456         view->repaints = 0;
4457         rectItem->update(10, 10, 10, 10);
4458         QTest::qWait(50);
4459         QTRY_VERIFY(view->repaints > 0);
4460
4461         QPixmap pix;
4462         QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
4463         QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->key, &pix));
4464
4465         QTransform t = rectItem->sceneTransform();
4466         // Map from scene coordinates to pixmap coordinates.
4467         // X origin in the pixmap is the most-left point
4468         // of the item's boundingRect in the scene.
4469         qreal adjust = t.mapRect(rectItem->boundingRect().toRect()).left();
4470         QRect rect = t.mapRect(QRect(10, 10, 10, 10)).adjusted(-adjust, 0, -adjust + 1, 1);
4471         QPixmap subpix = pix.copy(rect);
4472
4473         QImage im = subpix.toImage();
4474         for(int i = 0; i < im.width(); i++) {
4475             for(int j = 0; j < im.height(); j++)
4476                 QCOMPARE(qAlpha(im.pixel(i, j)), 255);
4477         }
4478
4479         delete view;
4480     }
4481
4482     // Test using ItemCoordinateCache and semi-transparent item
4483     {
4484         QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
4485         rectItem->setBrush(QColor(0, 0, 255, 125));
4486
4487         QGraphicsScene *scene = new QGraphicsScene();
4488         CustomView *view = new CustomView;
4489         view->setScene(scene);
4490
4491         scene->addItem(rectItem);
4492         rectItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
4493         rectItem->rotate(15);
4494
4495         view->show();
4496         QVERIFY(QTest::qWaitForWindowExposed(view));
4497         view->repaints = 0;
4498         rectItem->update(10, 10, 10, 10);
4499         QTest::qWait(50);
4500         QTRY_VERIFY(view->repaints > 0);
4501
4502         QPixmap pix;
4503         QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
4504         QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->key, &pix));
4505
4506         QTransform t = rectItem->sceneTransform();
4507         // Map from scene coordinates to pixmap coordinates.
4508         // X origin in the pixmap is the most-left point
4509         // of the item's boundingRect in the scene.
4510         qreal adjust = t.mapRect(rectItem->boundingRect().toRect()).left();
4511         QRect rect = t.mapRect(QRect(10, 10, 10, 10)).adjusted(-adjust, 0, -adjust + 1, 1);
4512         QPixmap subpix = pix.copy(rect);
4513
4514         QImage im = subpix.toImage();
4515         for(int i = 0; i < im.width(); i++) {
4516             for(int j = 0; j < im.height(); j++)
4517                 QCOMPARE(qAlpha(im.pixel(i, j)), 125);
4518         }
4519
4520         delete view;
4521     }
4522 }
4523
4524 void tst_QGraphicsScene::taskQT_3674_doNotCrash()
4525 {
4526     QGraphicsScene scene;
4527
4528     QGraphicsView view(&scene);
4529     view.resize(200, 200);
4530
4531     QPixmap pixmap(view.size());
4532     QPainter painter(&pixmap);
4533     view.render(&painter);
4534     painter.end();
4535
4536     scene.addItem(new QGraphicsWidget);
4537     scene.setBackgroundBrush(Qt::green);
4538
4539     QApplication::processEvents();
4540     QApplication::processEvents();
4541 }
4542
4543 void tst_QGraphicsScene::zeroScale()
4544 {
4545     //should not crash
4546     QGraphicsScene scene;
4547     scene.setSceneRect(-100, -100, 100, 100);
4548     QGraphicsView view(&scene);
4549
4550     ChangedListener cl;
4551     connect(&scene, SIGNAL(changed(QList<QRectF>)), &cl, SLOT(changed(QList<QRectF>)));
4552
4553     QGraphicsRectItem *rect1 = new QGraphicsRectItem(0, 0, 0.0000001, 0.00000001);
4554     scene.addItem(rect1);
4555     rect1->setRotation(82);
4556     rect1->setScale(0.00000001);
4557
4558     QApplication::processEvents();
4559     QTRY_COMPARE(cl.changes.count(), 1);
4560     QGraphicsRectItem *rect2 = new QGraphicsRectItem(-0.0000001, -0.0000001, 0.0000001, 0.0000001);
4561     rect2->setScale(0.00000001);
4562     scene.addItem(rect2);
4563     rect1->setPos(20,20);
4564     QApplication::processEvents();
4565     QTRY_COMPARE(cl.changes.count(), 2);
4566 }
4567
4568 void tst_QGraphicsScene::taskQTBUG_15977_renderWithDeviceCoordinateCache()
4569 {
4570     QGraphicsScene scene;
4571     scene.setSceneRect(0, 0, 100, 100);
4572     QGraphicsRectItem *rect = scene.addRect(0, 0, 100, 100);
4573     rect->setPen(Qt::NoPen);
4574     rect->setBrush(Qt::red);
4575     rect->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
4576
4577     QImage image(100, 100, QImage::Format_RGB32);
4578     QPainter p(&image);
4579     scene.render(&p);
4580     p.end();
4581
4582     QImage expected(100, 100, QImage::Format_RGB32);
4583     p.begin(&expected);
4584     p.fillRect(expected.rect(), Qt::red);
4585     p.end();
4586
4587     QCOMPARE(image, expected);
4588 }
4589
4590 void tst_QGraphicsScene::taskQTBUG_16401_focusItem()
4591 {
4592     QGraphicsScene scene;
4593     QGraphicsView view(&scene);
4594     QGraphicsRectItem *rect = scene.addRect(0, 0, 100, 100);
4595     rect->setFlag(QGraphicsItem::ItemIsFocusable);
4596
4597     view.show();
4598     QApplication::setActiveWindow(&view);
4599     QVERIFY(QTest::qWaitForWindowActive(&view));
4600
4601     QVERIFY(!scene.focusItem());
4602
4603     rect->setFocus();
4604     QCOMPARE(scene.focusItem(), rect);
4605     QFocusEvent focusOut(QEvent::FocusOut);
4606     QApplication::sendEvent(&view, &focusOut);
4607     QVERIFY(!scene.focusItem());
4608     QFocusEvent focusIn(QEvent::FocusIn);
4609     QApplication::sendEvent(&view, &focusIn);
4610     QCOMPARE(scene.focusItem(), rect);
4611
4612     rect->clearFocus();
4613     QVERIFY(!scene.focusItem());
4614     QApplication::sendEvent(&view, &focusOut);
4615     QVERIFY(!scene.focusItem());
4616     QApplication::sendEvent(&view, &focusIn);
4617     QVERIFY(!scene.focusItem());
4618 }
4619
4620 QTEST_MAIN(tst_QGraphicsScene)
4621 #include "tst_qgraphicsscene.moc"