diff --git a/src/widgets/graphicsview/qgraphicsscene.h b/src/widgets/graphicsview/qgraphicsscene.h index ba47d45d63..d95237baef 100644 --- a/src/widgets/graphicsview/qgraphicsscene.h +++ b/src/widgets/graphicsview/qgraphicsscene.h @@ -313,6 +313,7 @@ private: friend class QGraphicsEffect; friend class QGraphicsSceneIndex; friend class QGraphicsSceneIndexPrivate; + friend class QGraphicsSceneLinearIndex; friend class QGraphicsSceneBspTreeIndex; friend class QGraphicsSceneBspTreeIndexPrivate; friend class QGraphicsItemEffectSourcePrivate; diff --git a/src/widgets/graphicsview/qgraphicsscenelinearindex_p.h b/src/widgets/graphicsview/qgraphicsscenelinearindex_p.h index 7debcfb501..baf3de3755 100644 --- a/src/widgets/graphicsview/qgraphicsscenelinearindex_p.h +++ b/src/widgets/graphicsview/qgraphicsscenelinearindex_p.h @@ -70,31 +70,62 @@ class Q_AUTOTEST_EXPORT QGraphicsSceneLinearIndex : public QGraphicsSceneIndex Q_OBJECT public: - QGraphicsSceneLinearIndex(QGraphicsScene *scene = 0) : QGraphicsSceneIndex(scene) + QGraphicsSceneLinearIndex(QGraphicsScene *scene = 0) : QGraphicsSceneIndex(scene), m_itemsIsValid(true) { } QList items(Qt::SortOrder order = Qt::DescendingOrder) const - { Q_UNUSED(order); return m_items; } + { + Q_UNUSED(order); + return validList(); + } virtual QList estimateItems(const QRectF &rect, Qt::SortOrder order) const { Q_UNUSED(rect); Q_UNUSED(order); - return m_items; + return validList(); } protected : virtual void clear() - { m_items.clear(); } + { + m_items.clear(); + m_itemsIsValid = true; + } virtual void addItem(QGraphicsItem *item) - { m_items << item; } + { + if (m_itemsIsValid) + m_items << item; + } virtual void removeItem(QGraphicsItem *item) - { m_items.removeOne(item); } + { + Q_UNUSED(item); + m_itemsIsValid = false; + } private: - QList m_items; + mutable QList m_items; + mutable bool m_itemsIsValid; + + QList& validList() const + { + if (!m_itemsIsValid) + { + m_items.clear(); + + QList stack = scene()->d_func()->topLevelItems; + m_items << stack; + while (!stack.isEmpty()) + { + m_items << stack.last()->childItems(); + stack << stack.takeLast()->childItems(); + } + m_itemsIsValid = true; + } + return m_items; + } }; #endif // QT_NO_GRAPHICSVIEW