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 template<typename T> class QList;
71 
72 typedef bool (*QGraphicsSceneIndexIntersector)(const QGraphicsItem *item, const QRectF &exposeRect, Qt::ItemSelectionMode mode,
73                                                const QTransform &deviceTransform, const void *data);
74 
75 class Q_AUTOTEST_EXPORT QGraphicsSceneIndex : public QObject
76 {
77     Q_OBJECT
78 
79 public:
80     QGraphicsSceneIndex(QGraphicsScene *scene = nullptr);
81     virtual ~QGraphicsSceneIndex();
82 
83     QGraphicsScene *scene() const;
84 
85     virtual QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const  = 0;
86     virtual QList<QGraphicsItem *> items(const QPointF &pos, Qt::ItemSelectionMode mode,
87                                          Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
88     virtual QList<QGraphicsItem *> items(const QRectF &rect, Qt::ItemSelectionMode mode,
89                                          Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
90     virtual QList<QGraphicsItem *> items(const QPolygonF &polygon, Qt::ItemSelectionMode mode,
91                                          Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
92     virtual QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode,
93                                          Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const;
94     virtual QList<QGraphicsItem *> estimateItems(const QPointF &point, Qt::SortOrder order) const;
95     virtual QList<QGraphicsItem *> estimateItems(const QRectF &rect, Qt::SortOrder order) const = 0;
96     virtual QList<QGraphicsItem *> estimateTopLevelItems(const QRectF &, Qt::SortOrder order) const;
97 
98 protected Q_SLOTS:
99     virtual void updateSceneRect(const QRectF &rect);
100 
101 protected:
102     virtual void clear();
103     virtual void addItem(QGraphicsItem *item) = 0;
104     virtual void removeItem(QGraphicsItem *item) = 0;
105     virtual void deleteItem(QGraphicsItem *item);
106 
107     virtual void itemChange(const QGraphicsItem *item, QGraphicsItem::GraphicsItemChange, const void *const value);
108     virtual void prepareBoundingRectChange(const QGraphicsItem *item);
109 
110     QGraphicsSceneIndex(QGraphicsSceneIndexPrivate &dd, QGraphicsScene *scene);
111 
112     friend class QGraphicsScene;
113     friend class QGraphicsScenePrivate;
114     friend class QGraphicsItem;
115     friend class QGraphicsItemPrivate;
116     friend class QGraphicsSceneBspTreeIndex;
117 private:
118     Q_DISABLE_COPY_MOVE(QGraphicsSceneIndex)
119     Q_DECLARE_PRIVATE(QGraphicsSceneIndex)
120 };
121 
122 class QGraphicsSceneIndexPrivate : public QObjectPrivate
123 {
124     Q_DECLARE_PUBLIC(QGraphicsSceneIndex)
125 public:
126     QGraphicsSceneIndexPrivate(QGraphicsScene *scene);
127     ~QGraphicsSceneIndexPrivate();
128 
129     void init();
130     static bool itemCollidesWithPath(const QGraphicsItem *item, const QPainterPath &path, Qt::ItemSelectionMode mode);
131 
132     void recursive_items_helper(QGraphicsItem *item, QRectF exposeRect,
133                                 QGraphicsSceneIndexIntersector intersect, QList<QGraphicsItem *> *items,
134                                 const QTransform &viewTransform,
135                                 Qt::ItemSelectionMode mode, qreal parentOpacity, const void *intersectData) const;
136     inline void items_helper(const QRectF &rect, QGraphicsSceneIndexIntersector intersect,
137                              QList<QGraphicsItem *> *items, const QTransform &viewTransform,
138                              Qt::ItemSelectionMode mode, Qt::SortOrder order, const void *intersectData) const;
139 
140     QGraphicsScene *scene;
141 };
142 
items_helper(const QRectF & rect,QGraphicsSceneIndexIntersector intersect,QList<QGraphicsItem * > * items,const QTransform & viewTransform,Qt::ItemSelectionMode mode,Qt::SortOrder order,const void * intersectData)143 inline void QGraphicsSceneIndexPrivate::items_helper(const QRectF &rect, QGraphicsSceneIndexIntersector intersect,
144                                                      QList<QGraphicsItem *> *items, const QTransform &viewTransform,
145                                                      Qt::ItemSelectionMode mode, Qt::SortOrder order, const void *intersectData) const
146 {
147     Q_Q(const QGraphicsSceneIndex);
148     const QList<QGraphicsItem *> tli = q->estimateTopLevelItems(rect, Qt::AscendingOrder);
149     for (int i = 0; i < tli.size(); ++i)
150         recursive_items_helper(tli.at(i), rect, intersect, items, viewTransform, mode, 1.0, intersectData);
151     if (order == Qt::DescendingOrder) {
152         const int n = items->size();
153         for (int i = 0; i < n / 2; ++i)
154             items->swapItemsAt(i, n - i - 1);
155     }
156 }
157 
158 QT_END_NAMESPACE
159 
160 #endif // QGRAPHICSSCENEINDEX_H
161