From f21957b3bb12a16a905f79f3a791931eb77663d9 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Fri, 13 Nov 2009 13:09:40 +1000 Subject: Enforce unique role names for XmlListModel. --- src/declarative/util/qmlxmllistmodel.cpp | 26 ++++++++-------------- .../declarative/qmlxmllistmodel/data/unique.qml | 8 +++++++ .../qmlxmllistmodel/tst_qmlxmllistmodel.cpp | 15 +++++++++++++ 3 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 tests/auto/declarative/qmlxmllistmodel/data/unique.qml diff --git a/src/declarative/util/qmlxmllistmodel.cpp b/src/declarative/util/qmlxmllistmodel.cpp index 3d90b44..8407b1d 100644 --- a/src/declarative/util/qmlxmllistmodel.cpp +++ b/src/declarative/util/qmlxmllistmodel.cpp @@ -132,8 +132,6 @@ struct QmlXmlRoleList : public QmlConcreteList QmlXmlRoleList(QmlXmlListModelPrivate *p) : model(p) {} virtual void append(QmlXmlListModelRole *role); - //XXX clear, removeAt, and insert need to invalidate any cached data (in data table) as well - // (and the model should emit the appropriate signals) virtual void clear(); virtual void removeAt(int i); virtual void insert(int i, QmlXmlListModelRole *role); @@ -309,7 +307,7 @@ void QmlXmlQuery::doSubQueryJob() b.seek(0); } - //XXX this method is much slower, but would work better for incremental loading + //this method is much slower, but works better for incremental loading /*for (int j = 0; j < m_size; ++j) { QList resultList; for (int i = 0; i < m_roleObjects->size(); ++i) { @@ -338,13 +336,6 @@ void QmlXmlQuery::doSubQueryJob() }*/ } - -//TODO: error handling (currently quite fragile) -// profile doQuery and doSubquery -// support complex/nested objects? -// how do we handle data updates (like rss feed -- usually items inserted at beginning) - - class QmlXmlListModelPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QmlXmlListModel) @@ -373,14 +364,12 @@ public: }; -void QmlXmlRoleList::append(QmlXmlListModelRole *role) { - QmlConcreteList::append(role); - model->roles << model->highestRole; - model->roleNames << role->name(); - ++model->highestRole; +void QmlXmlRoleList::append(QmlXmlListModelRole *role) +{ + insert(size(), role); } -//XXX clear, removeAt, and insert need to invalidate any cached data (in data table) as well +//### clear, removeAt, and insert need to invalidate any cached data (in data table) as well // (and the model should emit the appropriate signals) void QmlXmlRoleList::clear() { @@ -396,10 +385,13 @@ void QmlXmlRoleList::removeAt(int i) QmlConcreteList::removeAt(i); } -//### we should enforce unique role names void QmlXmlRoleList::insert(int i, QmlXmlListModelRole *role) { QmlConcreteList::insert(i, role); + if (model->roleNames.contains(role->name())) { + qmlInfo(role) << QObject::tr("\"%1\" duplicates a previous role name and will be disabled.").arg(role->name()); + return; + } model->roles.insert(i, model->highestRole); model->roleNames.insert(i, role->name()); ++model->highestRole; diff --git a/tests/auto/declarative/qmlxmllistmodel/data/unique.qml b/tests/auto/declarative/qmlxmllistmodel/data/unique.qml new file mode 100644 index 0000000..ed0f293 --- /dev/null +++ b/tests/auto/declarative/qmlxmllistmodel/data/unique.qml @@ -0,0 +1,8 @@ +import Qt 4.6 + +XmlListModel { + source: "model.xml" + query: "/Pets/Pet" + XmlRole { name: "name"; query: "name/string()" } + XmlRole { name: "name"; query: "type/string()" } +} diff --git a/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp b/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp index 71bc4f9..a68006d 100644 --- a/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp +++ b/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp @@ -58,6 +58,7 @@ private slots: void attributes(); void roles(); void roleErrors(); + void uniqueRoleNames(); private: QmlEngine engine; @@ -175,6 +176,20 @@ void tst_qmlxmllistmodel::roleErrors() delete listModel; } +void tst_qmlxmllistmodel::uniqueRoleNames() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/unique.qml")); + QTest::ignoreMessage(QtWarningMsg, "QML QmlXmlListModelRole (file://" SRCDIR "/data/unique.qml:7:5) \"name\" duplicates a previous role name and will be disabled."); + QmlXmlListModel *listModel = qobject_cast(component.create()); + QVERIFY(listModel != 0); + QTRY_COMPARE(listModel->count(), 9); + + QList roles = listModel->roles(); + QCOMPARE(roles.count(), 1); + + delete listModel; +} + QTEST_MAIN(tst_qmlxmllistmodel) #include "tst_qmlxmllistmodel.moc" -- cgit v0.12