| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QGRAPHICSSCENEINDEX_H |
| 41 | #define QGRAPHICSSCENEINDEX_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists for the convenience |
| 48 | // of other Qt classes. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 55 | #include "qgraphicsscene_p.h" |
| 56 | #include "qgraphicsscene.h" |
| 57 | #include <private/qobject_p.h> |
| 58 | |
| 59 | #include <QtCore/qnamespace.h> |
| 60 | #include <QtCore/qobject.h> |
| 61 | #include <QtGui/qtransform.h> |
| 62 | |
| 63 | QT_REQUIRE_CONFIG(graphicsview); |
| 64 | |
| 65 | QT_BEGIN_NAMESPACE |
| 66 | |
| 67 | class QGraphicsSceneIndexPrivate; |
| 68 | class QPointF; |
| 69 | class QRectF; |
| 70 | |
| 71 | typedef bool (*QGraphicsSceneIndexIntersector)(const QGraphicsItem *item, const QRectF &exposeRect, Qt::ItemSelectionMode mode, |
| 72 | const QTransform &deviceTransform, const void *data); |
| 73 | |
| 74 | class Q_AUTOTEST_EXPORT QGraphicsSceneIndex : public QObject |
| 75 | { |
| 76 | Q_OBJECT |
| 77 | |
| 78 | public: |
| 79 | QGraphicsSceneIndex(QGraphicsScene *scene = nullptr); |
| 80 | virtual ~QGraphicsSceneIndex(); |
| 81 | |
| 82 | QGraphicsScene *scene() const; |
| 83 | |
| 84 | virtual QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const = 0; |
| 85 | virtual QList<QGraphicsItem *> items(const QPointF &pos, Qt::ItemSelectionMode mode, |
| 86 | Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const; |
| 87 | virtual QList<QGraphicsItem *> items(const QRectF &rect, Qt::ItemSelectionMode mode, |
| 88 | Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const; |
| 89 | virtual QList<QGraphicsItem *> items(const QPolygonF &polygon, Qt::ItemSelectionMode mode, |
| 90 | Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const; |
| 91 | virtual QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode, |
| 92 | Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const; |
| 93 | virtual QList<QGraphicsItem *> estimateItems(const QPointF &point, Qt::SortOrder order) const; |
| 94 | virtual QList<QGraphicsItem *> estimateItems(const QRectF &rect, Qt::SortOrder order) const = 0; |
| 95 | virtual QList<QGraphicsItem *> estimateTopLevelItems(const QRectF &, Qt::SortOrder order) const; |
| 96 | |
| 97 | protected Q_SLOTS: |
| 98 | virtual void updateSceneRect(const QRectF &rect); |
| 99 | |
| 100 | protected: |
| 101 | virtual void clear(); |
| 102 | virtual void addItem(QGraphicsItem *item) = 0; |
| 103 | virtual void removeItem(QGraphicsItem *item) = 0; |
| 104 | virtual void deleteItem(QGraphicsItem *item); |
| 105 | |
| 106 | virtual void itemChange(const QGraphicsItem *item, QGraphicsItem::GraphicsItemChange, const void *const value); |
| 107 | virtual void prepareBoundingRectChange(const QGraphicsItem *item); |
| 108 | |
| 109 | QGraphicsSceneIndex(QGraphicsSceneIndexPrivate &dd, QGraphicsScene *scene); |
| 110 | |
| 111 | friend class QGraphicsScene; |
| 112 | friend class QGraphicsScenePrivate; |
| 113 | friend class QGraphicsItem; |
| 114 | friend class QGraphicsItemPrivate; |
| 115 | friend class QGraphicsSceneBspTreeIndex; |
| 116 | private: |
| 117 | Q_DISABLE_COPY_MOVE(QGraphicsSceneIndex) |
| 118 | Q_DECLARE_PRIVATE(QGraphicsSceneIndex) |
| 119 | }; |
| 120 | |
| 121 | class QGraphicsSceneIndexPrivate : public QObjectPrivate |
| 122 | { |
| 123 | Q_DECLARE_PUBLIC(QGraphicsSceneIndex) |
| 124 | public: |
| 125 | QGraphicsSceneIndexPrivate(QGraphicsScene *scene); |
| 126 | ~QGraphicsSceneIndexPrivate(); |
| 127 | |
| 128 | void init(); |
| 129 | static bool itemCollidesWithPath(const QGraphicsItem *item, const QPainterPath &path, Qt::ItemSelectionMode mode); |
| 130 | |
| 131 | void recursive_items_helper(QGraphicsItem *item, QRectF exposeRect, |
| 132 | QGraphicsSceneIndexIntersector intersect, QList<QGraphicsItem *> *items, |
| 133 | const QTransform &viewTransform, |
| 134 | Qt::ItemSelectionMode mode, qreal parentOpacity, const void *intersectData) const; |
| 135 | inline void items_helper(const QRectF &rect, QGraphicsSceneIndexIntersector intersect, |
| 136 | QList<QGraphicsItem *> *items, const QTransform &viewTransform, |
| 137 | Qt::ItemSelectionMode mode, Qt::SortOrder order, const void *intersectData) const; |
| 138 | |
| 139 | QGraphicsScene *scene; |
| 140 | }; |
| 141 | |
| 142 | inline void QGraphicsSceneIndexPrivate::items_helper(const QRectF &rect, QGraphicsSceneIndexIntersector intersect, |
| 143 | QList<QGraphicsItem *> *items, const QTransform &viewTransform, |
| 144 | Qt::ItemSelectionMode mode, Qt::SortOrder order, const void *intersectData) const |
| 145 | { |
| 146 | Q_Q(const QGraphicsSceneIndex); |
| 147 | const QList<QGraphicsItem *> tli = q->estimateTopLevelItems(rect, Qt::AscendingOrder); |
| 148 | for (int i = 0; i < tli.size(); ++i) |
| 149 | recursive_items_helper(tli.at(i), rect, intersect, items, viewTransform, mode, 1.0, intersectData); |
| 150 | if (order == Qt::DescendingOrder) { |
| 151 | const int n = items->size(); |
| 152 | for (int i = 0; i < n / 2; ++i) |
| 153 | items->swapItemsAt(i, n - i - 1); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | QT_END_NAMESPACE |
| 158 | |
| 159 | #endif // QGRAPHICSSCENEINDEX_H |
| 160 | |