From ee44073a48392972f704d36f8794171700dceba4 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 1 Jul 2009 11:57:37 +1000 Subject: FolderListModel fixes. --- src/declarative/extra/qmlfolderlistmodel.cpp | 48 +++++++++++++++++++++------- src/declarative/extra/qmlfolderlistmodel.h | 2 ++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/declarative/extra/qmlfolderlistmodel.cpp b/src/declarative/extra/qmlfolderlistmodel.cpp index abec01e..649169b 100644 --- a/src/declarative/extra/qmlfolderlistmodel.cpp +++ b/src/declarative/extra/qmlfolderlistmodel.cpp @@ -51,7 +51,7 @@ QT_MODULE(Declarative) class QmlFolderListModelPrivate : public QObjectPrivate { public: - QmlFolderListModelPrivate() { + QmlFolderListModelPrivate() : count(0) { folder = QDir::currentPath(); nameFilters << "*"; } @@ -60,6 +60,7 @@ public: QString folder; QStringList nameFilters; QModelIndex folderIndex; + int count; }; QmlFolderListModel::QmlFolderListModel(QObject *parent) @@ -69,6 +70,11 @@ QmlFolderListModel::QmlFolderListModel(QObject *parent) d->model.setFilter(QDir::AllDirs | QDir::Files | QDir::Drives); connect(&d->model, SIGNAL(rowsInserted(const QModelIndex&,int,int)) , this, SLOT(inserted(const QModelIndex&,int,int))); + connect(&d->model, SIGNAL(rowsRemoved(const QModelIndex&,int,int)) + , this, SLOT(removed(const QModelIndex&,int,int))); + connect(&d->model, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)) + , this, SLOT(dataChanged(const QModelIndex&,const QModelIndex&))); + connect(&d->model, SIGNAL(modelReset()), this, SLOT(refresh())); } QmlFolderListModel::~QmlFolderListModel() @@ -91,9 +97,7 @@ QHash QmlFolderListModel::data(int index, const QList &roles) int QmlFolderListModel::count() const { Q_D(const QmlFolderListModel); - if (!d->folderIndex.isValid()) - return 0; - return d->model.rowCount(d->folderIndex); + return d->count; } QList QmlFolderListModel::roles() const @@ -164,22 +168,42 @@ bool QmlFolderListModel::isFolder(int index) const void QmlFolderListModel::refresh() { Q_D(QmlFolderListModel); - int prevCount = count(); d->folderIndex = QModelIndex(); - if (prevCount) - emit itemsRemoved(0, prevCount); + if (d->count) { + int tmpCount = d->count; + d->count = 0; + emit itemsRemoved(0, tmpCount); + } d->folderIndex = d->model.index(d->folder); - qDebug() << "count" << count(); - if (count()) - emit itemsInserted(0, count()); + d->count = d->model.rowCount(d->folderIndex); + if (d->count) { + emit itemsInserted(0, d->count); + } } void QmlFolderListModel::inserted(const QModelIndex &index, int start, int end) { Q_D(QmlFolderListModel); - qDebug() << "inserted" << start << end; - if (index == d->folderIndex) + if (index == d->folderIndex) { + d->count = d->model.rowCount(d->folderIndex); emit itemsInserted(start, end - start + 1); + } +} + +void QmlFolderListModel::removed(const QModelIndex &index, int start, int end) +{ + Q_D(QmlFolderListModel); + if (index == d->folderIndex) { + d->count = d->model.rowCount(d->folderIndex); + emit itemsRemoved(start, end - start + 1); + } +} + +void QmlFolderListModel::dataChanged(const QModelIndex &start, const QModelIndex &end) +{ + Q_D(QmlFolderListModel); + if (start.parent() == d->folderIndex) + emit itemsChanged(start.row(), end.row() - start.row() + 1, roles()); } QML_DEFINE_TYPE(QmlFolderListModel,FolderListModel) diff --git a/src/declarative/extra/qmlfolderlistmodel.h b/src/declarative/extra/qmlfolderlistmodel.h index a6e8526..d46ec79 100644 --- a/src/declarative/extra/qmlfolderlistmodel.h +++ b/src/declarative/extra/qmlfolderlistmodel.h @@ -88,6 +88,8 @@ Q_SIGNALS: private Q_SLOTS: void refresh(); void inserted(const QModelIndex &index, int start, int end); + void removed(const QModelIndex &index, int start, int end); + void dataChanged(const QModelIndex &start, const QModelIndex &end); private: Q_DECLARE_PRIVATE(QmlFolderListModel) -- cgit v0.12