aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Vrátil <dan@progdan.cz>2014-07-29 11:11:11 +0200
committerAlbert Astals Cid <albert.astals@canonical.com>2014-07-30 13:17:35 +0200
commit47ffb30a69285d6ced1287f14ff5842a298e60e0 (patch)
tree9f83fcf67d3d0baa9400a0452bd867000a959a97
parent872008a0e053d739636e4ad8ecbbc941b3fdc99d (diff)
Fix QQmlDelegateModel getting out of sync
Fixes a regression introduced by a0aefe1, which caused that the source data model and adaptorModel could sometimes get out of sync. Change-Id: Ia6b5fc380cc6cf6549ae857e6da54e088a5dadb5 Reviewed-by: Alan Alpert (Personal) <416365416c@gmail.com>
-rw-r--r--src/qml/types/qqmldelegatemodel.cpp7
-rw-r--r--tests/auto/quick/qquicklistview/data/layoutChangeSort.qml58
-rw-r--r--tests/auto/quick/qquicklistview/qquicklistview.pro7
-rw-r--r--tests/auto/quick/qquicklistview/randomsortmodel.cpp107
-rw-r--r--tests/auto/quick/qquicklistview/randomsortmodel.h64
-rw-r--r--tests/auto/quick/qquicklistview/tst_qquicklistview.cpp38
6 files changed, 273 insertions, 8 deletions
diff --git a/src/qml/types/qqmldelegatemodel.cpp b/src/qml/types/qqmldelegatemodel.cpp
index 4591d42710..23bdeb55b2 100644
--- a/src/qml/types/qqmldelegatemodel.cpp
+++ b/src/qml/types/qqmldelegatemodel.cpp
@@ -1558,12 +1558,7 @@ void QQmlDelegateModel::_q_layoutChanged(const QList<QPersistentModelIndex> &par
if (i == index.row())
continue;
- QVector<Compositor::Insert> inserts;
- QVector<Compositor::Remove> removes;
- d->m_compositor.listItemsMoved(&d->m_adaptorModel, i, index.row(), 1, &removes, &inserts);
- if (!removes.isEmpty() || !inserts.isEmpty()) {
- d->itemsMoved(removes, inserts);
- }
+ _q_itemsMoved(i, index.row(), 1);
}
d->m_storedPersistentIndexes.clear();
diff --git a/tests/auto/quick/qquicklistview/data/layoutChangeSort.qml b/tests/auto/quick/qquicklistview/data/layoutChangeSort.qml
new file mode 100644
index 0000000000..e54f164e45
--- /dev/null
+++ b/tests/auto/quick/qquicklistview/data/layoutChangeSort.qml
@@ -0,0 +1,58 @@
+import QtQuick 2.0
+import QtQml.Models 2.1
+
+Rectangle {
+ id: root
+ width: 240
+ height: 320
+ color: "#ffffff"
+
+
+ Component {
+ id: myDelegate
+ Rectangle {
+ id: wrapper
+ objectName: "wrapper"
+ height: 20
+ width: 240
+ Text {
+ objectName: "delegateText"
+ text: display
+ }
+ color: ListView.isCurrentItem ? "lightsteelblue" : "white"
+ }
+ }
+
+ DelegateModel {
+ id: delegateModel
+ objectName: "delegateModel"
+ model: testModel
+ delegate: myDelegate
+ }
+
+ ListView {
+ id: list
+ objectName: "listView"
+ model: delegateModel;
+ focus: true
+ anchors.fill: parent
+
+
+ section {
+ property: "SortRole";
+ delegate: Rectangle {
+ width: parent.width;
+ height: 20;
+ color: "steelblue";
+
+ Text {
+ anchors {
+ fill: parent;
+ margins: 5;
+ }
+ text: section;
+ }
+ }
+ }
+ }
+}
diff --git a/tests/auto/quick/qquicklistview/qquicklistview.pro b/tests/auto/quick/qquicklistview/qquicklistview.pro
index 2ae04d32fe..c9b634b9e8 100644
--- a/tests/auto/quick/qquicklistview/qquicklistview.pro
+++ b/tests/auto/quick/qquicklistview/qquicklistview.pro
@@ -4,10 +4,13 @@ TARGET = tst_qquicklistview
macx:CONFIG -= app_bundle
HEADERS += incrementalmodel.h \
- proxytestinnermodel.h
+ proxytestinnermodel.h \
+ randomsortmodel.h
SOURCES += tst_qquicklistview.cpp \
incrementalmodel.cpp \
- proxytestinnermodel.cpp
+ proxytestinnermodel.cpp \
+ randomsortmodel.cpp
+
include (../../shared/util.pri)
include (../shared/util.pri)
diff --git a/tests/auto/quick/qquicklistview/randomsortmodel.cpp b/tests/auto/quick/qquicklistview/randomsortmodel.cpp
new file mode 100644
index 0000000000..e9082e39c9
--- /dev/null
+++ b/tests/auto/quick/qquicklistview/randomsortmodel.cpp
@@ -0,0 +1,107 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "randomsortmodel.h"
+
+RandomSortModel::RandomSortModel(QObject* parent):
+ QAbstractListModel(parent)
+{
+ for (int i = 0; i < 10; ++i) {
+ mData.append(qMakePair(QString::fromLatin1("Item %1").arg(i), i * 10));
+ }
+}
+
+QHash<int, QByteArray> RandomSortModel::roleNames() const
+{
+ QHash<int,QByteArray> roles = QAbstractItemModel::roleNames();
+ roles[Qt::UserRole] = "SortRole";
+ return roles;
+}
+
+
+int RandomSortModel::rowCount(const QModelIndex& parent) const
+{
+ if (!parent.isValid())
+ return mData.count();
+
+ return 0;
+}
+
+QVariant RandomSortModel::data(const QModelIndex& index, int role) const
+{
+ if (!index.isValid()) {
+ return QVariant();
+ }
+
+ if (index.row() >= mData.count()) {
+ return QVariant();
+ }
+
+ if (role == Qt::DisplayRole) {
+ return QString::fromLatin1("%1 (weight %2)").arg(mData[index.row()].first).arg(mData[index.row()].second);
+ } else if (role == Qt::UserRole) {
+ return mData[index.row()].second;
+ }
+
+ return QVariant();
+}
+
+void RandomSortModel::randomize()
+{
+ const int row = qrand() % mData.count();
+ int random;
+ bool exists = false;
+ // Make sure we won't end up with two items with the same weight, as that
+ // would make unit-testing much harder
+ do {
+ exists = false;
+ random = qrand() % (mData.count() * 10);
+ QList<QPair<QString, int> >::ConstIterator iter, end;
+ for (iter = mData.constBegin(), end = mData.constEnd(); iter != end; ++iter) {
+ if ((*iter).second == random) {
+ exists = true;
+ break;
+ }
+ }
+ } while (exists);
+ mData[row].second = random;
+ Q_EMIT dataChanged(index(row, 0), index(row, 0));
+}
diff --git a/tests/auto/quick/qquicklistview/randomsortmodel.h b/tests/auto/quick/qquicklistview/randomsortmodel.h
new file mode 100644
index 0000000000..8d28698d9b
--- /dev/null
+++ b/tests/auto/quick/qquicklistview/randomsortmodel.h
@@ -0,0 +1,64 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef RANDOMSORTMODEL_H
+#define RANDOMSORTMODEL_H
+
+#include <QAbstractListModel>
+
+class RandomSortModel : public QAbstractListModel
+{
+ Q_OBJECT
+
+public:
+ explicit RandomSortModel(QObject* parent = 0);
+ QHash<int, QByteArray> roleNames() const;
+
+ QVariant data(const QModelIndex& index, int role) const;
+ int rowCount(const QModelIndex& parent = QModelIndex()) const;
+
+ void randomize();
+
+ private:
+ QList<QPair<QString, int> > mData;
+};
+
+#endif // RANDOMSORTMODEL_H
diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp
index b561258351..4fcf186690 100644
--- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp
+++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp
@@ -55,6 +55,7 @@
#include "../shared/visualtestutil.h"
#include "incrementalmodel.h"
#include "proxytestinnermodel.h"
+#include "randomsortmodel.h"
#include <math.h>
Q_DECLARE_METATYPE(Qt::LayoutDirection)
@@ -231,6 +232,8 @@ private slots:
void QTBUG_38209();
void programmaticFlickAtBounds();
+ void layoutChange();
+
private:
template <class T> void items(const QUrl &source);
template <class T> void changed(const QUrl &source);
@@ -7365,6 +7368,41 @@ void tst_QQuickListView::programmaticFlickAtBounds()
QVERIFY(!spy.wait(100));
}
+void tst_QQuickListView::layoutChange()
+{
+ RandomSortModel *model = new RandomSortModel;
+ QSortFilterProxyModel *sortModel = new QSortFilterProxyModel;
+ sortModel->setSourceModel(model);
+ sortModel->setSortRole(Qt::UserRole);
+ sortModel->setDynamicSortFilter(true);
+ sortModel->sort(0);
+
+ QScopedPointer<QQuickView> window(createView());
+ window->rootContext()->setContextProperty("testModel", QVariant::fromValue(sortModel));
+ window->setSource(testFileUrl("layoutChangeSort.qml"));
+ window->show();
+ QVERIFY(QTest::qWaitForWindowExposed(window.data()));
+
+ QQuickListView *listview = window->rootObject()->findChild<QQuickListView *>("listView");
+ QVERIFY(listview);
+
+ for (int iter = 0; iter < 100; iter++) {
+ for (int i = 0; i < sortModel->rowCount(); ++i) {
+ QQuickItem *delegateItem = listview->itemAt(10, 10 + 2 * i * 20 + 20); // item + group
+ QVERIFY(delegateItem);
+ QQuickItem *delegateText = delegateItem->findChild<QQuickItem *>("delegateText");
+ QVERIFY(delegateText);
+
+ QCOMPARE(delegateText->property("text").toString(),
+ sortModel->index(i, 0, QModelIndex()).data().toString());
+ }
+
+ model->randomize();
+ listview->forceLayout();
+ QTest::qWait(5); // give view a chance to update
+ }
+}
+
QTEST_MAIN(tst_QQuickListView)
#include "tst_qquicklistview.moc"