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