Qt 4.8
qgraphicsscenebsptreeindex_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //
43 // W A R N I N G
44 // -------------
45 //
46 // This file is not part of the Qt API. It exists for the convenience
47 // of other Qt classes. This header file may change from version to
48 // version without notice, or even be removed.
49 //
50 // We mean it.
51 //
52 
53 #ifndef QGRAPHICSBSPTREEINDEX_H
54 #define QGRAPHICSBSPTREEINDEX_H
55 
56 #include <QtCore/qglobal.h>
57 
58 #if !defined(QT_NO_GRAPHICSVIEW) || (QT_EDITION & QT_MODULE_GRAPHICSVIEW) != QT_MODULE_GRAPHICSVIEW
59 
60 #include "qgraphicssceneindex_p.h"
61 #include "qgraphicsitem_p.h"
62 #include "qgraphicsscene_bsp_p.h"
63 
64 #include <QtCore/qrect.h>
65 #include <QtCore/qlist.h>
66 
68 
69 static const int QGRAPHICSSCENE_INDEXTIMER_TIMEOUT = 2000;
70 
71 class QGraphicsScene;
73 
75 {
76  Q_OBJECT
78 public:
81 
82  QList<QGraphicsItem *> estimateItems(const QRectF &rect, Qt::SortOrder order) const;
85 
86  int bspTreeDepth();
87  void setBspTreeDepth(int depth);
88 
89 protected Q_SLOTS:
90  void updateSceneRect(const QRectF &rect);
91 
92 protected:
93  bool event(QEvent *event);
94  void clear();
95 
96  void addItem(QGraphicsItem *item);
97  void removeItem(QGraphicsItem *item);
98  void prepareBoundingRectChange(const QGraphicsItem *item);
99 
100  void itemChange(const QGraphicsItem *item, QGraphicsItem::GraphicsItemChange change, const void *const value);
101 
102 private :
105  Q_PRIVATE_SLOT(d_func(), void _q_updateSortCache())
106  Q_PRIVATE_SLOT(d_func(), void _q_updateIndex())
107 
110 };
111 
113 {
115 public:
116  QGraphicsSceneBspTreeIndexPrivate(QGraphicsScene *scene);
117 
125 
130 
133  void purgeRemovedItems();
134 
135  void _q_updateIndex();
136  void startIndexTimer(int interval = QGRAPHICSSCENE_INDEXTIMER_TIMEOUT);
137  void resetIndex();
138 
139  void _q_updateSortCache();
142  void invalidateSortCache();
143  void addItem(QGraphicsItem *item, bool recursive = false);
144  void removeItem(QGraphicsItem *item, bool recursive = false, bool moveToUnindexedItems = false);
145  QList<QGraphicsItem *> estimateItems(const QRectF &, Qt::SortOrder, bool b = false);
146 
147  static void climbTree(QGraphicsItem *item, int *stackingOrder);
148 
149  static inline bool closestItemFirst_withCache(const QGraphicsItem *item1, const QGraphicsItem *item2)
150  {
151  return item1->d_ptr->globalStackingOrder < item2->d_ptr->globalStackingOrder;
152  }
153  static inline bool closestItemLast_withCache(const QGraphicsItem *item1, const QGraphicsItem *item2)
154  {
155  return item1->d_ptr->globalStackingOrder >= item2->d_ptr->globalStackingOrder;
156  }
157 
158  static void sortItems(QList<QGraphicsItem *> *itemList, Qt::SortOrder order,
159  bool cached, bool onlyTopLevelItems = false);
160 };
161 
162 static inline bool QRectF_intersects(const QRectF &s, const QRectF &r)
163 {
164  qreal xp = s.left();
165  qreal yp = s.top();
166  qreal w = s.width();
167  qreal h = s.height();
168  qreal l1 = xp;
169  qreal r1 = xp;
170  if (w < 0)
171  l1 += w;
172  else
173  r1 += w;
174 
175  qreal l2 = r.left();
176  qreal r2 = r.left();
177  if (w < 0)
178  l2 += r.width();
179  else
180  r2 += r.width();
181 
182  if (l1 >= r2 || l2 >= r1)
183  return false;
184 
185  qreal t1 = yp;
186  qreal b1 = yp;
187  if (h < 0)
188  t1 += h;
189  else
190  b1 += h;
191 
192  qreal t2 = r.top();
193  qreal b2 = r.top();
194  if (r.height() < 0)
195  t2 += r.height();
196  else
197  b2 += r.height();
198 
199  return !(t1 >= b2 || t2 >= b1);
200 }
201 
203 
204 #endif // QT_NO_GRAPHICSVIEW
205 
206 #endif // QGRAPHICSBSPTREEINDEX_H
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items...
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
QScopedPointer< QGraphicsItemPrivate > d_ptr
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
qreal left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:525
The QGraphicsSceneBspTreeIndex class provides an implementation of a BSP indexing algorithm for disco...
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
GraphicsItemChange
This enum describes the state changes that are notified by QGraphicsItem::itemChange().
#define Q_SLOTS
Definition: qobjectdefs.h:71
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
static const int QGRAPHICSSCENE_INDEXTIMER_TIMEOUT
static bool closestItemLast_withCache(const QGraphicsItem *item1, const QGraphicsItem *item2)
virtual void removeItem(QGraphicsItem *item)=0
This pure virtual function removes an item to the scene index.
static bool closestItemFirst_withCache(const QGraphicsItem *item1, const QGraphicsItem *item2)
SortOrder
Definition: qnamespace.h:189
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void itemChange(const QGraphicsItem *item, QGraphicsItem::GraphicsItemChange, const void *const value)
This virtual function is called by QGraphicsItem to notify the index that some part of the item &#39;s st...
int bspTreeDepth
the depth of the BSP index tree
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
virtual void prepareBoundingRectChange(const QGraphicsItem *item)
Notify the index for a geometry change of an item.
virtual QList< QGraphicsItem * > estimateTopLevelItems(const QRectF &, Qt::SortOrder order) const
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
The QGraphicsSceneIndex class provides a base class to implement a custom indexing algorithm for disc...
virtual void clear()
This virtual function removes all items in the scene index.
#define Q_OBJECT
Definition: qobjectdefs.h:157
static bool QRectF_intersects(const QRectF &s, const QRectF &r)
virtual void addItem(QGraphicsItem *item)=0
This pure virtual function inserts an item to the scene index.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
virtual QList< QGraphicsItem * > estimateItems(const QPointF &point, Qt::SortOrder order) const
This virtual function return an estimation of items at position point.
virtual void updateSceneRect(const QRectF &rect)
Notifies the index that the scene&#39;s scene rect has changed.
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
qreal top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:526
friend class QGraphicsSceneBspTreeIndex
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
#define class
virtual QList< QGraphicsItem * > items(Qt::SortOrder order=Qt::DescendingOrder) const =0
This pure virtual function all items in the index and sort them using order.
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define private
Definition: qregion_qws.cpp:43
The QList class is a template class that provides lists.
Definition: qdatastream.h:62