| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QGRAPHICSSCENEBSPTREE_P_H |
| 5 | #define QGRAPHICSSCENEBSPTREE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 19 | #include <QtCore/qlist.h> |
| 20 | #include <QtCore/qrect.h> |
| 21 | #include <QtCore/qset.h> |
| 22 | |
| 23 | QT_REQUIRE_CONFIG(graphicsview); |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QGraphicsItem; |
| 28 | |
| 29 | class QGraphicsSceneBspTree |
| 30 | { |
| 31 | public: |
| 32 | struct Node |
| 33 | { |
| 34 | enum Type { Horizontal, Vertical, Leaf }; |
| 35 | union { |
| 36 | qreal offset; |
| 37 | int leafIndex; |
| 38 | }; |
| 39 | Type type; |
| 40 | }; |
| 41 | |
| 42 | QGraphicsSceneBspTree(); |
| 43 | ~QGraphicsSceneBspTree(); |
| 44 | |
| 45 | void initialize(const QRectF &rect, int depth); |
| 46 | void clear(); |
| 47 | |
| 48 | void insertItem(QGraphicsItem *item, const QRectF &rect); |
| 49 | void removeItem(QGraphicsItem *item, const QRectF &rect); |
| 50 | void removeItems(const QSet<QGraphicsItem *> &items); |
| 51 | |
| 52 | QList<QGraphicsItem *> items(const QRectF &rect, bool onlyTopLevelItems = false) const; |
| 53 | int leafCount() const; |
| 54 | |
| 55 | inline int firstChildIndex(int index) const |
| 56 | { return index * 2 + 1; } |
| 57 | |
| 58 | inline int parentIndex(int index) const |
| 59 | { return index > 0 ? ((index & 1) ? ((index - 1) / 2) : ((index - 2) / 2)) : -1; } |
| 60 | |
| 61 | QString debug(int index) const; |
| 62 | |
| 63 | private: |
| 64 | void initialize(const QRectF &rect, int depth, int index); |
| 65 | template<typename Visitor> |
| 66 | void climbTree(Visitor &&visitor, const QRectF &rect, int index = 0) const; |
| 67 | QRectF rectForIndex(int index) const; |
| 68 | |
| 69 | QList<Node> nodes; |
| 70 | QList<QList<QGraphicsItem *>> leaves; |
| 71 | int leafCnt; |
| 72 | QRectF rect; |
| 73 | }; |
| 74 | |
| 75 | QT_END_NAMESPACE |
| 76 | |
| 77 | #endif // QGRAPHICSSCENEBSPTREE_P_H |
| 78 | |