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;
39 bool showDotDot =
false;
40 bool showOnlyReadable =
false;
41 bool showHidden =
false;
42 bool caseSensitive =
true;
43 bool sortCaseSensitive =
true;
44 bool resettingModel =
false;
46 ~QQuickFolderListModelPrivate() {}
50 void finishModelReset();
53 void _q_directoryChanged(
const QString &directory,
const QList<FileProperty> &list);
54 void _q_directoryUpdated(
const QString &directory,
const QList<FileProperty> &list,
int fromIndex,
int toIndex);
55 void _q_sortFinished(
const QList<FileProperty> &list);
56 void _q_statusChanged(QQuickFolderListModel::Status s);
58 static QString resolvePath(
const QUrl &path);
62void QQuickFolderListModelPrivate::init()
64 Q_Q(QQuickFolderListModel);
65 qRegisterMetaType<QList<FileProperty> >(
"QList<FileProperty>");
66 qRegisterMetaType<QQuickFolderListModel::Status>(
"QQuickFolderListModel::Status");
67 q->connect(&fileInfoThread, SIGNAL(directoryChanged(QString,QList<FileProperty>)),
68 q, SLOT(_q_directoryChanged(QString,QList<FileProperty>)));
69 q->connect(&fileInfoThread, SIGNAL(directoryUpdated(QString,QList<FileProperty>,
int,
int)),
70 q, SLOT(_q_directoryUpdated(QString,QList<FileProperty>,
int,
int)));
71 q->connect(&fileInfoThread, SIGNAL(sortFinished(QList<FileProperty>)),
72 q, SLOT(_q_sortFinished(QList<FileProperty>)));
73 q->connect(&fileInfoThread, SIGNAL(statusChanged(QQuickFolderListModel::Status)),
74 q, SLOT(_q_statusChanged(QQuickFolderListModel::Status)));
75 q->connect(q, SIGNAL(rowCountChanged()), q, SIGNAL(countChanged()));
79void QQuickFolderListModelPrivate::updateSorting()
81 Q_Q(QQuickFolderListModel);
83 QDir::SortFlags flags;
86 case QQuickFolderListModel::Unsorted:
87 flags |= QDir::Unsorted;
89 case QQuickFolderListModel::Name:
92 case QQuickFolderListModel::Time:
95 case QQuickFolderListModel::Size:
98 case QQuickFolderListModel::Type:
103 emit q->layoutAboutToBeChanged();
106 flags |= QDir::Reversed;
107 if (!sortCaseSensitive)
108 flags |= QDir::IgnoreCase;
110 fileInfoThread.setSortFlags(flags);
113void QQuickFolderListModelPrivate::finishModelReset()
115 Q_Q(QQuickFolderListModel);
116 const bool wasDataEmpty = data.isEmpty();
118 qCDebug(lcFolderListModel) <<
"about to emit endResetModel";
121 emit q->rowCountChanged();
122 if (status != QQuickFolderListModel::Null) {
123 status = QQuickFolderListModel::Null;
124 emit q->statusChanged();
126 resettingModel =
false;
129void QQuickFolderListModelPrivate::_q_directoryChanged(
const QString &directory,
const QList<FileProperty> &list)
131 qCDebug(lcFolderListModel) <<
"_q_directoryChanged called with directory" << directory;
132 Q_Q(QQuickFolderListModel);
134 if (!resettingModel) {
135 resettingModel =
true;
136 q->beginResetModel();
141 qCDebug(lcFolderListModel) <<
"- endResetModel called";
142 emit q->rowCountChanged();
143 emit q->folderChanged();
144 resettingModel =
false;
148void QQuickFolderListModelPrivate::_q_directoryUpdated(
const QString &directory,
const QList<FileProperty> &list,
int fromIndex,
int toIndex)
150 Q_Q(QQuickFolderListModel);
154 if (data.size() == list.size()) {
155 QModelIndex modelIndexFrom = q->createIndex(fromIndex, 0);
156 QModelIndex modelIndexTo = q->createIndex(toIndex, 0);
158 emit q->dataChanged(modelIndexFrom, modelIndexTo);
164 if (data.size() > 0) {
165 q->beginRemoveRows(parent, 0, data.size() - 1);
169 if (list.size() > 0) {
170 if (toIndex > list.size() - 1)
171 toIndex = list.size() - 1;
172 q->beginInsertRows(parent, 0, data.size() - 1);
175 emit q->rowCountChanged();
179void QQuickFolderListModelPrivate::_q_sortFinished(
const QList<FileProperty> &list)
181 Q_Q(QQuickFolderListModel);
182 qCDebug(lcFolderListModel) <<
"_q_sortFinished called with" << list.size() <<
"files";
185 if (data.size() > 0) {
186 qCDebug(lcFolderListModel) <<
"- removing all existing rows...";
187 q->beginRemoveRows(parent, 0, data.size()-1);
190 qCDebug(lcFolderListModel) <<
"- ...removed all existing rows";
193 qCDebug(lcFolderListModel) <<
"- inserting sorted rows...";
194 q->beginInsertRows(parent, 0, list.size()-1);
197 qCDebug(lcFolderListModel) <<
"- ... inserted sorted rows";
210QString QQuickFolderListModelPrivate::resolvePath(
const QUrl &path)
212 QString localPath = QQmlFile::urlToLocalFileOrQrc(path);
213 QUrl localUrl = QUrl(localPath);
214 QString fullPath = localUrl.path();
215 if (localUrl.scheme().size())
216 fullPath = localUrl.scheme() + QLatin1Char(
':') + fullPath;
217 return QDir::cleanPath(fullPath);
332QQuickFolderListModel::QQuickFolderListModel(QObject *parent)
333 : QAbstractListModel(parent), d_ptr(
new QQuickFolderListModelPrivate(
this))
335 Q_D(QQuickFolderListModel);
336 d->roleNames[FileNameRole] =
"fileName";
337 d->roleNames[FilePathRole] =
"filePath";
338 d->roleNames[FileBaseNameRole] =
"fileBaseName";
339 d->roleNames[FileSuffixRole] =
"fileSuffix";
340 d->roleNames[FileSizeRole] =
"fileSize";
341 d->roleNames[FileLastModifiedRole] =
"fileModified";
342 d->roleNames[FileLastReadRole] =
"fileAccessed";
343 d->roleNames[FileIsDirRole] =
"fileIsDir";
344 d->roleNames[FileUrlRole] =
"fileUrl";
352QVariant QQuickFolderListModel::data(
const QModelIndex &index,
int role)
const
354 Q_D(
const QQuickFolderListModel);
357 const int row = index.row();
358 if (row < 0 || row >= d->data.size())
364 rv = d->data.at(row).fileName();
367 rv = d->data.at(row).filePath();
369 case FileBaseNameRole:
370 rv = d->data.at(row).baseName();
373 rv = d->data.at(row).suffix();
376 rv = d->data.at(row).size();
378 case FileLastModifiedRole:
379 rv = d->data.at(row).lastModified();
381 case FileLastReadRole:
382 rv = d->data.at(row).lastRead();
385 rv = d->data.at(row).isDir();
388 rv = QUrl::fromLocalFile(d->data.at(row).filePath());
438void QQuickFolderListModel::setFolder(
const QUrl &folder)
440 Q_D(QQuickFolderListModel);
442 if (folder == d->currentDir)
450 if (d->resettingModel)
451 d->finishModelReset();
453 d->resettingModel =
true;
455 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(folder);
457 qCDebug(lcFolderListModel) <<
"about to emit beginResetModel since our folder was set to" << folder;
461 if (!d->currentDir.isEmpty())
462 d->fileInfoThread.removePath(d->currentDir.path());
464 d->currentDir = folder;
466 QFileInfo info(resolvedPath);
467 if (!info.exists() || !info.isDir()) {
468 d->finishModelReset();
472 d->fileInfoThread.setPath(resolvedPath);
489void QQuickFolderListModel::setRootFolder(
const QUrl &path)
491 Q_D(QQuickFolderListModel);
496 QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(path);
498 QFileInfo info(resolvedPath);
499 if (!info.exists() || !info.isDir())
501 if (path != d->rootDir) {
502 d->fileInfoThread.setRootPath(resolvedPath);
504 emit rootFolderChanged();
515QUrl QQuickFolderListModel::parentFolder()
const
517 Q_D(
const QQuickFolderListModel);
519 QString localFile = d->currentDir.toLocalFile();
520 if (!localFile.isEmpty()) {
522 if (dir.isRoot() || !dir.cdUp())
524 localFile = dir.path();
526 const QString path = d->currentDir.path();
527 const int pos = path.lastIndexOf(QLatin1Char(
'/'));
530 localFile = path.left(pos);
532 return QUrl::fromLocalFile(localFile);
557void QQuickFolderListModel::setNameFilters(
const QStringList &filters)
559 Q_D(QQuickFolderListModel);
560 if (d->nameFilters == filters)
562 d->fileInfoThread.setNameFilters(filters);
563 d->nameFilters = filters;
564 emit nameFilterChanged();
571void QQuickFolderListModel::componentComplete()
573 Q_D(QQuickFolderListModel);
574 QString localPath = QQmlFile::urlToLocalFileOrQrc(d->currentDir);
575 if (localPath.isEmpty() || !QDir(localPath).exists())
576 setFolder(QUrl::fromLocalFile(QDir::currentPath()));
577 d->fileInfoThread.start(QThread::LowPriority);
732void QQuickFolderListModel::setShowDirsFirst(
bool on)
734 Q_D(QQuickFolderListModel);
736 if (on != d->showDirsFirst) {
737 d->fileInfoThread.setShowDirsFirst(on);
738 d->showDirsFirst = on;
739 emit showDirsFirstChanged();
760void QQuickFolderListModel::setShowDotAndDotDot(
bool on)
762 Q_D(QQuickFolderListModel);
764 if (on != d->showDotAndDotDot) {
765 d->fileInfoThread.setShowDotAndDotDot(on);
766 d->showDotAndDotDot = on;
767 emit showDotAndDotDotChanged();
870void QQuickFolderListModel::setShowOnlyReadable(
bool on)
872 Q_D(QQuickFolderListModel);
874 if (on != d->showOnlyReadable) {
875 d->fileInfoThread.setShowOnlyReadable(on);
876 d->showOnlyReadable = on;
877 emit showOnlyReadableChanged();
896void QQuickFolderListModel::setCaseSensitive(
bool on)
898 Q_D(QQuickFolderListModel);
900 if (on != d->caseSensitive) {
901 d->fileInfoThread.setCaseSensitive(on);
902 d->caseSensitive = on;
903 emit caseSensitiveChanged();
960void QQuickFolderListModel::setSortCaseSensitive(
bool on)
962 Q_D(QQuickFolderListModel);
964 if (on != d->sortCaseSensitive) {
965 d->sortCaseSensitive = on;
967 emit sortCaseSensitiveChanged();