summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorWarwick Allison <warwick.allison@nokia.com>2010-05-18 01:37:20 (GMT)
committerWarwick Allison <warwick.allison@nokia.com>2010-05-18 01:37:20 (GMT)
commit4fa070d69c84de373eeed79aea569d408757cd52 (patch)
tree7bdc3e8690cd6f4e51a61297e08e4eef7e26f08d /src
parente971948611b4790f0b3a05a29d8e7ab2e72e95f4 (diff)
downloadQt-4fa070d69c84de373eeed79aea569d408757cd52.zip
Qt-4fa070d69c84de373eeed79aea569d408757cd52.tar.gz
Qt-4fa070d69c84de373eeed79aea569d408757cd52.tar.bz2
Port from QListModelInterface to QAbstractListModel.
Diffstat (limited to 'src')
-rw-r--r--src/imports/dirmodel/qdeclarativefolderlistmodel.cpp76
-rw-r--r--src/imports/dirmodel/qdeclarativefolderlistmodel.h19
2 files changed, 44 insertions, 51 deletions
diff --git a/src/imports/dirmodel/qdeclarativefolderlistmodel.cpp b/src/imports/dirmodel/qdeclarativefolderlistmodel.cpp
index 11f9733..50ef4ae 100644
--- a/src/imports/dirmodel/qdeclarativefolderlistmodel.cpp
+++ b/src/imports/dirmodel/qdeclarativefolderlistmodel.cpp
@@ -110,8 +110,13 @@ public:
*/
QDeclarativeFolderListModel::QDeclarativeFolderListModel(QObject *parent)
- : QListModelInterface(parent)
+ : QAbstractListModel(parent)
{
+ QHash<int, QByteArray> roles;
+ roles[FileNameRole] = "fileName";
+ roles[FilePathRole] = "filePath";
+ setRoleNames(roles);
+
d = new QDeclarativeFolderListModelPrivate;
d->model.setFilter(QDir::AllDirs | QDir::Files | QDir::Drives | QDir::NoDotAndDotDot);
connect(&d->model, SIGNAL(rowsInserted(const QModelIndex&,int,int))
@@ -129,56 +134,35 @@ QDeclarativeFolderListModel::~QDeclarativeFolderListModel()
delete d;
}
-QHash<int,QVariant> QDeclarativeFolderListModel::data(int index, const QList<int> &roles) const
-{
- Q_UNUSED(roles);
- QHash<int,QVariant> folderData;
- QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
- if (modelIndex.isValid()) {
- folderData[QDirModel::FileNameRole] = d->model.data(modelIndex, QDirModel::FileNameRole);
- folderData[QDirModel::FilePathRole] = QUrl::fromLocalFile(d->model.data(modelIndex, QDirModel::FilePathRole).toString());
- }
-
- return folderData;
-}
-
-QVariant QDeclarativeFolderListModel::data(int index, int role) const
+QVariant QDeclarativeFolderListModel::data(const QModelIndex &index, int role) const
{
QVariant rv;
- QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
+ QModelIndex modelIndex = d->model.index(index.row(), 0, d->folderIndex);
if (modelIndex.isValid()) {
- if (role == QDirModel::FileNameRole)
- rv = d->model.data(modelIndex, QDirModel::FileNameRole);
- else if (role == QDirModel::FilePathRole)
+ if (role == FileNameRole)
+ rv = d->model.data(modelIndex, QDirModel::FileNameRole).toString();
+ else if (role == FilePathRole)
rv = QUrl::fromLocalFile(d->model.data(modelIndex, QDirModel::FilePathRole).toString());
}
-
return rv;
}
-int QDeclarativeFolderListModel::count() const
+QString QDeclarativeFolderListModel::fileName(int index) const
{
- return d->count;
+ QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
+ return d->model.data(modelIndex, QDirModel::FileNameRole).toString();
}
-QList<int> QDeclarativeFolderListModel::roles() const
+QUrl QDeclarativeFolderListModel::filePath(int index) const
{
- QList<int> r;
- r << QDirModel::FileNameRole;
- r << QDirModel::FilePathRole;
- return r;
+ QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
+ return QUrl::fromLocalFile(d->model.data(modelIndex, QDirModel::FilePathRole).toString());
}
-QString QDeclarativeFolderListModel::toString(int role) const
+int QDeclarativeFolderListModel::rowCount(const QModelIndex &parent) const
{
- switch (role) {
- case QDirModel::FileNameRole:
- return QLatin1String("fileName");
- case QDirModel::FilePathRole:
- return QLatin1String("filePath");
- }
-
- return QString();
+ Q_UNUSED(parent);
+ return d->count;
}
/*!
@@ -313,37 +297,41 @@ void QDeclarativeFolderListModel::refresh()
{
d->folderIndex = QModelIndex();
if (d->count) {
- int tmpCount = d->count;
+ emit beginRemoveRows(QModelIndex(), 0, d->count);
d->count = 0;
- emit itemsRemoved(0, tmpCount);
+ emit endRemoveRows();
}
d->folderIndex = d->model.index(d->folder.toLocalFile());
- d->count = d->model.rowCount(d->folderIndex);
- if (d->count) {
- emit itemsInserted(0, d->count);
+ int newcount = d->model.rowCount(d->folderIndex);
+ if (newcount) {
+ emit beginInsertRows(QModelIndex(), 0, newcount-1);
+ d->count = newcount;
+ emit endInsertRows();
}
}
void QDeclarativeFolderListModel::inserted(const QModelIndex &index, int start, int end)
{
if (index == d->folderIndex) {
+ emit beginInsertRows(QModelIndex(), start, end);
d->count = d->model.rowCount(d->folderIndex);
- emit itemsInserted(start, end - start + 1);
+ emit endInsertRows();
}
}
void QDeclarativeFolderListModel::removed(const QModelIndex &index, int start, int end)
{
if (index == d->folderIndex) {
+ emit beginRemoveRows(QModelIndex(), start, end);
d->count = d->model.rowCount(d->folderIndex);
- emit itemsRemoved(start, end - start + 1);
+ emit endRemoveRows();
}
}
void QDeclarativeFolderListModel::dataChanged(const QModelIndex &start, const QModelIndex &end)
{
if (start.parent() == d->folderIndex)
- emit itemsChanged(start.row(), end.row() - start.row() + 1, roles());
+ emit dataChanged(index(start.row(),0), index(end.row(),0));
}
/*!
diff --git a/src/imports/dirmodel/qdeclarativefolderlistmodel.h b/src/imports/dirmodel/qdeclarativefolderlistmodel.h
index 0ca935c..86a7588 100644
--- a/src/imports/dirmodel/qdeclarativefolderlistmodel.h
+++ b/src/imports/dirmodel/qdeclarativefolderlistmodel.h
@@ -45,13 +45,13 @@
#include <qdeclarative.h>
#include <QStringList>
#include <QUrl>
-#include <private/qlistmodelinterface_p.h>
+#include <QAbstractListModel>
class QDeclarativeContext;
class QModelIndex;
class QDeclarativeFolderListModelPrivate;
-class QDeclarativeFolderListModel : public QListModelInterface, public QDeclarativeParserStatus
+class QDeclarativeFolderListModel : public QAbstractListModel, public QDeclarativeParserStatus
{
Q_OBJECT
Q_INTERFACES(QDeclarativeParserStatus)
@@ -64,16 +64,21 @@ class QDeclarativeFolderListModel : public QListModelInterface, public QDeclarat
Q_PROPERTY(bool showDirs READ showDirs WRITE setShowDirs)
Q_PROPERTY(bool showDotAndDotDot READ showDotAndDotDot WRITE setShowDotAndDotDot)
Q_PROPERTY(bool showOnlyReadable READ showOnlyReadable WRITE setShowOnlyReadable)
+ Q_PROPERTY(int count READ count)
public:
QDeclarativeFolderListModel(QObject *parent = 0);
~QDeclarativeFolderListModel();
- 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;
+ enum Roles { FileNameRole = Qt::UserRole+1, FilePathRole = Qt::UserRole+2 };
+
+ int rowCount(const QModelIndex &parent) const;
+ QVariant data(const QModelIndex &index, int role) const;
+
+ int count() const { return rowCount(QModelIndex()); }
+
+ Q_INVOKABLE QString fileName(int index) const;
+ Q_INVOKABLE QUrl filePath(int index) const;
QUrl folder() const;
void setFolder(const QUrl &folder);