summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2009-09-10 22:32:18 (GMT)
committerMartin Jones <martin.jones@nokia.com>2009-09-10 22:32:18 (GMT)
commitad96554aa5eae45d256120dbaf840da0bcee2fba (patch)
tree8c0389713eb7d9fb7ac31d9e700f2edb85c3979e
parent033f7de03ed83cfa68436660e6354cb94452ee25 (diff)
downloadQt-ad96554aa5eae45d256120dbaf840da0bcee2fba.zip
Qt-ad96554aa5eae45d256120dbaf840da0bcee2fba.tar.gz
Qt-ad96554aa5eae45d256120dbaf840da0bcee2fba.tar.bz2
Implement itemsMoved() in ListView.
No animation possible currently.
-rw-r--r--src/declarative/fx/qfxlistview.cpp78
-rw-r--r--src/declarative/fx/qfxvisualitemmodel.cpp19
-rw-r--r--src/declarative/util/qmllistmodel.cpp2
3 files changed, 73 insertions, 26 deletions
diff --git a/src/declarative/fx/qfxlistview.cpp b/src/declarative/fx/qfxlistview.cpp
index 49de016..a07b3c0 100644
--- a/src/declarative/fx/qfxlistview.cpp
+++ b/src/declarative/fx/qfxlistview.cpp
@@ -377,7 +377,6 @@ public:
QmlComponent *highlightComponent;
FxListItem *highlight;
FxListItem *trackedItem;
- QFxItem *activeItem; //XXX fix
enum MovementReason { Other, Key, Mouse };
MovementReason moveReason;
int buffer;
@@ -1431,9 +1430,7 @@ void QFxListView::itemResized()
Q_D(QFxListView);
QFxItem *item = qobject_cast<QFxItem*>(sender());
if (item) {
- d->activeItem = item; // Ick - don't delete the sender
d->layout();
- d->activeItem = 0;
d->fixupPosition();
}
}
@@ -1642,33 +1639,66 @@ void QFxListView::destroyRemoved()
void QFxListView::itemsMoved(int from, int to, int count)
{
- qWarning() << "ListView does not support moving in models";
-
Q_D(QFxListView);
- int fromCount = count;
- int toCount = count;
- bool fromVisible = d->mapRangeFromModel(from, fromCount);
- bool toVisible = d->mapRangeFromModel(to, toCount);
-
- if (!fromVisible && !toVisible) {
- // The items are outside the visible range.
- if (d->visibleItems.count())
- d->visibleIndex = -1;
- for (int i = 0; i < d->visibleItems.count(); ++i) {
- FxListItem *listItem = d->visibleItems.at(i);
- if (listItem->index != -1) {
- listItem->index = d->model->indexOf(listItem->item, this);
- if (d->visibleIndex < 0)
- d->visibleIndex = listItem->index;
+ QHash<int,FxListItem*> moved;
+ int moveBy = 0;
+
+ QList<FxListItem*>::Iterator it = d->visibleItems.begin();
+ while (it != d->visibleItems.end()) {
+ FxListItem *item = *it;
+ if (item->index >= from && item->index < from + count) {
+ // take the items that are moving
+ item->index += (to-from);
+ moved.insert(item->index, item);
+ moveBy += item->size();
+ it = d->visibleItems.erase(it);
+ } else {
+ if (item->index > from && item->index != -1) {
+ // move everything after the moved items.
+ item->index -= count;
+ item->setPosition(item->position()-moveBy);
}
+ ++it;
}
- if (d->currentItem) {
- d->currentItem->index = d->model->indexOf(d->currentItem->item, this);
- d->currentIndex = d->currentItem->index;
+ }
+
+ int remaining = count;
+ int endIndex = d->visibleIndex;
+ it = d->visibleItems.begin();
+ while (it != d->visibleItems.end()) {
+ FxListItem *item = *it;
+ if (remaining && item->index >= to && item->index < to + count) {
+ // place items in the target position, reusing any existing items
+ FxListItem *movedItem = moved.take(item->index);
+ if (!movedItem)
+ movedItem = d->createItem(item->index);
+ it = d->visibleItems.insert(it, movedItem);
+ ++it;
+ --remaining;
+ } else {
+ if (item->index != -1) {
+ if (item->index >= to) {
+ // update everything after the moved items.
+ item->index += count;
+ }
+ endIndex = item->index;
+ }
+ ++it;
}
- return;
}
+ // If we have moved items to the end of the visible items
+ // then add any existing moved items that we have
+ while (FxListItem *item = moved.take(endIndex+1)) {
+ d->visibleItems.append(item);
+ ++endIndex;
+ }
+
+ // Whatever moved items remain are no longer visible items.
+ while (moved.count())
+ d->releaseItem(moved.take(moved.begin().key()));
+
+ d->layout();
}
void QFxListView::createdItem(int index, QFxItem *item)
diff --git a/src/declarative/fx/qfxvisualitemmodel.cpp b/src/declarative/fx/qfxvisualitemmodel.cpp
index cac8b8d..30879a2 100644
--- a/src/declarative/fx/qfxvisualitemmodel.cpp
+++ b/src/declarative/fx/qfxvisualitemmodel.cpp
@@ -695,7 +695,8 @@ QFxVisualDataModel::ReleaseFlags QFxVisualDataModel::release(QFxItem *item)
if (inPackage)
emit destroyingPackage(qobject_cast<QmlPackage*>(obj));
stat |= Destroyed;
- delete obj;
+ obj->setParent(0);
+ obj->deleteLater();
} else if (!inPackage) {
stat |= Referenced;
}
@@ -916,6 +917,22 @@ void QFxVisualDataModel::_q_itemsMoved(int from, int to, int count)
++iter;
}
}
+ for (QHash<int,QFxVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin();
+ iter != d->m_cache.end(); ) {
+
+ if (iter.key() >= qMin(from,to) && iter.key() < qMax(from+count,to+count)) {
+ QFxVisualDataModelPrivate::ObjectRef objRef = *iter;
+ int index = iter.key() + from - to;
+ iter = d->m_cache.erase(iter);
+
+ items.insert(index, objRef);
+
+ QFxVisualDataModelData *data = d->data(objRef.obj);
+ data->setIndex(index);
+ } else {
+ ++iter;
+ }
+ }
d->m_cache.unite(items);
emit itemsMoved(from, to, count);
diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp
index 19499a1..345bc3b 100644
--- a/src/declarative/util/qmllistmodel.cpp
+++ b/src/declarative/util/qmllistmodel.cpp
@@ -497,7 +497,7 @@ void QmlListModel::insert(int index, const QScriptValue& valuemap)
*/
void QmlListModel::move(int from, int to, int n)
{
- if (from+n > count() || to+n > count() || n==0 || from==to)
+ if (from+n > count() || to+n > count() || n==0 || from==to || from < 0 || to < 0)
return;
if (from > to) {
// Only move forwards - flip if backwards moving