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