diff options
| author | Andrew den Exter <andrew.den-exter@nokia.com> | 2011-06-22 18:26:55 +1000 |
|---|---|---|
| committer | Andrew den Exter <andrew.den-exter@nokia.com> | 2011-06-22 18:26:55 +1000 |
| commit | 32146174369ae79f9d9169e4537ece1e5e8cb3b6 (patch) | |
| tree | 23711a3021a642b44a53a9244abb7e5da345b563 | |
| parent | 1aaa43a08f7c6bc979f9e6c89eb5e4346db908c8 (diff) | |
Instanciate the VisualItemModel attached object for proxied model items.
Instead of for just the original items, and ensure the indexes are
updated when the model changes.
6 files changed, 78 insertions, 29 deletions
diff --git a/examples/declarative/dragtarget/launcher/IconDelegate.qml b/examples/declarative/dragtarget/launcher/IconDelegate.qml index 2e56632c25..f6cf8f47b6 100644 --- a/examples/declarative/dragtarget/launcher/IconDelegate.qml +++ b/examples/declarative/dragtarget/launcher/IconDelegate.qml @@ -24,7 +24,7 @@ Item { anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter } drag.target: dragArea - drag.data: index + drag.data: iconDelegate.VisualItemModel.index Binding { target: dragArea diff --git a/examples/declarative/dragtarget/launcher/launcher.qml b/examples/declarative/dragtarget/launcher/launcher.qml index 5062e26c7a..6aa15c8d16 100644 --- a/examples/declarative/dragtarget/launcher/launcher.qml +++ b/examples/declarative/dragtarget/launcher/launcher.qml @@ -59,20 +59,19 @@ Rectangle { keys: [ "applications" ] onEntered: { - sourceIndex = applicationsView.indexAt(drag.x, drag.y) - destinationIndex = sourceIndex - if (destinationIndex == -1) - drag.accepted = false + sourceIndex = drag.data + destinationIndex = applicationsView.indexAt(drag.x, drag.y) } onPositionChanged: { var index = applicationsView.indexAt(drag.x, drag.y) if (index != -1) { - applicationsVisualModel.move(destinationIndex, index, 1) + if (destinationIndex != -1) + applicationsVisualModel.move(destinationIndex, index, 1) destinationIndex = index } } -// onDropped: applicationsModel.move(sourceIndex, destinationIndex, 1) -// onExited: applicationsModel.move(destinationIndex, sourceIndex, 1) + onDropped: applicationsModel.move(sourceIndex, destinationIndex, 1) + onExited: applicationsVisualModel.move(destinationIndex, sourceIndex, 1) } DragTarget { @@ -85,16 +84,16 @@ Rectangle { onEntered: { sourceIndex = drag.data destinationIndex = applicationsView.indexAt(drag.x, drag.y) - if (destinationIndex == -1) { - drag.accepted = false - } else { + if (destinationIndex != -1) applicationsVisualModel.insert(destinationIndex, favoritesVisualModel, sourceIndex, 1) - } } onPositionChanged: { var index = applicationsView.indexAt(drag.x, drag.y) if (index != -1) { - applicationsVisualModel.move(destinationIndex, index, 1) + if (destinationIndex != -1) + applicationsVisualModel.move(destinationIndex, index, 1) + else + applicationsVisualModel.insert(index, favoritesVisualModel, sourceIndex, 1) destinationIndex = index } } @@ -159,20 +158,19 @@ Rectangle { keys: [ "favorites" ] onEntered: { - sourceIndex = favoritesView.indexAt(drag.x, drag.y) - destinationIndex = sourceIndex - if (destinationIndex == -1) - drag.accepted = false + sourceIndex = drag.data + destinationIndex = favoritesView.indexAt(drag.x, drag.y) } onPositionChanged: { var index = favoritesView.indexAt(drag.x, drag.y) if (index != -1) { - favoritesVisualModel.move(destinationIndex, index, 1) + if (destinationIndex != -1) + favoritesVisualModel.move(destinationIndex, index, 1) destinationIndex = index } } -// onDropped: favoritesModel.move(sourceIndex, destinationIndex, 1) -// onExited: favoritesVisualModel.move(destinationIndex, sourceIndex, 1) + onDropped: favoritesModel.move(sourceIndex, destinationIndex, 1) + onExited: favoritesVisualModel.move(destinationIndex, sourceIndex, 1) } DragTarget { @@ -187,16 +185,16 @@ Rectangle { onEntered: { sourceIndex = drag.data destinationIndex = favoritesView.indexAt(drag.x, drag.y) - if (destinationIndex == -1) { - drag.accepted = false - } else { + if (destinationIndex != -1) favoritesVisualModel.insert(destinationIndex, applicationsVisualModel, sourceIndex, 1) - } } onPositionChanged: { var index = favoritesView.indexAt(drag.x, drag.y) if (index != -1) { - favoritesVisualModel.move(destinationIndex, index, 1) + if (destinationIndex != -1) + favoritesVisualModel.move(destinationIndex, index, 1) + else + favoritesVisualModel.insert(index, applicationsVisualModel, sourceIndex, 1) destinationIndex = index } } diff --git a/examples/declarative/modelviews/visualdatamodel/ComposerBubble.qml b/examples/declarative/modelviews/visualdatamodel/ComposerBubble.qml index ee988e8531..9a5932298c 100644 --- a/examples/declarative/modelviews/visualdatamodel/ComposerBubble.qml +++ b/examples/declarative/modelviews/visualdatamodel/ComposerBubble.qml @@ -81,12 +81,18 @@ Rectangle { Rectangle { anchors { fill: parent; rightMargin: 1; bottomMargin: 1 } - color: "#000000" + color: "#202020" opacity: sendArea.pressed ? 0.5 : (composer.sending || composer.sent ? 0.0 : 1.0) Behavior on opacity { NumberAnimation { duration: 150 } } + Rectangle { + anchors.fill: parent + radius: 4 + color: "#080808" + } + Text { id: sendText anchors.fill: parent diff --git a/examples/declarative/modelviews/visualdatamodel/visualdatamodel.qml b/examples/declarative/modelviews/visualdatamodel/visualdatamodel.qml index 2049fbd3ba..2906ea9333 100644 --- a/examples/declarative/modelviews/visualdatamodel/visualdatamodel.qml +++ b/examples/declarative/modelviews/visualdatamodel/visualdatamodel.qml @@ -72,7 +72,7 @@ Rectangle { if (item.messageId != message.messageId) continue visualModel.replace(j, item) - // visualModel.move(item.visualIndex, j + 1 + // visualModel.move(item.VisualItemModel.index, j + 1 // visualModel.replace(j + 1, j) break } diff --git a/src/declarative/items/qsgvisualitemmodel.cpp b/src/declarative/items/qsgvisualitemmodel.cpp index 01feae769f..c07b2937f8 100644 --- a/src/declarative/items/qsgvisualitemmodel.cpp +++ b/src/declarative/items/qsgvisualitemmodel.cpp @@ -109,6 +109,7 @@ public: void itemAppended() { Q_Q(QSGVisualItemModel); QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(children.last().item); + attached->setModel(q); attached->setIndex(count()-1); emit q->itemsInserted(count()-1, 1); emit q->countChanged(); @@ -137,6 +138,19 @@ public: transactionChanges.clear(); } + void invalidateIndexes(int start, int end = INT_MAX) { + foreach (const Item &item, children) { + QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(item.item); + if (attached->m_index >= start && attached->m_index < end) + attached->setIndex(-1); + } + for (QHash<QSGItem *, QSGVisualDataModel *>::iterator it = itemModels.begin(); it != itemModels.end(); ++it) { + QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(it.key()); + if (attached->m_index >= start && attached->m_index < end) + attached->setIndex(-1); + } + } + class Item { public: Item(QSGItem *i) : item(i), ref(0) {} @@ -257,6 +271,9 @@ QSGItem *QSGVisualItemModel::item(int index, bool complete) if (!range.internal()) { QSGVisualDataModel *model = static_cast<QSGVisualDataModel *>(range.list); QSGItem *item = model->item(range.index + offset, complete); + QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(item); + attached->setModel(this); + attached->setIndex(index); d->itemModels.insert(item, model); if (model->completePending()) d->pendingModel = model; @@ -358,6 +375,9 @@ void QSGVisualItemModel::append(QSGItem *item) Q_D(QSGVisualItemModel); int index = d->count(); if (d->appendData(item)) { + QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(item); + attached->setModel(this); + attached->setIndex(count() - 1); if (d->transaction) { d->transactionChanges.insertInsert(index, index + 1); } else { @@ -403,6 +423,10 @@ void QSGVisualItemModel::insert(int index, QSGItem *item) qDebug() << Q_FUNC_INFO << index; Q_D(QSGVisualItemModel); if (d->insertData(index, item)) { + QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(item); + attached->setModel(this); + attached->setIndex(index); + d->invalidateIndexes(index); if (d->transaction) { d->transactionChanges.insertInsert(index, index + 1); } else { @@ -436,9 +460,11 @@ void QSGVisualItemModel::insert(int destinationIndex, QSGVisualItemModel *source } } + d->invalidateIndexes(destinationIndex); emit itemsInserted(destinationIndex, count); sourceModel->d_func()->removeAt(sourceIndex, count); + sourceModel->d_func()->invalidateIndexes(sourceIndex); emit sourceModel->itemsRemoved(sourceIndex, count); emit sourceModel->countChanged(); } @@ -449,6 +475,7 @@ void QSGVisualItemModel::remove(int index, int count) if (!d->transaction) d->childrenChanged = false; d->removeAt(index, count); + d->invalidateIndexes(index); if (d->transaction) { d->transactionChanges.insertRemove(index, index + count); } else { @@ -467,6 +494,7 @@ void QSGVisualItemModel::move(int from, int to, int count) if (!d->transaction) d->childrenChanged = false; d->move(from, to, count); + d->invalidateIndexes(qMin(from, to), qMax(from, to) + count); if (d->transaction) { d->transactionChanges.insertMove(from, from + count, to); } else { @@ -491,12 +519,16 @@ void QSGVisualItemModel::replace(int index, QSGItem *item) if (from != index) d->transactionChanges.insertMove(from, from + 1, index); + d->invalidateIndexes(index); if (!d->transaction) d->emitTransactionChanges(); } } else { d->replaceAt(index, item); } + QSGVisualItemModelAttached *attached = QSGVisualItemModelAttached::properties(item); + attached->setModel(this); + attached->setIndex(index); } void QSGVisualItemModel::replace(int destinationIndex, QSGVisualItemModel *sourceModel, int sourceIndex, int count) @@ -520,6 +552,7 @@ void QSGVisualItemModel::replace(int destinationIndex, QSGVisualItemModel *sourc } } sourceModel->d_func()->removeAt(sourceIndex, count); + sourceModel->d_func()->invalidateIndexes(sourceIndex); emit sourceModel->itemsRemoved(sourceIndex, count); emit sourceModel->countChanged(); } @@ -534,6 +567,7 @@ void QSGVisualItemModel::_q_itemsInserted(int index, int count) d->listItemsInserted(static_cast<QSGVisualDataModel *>(sender()), index, index + count, &inserts); if (inserts.count() > 0) { + d->invalidateIndexes(index); QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(this)); QScriptValue insertIndexes = engine->newArray(inserts.count()); for (int i = 0; i < inserts.count(); ++i) { @@ -562,6 +596,7 @@ void QSGVisualItemModel::_q_itemsRemoved(int index, int count) d->childrenChanged = false; d->listItemsRemoved(static_cast<QSGVisualDataModel *>(sender()), index, index + count, &removes); d->transactionChanges.append(removes); + d->invalidateIndexes(index); d->emitTransactionChanges(); emit countChanged(); if (d->childrenChanged) @@ -575,6 +610,7 @@ void QSGVisualItemModel::_q_itemsMoved(int from, int to, int count) d->childrenChanged = false; d->listItemsMoved(static_cast<QSGVisualDataModel *>(sender()), from, from + count, to, &moves); d->transactionChanges.append(moves); + d->invalidateIndexes(qMin(from, to), qMax(from, to) + count); d->emitTransactionChanges(); if (d->childrenChanged) emit childrenChanged(); diff --git a/src/declarative/items/qsgvisualitemmodel_p.h b/src/declarative/items/qsgvisualitemmodel_p.h index bfcb91360d..dc13b5beb8 100644 --- a/src/declarative/items/qsgvisualitemmodel_p.h +++ b/src/declarative/items/qsgvisualitemmodel_p.h @@ -44,10 +44,13 @@ #define QSGVISUALITEMMODEL_P_H #include <QtDeclarative/qdeclarative.h> +#include <QtDeclarative/qsgitem.h> #include <QtCore/qobject.h> #include <QtCore/qabstractitemmodel.h> #include <QtScript/qscriptvalue.h> +#include <private/qdeclarativeguard_p.h> + QT_BEGIN_HEADER Q_DECLARE_METATYPE(QModelIndex) @@ -158,7 +161,6 @@ private: Q_DISABLE_COPY(QSGVisualItemModel) }; - class Q_DECLARATIVE_EXPORT QSGVisualDataModel : public QSGVisualModel { Q_OBJECT @@ -287,7 +289,11 @@ public: } Q_PROPERTY(int index READ index NOTIFY indexChanged) - int index() const { return m_index; } + int index() { + if (m_index == -1 && m_model) + m_index = m_model->indexOf(qobject_cast<QSGItem *>(parent()), 0); + return m_index; + } void setIndex(int idx) { if (m_index != idx) { m_index = idx; @@ -295,6 +301,8 @@ public: } } + void setModel(QSGVisualItemModel *model) { m_model = model; } + static QSGVisualItemModelAttached *properties(QObject *obj) { QSGVisualItemModelAttached *rv = attachedProperties.value(obj); if (!rv) { @@ -309,6 +317,7 @@ Q_SIGNALS: public: int m_index; + QDeclarativeGuard<QSGVisualItemModel> m_model; static QHash<QObject*, QSGVisualItemModelAttached*> attachedProperties; }; |
