summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Zander <thomas.zander@trolltech.com>2009-06-08 12:19:36 +0200
committerThomas Zander <thomas.zander@trolltech.com>2009-06-08 12:22:01 +0200
commitcfcb45bfc3c5f0ef17227d4f6f48780468df96b7 (patch)
treee5acc6a428a20d64dca80e4dc7296c559d4004db
parent0b50196b7b4931b8f37e79824cf0fedf1ad0e901 (diff)
Use a creator with recycled items memory for the flowview
-rw-r--r--src/experimental/qgraphicsflowview.cpp44
1 files changed, 37 insertions, 7 deletions
diff --git a/src/experimental/qgraphicsflowview.cpp b/src/experimental/qgraphicsflowview.cpp
index 56da1f8..928b3da 100644
--- a/src/experimental/qgraphicsflowview.cpp
+++ b/src/experimental/qgraphicsflowview.cpp
@@ -53,6 +53,15 @@ private:
Q_DECLARE_PRIVATE(QtGraphicsListViewItem)
};
+class QtGraphicsFlowViewItemCreator : public QtGraphicsListViewItemCreatorBase
+{
+public:
+ virtual QtGraphicsListViewItem *create(int index, QtGraphicsListView *view);
+ virtual void recycle(QtGraphicsListViewItem *item);
+
+private:
+ QList<QtGraphicsListViewItem*> m_recycledItems;
+};
QtGraphicsFlowViewPrivate::QtGraphicsFlowViewPrivate()
: itemSize(80, 60)
@@ -108,6 +117,23 @@ QtGraphicsFlowViewItem::QtGraphicsFlowViewItem(int index, QtGraphicsListView *la
setCacheMode(DeviceCoordinateCache);
}
+QtGraphicsListViewItem *QtGraphicsFlowViewItemCreator::create(int index, QtGraphicsListView *view)
+{
+ QtGraphicsListViewItem *item;
+ if (m_recycledItems.isEmpty()) {
+ item = new QtGraphicsFlowViewItem(index, view);
+ } else {
+ item = m_recycledItems.takeLast();
+ item->setIndex(index);
+ }
+ return item;
+}
+
+void QtGraphicsFlowViewItemCreator::recycle(QtGraphicsListViewItem *item)
+{
+ m_recycledItems.append(item);
+}
+
/*!
\class QtGraphicsFlowView
@@ -181,14 +207,16 @@ void QtGraphicsFlowView::doLayout()
const int amountNeeded = qMin(d->model->count() - d->firstIndex, visibleRows * itemsPerRow);
if (!d->items.isEmpty()) {
- // reuse old items by moving them around. Starting from firstIndex, ending at firstIndex + amountNeeded
+ // reuse old items by moving them around. Working hard to make them keep their index.
+ // Starting from firstIndex, ending at firstIndex + amountNeeded
const int index = d->firstIndex;
while (d->items.first()->index() < index) { // scrolling down
QtGraphicsListViewItem *item = d->items.takeFirst();
item->setIndex(d->items.last()->index() + 1);
d->items.append(item);
}
- while (d->items.last()->index() > index + d->items.count()) { // scrolling up
+ const int lastIndex = index + d->items.count();
+ while (d->items.last()->index() > lastIndex) { // scrolling up
QtGraphicsListViewItem *item = d->items.takeLast();
item->setIndex(d->items.first()->index() - 1);
d->items.prepend(item);
@@ -196,17 +224,19 @@ void QtGraphicsFlowView::doLayout()
}
while (amountNeeded < d->items.count()) // remove old ones
- delete d->items.takeLast();
+ d->creator->recycle(d->items.takeLast());
+ while (amountNeeded > d->items.count() && (d->items.isEmpty() || d->items.first()->index() > d->firstIndex))
+ d->items.prepend(d->creator->create(d->firstIndex, this));
qreal x = 0;
qreal y = 0;
for (int index = d->firstIndex; index < d->firstIndex + amountNeeded; ++index) {
QtGraphicsListViewItem *item = 0;
- const int itemIndex = index - d->firstIndex;
- if (itemIndex >= d->items.count()) {
- item = new QtGraphicsFlowViewItem(index, this);
+ const int indexInItems = index - d->firstIndex;
+ if (indexInItems >= d->items.count()) {
+ item = d->creator->create(index, this);
d->items.append(item);
} else {
- item = d->items.at(itemIndex);
+ item = d->items.at(indexInItems);
}
item->setGeometry(x, y, d->itemSize.width(), d->itemSize.height());
x += d->itemSize.width();