From 7daab8039abc32ab5be5706a08cb58905fe0e0b6 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Tue, 10 Jul 2012 17:01:52 +1000 Subject: Remove QListModelInterface. Implement ListModel and XmlListModel using QAbstractListModel instead. Task-number: QTBUG-15728 Change-Id: I14e03d90883d341f4b1d89c1e9fc9dc1534fde78 Reviewed-by: Glenn Watson --- src/imports/xmllistmodel/qqmlxmllistmodel.cpp | 99 +++++++++++++++------------ src/imports/xmllistmodel/qqmlxmllistmodel_p.h | 16 ++--- 2 files changed, 63 insertions(+), 52 deletions(-) (limited to 'src/imports') diff --git a/src/imports/xmllistmodel/qqmlxmllistmodel.cpp b/src/imports/xmllistmodel/qqmlxmllistmodel.cpp index 5a06855f1e..6914861724 100644 --- a/src/imports/xmllistmodel/qqmlxmllistmodel.cpp +++ b/src/imports/xmllistmodel/qqmlxmllistmodel.cpp @@ -57,7 +57,7 @@ #include #include -#include +#include Q_DECLARE_METATYPE(QQuickXmlQueryResult) @@ -528,7 +528,7 @@ void QQuickXmlQueryEngine::doSubQueryJob(XmlQueryJob *currentJob, QQuickXmlQuery }*/ } -class QQuickXmlListModelPrivate : public QObjectPrivate +class QQuickXmlListModelPrivate : public QAbstractItemModelPrivate { Q_DECLARE_PUBLIC(QQuickXmlListModel) public: @@ -712,7 +712,7 @@ void QQuickXmlListModelPrivate::clear_role(QQmlListProperty QQuickXmlListModel::roleObjects() return list; } -QHash QQuickXmlListModel::data(int index, const QList &roles) const +QModelIndex QQuickXmlListModel::index(int row, int column, const QModelIndex &parent) const { Q_D(const QQuickXmlListModel); - QHash rv; - for (int i = 0; i < roles.size(); ++i) { - int role = roles.at(i); - int roleIndex = d->roles.indexOf(role); - rv.insert(role, roleIndex == -1 ? QVariant() : d->data.value(roleIndex).value(index)); - } - return rv; + return !parent.isValid() && column == 0 && row >= 0 && row < d->size + ? createIndex(row, column) + : QModelIndex(); } -QVariant QQuickXmlListModel::data(int index, int role) const +int QQuickXmlListModel::rowCount(const QModelIndex &parent) const { Q_D(const QQuickXmlListModel); - int roleIndex = d->roles.indexOf(role); - return (roleIndex == -1) ? QVariant() : d->data.value(roleIndex).value(index); + return !parent.isValid() ? d->size : 0; } -/*! - \qmlproperty int QtQuick.XmlListModel2::XmlListModel::count - The number of data entries in the model. -*/ -int QQuickXmlListModel::count() const +QVariant QQuickXmlListModel::data(const QModelIndex &index, int role) const { Q_D(const QQuickXmlListModel); - return d->size; + const int roleIndex = d->roles.indexOf(role); + return (roleIndex == -1 || !index.isValid()) + ? QVariant() + : d->data.value(roleIndex).value(index.row()); } -QList QQuickXmlListModel::roles() const +QHash QQuickXmlListModel::roleNames() const { Q_D(const QQuickXmlListModel); - return d->roles; + QHash roleNames; + for (int i = 0; i < d->roles.count(); ++i) + roleNames.insert(d->roles.at(i), d->roleNames.at(i).toUtf8()); + return roleNames; } -QString QQuickXmlListModel::toString(int role) const +/*! + \qmlproperty int QtQuick.XmlListModel2::XmlListModel::count + The number of data entries in the model. +*/ +int QQuickXmlListModel::count() const { Q_D(const QQuickXmlListModel); - int index = d->roles.indexOf(role); - if (index == -1) - return QString(); - return d->roleNames.at(index); + return d->size; } /*! @@ -1071,11 +1069,11 @@ void QQuickXmlListModel::requestFinished() d->errorString = d->reply->errorString(); d->deleteReply(); - int count = this->count(); - d->data.clear(); - d->size = 0; - if (count > 0) { - emit itemsRemoved(0, count); + if (d->size > 0) { + beginRemoveRows(QModelIndex(), 0, d->size - 1); + d->data.clear(); + d->size = 0; + endRemoveRows(); emit countChanged(); } @@ -1157,21 +1155,34 @@ void QQuickXmlListModel::queryCompleted(const QQuickXmlQueryResult &result) } } if (!hasKeys) { - if (!(origCount == 0 && d->size == 0)) { - emit itemsRemoved(0, origCount); - emit itemsInserted(0, d->size); - emit countChanged(); + if (origCount > 0) { + beginRemoveRows(QModelIndex(), 0, origCount - 1); + endRemoveRows(); + } + if (d->size > 0) { + beginInsertRows(QModelIndex(), 0, d->size - 1); + endInsertRows(); } - } else { - for (int i=0; i 0) { + beginRemoveRows(QModelIndex(), index, index + count - 1); + endRemoveRows(); + } + } + for (int i=0; i 0) { + beginInsertRows(QModelIndex(), index, index + count - 1); + endInsertRows(); + } + } } + if (sizeChanged) + emit countChanged(); emit statusChanged(d->status); } diff --git a/src/imports/xmllistmodel/qqmlxmllistmodel_p.h b/src/imports/xmllistmodel/qqmlxmllistmodel_p.h index 5bc4c7b494..4a2ea6cfee 100644 --- a/src/imports/xmllistmodel/qqmlxmllistmodel_p.h +++ b/src/imports/xmllistmodel/qqmlxmllistmodel_p.h @@ -47,8 +47,7 @@ #include #include - -#include +#include #include QT_BEGIN_HEADER @@ -69,7 +68,7 @@ struct QQuickXmlQueryResult { QStringList keyRoleResultsCache; }; -class QQuickXmlListModel : public QListModelInterface, public QQmlParserStatus +class QQuickXmlListModel : public QAbstractListModel, public QQmlParserStatus { Q_OBJECT Q_INTERFACES(QQmlParserStatus) @@ -89,11 +88,12 @@ public: QQuickXmlListModel(QObject *parent = 0); ~QQuickXmlListModel(); - virtual QHash data(int index, const QList &roles = (QList())) const; - virtual QVariant data(int index, int role) const; - virtual int count() const; - virtual QList roles() const; - virtual QString toString(int role) const; + QModelIndex index(int row, int column, const QModelIndex &parent) const; + int rowCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; + QHash roleNames() const; + + int count() const; QQmlListProperty roleObjects(); -- cgit v1.2.3