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 QGRAPHICSSCENELINEARINDEX_H |
5 | #define QGRAPHICSSCENELINEARINDEX_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 | |
20 | #include <QtCore/qrect.h> |
21 | #include <QtCore/qlist.h> |
22 | #include <QtWidgets/qgraphicsitem.h> |
23 | #include <private/qgraphicssceneindex_p.h> |
24 | |
25 | QT_REQUIRE_CONFIG(graphicsview); |
26 | |
27 | QT_BEGIN_NAMESPACE |
28 | |
29 | class Q_AUTOTEST_EXPORT QGraphicsSceneLinearIndex : public QGraphicsSceneIndex |
30 | { |
31 | Q_OBJECT |
32 | |
33 | public: |
34 | QGraphicsSceneLinearIndex(QGraphicsScene *scene = nullptr) : QGraphicsSceneIndex(scene), m_numSortedElements(0) |
35 | { } |
36 | |
37 | QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const override |
38 | { Q_UNUSED(order); return m_items; } |
39 | |
40 | virtual QList<QGraphicsItem *> estimateItems(const QRectF &rect, Qt::SortOrder order) const override |
41 | { |
42 | Q_UNUSED(rect); |
43 | Q_UNUSED(order); |
44 | return m_items; |
45 | } |
46 | |
47 | protected : |
48 | virtual void clear() override |
49 | { |
50 | m_items.clear(); |
51 | m_numSortedElements = 0; |
52 | } |
53 | |
54 | virtual void addItem(QGraphicsItem *item) override |
55 | { m_items << item; } |
56 | |
57 | virtual void removeItem(QGraphicsItem *item) override |
58 | { |
59 | // Sort m_items if needed |
60 | if (m_numSortedElements < m_items.size()) |
61 | { |
62 | std::sort(first: m_items.begin() + m_numSortedElements, last: m_items.end() ); |
63 | std::inplace_merge(first: m_items.begin(), middle: m_items.begin() + m_numSortedElements, last: m_items.end()); |
64 | m_numSortedElements = m_items.size(); |
65 | } |
66 | |
67 | QList<QGraphicsItem*>::iterator element = std::lower_bound(m_items.begin(), m_items.end(), item); |
68 | if (element != m_items.end() && *element == item) |
69 | { |
70 | m_items.erase(pos: element); |
71 | --m_numSortedElements; |
72 | } |
73 | } |
74 | |
75 | private: |
76 | QList<QGraphicsItem*> m_items; |
77 | int m_numSortedElements; |
78 | }; |
79 | |
80 | QT_END_NAMESPACE |
81 | |
82 | #endif // QGRAPHICSSCENELINEARINDEX_H |
83 |