| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
|
| 5 |
#include <QTest> |
| 6 |
#include <QtGui/QPainterPath> |
| 7 |
#include <QtWidgets/qgraphicsscene.h> |
| 8 |
#include <private/qgraphicsscenebsptreeindex_p.h> |
| 9 |
#include <private/qgraphicssceneindex_p.h> |
| 10 |
#include <private/qgraphicsscenelinearindex_p.h> |
| 11 |
#include <QtWidgets/private/qapplication_p.h> |
| 12 |
|
| 13 |
class tst_QGraphicsSceneIndex : public QObject |
| 14 |
{ |
| 15 |
Q_OBJECT |
| 16 |
public slots: |
| 17 |
void initTestCase(); |
| 18 |
|
| 19 |
private slots: |
| 20 |
void scatteredItems_data(); |
| 21 |
void scatteredItems(); |
| 22 |
void overlappedItems_data(); |
| 23 |
void overlappedItems(); |
| 24 |
void movingItems_data(); |
| 25 |
void movingItems(); |
| 26 |
void connectedToSceneRectChanged(); |
| 27 |
void items(); |
| 28 |
void boundingRectPointIntersection_data(); |
| 29 |
void boundingRectPointIntersection(); |
| 30 |
void removeItems(); |
| 31 |
void clear(); |
| 32 |
|
| 33 |
private: |
| 34 |
void common_data(); |
| 35 |
QGraphicsSceneIndex *createIndex(const QString &name); |
| 36 |
}; |
| 37 |
|
| 38 |
void tst_QGraphicsSceneIndex::initTestCase() |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
void tst_QGraphicsSceneIndex::common_data() |
| 43 |
{ |
| 44 |
QTest::addColumn<QString>("indexMethod"); |
| 45 |
|
| 46 |
QTest::newRow("BSP") << QString("bsp"); |
| 47 |
QTest::newRow("Linear") << QString("linear"); |
| 48 |
} |
| 49 |
|
| 50 |
QGraphicsSceneIndex *tst_QGraphicsSceneIndex::createIndex(const QString &indexMethod) |
| 51 |
{ |
| 52 |
QGraphicsSceneIndex *index = nullptr; |
| 53 |
QGraphicsScene *scene = new QGraphicsScene(); |
| 54 |
if (indexMethod == "bsp") |
| 55 |
index = new QGraphicsSceneBspTreeIndex(scene); |
| 56 |
|
| 57 |
if (indexMethod == "linear") |
| 58 |
index = new QGraphicsSceneLinearIndex(scene); |
| 59 |
|
| 60 |
return index; |
| 61 |
} |
| 62 |
|
| 63 |
void tst_QGraphicsSceneIndex::scatteredItems_data() |
| 64 |
{ |
| 65 |
common_data(); |
| 66 |
} |
| 67 |
|
| 68 |
void tst_QGraphicsSceneIndex::scatteredItems() |
| 69 |
{ |
| 70 |
QFETCH(QString, indexMethod); |
| 71 |
|
| 72 |
QGraphicsScene scene; |
| 73 |
scene.setItemIndexMethod(indexMethod == "linear" ? QGraphicsScene::NoIndex : QGraphicsScene::BspTreeIndex); |
| 74 |
|
| 75 |
for (int i = 0; i < 10; ++i) |
| 76 |
scene.addRect(i*50, i*50, 40, 35); |
| 77 |
|
| 78 |
QCOMPARE(scene.items(QPointF(5, 5)).size(), 1); |
| 79 |
QCOMPARE(scene.items(QPointF(55, 55)).size(), 1); |
| 80 |
QCOMPARE(scene.items(QPointF(-100, -100)).size(), 0); |
| 81 |
|
| 82 |
QCOMPARE(scene.items(QRectF(0, 0, 10, 10)).size(), 1); |
| 83 |
QCOMPARE(scene.items(QRectF(0, 0, 1000, 1000)).size(), 10); |
| 84 |
QCOMPARE(scene.items(QRectF(-100, -1000, 0, 0)).size(), 0); |
| 85 |
} |
| 86 |
|
| 87 |
void tst_QGraphicsSceneIndex::overlappedItems_data() |
| 88 |
{ |
| 89 |
common_data(); |
| 90 |
} |
| 91 |
|
| 92 |
void tst_QGraphicsSceneIndex::overlappedItems() |
| 93 |
{ |
| 94 |
QFETCH(QString, indexMethod); |
| 95 |
|
| 96 |
QGraphicsScene scene; |
| 97 |
scene.setItemIndexMethod(indexMethod == "linear" ? QGraphicsScene::NoIndex : QGraphicsScene::BspTreeIndex); |
| 98 |
|
| 99 |
for (int i = 0; i < 10; ++i) |
| 100 |
for (int j = 0; j < 10; ++j) |
| 101 |
scene.addRect(i*50, j*50, 200, 200)->setPen(QPen(Qt::black, 0)); |
| 102 |
|
| 103 |
QCOMPARE(scene.items(QPointF(5, 5)).size(), 1); |
| 104 |
QCOMPARE(scene.items(QPointF(55, 55)).size(), 4); |
| 105 |
QCOMPARE(scene.items(QPointF(105, 105)).size(), 9); |
| 106 |
QCOMPARE(scene.items(QPointF(-100, -100)).size(), 0); |
| 107 |
|
| 108 |
QCOMPARE(scene.items(QRectF(0, 0, 1000, 1000)).size(), 100); |
| 109 |
QCOMPARE(scene.items(QRectF(-100, -1000, 0, 0)).size(), 0); |
| 110 |
QCOMPARE(scene.items(QRectF(0, 0, 200, 200)).size(), 16); |
| 111 |
QCOMPARE(scene.items(QRectF(0, 0, 100, 100)).size(), 4); |
| 112 |
QCOMPARE(scene.items(QRectF(0, 0, 1, 100)).size(), 2); |
| 113 |
QCOMPARE(scene.items(QRectF(0, 0, 1, 1000)).size(), 10); |
| 114 |
} |
| 115 |
|
| 116 |
void tst_QGraphicsSceneIndex::movingItems_data() |
| 117 |
{ |
| 118 |
common_data(); |
| 119 |
} |
| 120 |
|
| 121 |
void tst_QGraphicsSceneIndex::movingItems() |
| 122 |
{ |
| 123 |
QFETCH(QString, indexMethod); |
| 124 |
|
| 125 |
QGraphicsScene scene; |
| 126 |
scene.setItemIndexMethod(indexMethod == "linear" ? QGraphicsScene::NoIndex : QGraphicsScene::BspTreeIndex); |
| 127 |
|
| 128 |
for (int i = 0; i < 10; ++i) |
| 129 |
scene.addRect(i*50, i*50, 40, 35); |
| 130 |
|
| 131 |
QGraphicsRectItem *box = scene.addRect(0, 0, 10, 10); |
| 132 |
QCOMPARE(scene.items(QPointF(5, 5)).size(), 2); |
| 133 |
QCOMPARE(scene.items(QPointF(-1, -1)).size(), 0); |
| 134 |
QCOMPARE(scene.items(QRectF(0, 0, 5, 5)).size(), 2); |
| 135 |
|
| 136 |
box->setPos(10, 10); |
| 137 |
QCOMPARE(scene.items(QPointF(9, 9)).size(), 1); |
| 138 |
QCOMPARE(scene.items(QPointF(15, 15)).size(), 2); |
| 139 |
QCOMPARE(scene.items(QRectF(0, 0, 1, 1)).size(), 1); |
| 140 |
|
| 141 |
box->setPos(-5, -5); |
| 142 |
QCOMPARE(scene.items(QPointF(-1, -1)).size(), 1); |
| 143 |
QCOMPARE(scene.items(QRectF(0, 0, 1, 1)).size(), 2); |
| 144 |
|
| 145 |
QCOMPARE(scene.items(QRectF(0, 0, 1000, 1000)).size(), 11); |
| 146 |
} |
| 147 |
|
| 148 |
void tst_QGraphicsSceneIndex::connectedToSceneRectChanged() |
| 149 |
{ |
| 150 |
|
| 151 |
class MyScene : public QGraphicsScene |
| 152 |
{ |
| 153 |
public: |
| 154 |
using QGraphicsScene::receivers; |
| 155 |
}; |
| 156 |
|
| 157 |
MyScene scene; // Uses QGraphicsSceneBspTreeIndex by default. |
| 158 |
QCOMPARE(scene.receivers(SIGNAL(sceneRectChanged(QRectF))), 1); |
| 159 |
|
| 160 |
scene.setItemIndexMethod(QGraphicsScene::NoIndex); // QGraphicsSceneLinearIndex |
| 161 |
QCOMPARE(scene.receivers(SIGNAL(sceneRectChanged(QRectF))), 1); |
| 162 |
} |
| 163 |
|
| 164 |
void tst_QGraphicsSceneIndex::items() |
| 165 |
{ |
| 166 |
QGraphicsScene scene; |
| 167 |
QGraphicsItem *item1 = scene.addRect(0, 0, 10, 10); |
| 168 |
QGraphicsItem *item2 = scene.addRect(10, 10, 10, 10); |
| 169 |
QCOMPARE(scene.items().size(), 2); |
| 170 |
|
| 171 |
// Move from unindexed items into bsp tree. |
| 172 |
QTest::qWait(50); |
| 173 |
QCOMPARE(scene.items().size(), 2); |
| 174 |
|
| 175 |
// Add untransformable item. |
| 176 |
QGraphicsItem *item3 = new QGraphicsRectItem(QRectF(20, 20, 10, 10)); |
| 177 |
item3->setFlag(QGraphicsItem::ItemIgnoresTransformations); |
| 178 |
scene.addItem(item3); |
| 179 |
QCOMPARE(scene.items().size(), 3); |
| 180 |
|
| 181 |
// Move from unindexed items into untransformable items. |
| 182 |
QTest::qWait(50); |
| 183 |
QCOMPARE(scene.items().size(), 3); |
| 184 |
|
| 185 |
// Move from untransformable items into unindexed items. |
| 186 |
item3->setFlag(QGraphicsItem::ItemIgnoresTransformations, false); |
| 187 |
QCOMPARE(scene.items().size(), 3); |
| 188 |
QTest::qWait(50); |
| 189 |
QCOMPARE(scene.items().size(), 3); |
| 190 |
|
| 191 |
// Make all items untransformable. |
| 192 |
item1->setFlag(QGraphicsItem::ItemIgnoresTransformations); |
| 193 |
item2->setParentItem(item1); |
| 194 |
item3->setParentItem(item2); |
| 195 |
QCOMPARE(scene.items().size(), 3); |
| 196 |
|
| 197 |
// Move from unindexed items into untransformable items. |
| 198 |
QTest::qWait(50); |
| 199 |
QCOMPARE(scene.items().size(), 3); |
| 200 |
} |
| 201 |
|
| 202 |
class CustomShapeItem : public QGraphicsItem |
| 203 |
{ |
| 204 |
public: |
| 205 |
CustomShapeItem(const QPainterPath &shape) : QGraphicsItem(0), mShape(shape) {} |
| 206 |
|
| 207 |
QPainterPath shape() const override { return mShape; } |
| 208 |
QRectF boundingRect() const override { return mShape.boundingRect(); } |
| 209 |
void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override {} |
| 210 |
private: |
| 211 |
QPainterPath mShape; |
| 212 |
}; |
| 213 |
|
| 214 |
Q_DECLARE_METATYPE(Qt::ItemSelectionMode) |
| 215 |
Q_DECLARE_METATYPE(QPainterPath) |
| 216 |
|
| 217 |
void tst_QGraphicsSceneIndex::boundingRectPointIntersection_data() |
| 218 |
{ |
| 219 |
QTest::addColumn<QPainterPath>("itemShape"); |
| 220 |
QTest::addColumn<Qt::ItemSelectionMode>("mode"); |
| 221 |
|
| 222 |
QTest::newRow("zero shape - intersects rect") << QPainterPath() << Qt::IntersectsItemBoundingRect; |
| 223 |
QTest::newRow("zero shape - contains rect") << QPainterPath() << Qt::ContainsItemBoundingRect; |
| 224 |
|
| 225 |
QPainterPath triangle; |
| 226 |
triangle.moveTo(50, 0); |
| 227 |
triangle.lineTo(0, 50); |
| 228 |
triangle.lineTo(100, 50); |
| 229 |
triangle.lineTo(50, 0); |
| 230 |
QTest::newRow("triangle shape - intersects rect") << triangle << Qt::IntersectsItemBoundingRect; |
| 231 |
QTest::newRow("triangle shape - contains rect") << triangle << Qt::ContainsItemBoundingRect; |
| 232 |
|
| 233 |
QPainterPath rect; |
| 234 |
rect.addRect(QRectF(0, 0, 100, 100)); |
| 235 |
QTest::newRow("rectangle shape - intersects rect") << rect << Qt::IntersectsItemBoundingRect; |
| 236 |
QTest::newRow("rectangle shape - contains rect") << rect << Qt::ContainsItemBoundingRect; |
| 237 |
} |
| 238 |
|
| 239 |
void tst_QGraphicsSceneIndex::boundingRectPointIntersection() |
| 240 |
{ |
| 241 |
QFETCH(QPainterPath, itemShape); |
| 242 |
QFETCH(Qt::ItemSelectionMode, mode); |
| 243 |
|
| 244 |
QGraphicsScene scene; |
| 245 |
CustomShapeItem *item = new CustomShapeItem(itemShape); |
| 246 |
scene.addItem(item); |
| 247 |
QList<QGraphicsItem*> items = scene.items(QPointF(0, 0), mode, Qt::AscendingOrder); |
| 248 |
QVERIFY(!items.isEmpty()); |
| 249 |
QCOMPARE(items.first(), item); |
| 250 |
} |
| 251 |
|
| 252 |
class RectWidget : public QGraphicsWidget |
| 253 |
{ |
| 254 |
Q_OBJECT |
| 255 |
public: |
| 256 |
RectWidget(QGraphicsItem *parent = nullptr) : QGraphicsWidget(parent) |
| 257 |
{ |
| 258 |
} |
| 259 |
|
| 260 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem * /* option */, QWidget * /* widget */) override |
| 261 |
{ |
| 262 |
painter->setBrush(brush); |
| 263 |
painter->drawRect(boundingRect()); |
| 264 |
} |
| 265 |
public: |
| 266 |
QBrush brush; |
| 267 |
}; |
| 268 |
|
| 269 |
void tst_QGraphicsSceneIndex::removeItems() |
| 270 |
{ |
| 271 |
QGraphicsScene scene; |
| 272 |
|
| 273 |
RectWidget *parent = new RectWidget; |
| 274 |
parent->brush = QBrush(QColor(Qt::magenta)); |
| 275 |
parent->setGeometry(250, 250, 400, 400); |
| 276 |
|
| 277 |
RectWidget *widget = new RectWidget(parent); |
| 278 |
widget->brush = QBrush(QColor(Qt::blue)); |
| 279 |
widget->setGeometry(10, 10, 200, 200); |
| 280 |
|
| 281 |
RectWidget *widgetChild1 = new RectWidget(widget); |
| 282 |
widgetChild1->brush = QBrush(QColor(Qt::green)); |
| 283 |
widgetChild1->setGeometry(20, 20, 100, 100); |
| 284 |
|
| 285 |
RectWidget *widgetChild2 = new RectWidget(widgetChild1); |
| 286 |
widgetChild2->brush = QBrush(QColor(Qt::yellow)); |
| 287 |
widgetChild2->setGeometry(25, 25, 50, 50); |
| 288 |
|
| 289 |
scene.addItem(parent); |
| 290 |
|
| 291 |
QGraphicsView view(&scene); |
| 292 |
view.resize(600, 600); |
| 293 |
view.show(); |
| 294 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 295 |
|
| 296 |
scene.removeItem(widgetChild1); |
| 297 |
|
| 298 |
delete widgetChild1; |
| 299 |
|
| 300 |
//We move the parent |
| 301 |
scene.items(QRectF(295, 295, 50, 50)); |
| 302 |
|
| 303 |
//This should not crash |
| 304 |
} |
| 305 |
|
| 306 |
void tst_QGraphicsSceneIndex::clear() |
| 307 |
{ |
| 308 |
class MyItem : public QGraphicsItem |
| 309 |
{ |
| 310 |
public: |
| 311 |
MyItem(QGraphicsItem *parent = nullptr) : QGraphicsItem(parent), numPaints(0) {} |
| 312 |
int numPaints; |
| 313 |
protected: |
| 314 |
QRectF boundingRect() const override { return QRectF(0, 0, 10, 10); } |
| 315 |
void paint(QPainter * /* painter */, const QStyleOptionGraphicsItem *, QWidget *) override |
| 316 |
{ ++numPaints; } |
| 317 |
}; |
| 318 |
|
| 319 |
QGraphicsScene scene; |
| 320 |
scene.setSceneRect(0, 0, 100, 100); |
| 321 |
scene.addItem(new MyItem); |
| 322 |
|
| 323 |
QGraphicsView view(&scene); |
| 324 |
view.show(); |
| 325 |
QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 326 |
scene.clear(); |
| 327 |
|
| 328 |
// Make sure the index is re-generated after QGraphicsScene::clear(); |
| 329 |
// otherwise no items will be painted. |
| 330 |
MyItem *item = new MyItem; |
| 331 |
scene.addItem(item); |
| 332 |
qApp->processEvents(); |
| 333 |
QTRY_VERIFY(item->numPaints > 0); |
| 334 |
} |
| 335 |
|
| 336 |
QTEST_MAIN(tst_QGraphicsSceneIndex) |
| 337 |
#include "tst_qgraphicssceneindex.moc" |