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 QGRAPHICSSCENEINDEX_H
5#define QGRAPHICSSCENEINDEX_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 "qgraphicsscene_p.h"
20#include "qgraphicsscene.h"
21#include <private/qobject_p.h>
22
23#include <QtCore/qnamespace.h>
24#include <QtCore/qobject.h>
25#include <QtGui/qtransform.h>
26
27QT_REQUIRE_CONFIG(graphicsview);
28
29QT_BEGIN_NAMESPACE
30
31class QGraphicsSceneIndexPrivate;
32class QPointF;
33class QRectF;
34
35typedef bool (*QGraphicsSceneIndexIntersector)(const QGraphicsItem *item, const QRectF &exposeRect, Qt::ItemSelectionMode mode,
36 const QTransform &deviceTransform, const void *data);
37
38class Q_AUTOTEST_EXPORT QGraphicsSceneIndex : public QObject
39{
40 Q_OBJECT
41
42public:
43 QGraphicsSceneIndex(QGraphicsScene *scene = nullptr);
44 virtual ~QGraphicsSceneIndex();
45
46 QGraphicsScene *scene() const;
47
48 virtual QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const = 0;
49 virtual QList<QGraphicsItem *> items(const QPointF &pos, Qt::ItemSelectionMode mode,
50 Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
51 virtual QList<QGraphicsItem *> items(const QRectF &rect, Qt::ItemSelectionMode mode,
52 Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
53 virtual QList<QGraphicsItem *> items(const QPolygonF &polygon, Qt::ItemSelectionMode mode,
54 Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
55 virtual QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode,
56 Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
57 virtual QList<QGraphicsItem *> estimateItems(const QPointF &point, Qt::SortOrder order) const;
58 virtual QList<QGraphicsItem *> estimateItems(const QRectF &rect, Qt::SortOrder order) const = 0;
59 virtual QList<QGraphicsItem *> estimateTopLevelItems(const QRectF &, Qt::SortOrder order) const;
60
61protected Q_SLOTS:
62 virtual void updateSceneRect(const QRectF &rect);
63
64protected:
65 virtual void clear();
66 virtual void addItem(QGraphicsItem *item) = 0;
67 virtual void removeItem(QGraphicsItem *item) = 0;
68 virtual void deleteItem(QGraphicsItem *item);
69
70 virtual void itemChange(const QGraphicsItem *item, QGraphicsItem::GraphicsItemChange, const void *const value);
71 virtual void prepareBoundingRectChange(const QGraphicsItem *item);
72
73 QGraphicsSceneIndex(QGraphicsSceneIndexPrivate &dd, QGraphicsScene *scene);
74
75 friend class QGraphicsScene;
76 friend class QGraphicsScenePrivate;
77 friend class QGraphicsItem;
78 friend class QGraphicsItemPrivate;
79 friend class QGraphicsSceneBspTreeIndex;
80private:
81 Q_DISABLE_COPY_MOVE(QGraphicsSceneIndex)
82 Q_DECLARE_PRIVATE(QGraphicsSceneIndex)
83};
84
85class QGraphicsSceneIndexPrivate : public QObjectPrivate
86{
87 Q_DECLARE_PUBLIC(QGraphicsSceneIndex)
88public:
89 QGraphicsSceneIndexPrivate(QGraphicsScene *scene);
90 ~QGraphicsSceneIndexPrivate();
91
92 void init();
93 static bool itemCollidesWithPath(const QGraphicsItem *item, const QPainterPath &path, Qt::ItemSelectionMode mode);
94
95 void recursive_items_helper(QGraphicsItem *item, QRectF exposeRect,
96 QGraphicsSceneIndexIntersector intersect, QList<QGraphicsItem *> *items,
97 const QTransform &viewTransform,
98 Qt::ItemSelectionMode mode, qreal parentOpacity, const void *intersectData) const;
99 inline void items_helper(const QRectF &rect, QGraphicsSceneIndexIntersector intersect,
100 QList<QGraphicsItem *> *items, const QTransform &viewTransform,
101 Qt::ItemSelectionMode mode, Qt::SortOrder order, const void *intersectData) const;
102
103 QGraphicsScene *scene;
104};
105
106inline void QGraphicsSceneIndexPrivate::items_helper(const QRectF &rect, QGraphicsSceneIndexIntersector intersect,
107 QList<QGraphicsItem *> *items, const QTransform &viewTransform,
108 Qt::ItemSelectionMode mode, Qt::SortOrder order, const void *intersectData) const
109{
110 Q_Q(const QGraphicsSceneIndex);
111 const QList<QGraphicsItem *> tli = q->estimateTopLevelItems(rect, order: Qt::AscendingOrder);
112 for (int i = 0; i < tli.size(); ++i)
113 recursive_items_helper(item: tli.at(i), exposeRect: rect, intersect, items, viewTransform, mode, parentOpacity: 1.0, intersectData);
114 if (order == Qt::DescendingOrder) {
115 const int n = items->size();
116 for (int i = 0; i < n / 2; ++i)
117 items->swapItemsAt(i, j: n - i - 1);
118 }
119}
120
121QT_END_NAMESPACE
122
123#endif // QGRAPHICSSCENEINDEX_H
124

source code of qtbase/src/widgets/graphicsview/qgraphicssceneindex_p.h