diff options
author | Andrew den Exter <andrew.den-exter@nokia.com> | 2012-07-10 17:01:52 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-07-25 02:15:15 +0200 |
commit | 7daab8039abc32ab5be5706a08cb58905fe0e0b6 (patch) | |
tree | 9ceb8afdeb37ac4d9bc089545e73e31040b847e4 /src/imports | |
parent | b355aacb6e5c4f9b7ebb317125409ea0959d11d6 (diff) |
Remove QListModelInterface.
Implement ListModel and XmlListModel using QAbstractListModel
instead.
Task-number: QTBUG-15728
Change-Id: I14e03d90883d341f4b1d89c1e9fc9dc1534fde78
Reviewed-by: Glenn Watson <glenn.watson@nokia.com>
Diffstat (limited to 'src/imports')
-rw-r--r-- | src/imports/xmllistmodel/qqmlxmllistmodel.cpp | 99 | ||||
-rw-r--r-- | src/imports/xmllistmodel/qqmlxmllistmodel_p.h | 16 |
2 files changed, 63 insertions, 52 deletions
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 <QTimer> #include <QMutex> -#include <private/qobject_p.h> +#include <private/qabstractitemmodel_p.h> 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<QQuickXmlListModelRo */ QQuickXmlListModel::QQuickXmlListModel(QObject *parent) - : QListModelInterface(*(new QQuickXmlListModelPrivate), parent) + : QAbstractListModel(*(new QQuickXmlListModelPrivate), parent) { } @@ -734,48 +734,46 @@ QQmlListProperty<QQuickXmlListModelRole> QQuickXmlListModel::roleObjects() return list; } -QHash<int,QVariant> QQuickXmlListModel::data(int index, const QList<int> &roles) const +QModelIndex QQuickXmlListModel::index(int row, int column, const QModelIndex &parent) const { Q_D(const QQuickXmlListModel); - QHash<int, QVariant> 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<int> QQuickXmlListModel::roles() const +QHash<int, QByteArray> QQuickXmlListModel::roleNames() const { Q_D(const QQuickXmlListModel); - return d->roles; + QHash<int,QByteArray> 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<result.removed.count(); i++) - emit itemsRemoved(result.removed[i].first, result.removed[i].second); - for (int i=0; i<result.inserted.count(); i++) - emit itemsInserted(result.inserted[i].first, result.inserted[i].second); - - if (sizeChanged) - emit countChanged(); + for (int i=0; i<result.removed.count(); i++) { + const int index = result.removed[i].first; + const int count = result.removed[i].second; + if (count > 0) { + beginRemoveRows(QModelIndex(), index, index + count - 1); + endRemoveRows(); + } + } + for (int i=0; i<result.inserted.count(); i++) { + const int index = result.inserted[i].first; + const int count = result.inserted[i].second; + if (count > 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 <QtCore/qurl.h> #include <QtCore/qstringlist.h> - -#include <private/qlistmodelinterface_p.h> +#include <QtCore/qabstractitemmodel.h> #include <private/qv8engine_p.h> 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<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const; - virtual QVariant data(int index, int role) const; - virtual int count() const; - virtual QList<int> 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<int, QByteArray> roleNames() const; + + int count() const; QQmlListProperty<QQuickXmlListModelRole> roleObjects(); |