Skip to content

Commit

Permalink
Remove QListModelInterface.
Browse files Browse the repository at this point in the history
Implement ListModel and XmlListModel using QAbstractListModel
instead.

Task-number: QTBUG-15728

Change-Id: I14e03d90883d341f4b1d89c1e9fc9dc1534fde78
Reviewed-by: Glenn Watson <glenn.watson@nokia.com>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Jul 25, 2012
1 parent b355aac commit 7daab80
Show file tree
Hide file tree
Showing 29 changed files with 426 additions and 974 deletions.
99 changes: 55 additions & 44 deletions src/imports/xmllistmodel/qqmlxmllistmodel.cpp
Expand Up @@ -57,7 +57,7 @@
#include <QTimer>
#include <QMutex>

#include <private/qobject_p.h>
#include <private/qabstractitemmodel_p.h>

Q_DECLARE_METATYPE(QQuickXmlQueryResult)

Expand Down Expand Up @@ -528,7 +528,7 @@ void QQuickXmlQueryEngine::doSubQueryJob(XmlQueryJob *currentJob, QQuickXmlQuery
}*/
}

class QQuickXmlListModelPrivate : public QObjectPrivate
class QQuickXmlListModelPrivate : public QAbstractItemModelPrivate
{
Q_DECLARE_PUBLIC(QQuickXmlListModel)
public:
Expand Down Expand Up @@ -712,7 +712,7 @@ void QQuickXmlListModelPrivate::clear_role(QQmlListProperty<QQuickXmlListModelRo
*/

QQuickXmlListModel::QQuickXmlListModel(QObject *parent)
: QListModelInterface(*(new QQuickXmlListModelPrivate), parent)
: QAbstractListModel(*(new QQuickXmlListModelPrivate), parent)
{
}

Expand All @@ -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;
}

/*!
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions src/imports/xmllistmodel/qqmlxmllistmodel_p.h
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -89,11 +88,12 @@ class QQuickXmlListModel : public QListModelInterface, public QQmlParserStatus
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();

Expand Down
104 changes: 0 additions & 104 deletions src/qml/qml/qlistmodelinterface.cpp

This file was deleted.

83 changes: 0 additions & 83 deletions src/qml/qml/qlistmodelinterface_p.h

This file was deleted.

0 comments on commit 7daab80

Please sign in to comment.