1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
44 #if defined(Q_OS_WINCE)
50 #include <private/qgraphicsscene_p.h>
51 #include <private/qgraphicssceneindex_p.h>
53 #include "../../../gui/painting/qpathclipper/pathcompare.h"
54 #include "../../../shared/platforminputcontext.h"
55 #include <private/qinputmethod_p.h>
57 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
59 #define Q_CHECK_PAINTEVENTS \
60 if (::SwitchDesktop(::GetThreadDesktop(::GetCurrentThreadId())) == 0) \
61 QSKIP("The Graphics View doesn't get the paint events");
63 #define Q_CHECK_PAINTEVENTS
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)
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};
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};
118 class HoverItem : public QGraphicsRectItem
122 : QGraphicsRectItem(QRectF(-10, -10, 20, 20)), isHovered(false)
123 { setAcceptsHoverEvents(true); }
128 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
130 isHovered = (option->state & QStyle::State_MouseOver);
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);
138 painter->setBrush(QColor(Qt::blue).light(120));
140 painter->setBrush(Qt::gray);
142 painter->drawRoundRect(boundingRect().adjusted(0, 0, -5, -5));
146 class EventSpy : public QGraphicsWidget
150 EventSpy(QObject *watched, QEvent::Type type)
151 : _count(0), spied(type)
153 watched->installEventFilter(this);
156 EventSpy(QGraphicsScene *scene, QGraphicsItem *watched, QEvent::Type type)
157 : _count(0), spied(type)
159 scene->addItem(this);
160 watched->installSceneEventFilter(this);
163 int count() const { return _count; }
166 bool eventFilter(QObject *watched, QEvent *event)
169 if (event->type() == spied)
174 bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
177 if (event->type() == spied)
186 class tst_QGraphicsScene : public QObject
196 void itemIndexMethod();
198 void itemsBoundingRect_data();
199 void itemsBoundingRect();
201 void items_QPointF_data();
202 void items_QPointF();
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();
222 void focusItemLostFocus();
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();
241 void renderItemsWithNegativeWidthOrHeight();
242 void contextMenuEvent();
243 void contextMenuEvent_ItemIgnoresTransformations();
248 void eventsToDisabledItems();
250 void tabFocus_emptyScene();
251 void tabFocus_sceneWithFocusableItems();
252 void tabFocus_sceneWithFocusWidgets();
253 void tabFocus_sceneWithNestedFocusWidgets();
257 void changedSignal_data();
258 void changedSignal();
259 void stickyFocus_data();
262 void inputMethod_data();
264 void dispatchHoverOnPress();
265 void initialFocus_data();
270 void siblingIndexAlwaysValid();
271 void removeFullyTransparentItem();
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();
288 void tst_QGraphicsScene::initTestCase()
290 #ifdef Q_OS_WINCE //disable magic for WindowsCE
291 qApp->setAutoMaximizeThreshold(-1);
295 void tst_QGraphicsScene::cleanup()
297 // ensure not even skipped tests with custom input context leave it dangling
298 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
299 inputMethodPrivate->testContext = 0;
302 void tst_QGraphicsScene::construction()
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());
318 void tst_QGraphicsScene::sceneRect()
320 QGraphicsScene scene;
321 QSignalSpy sceneRectChanged(&scene, SIGNAL(sceneRectChanged(QRectF)));
322 QCOMPARE(scene.sceneRect(), QRectF());
323 QCOMPARE(sceneRectChanged.count(), 0);
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);
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());
338 QCOMPARE(scene.sceneRect(), QRectF(-5, -5, 15, 15));
339 QCOMPARE(sceneRectChanged.count(), 2);
340 QCOMPARE(sceneRectChanged.last().at(0).toRectF(), scene.sceneRect());
342 scene.setSceneRect(-100, -100, 10, 10);
343 QCOMPARE(sceneRectChanged.count(), 3);
344 QCOMPARE(sceneRectChanged.last().at(0).toRectF(), scene.sceneRect());
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());
354 scene.setSceneRect(QRectF());
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());
364 void tst_QGraphicsScene::itemIndexMethod()
366 QGraphicsScene scene;
367 QCOMPARE(scene.itemIndexMethod(), QGraphicsScene::BspTreeIndex);
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;
375 const int minY = -1000;
376 const int maxY = 2000;
377 const int minX = -1000;
378 const int maxX = 2000;
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));
386 QCOMPARE(scene.itemAt(x, y), item);
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++));
397 scene.setItemIndexMethod(QGraphicsScene::NoIndex);
398 QCOMPARE(scene.itemIndexMethod(), QGraphicsScene::NoIndex);
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++));
406 scene.setItemIndexMethod(QGraphicsScene::BspTreeIndex);
407 QCOMPARE(scene.itemIndexMethod(), QGraphicsScene::BspTreeIndex);
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++));
416 void tst_QGraphicsScene::bspTreeDepth()
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);
428 void tst_QGraphicsScene::items()
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;
436 const int minY = -1000;
437 const int maxY = 2000;
438 const int minX = -1000;
439 const int maxX = 2000;
443 QGraphicsScene scene;
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));
450 QCOMPARE(scene.items().size(), items.size());
451 scene.itemAt(0, 0); // trigger indexing
453 scene.removeItem(items.at(5));
455 QVERIFY(!scene.items().contains(0));
457 QVERIFY(!scene.items().contains(0));
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;
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));
475 void tst_QGraphicsScene::itemsBoundingRect_data()
477 QTest::addColumn<QList<QRectF> >("rects");
478 QTest::addColumn<QMatrix>("matrix");
479 QTest::addColumn<QRectF>("boundingRect");
481 QMatrix transformationMatrix;
482 transformationMatrix.translate(50, -50);
483 transformationMatrix.scale(2, 2);
484 transformationMatrix.rotate(90);
486 QTest::newRow("none")
490 QTest::newRow("{{0, 0, 10, 10}}")
491 << (QList<QRectF>() << QRectF(0, 0, 10, 10))
493 << QRectF(0, 0, 10, 10);
494 QTest::newRow("{{-10, -10, 10, 10}}")
495 << (QList<QRectF>() << QRectF(-10, -10, 10, 10))
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))
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);
516 for (int i = 0; i < 256; ++i)
517 all << QRectF(randomX[i], randomY[i], 10, 10);
521 << QRectF(-980, -994, 1988, 1983);
522 QTest::newRow("transformed all")
524 << transformationMatrix
525 << QRectF(-1928, -2010, 3966, 3976);
528 void tst_QGraphicsScene::itemsBoundingRect()
530 QFETCH(QList<QRectF>, rects);
531 QFETCH(QMatrix, matrix);
532 QFETCH(QRectF, boundingRect);
534 QGraphicsScene scene;
536 foreach (QRectF rect, rects) {
539 QGraphicsPathItem *item = scene.addPath(path);
540 item->setPen(QPen(Qt::black, 0));
541 item->setMatrix(matrix);
544 QCOMPARE(scene.itemsBoundingRect(), boundingRect);
547 void tst_QGraphicsScene::items_QPointF_data()
549 QTest::addColumn<QList<QRectF> >("items");
550 QTest::addColumn<QPointF>("point");
551 QTest::addColumn<QList<int> >("itemsAtPoint");
553 QTest::newRow("empty")
558 << (QList<QRectF>() << QRectF(0, 0, 10, 10))
560 << (QList<int>() << 0);
562 << (QList<QRectF>() << QRectF(0, 0, 10, 10))
564 << (QList<int>() << 0);
566 << (QList<QRectF>() << QRectF(0, 0, 10, 10))
568 << (QList<int>() << 0);
570 << (QList<QRectF>() << QRectF(0, 0, 10, 10))
574 << (QList<QRectF>() << QRectF(0, 0, 10, 10) << QRectF(9.9, 9.9, 10, 10))
576 << (QList<int>() << 1 << 0);
578 << (QList<QRectF>() << QRectF(0, 0, 10, 10) << QRectF(10, 10, 10, 10))
580 << (QList<int>() << 1);
582 << (QList<QRectF>() << QRectF(5, 5, 10, 10) << QRectF(10, 10, 10, 10))
584 << (QList<int>() << 1 << 0);
586 << (QList<QRectF>() << QRectF(5, 5, 10, 10) << QRectF(10, 10, 10, 10) << QRectF(0, 0, 20, 30))
588 << (QList<int>() << 2 << 1 << 0);
591 void tst_QGraphicsScene::items_QPointF()
593 QFETCH(QList<QRectF>, items);
594 QFETCH(QPointF, point);
595 QFETCH(QList<int>, itemsAtPoint);
597 QGraphicsScene scene;
600 QList<QGraphicsItem *> addedItems;
601 foreach(QRectF rect, items) {
603 path.addRect(0, 0, rect.width(), rect.height());
605 QGraphicsPathItem *item = scene.addPath(path);
606 item->setPen(QPen(Qt::black, 0));
607 item->setZValue(n++);
608 item->setPos(rect.topLeft());
612 QList<int> itemIndexes;
613 foreach (QGraphicsItem *item, scene.items(point))
614 itemIndexes << addedItems.indexOf(item);
616 QCOMPARE(itemIndexes, itemsAtPoint);
619 void tst_QGraphicsScene::items_QRectF()
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));
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);
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);
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);
653 void tst_QGraphicsScene::items_QRectF_2_data()
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");
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;
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;
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;
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;
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;
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;
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;
722 void tst_QGraphicsScene::items_QRectF_2()
724 QFETCH(QRectF, ellipseRect);
725 QFETCH(QRectF, sceneRect);
726 QFETCH(Qt::ItemSelectionMode, selectionMode);
727 QFETCH(bool, contained);
728 QFETCH(bool, containedRotated);
730 QGraphicsScene scene;
731 QGraphicsItem *item = scene.addEllipse(ellipseRect);
733 QCOMPARE(!scene.items(sceneRect, selectionMode).isEmpty(), contained);
735 QCOMPARE(!scene.items(sceneRect, selectionMode).isEmpty(), containedRotated);
738 void tst_QGraphicsScene::items_QPolygonF()
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));
751 QPolygonF poly1(item1->boundingRect());
752 QPolygonF poly2(item2->boundingRect());
753 QPolygonF poly3(item3->boundingRect());
754 QPolygonF poly4(item4->boundingRect());
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);
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));
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);
771 poly1 = QPolygonF(QRectF(-10, -10, 40, 10));
772 poly2 = QPolygonF(QRectF(-10, 10, 40, 10));
774 QCOMPARE(scene.items(poly1), QList<QGraphicsItem *>() << item2 << item1);
775 QCOMPARE(scene.items(poly2), QList<QGraphicsItem *>() << item4 << item3);
782 QCOMPARE(scene.items(poly1), QList<QGraphicsItem *>() << item1 << item2);
783 QCOMPARE(scene.items(poly2), QList<QGraphicsItem *>() << item3 << item4);
786 void tst_QGraphicsScene::items_QPolygonF_2()
788 QGraphicsScene scene;
789 QGraphicsItem *ellipse = scene.addEllipse(QRectF(0, 0, 100, 100));
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());
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);
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());
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);
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);
828 void tst_QGraphicsScene::items_QPainterPath()
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));
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());
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);
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));
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);
861 path1 = QPainterPath(); path1.addRect(QRectF(-10, -10, 40, 10));
862 path2 = QPainterPath(); path2.addRect(QRectF(-10, 10, 40, 10));
864 QCOMPARE(scene.items(path1), QList<QGraphicsItem *>() << item2 << item1);
865 QCOMPARE(scene.items(path2), QList<QGraphicsItem *>() << item4 << item3);
872 QCOMPARE(scene.items(path1), QList<QGraphicsItem *>() << item1 << item2);
873 QCOMPARE(scene.items(path2), QList<QGraphicsItem *>() << item3 << item4);
876 void tst_QGraphicsScene::items_QPainterPath_2()
878 QGraphicsScene scene;
879 QGraphicsItem *ellipse = scene.addEllipse(QRectF(0, 0, 100, 100));
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));
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());
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);
908 p5.addRect(QRectF(5, 5, 90, 90));
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());
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);
919 p6.addRect(QRectF(-5, -5, 110, 110));
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);
929 class CustomView : public QGraphicsView
932 CustomView() : repaints(0)
937 void paintEvent(QPaintEvent *event)
940 QGraphicsView::paintEvent(event);
944 void tst_QGraphicsScene::selectionChanged()
946 QGraphicsScene scene(0, 0, 1000, 1000);
947 QSignalSpy spy(&scene, SIGNAL(selectionChanged()));
948 QCOMPARE(spy.count(), 0);
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());
958 QGraphicsItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
959 QCOMPARE(spy.count(), 0); // selection didn't change
961 rect->setSelected(true);
962 QVERIFY(!rect->isSelected());
963 QCOMPARE(spy.count(), 0); // selection didn't change, item isn't selectable
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);
971 rect->setSelected(false);
972 QVERIFY(!rect->isSelected());
973 QCOMPARE(spy.count(), 2); // selection changed
974 QVERIFY(scene.selectedItems().isEmpty());
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);
985 QCOMPARE(spy.count(), 3); // the grandchild was added, so the selection changed once
987 scene.removeItem(parentItem);
988 QCOMPARE(spy.count(), 4); // the grandchild was removed, so the selection changed
990 rect->setSelected(true);
991 QCOMPARE(spy.count(), 5); // the rect was reselected, so the selection changed
993 scene.clearSelection();
994 QCOMPARE(spy.count(), 6); // the scene selection was cleared
996 rect->setSelected(true);
997 QCOMPARE(spy.count(), 7); // the rect was reselected, so the selection changed
999 rect->setFlag(QGraphicsItem::ItemIsSelectable, false);
1000 QCOMPARE(spy.count(), 8); // the rect was unselected, so the selection changed
1002 rect->setSelected(true);
1003 QCOMPARE(spy.count(), 8); // the rect is not longer selectable, so the selection does not change
1006 rect->setFlag(QGraphicsItem::ItemIsSelectable, true);
1007 rect->setSelected(true);
1008 QCOMPARE(spy.count(), 9); // the rect is again selectable, so the selection changed
1011 QCOMPARE(spy.count(), 10); // a selected item was deleted; selection changed
1014 void tst_QGraphicsScene::selectionChanged2()
1016 QGraphicsScene scene;
1017 QSignalSpy spy(&scene, SIGNAL(selectionChanged()));
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);
1024 QCOMPARE(spy.count(), 0);
1026 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
1027 event.setScenePos(QPointF(50, 50));
1028 event.setButton(Qt::LeftButton);
1029 qApp->sendEvent(&scene, &event);
1032 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
1033 event.setScenePos(QPointF(50, 50));
1034 event.setButton(Qt::LeftButton);
1035 qApp->sendEvent(&scene, &event);
1037 QVERIFY(item1->isSelected());
1038 QVERIFY(!item2->isSelected());
1039 QCOMPARE(spy.count(), 1);
1041 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
1042 event.setScenePos(QPointF(150, 150));
1043 event.setButton(Qt::LeftButton);
1044 qApp->sendEvent(&scene, &event);
1047 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
1048 event.setScenePos(QPointF(150, 150));
1049 event.setButton(Qt::LeftButton);
1050 qApp->sendEvent(&scene, &event);
1052 QVERIFY(!item1->isSelected());
1053 QVERIFY(item2->isSelected());
1054 QCOMPARE(spy.count(), 2);
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);
1062 QVERIFY(!item1->isSelected());
1063 QVERIFY(item2->isSelected());
1064 QCOMPARE(spy.count(), 2);
1066 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
1067 event.setScenePos(QPointF(50, 50));
1068 event.setButton(Qt::LeftButton);
1069 qApp->sendEvent(&scene, &event);
1071 QVERIFY(item1->isSelected());
1072 QVERIFY(!item2->isSelected());
1073 QCOMPARE(spy.count(), 3);
1076 void tst_QGraphicsScene::addItem()
1080 // 1) Create item, then scene, then add item
1081 QGraphicsItem *path = new QGraphicsEllipseItem(QRectF(-10, -10, 20, 20));
1082 QGraphicsScene scene;
1085 view.setScene(&scene);
1087 QVERIFY(QTest::qWaitForWindowExposed(&view));
1088 qApp->processEvents();
1091 scene.addItem(path);
1093 // Adding an item should always issue a repaint.
1094 QTRY_VERIFY(view.repaints > 0);
1097 QCOMPARE(scene.itemAt(0, 0), path);
1099 QGraphicsItem *path2 = new QGraphicsEllipseItem(QRectF(-10, -10, 20, 20));
1100 path2->setPos(100, 100);
1102 QCOMPARE(scene.itemAt(0, 0), path);
1103 QCOMPARE(scene.itemAt(100, 100), (QGraphicsItem *)0);
1104 scene.addItem(path2);
1106 // Adding an item should always issue a repaint.
1107 QTRY_VERIFY(view.repaints > 0);
1109 QCOMPARE(scene.itemAt(100, 100), path2);
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);
1117 QGraphicsItem *path2 = new QGraphicsEllipseItem(QRectF(-10, -10, 20, 20));
1118 path2->setPos(100, 100);
1119 scene.addItem(path2);
1121 QCOMPARE(scene.itemAt(0, 0), path);
1122 QCOMPARE(scene.itemAt(100, 100), path2);
1126 void tst_QGraphicsScene::addEllipse()
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);
1146 void tst_QGraphicsScene::addLine()
1148 QGraphicsScene scene;
1151 QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20),
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);
1167 void tst_QGraphicsScene::addPath()
1169 QGraphicsScene scene;
1171 p.addEllipse(QRectF(-10, -10, 20, 20));
1172 p.addEllipse(QRectF(-10, 20, 20, 20));
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));
1180 path->setPen(QPen(Qt::red, 0));
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");
1201 QCOMPARE(scene.itemAt(-10, 30), (QGraphicsItem *)0);
1202 QCOMPARE(scene.itemAt(10.1, 30), (QGraphicsItem *)0);
1205 void tst_QGraphicsScene::addPixmap()
1207 QGraphicsScene scene;
1208 QPixmap pix(":/Ash_European.jpg");
1209 QGraphicsPixmapItem *pixmap = scene.addPixmap(pix);
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);
1225 void tst_QGraphicsScene::addRect()
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));
1235 rect->setPen(QPen(Qt::red, 0));
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);
1248 void tst_QGraphicsScene::addText()
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());
1257 void tst_QGraphicsScene::removeItem()
1259 #if defined(Q_OS_WINCE) && !defined(GWES_ICONCURS)
1260 QSKIP("No mouse cursor support");
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);
1269 QGraphicsItem *item2 = scene.addRect(QRectF(0, 0, 10, 10));
1270 item2->setFlag(QGraphicsItem::ItemIsSelectable);
1271 QCOMPARE(scene.itemAt(0, 0), item2);
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());
1280 // Check that we are in a state that can receive paint events
1281 // (i.e., not logged out on Windows).
1284 // Removing a hovered item
1285 HoverItem *hoverItem = new HoverItem;
1286 scene.addItem(hoverItem);
1287 scene.setSceneRect(-50, -50, 100, 100);
1289 QGraphicsView view(&scene);
1290 view.setFixedSize(150, 150);
1292 QApplication::setActiveWindow(&view);
1293 QVERIFY(QTest::qWaitForWindowActive(&view));
1294 QTest::mouseMove(view.viewport(), QPoint(-1, -1));
1296 QMouseEvent moveEvent(QEvent::MouseMove, view.mapFromScene(hoverItem->scenePos() + QPointF(20, 20)), Qt::NoButton, 0, 0);
1297 QApplication::sendEvent(view.viewport(), &moveEvent);
1299 qApp->processEvents(); // update
1300 qApp->processEvents(); // draw
1301 QVERIFY(!hoverItem->isHovered);
1305 QTest::mouseMove(view.viewport(), view.mapFromScene(hoverItem->scenePos()), Qt::NoButton);
1307 QMouseEvent moveEvent(QEvent::MouseMove, view.mapFromScene(hoverItem->scenePos()), Qt::NoButton, 0, 0);
1308 QApplication::sendEvent(view.viewport(), &moveEvent);
1310 qApp->processEvents(); // update
1311 qApp->processEvents(); // draw
1312 QVERIFY(hoverItem->isHovered);
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);
1323 void tst_QGraphicsScene::focusItem()
1325 QGraphicsScene scene;
1326 QEvent activate(QEvent::WindowActivate);
1327 QApplication::sendEvent(&scene, &activate);
1329 QVERIFY(!scene.focusItem());
1330 QGraphicsItem *item = scene.addText("Qt");
1331 QVERIFY(!scene.focusItem());
1333 QVERIFY(!scene.focusItem());
1334 item->setFlag(QGraphicsItem::ItemIsFocusable);
1335 QVERIFY(!scene.focusItem());
1337 QCOMPARE(scene.focusItem(), item);
1339 QFocusEvent focusOut(QEvent::FocusOut);
1340 QApplication::sendEvent(&scene, &focusOut);
1342 QVERIFY(!scene.focusItem());
1344 QFocusEvent focusIn(QEvent::FocusIn);
1345 QApplication::sendEvent(&scene, &focusIn);
1346 QCOMPARE(scene.focusItem(), item);
1348 QGraphicsItem *item2 = scene.addText("Qt");
1349 item2->setFlag(QGraphicsItem::ItemIsFocusable);
1350 QCOMPARE(scene.focusItem(), item);
1353 QCOMPARE(scene.focusItem(), item2);
1355 QCOMPARE(scene.focusItem(), item);
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);
1365 QApplication::sendEvent(&scene, &focusOut);
1367 QVERIFY(!scene.focusItem());
1368 scene.removeItem(item2);
1371 QApplication::sendEvent(&scene, &focusIn);
1372 QVERIFY(!scene.focusItem());
1375 class FocusItem : public QGraphicsTextItem
1378 void focusOutEvent(QFocusEvent *)
1380 QVERIFY(!scene()->focusItem());
1384 void tst_QGraphicsScene::focusItemLostFocus()
1386 QGraphicsScene scene;
1387 QEvent activate(QEvent::WindowActivate);
1388 QApplication::sendEvent(&scene, &activate);
1390 FocusItem *item = new FocusItem;
1391 item->setTextInteractionFlags(Qt::TextEditorInteraction);
1392 scene.addItem(item);
1395 QCOMPARE(scene.focusItem(), (QGraphicsItem *)item);
1399 class ClearTestItem : public QGraphicsRectItem
1402 ClearTestItem(QGraphicsItem *parent = 0) : QGraphicsRectItem(parent) {}
1403 ~ClearTestItem() { qDeleteAll(items); }
1404 QList<QGraphicsItem *> items;
1407 void tst_QGraphicsScene::clear()
1409 QGraphicsScene scene;
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));
1415 QVERIFY(scene.items().isEmpty());
1416 QCOMPARE(scene.sceneRect(), QRectF(0, 0, 100, 100));
1418 ClearTestItem *firstItem = new ClearTestItem;
1419 QGraphicsItem *secondItem = new QGraphicsRectItem;
1420 firstItem->items += secondItem;
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);
1428 ClearTestItem *thirdItem = new ClearTestItem(firstItem);
1429 QGraphicsItem *forthItem = new QGraphicsRectItem(firstItem);
1430 thirdItem->items += forthItem;
1432 // must not crash even if firstItem deletes secondItem
1434 QVERIFY(scene.items().isEmpty());
1437 void tst_QGraphicsScene::setFocusItem()
1439 QGraphicsScene scene;
1440 QEvent activate(QEvent::WindowActivate);
1441 QApplication::sendEvent(&scene, &activate);
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);
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());
1458 QGraphicsItem *item2 = scene.addText("Qt");
1459 item2->setFlag(QGraphicsItem::ItemIsFocusable);
1461 scene.setFocusItem(item2);
1462 QVERIFY(!item->hasFocus());
1463 QVERIFY(item2->hasFocus());
1465 scene.setFocusItem(item);
1466 QVERIFY(item->hasFocus());
1467 QVERIFY(!item2->hasFocus());
1470 QVERIFY(!item->hasFocus());
1471 QVERIFY(!item2->hasFocus());
1474 QVERIFY(item->hasFocus());
1475 QVERIFY(!item2->hasFocus());
1477 scene.setFocusItem(0);
1478 QVERIFY(!item->hasFocus());
1479 QVERIFY(!item2->hasFocus());
1482 QVERIFY(!item->hasFocus());
1483 QVERIFY(!item2->hasFocus());
1486 void tst_QGraphicsScene::setFocusItem_inactive()
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);
1497 for (int i = 0; i < 3; ++i) {
1498 scene.setFocusItem(item);
1499 QCOMPARE(scene.focusItem(), item);
1500 QVERIFY(!item->hasFocus());
1506 void tst_QGraphicsScene::mouseGrabberItem()
1508 QGraphicsScene scene;
1509 QVERIFY(!scene.mouseGrabberItem());
1511 QGraphicsItem *item = scene.addRect(QRectF(-10, -10, 20, 20));
1512 item->setFlag(QGraphicsItem::ItemIsMovable);
1515 QGraphicsItem *item2 = scene.addRect(QRectF(-10, -10, 20, 20));
1516 item2->setFlag(QGraphicsItem::ItemIsMovable);
1517 item2->setZValue(0);
1519 for (int i = 0; i < 3; ++i) {
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;
1526 QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
1527 pressEvent.setButton(Qt::LeftButton);
1528 pressEvent.setScenePos(QPointF(0, 0));
1529 pressEvent.setScreenPos(QPoint(100, 100));
1531 QApplication::sendEvent(&scene, &pressEvent);
1532 QCOMPARE(scene.mouseGrabberItem(), topMostItem);
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);
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);
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());
1556 // Structural change: deleting the mouse grabber
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));
1566 QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove);
1567 moveEvent.setButtons(Qt::LeftButton);
1568 moveEvent.setScenePos(QPointF(0, 0));
1569 moveEvent.setScreenPos(QPoint(100, 100));
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);
1590 QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
1593 void tst_QGraphicsScene::hoverEvents_siblings()
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;
1605 item->setPos(lastItem->pos() + QPointF(sin(i / 3.0) * 17, cos(i / 3.0) * 17));
1611 QGraphicsView view(&scene);
1612 view.setRenderHint(QPainter::Antialiasing, true);
1613 #if defined(Q_OS_WINCE)
1614 view.setMinimumSize(230, 200);
1616 view.setMinimumSize(400, 300);
1619 view.scale(1.7, 1.7);
1621 qApp->setActiveWindow(&view);
1622 view.activateWindow();
1623 QVERIFY(QTest::qWaitForWindowActive(&view));
1625 QCursor::setPos(view.mapToGlobal(QPoint(-5, -5)));
1627 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
1628 mouseEvent.setScenePos(QPointF(-1000, -1000));
1629 QApplication::sendEvent(&scene, &mouseEvent);
1633 for (int j = 1; j >= 0; --j) {
1637 QVERIFY(!items.at(i)->isHovered);
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);
1644 qApp->processEvents(); // this posts updates from the scene to the view
1645 qApp->processEvents(); // which trigger a repaint here
1647 QTRY_VERIFY(items.at(i)->isHovered);
1649 QVERIFY(!items.at(i - 1)->isHovered);
1651 QVERIFY(!items.at(i + 1)->isHovered);
1653 if ((j && i == 15) || (!j && i == -1))
1657 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
1658 mouseEvent.setScenePos(QPointF(-1000, -1000));
1659 QApplication::sendEvent(&scene, &mouseEvent);
1661 qApp->processEvents(); // this posts updates from the scene to the view
1662 qApp->processEvents(); // which trigger a repaint here
1666 void tst_QGraphicsScene::hoverEvents_parentChild()
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;
1678 item->setParentItem(lastItem);
1679 item->setPos(sin(i / 3.0) * 17, cos(i / 3.0) * 17);
1684 QGraphicsView view(&scene);
1685 view.setRenderHint(QPainter::Antialiasing, true);
1686 #if defined(Q_OS_WINCE)
1687 view.setMinimumSize(230, 200);
1689 view.setMinimumSize(400, 300);
1692 view.scale(1.7, 1.7);
1694 qApp->setActiveWindow(&view);
1695 QVERIFY(QTest::qWaitForWindowActive(&view));
1697 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
1698 mouseEvent.setScenePos(QPointF(-1000, -1000));
1699 QApplication::sendEvent(&scene, &mouseEvent);
1701 for (int j = 1; j >= 0; --j) {
1705 QVERIFY(!items.at(i)->isHovered);
1708 QVERIFY(!items.at(13)->isHovered);
1710 mouseEvent.setScenePos(items.at(i)->mapToScene(0, 0));
1711 QApplication::sendEvent(&scene, &mouseEvent);
1713 qApp->processEvents(); // this posts updates from the scene to the view
1714 qApp->processEvents(); // which trigger a repaint here
1716 QTRY_VERIFY(items.at(i)->isHovered);
1718 QVERIFY(!items.at(i + 1)->isHovered);
1720 if ((j && i == 15) || (!j && i == -1))
1724 mouseEvent.setScenePos(QPointF(-1000, -1000));
1725 QApplication::sendEvent(&scene, &mouseEvent);
1727 qApp->processEvents(); // this posts updates from the scene to the view
1728 qApp->processEvents(); // which trigger a repaint here
1732 void tst_QGraphicsScene::createItemGroup()
1734 QGraphicsScene scene;
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));
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));
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));
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);
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);
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);
1775 // Now make parent2 a child of parent1, so all children2 are also children
1777 parent2->setParentItem(parent1);
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);
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);
1795 // Fixup the parent-child chain
1796 foreach (QGraphicsItem *item, children2)
1797 item->setParentItem(parent2);
1799 // These share no common parent
1800 group = scene.createItemGroup(children3);
1801 QCOMPARE(group->parentItem(), (QGraphicsItem *)0);
1802 scene.destroyItemGroup(group);
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);
1809 // These should have parent3 as a parent
1810 group = scene.createItemGroup(children3);
1811 QCOMPARE(group->parentItem(), parent3);
1812 scene.destroyItemGroup(group);
1814 // Now make them all children of parent1
1815 parent3->setParentItem(parent1);
1817 group = scene.createItemGroup(children3);
1818 QCOMPARE(group->parentItem(), parent3);
1819 scene.destroyItemGroup(group);
1821 group = scene.createItemGroup(children2);
1822 QCOMPARE(group->parentItem(), parent2);
1823 scene.destroyItemGroup(group);
1825 group = scene.createItemGroup(children1);
1826 QCOMPARE(group->parentItem(), parent1);
1827 scene.destroyItemGroup(group);
1829 QGraphicsItemGroup *emptyGroup = scene.createItemGroup(QList<QGraphicsItem *>());
1830 QCOMPARE(emptyGroup->children(), QList<QGraphicsItem *>());
1831 QVERIFY(!emptyGroup->parentItem());
1832 QCOMPARE(emptyGroup->scene(), &scene);
1835 class EventTester : public QGraphicsEllipseItem
1839 : QGraphicsEllipseItem(QRectF(-10, -10, 20, 20)), ignoreMouse(false)
1843 QList<QEvent::Type> eventTypes;
1846 bool sceneEvent(QEvent *event)
1848 eventTypes << QEvent::Type(event->type());
1849 switch (event->type()) {
1850 case QEvent::GraphicsSceneMousePress:
1851 case QEvent::GraphicsSceneMouseMove:
1852 case QEvent::GraphicsSceneMouseRelease:
1861 return QGraphicsEllipseItem::sceneEvent(event);
1865 void tst_QGraphicsScene::mouseEventPropagation()
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);
1875 a->setFlag(QGraphicsItem::ItemIsMovable);
1876 b->setFlag(QGraphicsItem::ItemIsMovable);
1877 c->setFlag(QGraphicsItem::ItemIsMovable);
1878 d->setFlag(QGraphicsItem::ItemIsMovable);
1885 // scene -> a -> b -> c -> d
1886 QGraphicsScene scene;
1887 QEvent activate(QEvent::WindowActivate);
1888 QApplication::sendEvent(&scene, &activate);
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));
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);
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);
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);
1932 d->setAcceptedMouseButtons(Qt::RightButton);
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);
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);
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);
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);
1973 // Disabled items eat events. c should not get this.
1974 d->setEnabled(false);
1975 d->setAcceptedMouseButtons(Qt::RightButton);
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);
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);
1996 // Clicking outside the items removes the mouse grabber
1999 void tst_QGraphicsScene::mouseEventPropagation_ignore()
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);
2009 a->setFlags(QGraphicsItem::ItemIsMovable);
2010 b->setFlags(QGraphicsItem::ItemIsMovable);
2011 c->setFlags(QGraphicsItem::ItemIsMovable);
2012 d->setFlags(QGraphicsItem::ItemIsMovable);
2014 // scene -> a -> b -> c -> d
2015 QGraphicsScene scene;
2018 // Prepare some events
2019 QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
2020 pressEvent.setButton(Qt::LeftButton);
2021 pressEvent.setScenePos(QPointF(0, 0));
2023 b->ignoreMouse = true;
2024 c->ignoreMouse = true;
2025 d->ignoreMouse = true;
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);
2045 a->ignoreMouse = true;
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);
2054 QVERIFY(!pressEvent.isAccepted());
2057 void tst_QGraphicsScene::mouseEventPropagation_focus()
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);
2067 a->setFlag(QGraphicsItem::ItemIsMovable);
2068 b->setFlag(QGraphicsItem::ItemIsMovable);
2069 c->setFlag(QGraphicsItem::ItemIsMovable);
2070 d->setFlag(QGraphicsItem::ItemIsMovable);
2072 // scene -> a -> b -> c -> d
2073 QGraphicsScene scene;
2074 QEvent activate(QEvent::WindowActivate);
2075 QApplication::sendEvent(&scene, &activate);
2079 // Prepare some events
2080 QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
2081 pressEvent.setButton(Qt::LeftButton);
2082 pressEvent.setScenePos(QPointF(0, 0));
2084 a->setFlag(QGraphicsItem::ItemIsFocusable);
2085 QVERIFY(!a->hasFocus());
2087 QApplication::sendEvent(&scene, &pressEvent);
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);
2097 void tst_QGraphicsScene::mouseEventPropagation_doubleclick()
2099 EventTester *a = new EventTester;
2100 EventTester *b = new EventTester;
2101 a->setFlags(QGraphicsItem::ItemIsMovable);
2102 b->setFlags(QGraphicsItem::ItemIsMovable);
2107 QGraphicsScene scene;
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));
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);
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);
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);
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);
2153 class Scene : public QGraphicsScene
2156 QVector<QPointF> mouseMovePoints;
2159 void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
2161 mouseMovePoints << event->scenePos();
2165 void tst_QGraphicsScene::mouseEventPropagation_mouseMove()
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);
2176 QCOMPARE(scene.mouseMovePoints.size(), 30);
2177 for (int i = 0; i < 30; ++i)
2178 QCOMPARE(scene.mouseMovePoints.at(i), QPointF(i, 5));
2181 class DndTester : public QGraphicsEllipseItem
2184 DndTester(const QRectF &rect)
2185 : QGraphicsEllipseItem(rect), lastEvent(0),
2186 ignoresDragEnter(false), ignoresDragMove(false)
2196 QGraphicsSceneDragDropEvent *lastEvent;
2197 QList<QEvent::Type> eventList;
2198 bool ignoresDragEnter;
2199 bool ignoresDragMove;
2202 void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
2204 storeLastEvent(event);
2205 event->setAccepted(!ignoresDragEnter);
2206 if (!ignoresDragEnter)
2207 event->setDropAction(Qt::IgnoreAction);
2208 eventList << event->type();
2211 void dragMoveEvent(QGraphicsSceneDragDropEvent *event)
2213 storeLastEvent(event);
2214 event->setAccepted(!ignoresDragMove);
2215 eventList << event->type();
2218 void dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
2220 storeLastEvent(event);
2221 eventList << event->type();
2224 void dropEvent(QGraphicsSceneDragDropEvent *event)
2226 storeLastEvent(event);
2227 eventList << event->type();
2231 void storeLastEvent(QGraphicsSceneDragDropEvent *event)
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());
2248 #ifndef QT_NO_DRAGANDDROP
2249 void tst_QGraphicsScene::dragAndDrop_simple()
2251 DndTester *item = new DndTester(QRectF(-10, -10, 20, 20));
2253 QGraphicsScene scene;
2254 scene.addItem(item);
2256 QGraphicsView view(&scene);
2257 view.setFixedSize(100, 100);
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);
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);
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);
2282 item->setAcceptDrops(true);
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);
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);
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);
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);
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);
2352 void tst_QGraphicsScene::dragAndDrop_disabledOrInvisible()
2354 DndTester *item = new DndTester(QRectF(-10, -10, 20, 20));
2355 item->setAcceptDrops(true);
2357 QGraphicsScene scene;
2358 scene.addItem(item);
2360 QGraphicsView view(&scene);
2361 view.setFixedSize(100, 100);
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);
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);
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);
2390 // Now disable the item
2391 item->setEnabled(false);
2392 QVERIFY(!item->isEnabled());
2393 QVERIFY(item->isVisible());
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);
2405 // Reenable it, and make it invisible
2406 item->setEnabled(true);
2407 item->setVisible(false);
2408 QVERIFY(item->isEnabled());
2409 QVERIFY(!item->isVisible());
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);
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);
2426 void tst_QGraphicsScene::dragAndDrop_propagate()
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);
2436 item1->setData(0, "item1");
2437 item2->setData(0, "item2");
2439 QGraphicsScene scene;
2440 scene.addItem(item1);
2441 scene.addItem(item2);
2443 QGraphicsView view(&scene);
2444 view.setFixedSize(100, 100);
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);
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());
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);
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);
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);
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);
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);
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);
2529 QCOMPARE(item1->eventList.size(), 7);
2530 QCOMPARE(item1->eventList.at(6), QEvent::GraphicsSceneDrop);
2531 QCOMPARE(item2->eventList.size(), 5);
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);
2540 void tst_QGraphicsScene::render_data()
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");
2548 QPainterPath clip_rect;
2549 clip_rect.addRect(50, 100, 200, 150);
2551 QPainterPath clip_ellipse;
2552 clip_ellipse.addEllipse(100,50,150,200);
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;
2612 void tst_QGraphicsScene::render()
2614 QFETCH(QRectF, targetRect);
2615 QFETCH(QRectF, sourceRect);
2616 QFETCH(Qt::AspectRatioMode, aspectRatioMode);
2617 QFETCH(QMatrix, matrix);
2618 QFETCH(QPainterPath, clip);
2620 QPixmap pix(30, 30);
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);
2629 item->setOffset(QPointF(3, 3));
2632 scene.setSceneRect(scene.itemsBoundingRect());
2634 QImage bigImage(300, 300, QImage::Format_RGB32);
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);
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);
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());
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))
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) {
2673 QLabel *expectedLabel = new QLabel;
2674 expectedLabel->setPixmap(QPixmap::fromImage(original));
2676 QLabel *newLabel = new QLabel;
2677 newLabel->setPixmap(QPixmap::fromImage(bigImage));
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);
2687 widget.setLayout(gridLayout);
2690 QTestEventLoop::instance().enterLoop(1);
2692 QFAIL("Images are not identical.");
2695 qDebug() << "Updating" << QTest::currentDataTag() << ":" << bigImage.save(fileName, "png");
2700 void tst_QGraphicsScene::renderItemsWithNegativeWidthOrHeight()
2702 QGraphicsScene scene(0, 0, 150, 150);
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);
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);
2716 QGraphicsView view(&scene);
2717 view.setFrameStyle(QFrame::NoFrame);
2718 view.resize(150, 150);
2720 QCOMPARE(view.viewport()->size(), QSize(150, 150));
2722 QImage expected(view.viewport()->size(), QImage::Format_RGB32);
2723 view.viewport()->render(&expected);
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);
2732 QCOMPARE(actual, expected);
2735 void tst_QGraphicsScene::contextMenuEvent()
2737 QGraphicsScene scene;
2738 QEvent activate(QEvent::WindowActivate);
2739 QApplication::sendEvent(&scene, &activate);
2741 EventTester *item = new EventTester;
2742 scene.addItem(item);
2743 item->setFlag(QGraphicsItem::ItemIsFocusable);
2746 QVERIFY(item->hasFocus());
2747 QVERIFY(scene.hasFocus());
2749 QGraphicsView view(&scene);
2751 view.activateWindow();
2752 QVERIFY(QTest::qWaitForWindowActive(&view));
2753 view.centerOn(item);
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);
2763 class ContextMenuItem : public QGraphicsRectItem
2766 ContextMenuItem() : QGraphicsRectItem(0, 0, 100, 100)
2767 { setBrush(Qt::red); }
2770 void contextMenuEvent(QGraphicsSceneContextMenuEvent *)
2771 { /* just accept */ }
2774 void tst_QGraphicsScene::contextMenuEvent_ItemIgnoresTransformations()
2776 QGraphicsScene scene(0, 0, 200, 200);
2777 ContextMenuItem *item = new ContextMenuItem;
2778 item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
2779 scene.addItem(item);
2782 QGraphicsView view(&scene, &topLevel);
2783 view.resize(200, 200);
2785 QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
2789 QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2791 QApplication::sendEvent(view.viewport(), &event);
2792 QVERIFY(event.isAccepted());
2795 QPoint pos(150, 150);
2796 QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2798 QApplication::sendEvent(view.viewport(), &event);
2799 QVERIFY(!event.isAccepted());
2801 view.scale(1.5, 1.5);
2804 QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2806 QApplication::sendEvent(view.viewport(), &event);
2807 QVERIFY(event.isAccepted());
2811 QContextMenuEvent event(QContextMenuEvent::Keyboard, pos, view.viewport()->mapToGlobal(pos));
2813 QApplication::sendEvent(view.viewport(), &event);
2814 QVERIFY(!event.isAccepted());
2818 void tst_QGraphicsScene::update()
2820 QGraphicsScene scene;
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);
2828 // This function forces indexing
2831 qRegisterMetaType<QList<QRectF> >("QList<QRectF>");
2832 QSignalSpy spy(&scene, SIGNAL(changed(QList<QRectF>)));
2834 // We update the scene.
2837 // This function forces a purge, which will post an update signal
2840 // This will process the pending update
2841 QApplication::instance()->processEvents();
2843 // Check that the update region is correct
2844 QCOMPARE(spy.count(), 1);
2846 foreach (QRectF rectF, qvariant_cast<QList<QRectF> >(spy.at(0).at(0)))
2848 QCOMPARE(region, QRectF(-100, -100, 200, 200));
2851 void tst_QGraphicsScene::update2()
2853 QGraphicsScene scene;
2854 scene.setSceneRect(-200, -200, 200, 200);
2856 view.setScene(&scene);
2858 qApp->setActiveWindow(&view);
2859 QVERIFY(QTest::qWaitForWindowActive(&view));
2860 QTRY_VERIFY(view.repaints >= 1);
2863 // Make sure QGraphicsScene::update only requires one event-loop iteration
2864 // before the view is updated.
2866 qApp->processEvents();
2867 QTRY_COMPARE(view.repaints, 1);
2870 // The same for partial scene updates.
2871 scene.update(QRectF(-100, -100, 100, 100));
2872 qApp->processEvents();
2873 QCOMPARE(view.repaints, 1);
2876 void tst_QGraphicsScene::views()
2878 QGraphicsScene scene;
2879 QGraphicsView view(&scene);
2881 QCOMPARE(scene.views().size(), 1);
2882 QCOMPARE(scene.views().at(0), &view);
2884 QGraphicsView view1(&scene);
2885 QCOMPARE(scene.views().size(), 2);
2886 QVERIFY(scene.views().contains(&view1));
2889 QCOMPARE(scene.views().size(), 1);
2890 QCOMPARE(scene.views().at(0), &view1);
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);
2899 QCOMPARE(scene.views().size(), 1);
2900 QCOMPARE(scene.views().at(0), &view1);
2903 class CustomScene : public QGraphicsScene
2906 CustomScene() : gotTimerEvent(false)
2911 void timerEvent(QTimerEvent *)
2913 gotTimerEvent = true;
2917 void tst_QGraphicsScene::event()
2919 // Test that QGraphicsScene properly propagates events to QObject.
2921 QTestEventLoop::instance().enterLoop(1);
2922 QVERIFY(scene.gotTimerEvent);
2925 class DisabledItemTester : public QGraphicsRectItem
2928 DisabledItemTester(const QRectF &rect, QGraphicsItem *parent = 0)
2929 : QGraphicsRectItem(rect, parent)
2932 QList<QEvent::Type> receivedSceneEvents;
2933 QList<QEvent::Type> receivedSceneEventFilters;
2936 bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
2938 receivedSceneEventFilters << event->type();
2939 return QGraphicsRectItem::sceneEventFilter(watched, event);
2942 bool sceneEvent(QEvent *event)
2944 receivedSceneEvents << event->type();
2945 return QGraphicsRectItem::sceneEvent(event);
2949 void tst_QGraphicsScene::eventsToDisabledItems()
2951 QGraphicsScene scene;
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
2957 scene.addItem(item1);
2958 scene.addItem(item2);
2960 item1->installSceneEventFilter(item2);
2962 QVERIFY(item1->receivedSceneEvents.isEmpty());
2963 QVERIFY(item2->receivedSceneEvents.isEmpty());
2964 QVERIFY(item1->receivedSceneEventFilters.isEmpty());
2965 QVERIFY(item2->receivedSceneEventFilters.isEmpty());
2967 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
2968 event.setButton(Qt::LeftButton);
2969 QApplication::sendEvent(&scene, &event);
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);
2979 item1->receivedSceneEvents.clear();
2980 item1->receivedSceneEventFilters.clear();
2981 item2->receivedSceneEvents.clear();
2982 item2->receivedSceneEventFilters.clear();
2984 item1->setEnabled(false); // disable the topmost item, eat mouse events
2986 event.setButton(Qt::LeftButton);
2987 event.setAccepted(false);
2988 QApplication::sendEvent(&scene, &event);
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);
2997 class ExposedPixmapItem : public QGraphicsPixmapItem
3000 ExposedPixmapItem(QGraphicsItem *item = 0)
3001 : QGraphicsPixmapItem(item)
3004 void paint(QPainter *, const QStyleOptionGraphicsItem *option, QWidget *)
3006 exposed = option->exposedRect;
3012 void tst_QGraphicsScene::exposedRect()
3014 ExposedPixmapItem *item = new ExposedPixmapItem;
3015 item->setPixmap(QPixmap(":/Ash_European.jpg"));
3016 QGraphicsScene scene;
3017 scene.addItem(item);
3019 QCOMPARE(item->exposed, QRectF());
3021 QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
3022 QPainter painter(&image);
3024 scene.render(&painter);
3025 QCOMPARE(item->exposed, item->boundingRect());
3027 painter.rotate(180);
3028 painter.translate(100, 100);
3030 scene.render(&painter);
3031 QCOMPARE(item->exposed, item->boundingRect());
3034 void tst_QGraphicsScene::tabFocus_emptyScene()
3036 QGraphicsScene scene;
3037 QDial *dial1 = new QDial;
3038 QGraphicsView *view = new QGraphicsView(&scene);
3039 QDial *dial2 = new QDial;
3041 QHBoxLayout *layout = new QHBoxLayout;
3042 layout->addWidget(dial1);
3043 layout->addWidget(view);
3044 layout->addWidget(dial2);
3047 widget.setLayout(layout);
3049 qApp->setActiveWindow(&widget);
3050 widget.activateWindow();
3051 QVERIFY(QTest::qWaitForWindowActive(&widget));
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());
3069 void tst_QGraphicsScene::tabFocus_sceneWithFocusableItems()
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);
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);
3085 QDial *dial1 = new QDial;
3086 QGraphicsView *view = new QGraphicsView(&scene);
3087 QDial *dial2 = new QDial;
3089 QHBoxLayout *layout = new QHBoxLayout;
3090 layout->addWidget(dial1);
3091 layout->addWidget(view);
3092 layout->addWidget(dial2);
3095 widget.setLayout(layout);
3097 qApp->setActiveWindow(&widget);
3098 widget.activateWindow();
3099 QVERIFY(QTest::qWaitForWindowActive(&widget));
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());
3110 QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3111 QApplication::processEvents();
3112 QTRY_VERIFY(dial2->hasFocus());
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());
3121 QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3122 QApplication::processEvents();
3123 QTRY_VERIFY(dial1->hasFocus());
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.
3128 QApplication::processEvents();
3129 QTRY_VERIFY(!view->hasFocus());
3130 QVERIFY(!view->viewport()->hasFocus());
3131 QTRY_VERIFY(scene.hasFocus());
3132 QVERIFY(item->hasFocus());
3135 QApplication::processEvents();
3136 QTRY_VERIFY(view->hasFocus());
3137 QTRY_VERIFY(view->viewport()->hasFocus());
3138 QTRY_VERIFY(scene.hasFocus());
3139 QTRY_VERIFY(item->hasFocus());
3141 // Check that everyone loses focus when the widget is hidden.
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));
3150 // Check that the correct item regains focus.
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());
3163 class FocusWidget : public QGraphicsWidget
3167 FocusWidget(QGraphicsItem *parent = 0)
3168 : QGraphicsWidget(parent), tabs(0), backTabs(0)
3170 setFocusPolicy(Qt::StrongFocus);
3174 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
3176 if (option->state & QStyle::State_HasFocus) {
3177 painter->fillRect(rect(), Qt::blue);
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));
3192 bool sceneEvent(QEvent *event)
3194 if (event->type() == QEvent::KeyPress) {
3195 QKeyEvent *k = static_cast<QKeyEvent *>(event);
3196 if (k->key() == Qt::Key_Tab)
3198 if (k->key() == Qt::Key_Backtab)
3201 return QGraphicsWidget::sceneEvent(event);
3204 void focusInEvent(QFocusEvent *)
3206 void focusOutEvent(QFocusEvent *)
3210 void tst_QGraphicsScene::tabFocus_sceneWithFocusWidgets()
3212 QGraphicsScene scene;
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);
3220 QDial *dial1 = new QDial;
3221 QGraphicsView *view = new QGraphicsView(&scene);
3222 view->setRenderHint(QPainter::Antialiasing);
3223 QDial *dial2 = new QDial;
3225 QHBoxLayout *layout = new QHBoxLayout;
3226 layout->addWidget(dial1);
3227 layout->addWidget(view);
3228 layout->addWidget(dial2);
3231 widget.setLayout(layout);
3233 qApp->setActiveWindow(&widget);
3234 widget.activateWindow();
3235 QVERIFY(QTest::qWaitForWindowActive(&widget));
3238 QTRY_VERIFY(dial1->hasFocus());
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());
3267 widget1->setFocus();
3268 view->viewport()->setFocus();
3272 qApp->setActiveWindow(&widget);
3273 widget.activateWindow();
3274 QVERIFY(QTest::qWaitForWindowActive(&widget));
3275 QTRY_VERIFY(widget1->hasFocus());
3278 void tst_QGraphicsScene::tabFocus_sceneWithNestedFocusWidgets()
3280 QGraphicsScene scene;
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);
3292 FocusWidget *widget2 = new FocusWidget;
3293 widget2->setPos(widget1->boundingRect().right(), 0);
3295 widget1->setData(0, "widget1");
3296 widget1_1->setData(0, "widget1_1");
3297 widget1_2->setData(0, "widget1_2");
3298 widget2->setData(0, "widget2");
3300 scene.addItem(widget1);
3301 scene.addItem(widget2);
3303 QDial *dial1 = new QDial;
3304 QGraphicsView *view = new QGraphicsView(&scene);
3305 view->setRenderHint(QPainter::Antialiasing);
3306 QDial *dial2 = new QDial;
3308 QHBoxLayout *layout = new QHBoxLayout;
3309 layout->addWidget(dial1);
3310 layout->addWidget(view);
3311 layout->addWidget(dial2);
3314 widget.setLayout(layout);
3316 qApp->setActiveWindow(&widget);
3317 widget.activateWindow();
3318 QVERIFY(QTest::qWaitForWindowActive(&widget));
3321 QTRY_VERIFY(dial1->hasFocus());
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);
3332 QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
3333 QApplication::processEvents();
3334 QTRY_VERIFY(widget1->hasFocus());
3335 QCOMPARE(focusInSpy_1.count(), 1);
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);
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);
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);
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);
3364 QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
3365 QApplication::processEvents();
3366 QTRY_VERIFY(widget2->hasFocus());
3367 QCOMPARE(focusInSpy_2.count(), 2);
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);
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);
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);
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);
3396 widget1->setFocus();
3397 view->viewport()->setFocus();
3401 qApp->setActiveWindow(&widget);
3402 widget.activateWindow();
3403 QVERIFY(QTest::qWaitForWindowActive(&widget));
3404 QTRY_VERIFY(widget1->hasFocus());
3407 void tst_QGraphicsScene::style()
3409 QPointer<QWindowsStyle> windowsStyle = new QWindowsStyle;
3411 QGraphicsScene scene;
3412 QLineEdit *edit = new QLineEdit;
3413 QGraphicsProxyWidget *proxy = scene.addWidget(edit);
3415 EventSpy sceneSpy(&scene, QEvent::StyleChange);
3416 EventSpy proxySpy(proxy, QEvent::StyleChange);
3417 EventSpy editSpy(edit, QEvent::StyleChange);
3419 QCOMPARE(scene.style(), QApplication::style());
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);
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
3439 void tst_QGraphicsScene::task139710_bspTreeCrash()
3441 // create a scene with 2000 items
3442 QGraphicsScene scene(0, 0, 1000, 1000);
3444 for (int i = 0; i < 2; ++i) {
3445 // trigger delayed item indexing
3446 qApp->processEvents();
3447 scene.setSceneRect(0, 0, 10000, 10000);
3449 // delete all items in the scene - pointers are now likely to be recycled
3450 foreach (QGraphicsItem *item, scene.items()) {
3451 scene.removeItem(item);
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);
3461 // trigger delayed item indexing for the first 1000 items
3462 qApp->processEvents();
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);
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)))
3477 void tst_QGraphicsScene::task139782_containsItemBoundingRect()
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);
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));
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));
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));
3495 void tst_QGraphicsScene::task176178_itemIndexMethodBreaksSceneRect()
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());
3506 void tst_QGraphicsScene::task160653_selectionChanged()
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()) {
3513 item->flags() | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
3514 item->setSelected(true);
3516 QVERIFY(scene.items().size() > 1);
3517 QCOMPARE(scene.items().size(), scene.selectedItems().size());
3519 QSignalSpy spy(&scene, SIGNAL(selectionChanged()));
3520 QGraphicsView view(&scene);
3522 view.viewport(), Qt::LeftButton, 0, view.mapFromScene(scene.items().first()->scenePos()));
3523 QCOMPARE(spy.count(), 1);
3526 void tst_QGraphicsScene::task250680_childClip()
3528 QGraphicsRectItem *clipper = new QGraphicsRectItem;
3529 clipper->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
3530 clipper->setPen(QPen(Qt::green, 0));
3531 clipper->setRect(200, 200, 640, 480);
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);
3539 QGraphicsScene scene;
3540 scene.addItem(clipper);
3543 path.addRect(-25, -25, 50, 50);
3544 QVERIFY(QPathCompare::comparePaths(rect->clipPath().simplified(), path));
3546 QCOMPARE(scene.items(QRectF(320, 240, 5, 5)).size(), 2);
3548 QCOMPARE(scene.items(QRectF(320, 240, 5, 5)).size(), 2);
3551 void tst_QGraphicsScene::sorting_data()
3553 QTest::addColumn<bool>("cache");
3555 QTest::newRow("Normal sorting") << false;
3556 QTest::newRow("Cached sorting") << true;
3559 void tst_QGraphicsScene::sorting()
3561 QFETCH(bool, cache);
3563 QGraphicsScene scene;
3564 scene.setSortCacheEnabled(cache);
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));
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);
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);
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);
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");
3618 foreach (QGraphicsItem *item, scene.items())
3619 item->setFlag(QGraphicsItem::ItemIsSelectable);
3621 // QGraphicsView view(&scene);
3622 // view.setDragMode(QGraphicsView::RubberBandDrag);
3625 qDebug() << "items: {";
3626 foreach (QGraphicsItem *item, scene.items(32, 31, 4, 55))
3627 qDebug() << "\t" << item->data(0).toString();
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
3646 class ChangedListener : public QObject
3650 QList<QList<QRectF> > changes;
3653 void changed(const QList<QRectF> &dirty)
3659 void tst_QGraphicsScene::changedSignal_data()
3661 QTest::addColumn<bool>("withView");
3663 QTest::newRow("without view") << false;
3664 QTest::newRow("with view") << true;
3667 void tst_QGraphicsScene::changedSignal()
3669 QFETCH(bool, withView);
3670 QGraphicsScene scene;
3672 connect(&scene, SIGNAL(changed(QList<QRectF>)), &cl, SLOT(changed(QList<QRectF>)));
3674 QGraphicsView *view = 0;
3676 view = new QGraphicsView(&scene);
3678 QGraphicsRectItem *rect = new QGraphicsRectItem(0, 0, 10, 10);
3679 rect->setPen(QPen(Qt::black, 0));
3680 scene.addItem(rect);
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));
3687 rect->setPos(20, 0);
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));
3696 QCOMPARE(scene.sceneRect(), QRectF(0, 0, 30, 10));
3702 void tst_QGraphicsScene::stickyFocus_data()
3704 QTest::addColumn<bool>("sticky");
3705 QTest::newRow("sticky") << true;
3706 QTest::newRow("not sticky") << false;
3709 void tst_QGraphicsScene::stickyFocus()
3711 QFETCH(bool, sticky);
3713 QGraphicsScene scene;
3714 QEvent activate(QEvent::WindowActivate);
3715 QApplication::sendEvent(&scene, &activate);
3717 QGraphicsTextItem *text = scene.addText("Hei");
3718 text->setTextInteractionFlags(Qt::TextEditorInteraction);
3721 scene.setStickyFocus(sticky);
3723 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
3724 event.setScenePos(QPointF(-10, -10)); // outside item
3725 event.setButton(Qt::LeftButton);
3726 qApp->sendEvent(&scene, &event);
3728 QCOMPARE(text->hasFocus(), sticky);
3731 void tst_QGraphicsScene::sendEvent()
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);
3742 void tst_QGraphicsScene::inputMethod_data()
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;
3753 class InputMethodTester : public QGraphicsRectItem
3755 void inputMethodEvent(QInputMethodEvent *) { ++eventCalls; }
3756 QVariant inputMethodQuery(Qt::InputMethodQuery) const { ++queryCalls; return QVariant(); }
3759 mutable int queryCalls;
3762 void tst_QGraphicsScene::inputMethod()
3764 PlatformInputContext inputContext;
3765 QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
3766 inputMethodPrivate->testContext = &inputContext;
3769 QFETCH(bool, callFocusItem);
3771 InputMethodTester *item = new InputMethodTester;
3772 item->setFlags((QGraphicsItem::GraphicsItemFlags)flags);
3774 QGraphicsScene scene;
3775 QGraphicsView view(&scene);
3777 QApplication::setActiveWindow(&view);
3779 QVERIFY(QTest::qWaitForWindowActive(&view));
3780 QCOMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view));
3782 inputContext.m_resetCallCount = 0;
3783 inputContext.m_commitCallCount = 0;
3784 scene.addItem(item);
3785 QInputMethodEvent event;
3787 scene.setFocusItem(item);
3788 QCOMPARE(!!(item->flags() & QGraphicsItem::ItemIsFocusable), scene.focusItem() == item);
3789 QCOMPARE(inputContext.m_resetCallCount, 0);
3791 item->eventCalls = 0;
3792 qApp->sendEvent(&scene, &event);
3793 QCOMPARE(item->eventCalls, callFocusItem ? 1 : 0);
3795 item->queryCalls = 0;
3796 scene.inputMethodQuery((Qt::InputMethodQuery)0);
3797 QCOMPARE(item->queryCalls, callFocusItem ? 1 : 0);
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
3805 item->eventCalls = 0;
3806 qApp->sendEvent(&scene, &event);
3807 QCOMPARE(item->eventCalls, 0);
3809 item->queryCalls = 0;
3810 scene.inputMethodQuery((Qt::InputMethodQuery)0);
3811 QCOMPARE(item->queryCalls, 0);
3814 void tst_QGraphicsScene::dispatchHoverOnPress()
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);
3825 tester1->eventTypes.clear();
3826 tester2->eventTypes.clear();
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);
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);
3882 void tst_QGraphicsScene::initialFocus_data()
3884 QTest::addColumn<bool>("activeScene");
3885 QTest::addColumn<bool>("explicitSetFocus");
3886 QTest::addColumn<bool>("isPanel");
3887 QTest::addColumn<bool>("shouldHaveFocus");
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;
3899 void tst_QGraphicsScene::initialFocus()
3901 QFETCH(bool, activeScene);
3902 QFETCH(bool, explicitSetFocus);
3903 QFETCH(bool, isPanel);
3904 QFETCH(bool, shouldHaveFocus);
3906 QGraphicsRectItem *rect = new QGraphicsRectItem;
3907 rect->setFlag(QGraphicsItem::ItemIsFocusable);
3908 QVERIFY(!rect->hasFocus());
3911 rect->setFlag(QGraphicsItem::ItemIsPanel);
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)
3918 QGraphicsScene scene;
3919 QVERIFY(!scene.isActive());
3922 QEvent windowActivate(QEvent::WindowActivate);
3923 qApp->sendEvent(&scene, &windowActivate);
3927 scene.addItem(rect);
3930 QEvent windowActivate(QEvent::WindowActivate);
3931 qApp->sendEvent(&scene, &windowActivate);
3935 QCOMPARE(rect->hasFocus(), shouldHaveFocus);
3938 class PolishItem : public QGraphicsTextItem
3941 PolishItem(QGraphicsItem *parent = 0)
3942 : QGraphicsTextItem(parent), polished(false), deleteChildrenInPolish(true), addChildrenInPolish(false) { }
3945 bool deleteChildrenInPolish;
3946 bool addChildrenInPolish;
3948 QVariant itemChange(GraphicsItemChange change, const QVariant& value)
3950 if (change == ItemVisibleChange) {
3952 if (deleteChildrenInPolish)
3953 qDeleteAll(childItems());
3954 if (addChildrenInPolish) {
3955 for (int i = 0; i < 10; ++i)
3956 new PolishItem(this);
3959 return QGraphicsItem::itemChange(change, value);
3963 void tst_QGraphicsScene::polishItems()
3965 QGraphicsScene scene;
3966 PolishItem *parent = new PolishItem;
3967 scene.addItem(parent);
3968 PolishItem *child = new PolishItem(parent);
3970 // test that QGraphicsScenePrivate::_q_polishItems() doesn't crash
3971 QMetaObject::invokeMethod(&scene,"_q_polishItems");
3974 void tst_QGraphicsScene::polishItems2()
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);
3985 // Wait for the polish event to be delivered.
3986 QVERIFY(!item->polished);
3987 QApplication::sendPostedEvents(&scene, QEvent::MetaCall);
3988 QVERIFY(item->polished);
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);
3998 QApplication::sendPostedEvents(&scene, QEvent::MetaCall);
3999 foreach (QGraphicsItem *child, children)
4000 QVERIFY(static_cast<PolishItem *>(child)->polished);
4003 void tst_QGraphicsScene::isActive()
4005 QGraphicsScene scene1;
4006 QVERIFY(!scene1.isActive());
4007 QGraphicsScene scene2;
4008 QVERIFY(!scene2.isActive());
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);
4019 QVERIFY(!scene1.isActive());
4020 QVERIFY(!scene2.isActive());
4022 view1->setVisible(false);
4025 QApplication::setActiveWindow(&toplevel1);
4026 QVERIFY(QTest::qWaitForWindowActive(&toplevel1));
4027 QCOMPARE(QApplication::activeWindow(), &toplevel1);
4029 QVERIFY(!scene1.isActive()); //it is hidden;
4030 QVERIFY(scene2.isActive());
4031 QVERIFY(!scene1.hasFocus());
4032 QVERIFY(scene2.hasFocus());
4035 QVERIFY(scene1.isActive());
4036 QVERIFY(scene2.isActive());
4037 QVERIFY(!scene1.hasFocus());
4038 QVERIFY(scene2.hasFocus());
4042 QVERIFY(scene1.isActive());
4043 QVERIFY(!scene2.isActive());
4044 QVERIFY(scene1.hasFocus());
4045 QVERIFY(!scene2.hasFocus());
4049 QTRY_VERIFY(!scene1.isActive());
4050 QTRY_VERIFY(!scene2.isActive());
4051 QVERIFY(!scene1.hasFocus());
4052 QVERIFY(!scene2.hasFocus());
4055 QApplication::setActiveWindow(&toplevel1);
4056 QVERIFY(QTest::qWaitForWindowActive(&toplevel1));
4057 QCOMPARE(QApplication::activeWindow(), &toplevel1);
4059 QTRY_VERIFY(scene1.isActive());
4060 QTRY_VERIFY(!scene2.isActive());
4061 QVERIFY(scene1.hasFocus());
4062 QVERIFY(!scene2.hasFocus());
4065 QVERIFY(scene1.isActive());
4066 QVERIFY(scene2.isActive());
4067 QVERIFY(scene1.hasFocus());
4068 QVERIFY(!scene2.hasFocus());
4071 QVERIFY(!scene1.isActive());
4072 QVERIFY(!scene2.isActive());
4073 QVERIFY(!scene1.hasFocus());
4074 QVERIFY(!scene2.hasFocus());
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);
4086 QVERIFY(!scene1.isActive());
4087 QVERIFY(!scene2.isActive());
4088 QVERIFY(!scene1.hasFocus());
4089 QVERIFY(!scene2.hasFocus());
4092 QApplication::setActiveWindow(&toplevel2);
4093 QVERIFY(QTest::qWaitForWindowActive(&toplevel2));
4094 QCOMPARE(QApplication::activeWindow(), &toplevel2);
4096 QVERIFY(scene1.isActive());
4097 QVERIFY(!scene2.isActive());
4098 QVERIFY(scene1.hasFocus());
4099 QVERIFY(!scene2.hasFocus());
4101 view2->setScene(&scene2);
4103 QVERIFY(scene1.isActive());
4104 QVERIFY(scene2.isActive());
4105 QVERIFY(scene1.hasFocus());
4106 QVERIFY(!scene2.hasFocus());
4108 view1->setScene(&scene2);
4109 QVERIFY(!scene1.isActive());
4110 QVERIFY(scene2.isActive());
4111 QVERIFY(!scene1.hasFocus());
4112 QVERIFY(scene2.hasFocus());
4115 QVERIFY(!scene1.isActive());
4116 QVERIFY(scene2.isActive());
4117 QVERIFY(!scene1.hasFocus());
4118 QVERIFY(scene2.hasFocus());
4120 view1->setScene(&scene1);
4121 QVERIFY(!scene1.isActive());
4122 QVERIFY(scene2.isActive());
4123 QVERIFY(!scene1.hasFocus());
4124 QVERIFY(scene2.hasFocus());
4127 QVERIFY(scene1.isActive());
4128 QVERIFY(scene2.isActive());
4129 QVERIFY(!scene1.hasFocus());
4130 QVERIFY(scene2.hasFocus());
4133 QVERIFY(scene1.isActive());
4134 QVERIFY(!scene2.isActive());
4135 QVERIFY(scene1.hasFocus());
4136 QVERIFY(!scene2.hasFocus());
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));
4145 QVERIFY(!scene1.isActive());
4146 QVERIFY(!scene2.isActive());
4147 QVERIFY(!scene1.hasFocus());
4148 QVERIFY(!scene2.hasFocus());
4150 topLevelView.setScene(&scene1);
4151 QVERIFY(scene1.isActive());
4152 QVERIFY(!scene2.isActive());
4153 QVERIFY(scene1.hasFocus());
4154 QVERIFY(!scene2.hasFocus());
4157 QVERIFY(scene1.isActive());
4158 QVERIFY(!scene2.isActive());
4159 QVERIFY(scene1.hasFocus());
4160 QVERIFY(!scene2.hasFocus());
4163 QVERIFY(scene1.isActive());
4164 QVERIFY(!scene2.isActive());
4165 QVERIFY(scene1.hasFocus());
4166 QVERIFY(!scene2.hasFocus());
4168 QApplication::setActiveWindow(&toplevel2);
4169 QVERIFY(QTest::qWaitForWindowActive(&toplevel2));
4171 QVERIFY(!scene1.isActive());
4172 QVERIFY(scene2.isActive());
4173 QVERIFY(!scene1.hasFocus());
4174 QVERIFY(scene2.hasFocus());
4177 QVERIFY(!scene1.isActive());
4178 QVERIFY(!scene2.isActive());
4179 QVERIFY(!scene1.hasFocus());
4180 QVERIFY(!scene2.hasFocus());
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);
4190 QVERIFY(!scene1.isActive());
4191 QVERIFY(!scene2.isActive());
4192 QVERIFY(!scene1.hasFocus());
4193 QVERIFY(!scene2.hasFocus());
4197 QApplication::setActiveWindow(&toplevel3);
4198 QVERIFY(QTest::qWaitForWindowActive(&toplevel3));
4199 QCOMPARE(QApplication::activeWindow(), &toplevel3);
4201 QVERIFY(scene1.isActive());
4202 QVERIFY(!scene2.isActive());
4203 QVERIFY(scene1.hasFocus());
4204 QVERIFY(!scene2.hasFocus());
4206 layout->addWidget(view2);
4207 QApplication::processEvents();
4208 QVERIFY(scene1.isActive());
4209 QVERIFY(scene2.isActive());
4210 QVERIFY(scene1.hasFocus());
4211 QVERIFY(!scene2.hasFocus());
4213 view1->setParent(0);
4214 QVERIFY(!scene1.isActive());
4215 QVERIFY(scene2.isActive());
4216 QVERIFY(!scene1.hasFocus());
4217 QVERIFY(scene2.hasFocus());
4221 QVERIFY(!scene1.isActive());
4222 QVERIFY(!scene2.isActive());
4223 QVERIFY(!scene1.hasFocus());
4224 QVERIFY(!scene2.hasFocus());
4228 void tst_QGraphicsScene::siblingIndexAlwaysValid()
4230 QGraphicsScene scene;
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);
4244 //Then we make the child a top level
4245 child->setParentItem(0);
4247 //This is trigerred by a repaint...
4248 QGraphicsScenePrivate::get(&scene)->index->estimateTopLevelItems(QRectF(), Qt::AscendingOrder);
4252 //If there are in the list that's bad, we crash...
4253 QVERIFY(!QGraphicsScenePrivate::get(&scene)->topLevelItems.contains(static_cast<QGraphicsItem *>(child)));
4256 QGraphicsScene scene2;
4257 // works with bsp tree index
4258 scene2.setItemIndexMethod(QGraphicsScene::NoIndex);
4260 QGraphicsView view2(&scene2);
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);
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);
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);
4280 // handle events as a real life app would do
4281 QApplication::processEvents();
4283 // now delete the red rect
4286 // handle events as a real life app would do
4287 QApplication::processEvents();
4289 //We should not crash
4293 void tst_QGraphicsScene::removeFullyTransparentItem()
4295 QGraphicsScene scene;
4297 QGraphicsItem *parent = scene.addRect(0, 0, 100, 100);
4298 parent->setFlag(QGraphicsItem::ItemHasNoContents);
4300 QGraphicsItem *child = scene.addRect(0, 0, 100, 100);
4301 child->setParentItem(parent);
4304 view.setScene(&scene);
4306 qApp->setActiveWindow(&view);
4307 QVERIFY(QTest::qWaitForWindowActive(&view));
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.
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);
4320 // Re-add child. There's nothing new to display (child is still
4321 // effectively hidden), so it shouldn't trigger an update.
4323 child->setParentItem(parent);
4324 QVERIFY(scene.items().contains(child));
4325 QVERIFY(qFuzzyIsNull(child->effectiveOpacity()));
4326 QApplication::processEvents();
4327 QCOMPARE(view.repaints, 0);
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);
4336 void tst_QGraphicsScene::taskQTBUG_5904_crashWithDeviceCoordinateCache()
4338 QGraphicsScene scene;
4339 QGraphicsRectItem *rectItem = scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
4341 rectItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
4343 QPixmap pixmap(100,200);
4344 QPainter painter(&pixmap);
4345 painter.setRenderHint(QPainter::Antialiasing);
4346 scene.render(&painter);
4348 // No crash, then it passed!
4351 void tst_QGraphicsScene::taskQT657_paintIntoCacheWithTransparentParts()
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);
4358 QGraphicsScene *scene = new QGraphicsScene();
4359 CustomView *view = new CustomView;
4360 view->setScene(scene);
4362 QGraphicsProxyWidget *proxy = scene->addWidget(w);
4363 proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
4367 QVERIFY(QTest::qWaitForWindowExposed(view));
4369 proxy->update(10, 10, 10, 10);
4371 QTRY_VERIFY(view->repaints > 0);
4374 QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(proxy);
4375 QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->deviceData.value(view->viewport()).key, &pix));
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);
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);
4394 void tst_QGraphicsScene::taskQTBUG_7863_paintIntoCacheWithTransparentParts()
4396 // Test using DeviceCoordinateCache and semi-transparent item
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);
4404 QGraphicsScene *scene = new QGraphicsScene();
4405 CustomView *view = new CustomView;
4406 view->setScene(scene);
4408 scene->addItem(backItem);
4409 rectItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
4410 backItem->rotate(15);
4413 QVERIFY(QTest::qWaitForWindowExposed(view));
4415 rectItem->update(10, 10, 10, 10);
4417 QTRY_VERIFY(view->repaints > 0);
4420 QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
4421 QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->deviceData.value(view->viewport()).key, &pix));
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);
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);
4441 // Test using ItemCoordinateCache and opaque item
4443 QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
4444 rectItem->setBrush(QColor(0, 0, 255));
4446 QGraphicsScene *scene = new QGraphicsScene();
4447 CustomView *view = new CustomView;
4448 view->setScene(scene);
4450 scene->addItem(rectItem);
4451 rectItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
4452 rectItem->rotate(15);
4455 QVERIFY(QTest::qWaitForWindowExposed(view));
4457 rectItem->update(10, 10, 10, 10);
4459 QTRY_VERIFY(view->repaints > 0);
4462 QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
4463 QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->key, &pix));
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);
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);
4482 // Test using ItemCoordinateCache and semi-transparent item
4484 QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
4485 rectItem->setBrush(QColor(0, 0, 255, 125));
4487 QGraphicsScene *scene = new QGraphicsScene();
4488 CustomView *view = new CustomView;
4489 view->setScene(scene);
4491 scene->addItem(rectItem);
4492 rectItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
4493 rectItem->rotate(15);
4496 QVERIFY(QTest::qWaitForWindowExposed(view));
4498 rectItem->update(10, 10, 10, 10);
4500 QTRY_VERIFY(view->repaints > 0);
4503 QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
4504 QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->key, &pix));
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);
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);
4524 void tst_QGraphicsScene::taskQT_3674_doNotCrash()
4526 QGraphicsScene scene;
4528 QGraphicsView view(&scene);
4529 view.resize(200, 200);
4531 QPixmap pixmap(view.size());
4532 QPainter painter(&pixmap);
4533 view.render(&painter);
4536 scene.addItem(new QGraphicsWidget);
4537 scene.setBackgroundBrush(Qt::green);
4539 QApplication::processEvents();
4540 QApplication::processEvents();
4543 void tst_QGraphicsScene::zeroScale()
4546 QGraphicsScene scene;
4547 scene.setSceneRect(-100, -100, 100, 100);
4548 QGraphicsView view(&scene);
4551 connect(&scene, SIGNAL(changed(QList<QRectF>)), &cl, SLOT(changed(QList<QRectF>)));
4553 QGraphicsRectItem *rect1 = new QGraphicsRectItem(0, 0, 0.0000001, 0.00000001);
4554 scene.addItem(rect1);
4555 rect1->setRotation(82);
4556 rect1->setScale(0.00000001);
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);
4568 void tst_QGraphicsScene::taskQTBUG_15977_renderWithDeviceCoordinateCache()
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);
4577 QImage image(100, 100, QImage::Format_RGB32);
4582 QImage expected(100, 100, QImage::Format_RGB32);
4584 p.fillRect(expected.rect(), Qt::red);
4587 QCOMPARE(image, expected);
4590 void tst_QGraphicsScene::taskQTBUG_16401_focusItem()
4592 QGraphicsScene scene;
4593 QGraphicsView view(&scene);
4594 QGraphicsRectItem *rect = scene.addRect(0, 0, 100, 100);
4595 rect->setFlag(QGraphicsItem::ItemIsFocusable);
4598 QApplication::setActiveWindow(&view);
4599 QVERIFY(QTest::qWaitForWindowActive(&view));
4601 QVERIFY(!scene.focusItem());
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);
4613 QVERIFY(!scene.focusItem());
4614 QApplication::sendEvent(&view, &focusOut);
4615 QVERIFY(!scene.focusItem());
4616 QApplication::sendEvent(&view, &focusIn);
4617 QVERIFY(!scene.focusItem());
4620 QTEST_MAIN(tst_QGraphicsScene)
4621 #include "tst_qgraphicsscene.moc"