summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarius Bugge Monsen <mmonsen@trolltech.com>2009-06-08 14:04:45 +0200
committerMarius Bugge Monsen <mmonsen@trolltech.com>2009-06-08 14:04:45 +0200
commit7145596574319d5b1cb0784a7dba5e7519865e13 (patch)
tree68aba2363b6732193adfbcad5075e800018e7194 /src
parent7178b13b6950a79f6fac3e1d99bc7dd73116fcc5 (diff)
parentcfcb45bfc3c5f0ef17227d4f6f48780468df96b7 (diff)
Merge branch 'master' of git@scm.dev.nokia.troll.no:research/itemviews-ng
Diffstat (limited to 'src')
-rw-r--r--src/experimental/qgraphicsflowview.cpp44
-rw-r--r--src/qgraphicslistview.cpp29
-rw-r--r--src/qgraphicslistview.h2
3 files changed, 64 insertions, 11 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();
diff --git a/src/qgraphicslistview.cpp b/src/qgraphicslistview.cpp
index 0ffd73b..06cd98e 100644
--- a/src/qgraphicslistview.cpp
+++ b/src/qgraphicslistview.cpp
@@ -206,6 +206,7 @@ QtGraphicsListView *QtGraphicsListViewItem::view() const
*/
/*!
+ Destructor.
*/
QtGraphicsListViewItemCreatorBase::~QtGraphicsListViewItemCreatorBase()
{
@@ -216,7 +217,8 @@ QtGraphicsListViewItemCreatorBase::~QtGraphicsListViewItemCreatorBase()
*/
/*!
- */
+ This method basically does a recycle and a create in one go.
+*/
QtGraphicsListViewItem *QtGraphicsListViewItemCreatorBase::reassign(int index, QtGraphicsListViewItem *item)
{
Q_ASSERT(item);
@@ -225,15 +227,26 @@ QtGraphicsListViewItem *QtGraphicsListViewItemCreatorBase::reassign(int index, Q
}
/*!
- */
+ Give back the \a item to the creator pool of unused objects.
+*/
void QtGraphicsListViewItemCreatorBase::recycle(QtGraphicsListViewItem *item)
{
delete item;
}
/*!
+ \fn QtGraphicsListViewItem *QtGraphicsListViewItemCreatorBase::create(int index, QtGraphicsListView *view)
+ The view will call the create() method for every visible item it needs to display on screen.
+ This is a factory method to create the item, or reuse a previously recycled one after which
+ the ownership of the item lies with the caller.
+
+ The item will be initialized to belong to \a view and will get the \a index set to it
+ allowing it to show the correct content.
+*/
+
+/*!
\class QtGraphicsListViewItemCreator
- */
+*/
// QtGraphicsListViewPrivate
@@ -1278,4 +1291,14 @@ void QtGraphicsListView::itemGeometryChanged(QtGraphicsListViewItem *item)
}
}
+/*!
+ \class QtGraphicsListViewItemCreatorBase
+ \brief a factory baseclass to create QtGraphicsListViewItem objects for the QtGraphicsListView
+*/
+
+/*!
+ \class QtGraphicsListViewItemCreator
+ \brief a easy to use factory class to create QtGraphicsListViewItem objects for the QtGraphicsListView
+*/
+
#include "moc_qgraphicslistview.cpp"
diff --git a/src/qgraphicslistview.h b/src/qgraphicslistview.h
index 5dd65a5..7203f70 100644
--- a/src/qgraphicslistview.h
+++ b/src/qgraphicslistview.h
@@ -70,7 +70,7 @@ private:
Q_DECLARE_PRIVATE(QtGraphicsListViewItem)
};
-class QtGraphicsListViewItemCreatorBase
+class Q_ITEMVIEWSNG_EXPORT QtGraphicsListViewItemCreatorBase
{
public:
virtual ~QtGraphicsListViewItemCreatorBase();