17class QQuickFolderListModelPrivate
19 Q_DECLARE_PUBLIC(QQuickFolderListModel)
22 QQuickFolderListModelPrivate(QQuickFolderListModel *q) : q_ptr(q) { }
24 QQuickFolderListModel *q_ptr;
27 FileInfoThread fileInfoThread;
28 QList<FileProperty> data;
29 QHash<
int, QByteArray> roleNames;
30 QQuickFolderListModel::SortField sortField = QQuickFolderListModel::Name;
31 QStringList nameFilters = { QLatin1String(
"*") };
32 QQuickFolderListModel::Status status = QQuickFolderListModel::Null;
33 bool sortReversed =
false;
34 bool showFiles =
true;
36 bool showDirsFirst =
false;
37 bool showDotAndDotDot =
false;
38 bool showOnlyReadable =
false;
39 bool showHidden =
false;
40 bool caseSensitive =
true;
41 bool sortCaseSensitive =
true;
42 bool resettingModel =
false;
44 ~QQuickFolderListModelPrivate() {}
48 void finishModelReset();
51 void _q_directoryChanged(
const QString &directory,
const QList<FileProperty> &list);
52 void _q_directoryUpdated(
const QString &directory,
const QList<FileProperty> &list,
int fromIndex,
int toIndex);
53 void _q_sortFinished(
const QList<FileProperty> &list);
54 void _q_statusChanged(QQuickFolderListModel::Status s);
56 static QString resolvePath(
const QUrl &path);
60void QQuickFolderListModelPrivate::init()
62 Q_Q(QQuickFolderListModel);
63 qRegisterMetaType<QList<FileProperty> >(
"QList<FileProperty>");
64 qRegisterMetaType<QQuickFolderListModel::Status>(
"QQuickFolderListModel::Status");
65 q->connect(&fileInfoThread, SIGNAL(directoryChanged(QString,QList<FileProperty>)),
66 q, SLOT(_q_directoryChanged(QString,QList<FileProperty>)));
67 q->connect(&fileInfoThread, SIGNAL(directoryUpdated(QString,QList<FileProperty>,
int,
int)),
68 q, SLOT(_q_directoryUpdated(QString,QList<FileProperty>,
int,
int)));
69 q->connect(&fileInfoThread, SIGNAL(sortFinished(QList<FileProperty>)),
70 q, SLOT(_q_sortFinished(QList<FileProperty>)));
71 q->connect(&fileInfoThread, SIGNAL(statusChanged(QQuickFolderListModel::Status)),
72 q, SLOT(_q_statusChanged(QQuickFolderListModel::Status)));
73 q->connect(q, SIGNAL(rowCountChanged()), q, SIGNAL(countChanged()));
77void QQuickFolderListModelPrivate::updateSorting()
79 Q_Q(QQuickFolderListModel);
81 QDir::SortFlags flags;
84 case QQuickFolderListModel::Unsorted:
85 flags |= QDir::Unsorted;
87 case QQuickFolderListModel::Name:
90 case QQuickFolderListModel::Time:
93 case QQuickFolderListModel::Size:
96 case QQuickFolderListModel::Type:
101 emit q->layoutAboutToBeChanged();
104 flags |= QDir::Reversed;
105 if (!sortCaseSensitive)
106 flags |= QDir::IgnoreCase;
108 fileInfoThread.setSortFlags(flags);
111void QQuickFolderListModelPrivate::finishModelReset()
113 Q_Q(QQuickFolderListModel);
114 const bool wasDataEmpty = data.isEmpty();
116 qCDebug(lcFolderListModel) <<
"about to emit endResetModel";
119 emit q->rowCountChanged();
120 if (status != QQuickFolderListModel::Null) {
121 status = QQuickFolderListModel::Null;
122 emit q->statusChanged();
124 resettingModel =
false;
127void QQuickFolderListModelPrivate::_q_directoryChanged(
const QString &directory,
const QList<FileProperty> &list)
129 qCDebug(lcFolderListModel) <<
"_q_directoryChanged called with directory" << directory;
130 Q_Q(QQuickFolderListModel);
132 if (!resettingModel) {
133 resettingModel =
true;
134 q->beginResetModel();
139 qCDebug(lcFolderListModel) <<
"- endResetModel called";
140 emit q->rowCountChanged();
141 emit q->folderChanged();
142 resettingModel =
false;
146void QQuickFolderListModelPrivate::_q_directoryUpdated(
const QString &directory,
const QList<FileProperty> &list,
int fromIndex,
int toIndex)
148 Q_Q(QQuickFolderListModel);
152 if (data.size() == list.size()) {
153 QModelIndex modelIndexFrom = q->createIndex(fromIndex, 0);
154 QModelIndex modelIndexTo = q->createIndex(toIndex, 0);
156 emit q->dataChanged(modelIndexFrom, modelIndexTo);
162 if (data.size() > 0) {
163 q->beginRemoveRows(parent, 0, data.size() - 1);
167 if (list.size() > 0) {
168 if (toIndex > list.size() - 1)
169 toIndex = list.size() - 1;
170 q->beginInsertRows(parent, 0, data.size() - 1);
173 emit q->rowCountChanged();
177void QQuickFolderListModelPrivate::_q_sortFinished(
const QList<FileProperty> &list)
179 Q_Q(QQuickFolderListModel);
180 qCDebug(lcFolderListModel) <<
"_q_sortFinished called with" << list.size() <<
"files";
183 if (data.size() > 0) {
184 qCDebug(lcFolderListModel) <<
"- removing all existing rows...";
185 q->beginRemoveRows(parent, 0, data.size()-1);
188 qCDebug(lcFolderListModel) <<
"- ...removed all existing rows";
191 qCDebug(lcFolderListModel) <<
"- inserting sorted rows...";
192 q->beginInsertRows(parent, 0, list.size()-1);
195 qCDebug(lcFolderListModel) <<
"- ... inserted sorted rows";
208QString QQuickFolderListModelPrivate::resolvePath(
const QUrl &path)
210 QString localPath = QQmlFile::urlToLocalFileOrQrc(path);
211 QUrl localUrl = QUrl(localPath);
212 QString fullPath = localUrl.path();
213 if (localUrl.scheme().size())
214 fullPath = localUrl.scheme() + QLatin1Char(
':') + fullPath;
215 return QDir::cleanPath(fullPath);
330QQuickFolderListModel::QQuickFolderListModel(QObject *parent)
331 : QAbstractListModel(parent), d_ptr(
new QQuickFolderListModelPrivate(
this))
333 Q_D(QQuickFolderListModel);
334 d->roleNames[FileNameRole] =
"fileName";
335 d->roleNames[FilePathRole] =
"filePath";
336 d->roleNames[FileBaseNameRole] =
"fileBaseName";
337 d->roleNames[FileSuffixRole] =
"fileSuffix";
338 d->roleNames[FileSizeRole] =
"fileSize";
339 d->roleNames[FileLastModifiedRole] =
"fileModified";
340 d->roleNames[FileLastReadRole] =
"fileAccessed";
341 d->roleNames[FileIsDirRole] =
"fileIsDir";
342 d->roleNames[FileUrlRole] =
"fileUrl";
350QVariant QQuickFolderListModel::data(
const QModelIndex &index,
int role)
const
352 Q_D(
const QQuickFolderListModel);
355 const int row = index.row();
356 if (row < 0 || row >= d->data.size())
362 rv = d->data.at(row).fileName();
365 rv = d->data.at(row).filePath();
367 case FileBaseNameRole:
368 rv = d->data.at(row).baseName();
371 rv = d->data.at(row).suffix();
374 rv = d->data.at(row).size();
376 case FileLastModifiedRole:
377 rv = d->data.at(row).lastModified();
379 case FileLastReadRole:
380 rv = d->data.at(row).lastRead();
383 rv = d->data.at(row).isDir();
386 rv = QUrl::fromLocalFile(d->data.at(row).filePath());
436void QQuickFolderListModel::setFolder(
const QUrl &folder)
438 Q_D(QQuickFolderListModel);
440 if (folder == d->currentDir)
448 if (d->resettingModel)
449 d->finishModelReset();
451 d->resettingModel =
true;
453 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(folder);
455 qCDebug(lcFolderListModel) <<
"about to emit beginResetModel since our folder was set to" << folder;
459 if (!d->currentDir.isEmpty())
460 d->fileInfoThread.removePath(d->currentDir.path());
462 d->currentDir = folder;
464 QFileInfo info(resolvedPath);
465 if (!info.exists() || !info.isDir()) {
466 d->finishModelReset();
470 d->fileInfoThread.setPath(resolvedPath);
487void QQuickFolderListModel::setRootFolder(
const QUrl &path)
489 Q_D(QQuickFolderListModel);
494 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(path);
496 QFileInfo info(resolvedPath);
497 if (!info.exists() || !info.isDir())
499 if (path != d->rootDir) {
500 d->fileInfoThread.setRootPath(resolvedPath);
502 emit rootFolderChanged();
513QUrl QQuickFolderListModel::parentFolder()
const
515 Q_D(
const QQuickFolderListModel);
517 QString localFile = d->currentDir.toLocalFile();
518 if (!localFile.isEmpty()) {
520 if (dir.isRoot() || !dir.cdUp())
522 localFile = dir.path();
524 const QString path = d->currentDir.path();
525 const int pos = path.lastIndexOf(QLatin1Char(
'/'));
528 localFile = path.left(pos);
530 return QUrl::fromLocalFile(localFile);
555void QQuickFolderListModel::setNameFilters(
const QStringList &filters)
557 Q_D(QQuickFolderListModel);
558 if (d->nameFilters == filters)
560 d->fileInfoThread.setNameFilters(filters);
561 d->nameFilters = filters;
562 emit nameFilterChanged();
569void QQuickFolderListModel::componentComplete()
571 Q_D(QQuickFolderListModel);
572 QString localPath = QQmlFile::urlToLocalFileOrQrc(d->currentDir);
573 if (localPath.isEmpty() || !QDir(localPath).exists())
574 setFolder(QUrl::fromLocalFile(QDir::currentPath()));
575 d->fileInfoThread.start(QThread::LowPriority);
730void QQuickFolderListModel::setShowDirsFirst(
bool on)
732 Q_D(QQuickFolderListModel);
734 if (on != d->showDirsFirst) {
735 d->fileInfoThread.setShowDirsFirst(on);
736 d->showDirsFirst = on;
737 emit showDirsFirstChanged();
758void QQuickFolderListModel::setShowDotAndDotDot(
bool on)
760 Q_D(QQuickFolderListModel);
762 if (on != d->showDotAndDotDot) {
763 d->fileInfoThread.setShowDotAndDotDot(on);
764 d->showDotAndDotDot = on;
765 emit showDotAndDotDotChanged();
812void QQuickFolderListModel::setShowOnlyReadable(
bool on)
814 Q_D(QQuickFolderListModel);
816 if (on != d->showOnlyReadable) {
817 d->fileInfoThread.setShowOnlyReadable(on);
818 d->showOnlyReadable = on;
819 emit showOnlyReadableChanged();
838void QQuickFolderListModel::setCaseSensitive(
bool on)
840 Q_D(QQuickFolderListModel);
842 if (on != d->caseSensitive) {
843 d->fileInfoThread.setCaseSensitive(on);
844 d->caseSensitive = on;
845 emit caseSensitiveChanged();
902void QQuickFolderListModel::setSortCaseSensitive(
bool on)
904 Q_D(QQuickFolderListModel);
906 if (on != d->sortCaseSensitive) {
907 d->sortCaseSensitive = on;
909 emit sortCaseSensitiveChanged();